统计机器学习-Multinoulli分布、多项式分布

Multinoulli分布(多元伯努利分布):

模型:        M u ( p ) Mu(p) Mu(p)

        d面获得每一面的概率:  p 1 , p 2 , . . . , p d p_1,p_2,...,p_d p1,p2,...,pd

分布函数:
p ( x ∣ p ) = ∏ k = 1 d p k x k p(x|p)=\prod_{k=1}^d p_k^{x_k} p(xp)=k=1dpkxk
E ( X ) = p E(X)=p E(X)=p
似然函数:
L = l o g ( ∏ n = 1 N ∏ k = 1 d p k x n k ) = l o g ( ∏ k = 1 d p k m k ) L=log(\prod_{n=1}^N \prod_{k=1}^d p_k^{x_{nk}})=log( \prod_{k=1}^d p_k^{m_k}) L=log(n=1Nk=1dpkxnk)=log(k=1dpkmk) m k = ∑ n x n k m_k=\sum_n x_{nk} mk=nxnk
极大似然估计:
L = l n ( ∏ n = 1 → N ∏ k = 1 → d p k x n k ) = l n ( ∏ k = 1 → d p k m k ) = ∑ k = 1 → d m k l n p k + λ ( ∑ k = 1 → d p k − 1 ) L = ln(\prod^{n=1\to N}\prod^{k=1\to d}p_k^{x_{nk}}) = ln(\prod^{k=1\to d}p_k^{m_k}) = \sum^{k=1\to d}m_k lnp_k+\lambda(\sum^{k=1\to d}p_k-1) L=ln(n=1Nk=1dpkxnk)=ln(k=1dpkmk)=k=1dmklnpk+λ(k=1dpk1)
               p k = m k λ p_k=\frac{m_k}{\lambda} pk=λmk    λ = − N \lambda=-N λ=N

其中    λ ( ∑ k = 1 d p k − 1 ) \lambda(\sum_{k=1}^{d}p_k-1) λ(k=1dpk1)   的由来
是因为    ∑ k = 1 d p k = 1 \sum_{k=1}^d p_k =1 k=1dpk=1   ,
(概率密度函数和为1),在做极大似然估计时候,必须满足这一条件。对于带有约束的优化问题,常用拉格朗日乘子法,   λ > 0 \lambda>0 λ>0  表示拉格朗日乘数,表示约束条件的强度。

多项式分布:

模型:        M u l t ( n , p ) Mult(n,p) Mult(n,p)
        d面获得每一面的概率:  p 1 , p 2 , . . . , p d p_1,p_2,...,p_d p1,p2,...,pd
        掷了n次,每面出现的次数: ( x 1 , x 2 , . . . , x d ) (x_1,x_2,...,x_d) (x1,x2,...,xd)
        满足条件: x 1 + x 2 + . . . + x d = n x_1+x_2+...+x_d=n x1+x2+...+xd=n
              x i ≥ 0 x_i≥0 xi0
分布函数:
C n x 1 C n − x 1 x 2 . . . C n − x 1 − x 2 + . . . x d − 1 x d p 1 x 1 . . . p d x d C_n^{x_1}C_{n-x_1}^{x_2}...C_{n-x_1-x_2+...x_{d-1}}^{x_d}p_1^{x_1}...p_d^{x_d} Cnx1Cnx1x2...Cnx1x2+...xd1xdp1x1...pdxd
f ( x ) = n ! x ( 1 ) ! . . . x ( d ) ! ( p 1 ) x ( 1 ) . . . ( p d ) x ( d ) f(x)=\frac{n!}{x^{(1)}!...x^{(d)}!}(p_1)^{x^{(1)}}...(p_d)^{x^{(d)}} f(x)=x(1)!...x(d)!n!(p1)x(1)...(pd)x(d)
多项式展开定理:
( p 1 + . . . + p d ) n = ∑ x ∈ Δ d , n n ! x ( 1 ) ! . . . x ( d ) ! ( p 1 ) x ( 1 ) . . . ( p d ) x ( d ) (p_1+...+p_d)^n=\sum_{x∈ \Delta d,n}\frac{n!}{x^{(1)}!...x^{(d)}!}(p_1)^{x^{(1)}}...(p_d)^{x^{(d)}} (p1+...+pd)n=xΔd,nx(1)!...x(d)!n!(p1)x(1)...(pd)x(d)
矩生成函数:
统计机器学习-Multinoulli分布、多项式分布_第1张图片
E ( x j ) = n p j E(x^j)=np_j E(xj)=npj
C o v [ x ( j ) , x ( j ′ ) ] = { n p j ( 1 − p j )                            ( j = j ′ ) − n p j p j ′                                   ( j ≠ j ′ ) Cov[x^{(j)},x^{(j')}]= \begin{cases} np_j(1-p_j) \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (j=j') \\ -np_jp_{j'} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (j≠j') \end{cases} Cov[x(j),x(j)]={npj(1pj)                          (j=j)npjpj                                 (j=j)
统计机器学习-Multinoulli分布、多项式分布_第2张图片

一个服从多项式分布的例子:

统计机器学习-Multinoulli分布、多项式分布_第3张图片
将 这 个 基 因 碱 基 序 列 可 视 化 将这个基因碱基序列可视化

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("打开文件失败")

统计机器学习-Multinoulli分布、多项式分布_第4张图片
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("打开文件失败")

统计机器学习-Multinoulli分布、多项式分布_第5张图片

你可能感兴趣的:(统计机器学习,Python)