/
high_dimensional.py
208 lines (189 loc) · 8.6 KB
/
high_dimensional.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 2 16:33:14 2020
@author: Wenshuo Wang
"""
import pandas as pd
import math
import numpy as np
from numpy import random
from scipy.stats import norm, multivariate_normal, uniform, chi2
from hilbertcurve.hilbertcurve import HilbertCurve
from joblib import Parallel, delayed
import timeit
import sys
# def cov(dim = 2, correlation = 0.5):
# res = np.eye(dim)
# for j in range(dim-1):
# res[j, j+1] = correlation
# res[j+1, j] = correlation
# return res
# def log_target_f(t, x):
# return(math.log(multivariate_normal.pdf(x[0:(t+1)],3*np.ones(t+1),4*cov(t+1, 0.5))+multivariate_normal.pdf(x[0:(t+1)],-3*np.ones(t+1),4*cov(t+1, 0.5))))
def Stratified_Matrix(ww, M):
w = ww.copy()
size = len(ww)
i = 0
j = 0
weight_matrix_res = np.zeros([M,size])
cumsum = np.zeros(M)
while i<M and j<size:
if (w[j]<1/M):
if ((cumsum[i]+w[j] * M) <= 1):
weight_matrix_res[i,j] = w[j] * M
cumsum[i]= cumsum[i] + w[j] * M
j = j + 1
else:
weight_matrix_res[i,j] =1-cumsum[i]
w[j] = w[j] - weight_matrix_res[i,j]/M
i = i + 1
else:
weight_matrix_res[i,j] = 1-cumsum[i]
w[j] = w[j] - weight_matrix_res[i,j]/M
i = i + 1
return weight_matrix_res
def General_Resampling_Weights(weights, rho):
resampling_weights = np.power(weights, rho)
if rho == 1:
weights_after = np.ones(len(weights))
else:
weights_after = np.power(weights, 1-rho)
resampling_weights = resampling_weights/np.sum(resampling_weights)
weights_after = weights_after/np.sum(weights_after)
return resampling_weights, weights_after
def Stratified_Resampling(particles, weights, size, rho):
# Wenshuo: now returns weighted particles
resampling_weights, weights_after = General_Resampling_Weights(weights, rho)
weight_matrix = Stratified_Matrix(resampling_weights, M = size)
indices = [random.choice(range(len(particles)), p = w) for w in weight_matrix]
res = [np.array([particles[i] for i in indices]), np.array([weights_after[i] for i in indices])]
return res
def Hilbert_Resampling(particles, weights, size, t, rho):
particles = np.array(particles)
# print(particles)
dim = particles.shape[1]
dim = t+1
pmax = [max(particles[:,k])+0.1 for k in range(dim)]
pmin = [min(particles[:,k])-0.1 for k in range(dim)]
unified_particles = np.array([[(par[k]-pmin[k])/(pmax[k]-pmin[k]) for k in range(dim)] for par in particles])
hilbert_mapping = [Hilbert_Mapping(up, dim=dim) for up in unified_particles]
# getting weights
resampling_weights, weights_after = General_Resampling_Weights(weights, rho)
Weighted_Sample = pd.concat([pd.DataFrame(particles),pd.DataFrame({"weight": resampling_weights, 'map': hilbert_mapping})],axis=1)
Weighted_Sample = Weighted_Sample.sort_values(by = ['map'], ascending = True)
Weighted_Sample.index = range(Weighted_Sample.shape[0])
w = Weighted_Sample['weight']
weight_matrix = Stratified_Matrix(w, M = size)
indices = [random.choice(range(len(particles)), p = w) for w in weight_matrix]
xx = np.array(Weighted_Sample.iloc[indices, list(range(particles.shape[1]))])
ww = np.array([weights_after[i] for i in indices])
return xx, ww
def Multinomial_Resampling(particles, weights, size, rho):
resampling_weights, weights_after = General_Resampling_Weights(weights, rho)
indices = [random.choice(range(len(particles)), p = weights) for _ in range(size)]
xx = np.array([particles[i] for i in indices])
ww = np.array([weights_after[i] for i in indices])
return xx, ww
def Multiple_Descendent_Proposal(particles, weights, t, multiple_des = 4, sd = 3):
T = particles.shape[1]
size = particles.shape[0]
x_prop = np.zeros((size*multiple_des,T))
weight = np.zeros(size*multiple_des)
for ipar in range(size):
for k in range(multiple_des):
x_prop[ipar*multiple_des+k] = particles[ipar]
x_prop[ipar*multiple_des+k,t] = random.normal(0,sd)
weight[ipar*multiple_des+k] = log_target_f(t, x_prop[ipar*multiple_des+k]) - log_target_f(t-1, particles[ipar]) - norm.logpdf(x_prop[ipar*multiple_des+k,t], 0, sd)
x_prop = np.array(x_prop)
weight = weight - np.max(weight)
weight = np.exp(weight)
weight = weight/np.sum(weight)
for ipar in range(size):
for k in range(multiple_des):
weight[ipar*multiple_des+k] = weight[ipar*multiple_des+k] * weights[ipar]
weight = weight/np.sum(weight)
return x_prop, weight
def Hilbert_Mapping(x, p = 8, dim = 10):
hilbert_curve = HilbertCurve(p, dim)
aa = [int(xx) for xx in x*2**p]
h = hilbert_curve.distance_from_coordinates(aa)
h = np.array(h/(2**(p*dim)))
return h
def Hilbert_Mapping_Inverse(h, p = 8, dim = 10):
hilbert_curve = HilbertCurve(p, dim)
aa = hilbert_curve.coordinates_from_distance(int(h*2**(p*dim)))
aa = np.array(aa) + 0.5
aa = aa/(2**p)
return aa
def Hilbert_Stratified_Proposal(particles, weights, t, multiple_des = 4, sd = 3):
# not done, add weights and rho; see Multiple_Descendent_Proposal
T = particles.shape[1]
size = particles.shape[0]
x_prop = np.zeros((size*multiple_des,T))
weight = np.zeros(size*multiple_des)
for ipar in range(size):
hh = [(uniform.rvs() + md)/multiple_des for md in range(multiple_des)]
vv = [Hilbert_Mapping_Inverse(h, dim = 1) for h in hh]
xx = [sd*norm.ppf(v) for v in vv]
for k in range(multiple_des):
x_prop[ipar*multiple_des+k] = particles[ipar]
x_prop[ipar*multiple_des+k,t] = xx[k]
weight[ipar*multiple_des+k] = log_target_f(t, x_prop[ipar*multiple_des+k]) - log_target_f(t-1, particles[ipar]) - norm.logpdf(x_prop[ipar*multiple_des+k,t], 0, sd)
x_prop = np.array(x_prop)
weight = weight - np.max(weight)
weight = np.exp(weight)
weight = weight/np.sum(weight)
for ipar in range(size):
for k in range(multiple_des):
weight[ipar*multiple_des+k] = weight[ipar*multiple_des+k] * weights[ipar]
weight = weight/np.sum(weight)
return x_prop, weight
def Sampling(rho, T = 10, size = 100, multiple_des = 4, sd = 3, prop = 'i.i.d.', resample = Hilbert_Resampling, print_step = True): #need modification
if print_step:
print("dimension "+ str(1) + "/" + str(T))
w = np.zeros(size)
xt1 = np.zeros((size,T))
xt1[:,0] = np.array([random.normal(0,sd) for _ in range(size)])
for i in range(size):
w[i] = log_target_f(0,xt1[i]) - norm.logpdf(xt1[i,0], 0, sd)
w = [x-max(w) for x in w]
w = [math.exp(x) for x in w]
w = [x/sum(w) for x in w]
w = np.array(w)
xt1, w = resample(xt1, w, size, 0, rho)
for t in range(1,T):
if print_step:
print("dimension "+ str(t+1) + "/" + str(T))
if prop == 'i.i.d.':
xt1star, w = Multiple_Descendent_Proposal(xt1, w, t, multiple_des, sd)
elif prop == 'SMG':
xt1star, w = Hilbert_Stratified_Proposal(xt1, w, t, multiple_des, sd)
if t<T-1:
xt1, w = resample(xt1star, w, size, t, rho)
if t==T-1:
return xt1star, w, np.linalg.norm(np.transpose(xt1star)@w)**2
# add variance of each step
def Adaptive_Sampling(ess_ratio, T = 10, size = 100, multiple_des = 4, sd = 3, prop = 'i.i.d.', resample = Hilbert_Resampling, print_step = True): #need modification
if print_step:
print("dimension "+ str(1) + "/" + str(T))
w = np.zeros(size)
xt1 = np.zeros((size,T))
xt1[:,0] = np.array([random.normal(0,sd) for _ in range(size)])
for i in range(size):
w[i] = log_target_f(0,xt1[i]) - norm.logpdf(xt1[i,0], 0, sd)
w = [x-max(w) for x in w]
w = [math.exp(x) for x in w]
w = [x/sum(w) for x in w]
w = np.array(w)
xt1, w = resample(xt1, w, size, 0, 1)
for t in range(1,T):
if print_step:
print("dimension "+ str(t+1) + "/" + str(T))
if prop == 'i.i.d.':
xt1star, w = Multiple_Descendent_Proposal(xt1, w, t, multiple_des, sd)
elif prop == 'SMG':
xt1star, w = Hilbert_Stratified_Proposal(xt1, w, t, multiple_des, sd)
if t<T-1 and 1/sum(w**2) < ess_ratio*size*multiple_des:
xt1, w = resample(xt1star, w, size, t, 1)
if t==T-1:
return xt1star, w, np.linalg.norm(np.transpose(xt1star)@w)**2