Multinoulli分佈(多元伯努利分佈):
模型:
d面🎲獲得每一面的概率:
分佈函數:
似然函數:
極大似然估計:
其中 的由來
是因爲 ,
(概率密度函數和爲1),在做極大似然估計時候,必須滿足這一條件。對於帶有約束的優化問題,常用拉格朗日乘子法, 表示拉格朗日乘數,表示約束條件的強度。
多項式分佈:
模型:
d面🎲獲得每一面的概率:
擲了n次,每面出現的次數:
滿足條件:
分佈函數:
多項式展開定理:
矩生成函數:
一個服從多項式分佈的例子:
Matplotlib:
import xlrd as xl
import numpy as np
from collections import Counter
import matplotlib.pyplot as plt
import pandas as pd
data = xl.open_workbook("等位基因.xlsx")
table = data.sheets()[0]
if data.sheet_loaded(sheet_name_or_index=0):
cols = table.ncols # 列數
lists = [table.col_values(_) for _ in range(cols)]
list_x = [_ for _ in range(1, len(lists) + 1)]
list_A = []
list_G = []
list_C = []
list_T = []
for item in lists:
dicts = dict(Counter(item))
list_A.append(dicts.get('A', 0))
list_G.append(dicts.get('G', 0))
list_C.append(dicts.get('C', 0))
list_T.append(dicts.get('T', 0))
columns = ('A', 'G', 'C', 'T')
data = []
data.append(list_A)
data.append(list_G)
data.append(list_C)
data.append(list_T)
data = np.array(data)
data = data.T
df = pd.DataFrame(data, columns=columns, index=[_ for _ in range(1, cols + 1)])
df.plot(kind='bar', stacked=True,colormap="cool_r",legend="reverse")
print(df)
ax=plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.xlabel("Sequence Position")
plt.ylabel("Bits")
plt.show()
else:
print("打開文件失敗")
Pyecharts:
import xlrd as xl
import numpy as np
from pyecharts.charts import *
from collections import Counter
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot
from pyecharts.globals import ThemeType
data = xl.open_workbook("等位基因.xlsx")
# table=data.sheet_by_name('Sheet1')
# table=data.sheet_by_index(0)
table = data.sheets()[0]
if data.sheet_loaded(sheet_name_or_index=0):
rows = table.nrows # 行數
cols = table.ncols # 列數
lists = [table.col_values(_) for _ in range(cols)]
list_x = [_ for _ in range(1, len(lists) + 1)]
list_A = []
list_G = []
list_C = []
list_T = []
for item in lists:
dicts = dict(Counter(item))
list_A.append(dicts.get('A', 0))
list_G.append(dicts.get('G', 0))
list_C.append(dicts.get('C', 0))
list_T.append(dicts.get('T', 0))
bar = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(list_x)
.add_yaxis("A", list_A, stack='stack1')
.add_yaxis("G", list_G, stack='stack1')
.add_yaxis("C", list_C, stack='stack1')
.add_yaxis("T", list_T, stack='stack1')
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(pos_left="10%"),
yaxis_opts=opts.AxisOpts(name="Bits"),
xaxis_opts=opts.AxisOpts(name="Sequence Position")))
make_snapshot(snapshot, bar.render(), "111.png")
else:
print("打開文件失敗")