Skip to content

Commit 191653a

Browse files
author
Aragorn
committed
mcmc logistic
1 parent a30218e commit 191653a

4 files changed

Lines changed: 583 additions & 90 deletions

File tree

code_in_notes/MCMC_independent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
domain=lambda x:(x[0]>=-1)*(x[1]>=-1)*(x[0]<=1)*(x[1]<=1)
1313
q=lambda y: 1/(2*np.pi)*np.exp(-1*y[0]**2/2-y[1]**2/2)
1414
h=lambda x: domain(x)
15-
h2=lambda x: np.sin(x[0])**2+np.log(abs(1+x[1]))
15+
h2=lambda x: np.sin(10*x[0])**2+np.log(abs(1+x[1]*10))
1616
#从均匀分布中采样
1717
def sample_q():
1818
return (nprd.normal(),nprd.normal())

code_in_notes/MCMC_logistic.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/python3
2+
## file: MonteCarlo.py
3+
4+
import numpy as np
5+
from numpy import random as nprd
6+
7+
##设定参数
8+
M =20000
9+
#真值
10+
beta_0=1
11+
beta_1=1
12+
beta_2=-1
13+
#样本量
14+
N=200
15+
#Logistic函数
16+
Logistic=lambda x: 1.0/(1+np.exp(-1*x))
17+
18+
##产生数据
19+
def gen_logit(N):
20+
Data=[]
21+
for n in range(N):
22+
x1=nprd.normal()*1.414+1
23+
x2=nprd.chisquare(2)
24+
d_star=beta_0+beta_1*x1+beta_2*x2
25+
p_star=Logistic(d_star)
26+
d=(1 if nprd.uniform()<p_star else 0)
27+
Data.append((d,x1,x2))
28+
return Data
29+
30+
#计算接受率
31+
def rho(beta_x,beta_y,Data):
32+
log_post_pai_x=(-1*beta_x[0]**2-beta_x[1]**2-beta_x[2]**2)/2
33+
log_post_pai_y=(-1*beta_y[0]**2-beta_y[1]**2-beta_y[2]**2)/2
34+
log_ratio=log_post_pai_y-log_post_pai_x
35+
for data in Data:
36+
w_b_x=beta_x[0]+data[1]*beta_x[1]+data[2]*beta_x[2]
37+
w_b_y=beta_y[0]+data[1]*beta_y[1]+data[2]*beta_y[2]
38+
F_b_x=Logistic(w_b_x)
39+
F_b_y=Logistic(w_b_y)
40+
log_post_pai_x=np.log((F_b_x if data[0]==1 else 1-F_b_x))
41+
log_post_pai_y=np.log((F_b_y if data[0]==1 else 1-F_b_y))
42+
log_ratio+=(log_post_pai_y-log_post_pai_x)
43+
return min(1,np.exp(log_ratio))
44+
45+
#随机游走
46+
def q_sampler(beta):
47+
return [b+nprd.normal(0,0.1) for b in beta]
48+
49+
##独立的MCMC算法,输入:
50+
## N_samples : 抽样次数
51+
## rho(x,y,Data) : 计算接受率
52+
## q_sampler(x): 给定x,从q中抽样的函数
53+
## x0 : 初始值
54+
## data : 数据
55+
def MH_RW(N_samples, rho, q_sampler, x0, data):
56+
X=[]
57+
x=x0
58+
for i in range(N_samples):
59+
y=q_sampler(x)
60+
if nprd.uniform()<=rho(x,y,data):
61+
X.append(y)
62+
x=y
63+
else:
64+
X.append(x)
65+
return X
66+
67+
## 从后验抽样:
68+
data=gen_logit(N)
69+
beta_post=MH_RW(M, rho, q_sampler, [0,0,0], data)
70+
beta0_post=[b[0] for b in beta_post]
71+
sub_beta0=beta0_post[int(M*0.2):]
72+
beta1_post=[b[1] for b in beta_post]
73+
sub_beta1=beta1_post[int(M*0.2):]
74+
beta2_post=[b[2] for b in beta_post]
75+
sub_beta2=beta2_post[int(M*0.2):]
76+
#后验均值
77+
mean_beta0=np.mean(sub_beta0)
78+
mean_beta1=np.mean(sub_beta1)
79+
mean_beta2=np.mean(sub_beta2)
80+
print("Mean beta0=",mean_beta0)
81+
print("Mean beta1=",mean_beta1)
82+
print("Mean beta2=",mean_beta2)

code_in_notes/MCMC_random_walk.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66

77
##设定参数
88
M =10000
9-
pai_cons=90*np.sqrt(2)/(2*np.pi)
10-
pai=lambda x: pai_cons* \
11-
np.exp(-90*(x[0]-0.5)**2-45*(x[1]+0.1)**2)
9+
pai=lambda x: np.exp(-90*(x[0]-0.5)**2-45*(x[1]+0.1)**2)
1210
domain=lambda x:(x[0]>=-1)*(x[1]>=-1)*(x[0]<=1)*(x[1]<=1)
1311
h=lambda x: domain(x)
14-
h2=lambda x: np.sin(x[0])**2+np.log(abs(1+x[1]))
12+
h2=lambda x: np.sin(10*x[0])**2+np.log(abs(1+x[1]*10))
1513
#从均匀分布中采样
1614
def sample_q(x):
17-
return (x[0]+0.3*nprd.normal(),x[1]+0.3*nprd.normal())
15+
return (x[0]+0.2*nprd.normal(),x[1]+0.2*nprd.normal())
1816

1917

2018
##独立的MCMC算法,输入:
@@ -48,3 +46,4 @@ def MH_RW(N_samples, pai, q_sampler, x0):
4846
subH2=H2[int(M*0.2):]
4947
integral2=np.pi/(90*np.sqrt(2))*np.mean(subH2)
5048
print("Intgral2=",integral2)
49+

notebook_python/MonteCarlo.ipynb

Lines changed: 496 additions & 84 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)