首页 > 编程语言 >180116 EM算法资料整理(博客、论文、工具包、视频、书籍、代码,更新ing)

180116 EM算法资料整理(博客、论文、工具包、视频、书籍、代码,更新ing)

时间:2023-06-08 21:31:39浏览次数:51  
标签:EM observation pmf 180116 theta new ing Data


# -*- coding: utf-8 -*-
"""
Created on Mon Jan 15 18:58:37 2018

@author: brucelau
"""

import glob
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.style.use('ggplot')
np.random.seed(1234)

np.set_printoptions(formatter={'all':lambda x: '%.3f' % x})
from IPython.display import Image
from numpy.core.umath_tests import matrix_multiply as mm
from scipy.optimize import minimize
from scipy.stats import bernoulli, binom
from scipy import stats

#%%
# 硬币投掷结果观测序列
observations = np.array([[1, 0, 0, 0, 1, 1, 0, 1, 0, 1],
                         [1, 1, 1, 1, 0, 1, 1, 1, 1, 1],
                         [1, 0, 1, 1, 1, 1, 1, 0, 1, 1],
                         [1, 0, 1, 0, 0, 0, 1, 1, 0, 0],
                         [0, 1, 1, 1, 0, 1, 1, 1, 0, 1]])

coin_A_pmf_observation_1 = binom.pmf(5,10,0.6)
coin_B_pmf_observation_1 = binom.pmf(5,10,0.5)
normalized_coin_A_pmf_observation_1 = coin_A_pmf_observation_1/(coin_A_pmf_observation_1+coin_B_pmf_observation_1)
normalized_coin_B_pmf_observation_1 = coin_B_pmf_observation_1/(coin_A_pmf_observation_1+coin_B_pmf_observation_1)

print ("%0.1f" %(normalized_coin_A_pmf_observation_1))
print ("%0.1f" %(normalized_coin_B_pmf_observation_1))

#%%
def em_single(priors, observations):
    """
    EM算法单次迭代
    Arguments
    ---------
    priors : [theta_A, theta_B]
    observations : [m X n matrix]

    Returns
    --------
    new_priors: [new_theta_A, new_theta_B]
    :param priors:
    :param observations:
    :return:
    """
    counts = {'A': {'H': 0, 'T': 0}, 'B': {'H': 0, 'T': 0}}
    theta_A = priors[0]
    theta_B = priors[1]
    # E step
    for observation in observations:
        len_observation = len(observation)
        num_heads = observation.sum()
        num_tails = len_observation - num_heads
        contribution_A = stats.binom.pmf(num_heads, len_observation, theta_A)
        contribution_B = stats.binom.pmf(num_heads, len_observation, theta_B)   # 两个二项分布
        weight_A = contribution_A / (contribution_A + contribution_B)
        weight_B = contribution_B / (contribution_A + contribution_B)
        # 更新在当前参数下A、B硬币产生的正反面次数
        counts['A']['H'] += weight_A * num_heads
        counts['A']['T'] += weight_A * num_tails
        counts['B']['H'] += weight_B * num_heads
        counts['B']['T'] += weight_B * num_tails
    # M step
    new_theta_A = counts['A']['H'] / (counts['A']['H'] + counts['A']['T'])
    new_theta_B = counts['B']['H'] / (counts['B']['H'] + counts['B']['T'])
    return [new_theta_A, new_theta_B]
#%%
def em(observations, prior, tol=1e-6, iterations=10000):
    """
    EM算法
    :param observations: 观测数据
    :param prior: 模型初值
    :param tol: 迭代结束阈值
    :param iterations: 最大迭代次数
    :return: 局部最优的模型参数
    """
    import math
    iteration = 0
    while iteration < iterations:
        new_prior = em_single(prior, observations)
        delta_change = np.abs(prior[0] - new_prior[0])
        print('The new_prior valuers are:',new_prior)
        if delta_change < tol:
            break
        else:
            prior = new_prior
            iteration += 1
    return [new_prior, iteration]

