簡易版蒙特卡洛採樣計算二項式分佈的參數

import numpy as np

import matplotlib.pyplot as plt 



 

X = np.array([1, 1, 1, 1, 1, 0, 0, 1, 1, 0])

#q = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])


 

print(np.random.normal(loc=0.001, scale=0.0, size=None))

 

p = []

sum = 1

 

for j in range(100):

    q = np.random.random_sample()

    for i in range(100):

        index = np.random.choice(a=X, size=1, replace=False, p=None)

        if index == 0:

            sum *= q

        if index == 1:

            sum *= 1 - q

    p.append([q, sum])

    sum = 1


 

p = np.array(p)

print(p)



 

p = p[np.argsort(p[:, 0])]


 

plt.plot(p[:,0], p[:, 1])

plt.show()

 

# x_max = np.max(p)

# x_min = np.min(p)

 

# result = []

# for i in p:

#     result.append((float(i) - x_min)/(x_max - x_min))

 

# print(result)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章