如何估计2个指数随机变量的混合参数(理想情况下在Python中)

如何估计2个指数随机变量的混合参数(理想情况下在Python中),python,scikit-learn,statistics,estimation,exponential-distribution,Python,Scikit Learn,Statistics,Estimation,Exponential Distribution,想象一个模拟实验,其中输出为n个总数,其中k个是从速率为a的指数随机变量中采样的,n-k是从速率为b的指数随机变量中采样的。限制条件是0

想象一个模拟实验,其中输出为n个总数,其中k个是从速率为a的指数随机变量中采样的,n-k是从速率为b的指数随机变量中采样的。限制条件是0

此外,我尝试了以下方法,效果不错:

首先,对于每一个整数百分位数(第一个百分位数、第二个百分位数、…、第99个百分位数),我使用指数分布的分位数封闭式方程(其中第I个分位数是第(I*100)个百分位数)(第I个分位数=−ln(1)− i) /λ,soλ=−ln(1)− i) /(第i分位数)。结果是一个列表,其中每个第i个元素对应于使用第(i+1)百分位的b估计值

然后,我使用MatlabPeak调用函数的Python实现在此列表上执行peak调用。然后,我获取得到的峰值列表并返回最小值。它似乎工作得相当好

我还将在StackExchangePost中实现EM解决方案,看看哪一个更好

编辑:我实现了EM解决方案,它在我的模拟中似乎运行得很好(n=1000,各种a和b)

from sys import stdin
from math import exp,log
from scipy.optimize import fmin
DATA = None

def pdf(x,l): # compute P(X=x) for an exponential rv X with rate l
    return l*exp(-1*l*x)

def logML(X,la,lb): # compute the log-ML of data points X given two exponentials with rates la and lb where la < lb
    ml = 0.0
    for x in X:
       ml += log(max(pdf(x,la),pdf(x,lb)))
    return ml

def f(x): # objective function to minimize
    assert DATA is not None, "DATA cannot be None"
    la,lb = x
    if la > lb: # force la <= lb
        return float('inf')
    elif la <= 0 or lb <= 0:
        return float('inf') # force la and lb > 0
    return -1*logML(DATA,la,lb)

if __name__ == "__main__":
    DATA = [float(x) for x in stdin.read().split()] # read input data
    Xbar = sum(DATA)/len(DATA) # compute mean
    x0 = [1/Xbar,1/Xbar] # start with la = lb = 1/mean
    result = fmin(f,x0,disp=DISP)
    print("ML Rates: la = %f and lb = %f" % tuple(result))