theta1,theta2 = em(observations,[0.6,0.5])
print(theta1,theta2)


标签:EM,observation,pmf,180116,theta,new,ing,Data
From: https://blog.51cto.com/guokliu/6443437

相关文章

  • HTML cellpadding与cellspacing属性
    9.1.6HTMLcellpadding与cellspacing属性先介绍一些概念巢(cell)--表格的内容巢补白(表格填充)(cellpadding)--代表巢外面的一个距离,用于隔开巢与巢空间巢空间(表格间距)(cellspacing)--代表表格边框与巢补白的距离,也是巢补白之间的距离引用网址:http://www.dreamdu.com/x......
  • spring事务概要
    Spring框架是一个2003年2月才出现的开源项目,该开源项目起源自RodJohnson在2002年末出版的《ExpertOne-on-OneJ2EEDesignandDevelopment》一书中的基础性代码。在该书中,RodJohnson倡导J2EE实用主义的设计思想,而Spring框架正是这一思想的更全面和具体的实现。Spring框......
  • libmemcached API介绍
    有关memcached的C语言接口——libmemcached的介绍:详情参见:http://docs.libmemcached.org/index.html  1.       创建和删除memcached_st结构。#include<libmemcached/memcached.h>memcachd_st ;该结构可以静态创建也可以由memcached_create动态创建。memc......
  • Database System Concepts——读书笔记 第二章 关系模型简介
    关系模型简介在关系模型中,术语relation用于指代table,而术语tuple用于指代row。类似地,术语attribute(属性)指的是表中的一column(列)。我们必须区分数据库模式和数据库实例,前者是数据库的逻辑设计,后者是给定时刻数据库中数据的快照。关系的模式指的是它的逻辑设计,而关系的实例指的......
  • Database System Concepts——读书笔记 第一章 介绍
    数据库系统概念——第一章数据库管理系统(DBMS)由相互关联的数据集合和访问这些数据的程序集合组成。数据库相对于文件系统,更规范化,提供条件查询能力,避免冗余数据。类似操作系统于底层硬件,提供抽象能力,易用性。physicallevel->logicallevel->viewlevelinstance和schem......
  • Database System Concepts——读书笔记 第三、四、五章 SQL简介
    SQL简介关系代数运算和SQL运算之间有着密切的联系。一个关键的区别是,与关系代数不同,SQL允许重复与select子句不同,union联合操作会自动消除重复项.如果我们想保留所有的副本,我们就必须用“unionall”代替“union.intersectall,exceptall您可以验证,如果r.A为null,则“1<r.A”......
  • VUE | Element组件库的 el-collapse 标签的用法
    Collapse折叠面板:通过折叠面板收纳内容区域。1.基础用法可以折叠展开多个面板,面板之间互不影响。示例代码<el-collapsev-model="activeNames"@change="handleChange"><el-collapse-itemtitle="一致性Consistency"name="1"><div>与现实生活......
  • toString方法
    //Student2类/**一般情况下toString方法堆我们来说没有什么用,所以要重写toString方法*Alt+enter*Alt+insert**/publicclassStudent2extendsObject{privateStringname;privateintage;publicStudent2(){super();}publicStudent2(String......
  • JAVA的springboot+vue学习平台管理系统,校园在线学习管理系统,附源码+数据库+论文+PPT
    1、项目介绍在Internet高速发展的今天,我们生活的各个领域都涉及到计算机的应用,其中包括学习平台的网络应用,在外国学习平台已经是很普遍的方式,不过国内的管理平台可能还处于起步阶段。学习平台具有学习信息管理功能的选择。学习平台采用java技术,基于springboot框架,mysql数据库进行......
  • Vue——登录小案例、scoped、ref属性、props其他、混入mixin、插件、Element-ui
    解析Vue项目#1为什么浏览器中访问某个地址,会显示某个页面组件 根组件:APP.vue必须是 <template><divid="app"><router-view></router-view></div> </template>1配置路由 router--->index.js---&......