File IS_stat.py changed (mode: 100644) (index dbc921a..25af2b8) |
... |
... |
def bisect(f, target, low, high): |
29 |
29 |
return low |
return low |
30 |
30 |
|
|
31 |
31 |
|
|
32 |
|
# нам позарез нужен ещё один, свой собственный словник |
|
33 |
|
# ten, na rozdil od defaultdict'a, neuklada chybejicí složky do slovníku |
|
|
32 |
|
#ё нам позарез нужен ещё один, свой собственный словник |
|
33 |
|
#č ten, na rozdil od defaultdict'a, neuklada chybejicí složky do slovníku |
34 |
34 |
class DefaultDict(dict): |
class DefaultDict(dict): |
35 |
35 |
def __init__(self, default_value=None): |
def __init__(self, default_value=None): |
36 |
36 |
self.default_value = default_value |
self.default_value = default_value |
|
... |
... |
class DefaultDict(dict): |
39 |
39 |
return self.default_value |
return self.default_value |
40 |
40 |
|
|
41 |
41 |
|
|
42 |
|
# |
|
43 |
|
# deme na to, koťě! |
|
44 |
|
# |
|
|
42 |
|
# |
|
43 |
|
#č deme na to, koťě! |
|
44 |
|
# |
45 |
45 |
|
|
46 |
|
# IS, (n-2)-krátá realizace, n>>2, n→∞ |
|
|
46 |
|
#č IS, (n-2)-krátá realizace, n>>2, n→∞ |
47 |
47 |
def IS(f, h_model, space_from_h='R', space_to_f='G', Nsim=int(1e4)): |
def IS(f, h_model, space_from_h='R', space_to_f='G', Nsim=int(1e4)): |
48 |
48 |
""" |
""" |
49 |
49 |
space_from_h |
space_from_h |
|
... |
... |
def IS(f, h_model, space_from_h='R', space_to_f='G', Nsim=int(1e4)): |
68 |
68 |
return CandyBox(to_sample, w=w) |
return CandyBox(to_sample, w=w) |
69 |
69 |
|
|
70 |
70 |
|
|
|
71 |
|
def IS_norm(f, mean=0, std=1, sampling_space='G', nis=1000, design=None): |
|
72 |
|
""" |
|
73 |
|
mean: [0.05, 2, 100500] |
|
74 |
|
std: [0.05, 2, 100500] |
|
75 |
|
|
|
76 |
|
design(nis, nvar) should return sampling plan in standard Gaussian space! |
|
77 |
|
""" |
|
78 |
|
|
|
79 |
|
if design is None: |
|
80 |
|
sampling_plan_G = np.random.randn(nis, f.nvar) |
|
81 |
|
else: |
|
82 |
|
sampling_plan_G = design(nis, f.nvar) |
|
83 |
|
|
|
84 |
|
|
|
85 |
|
#č pdf spočteme na původním Gaussovském designu |
|
86 |
|
# sample_pdf(sample / alpha) / np.prod(alpha) |
|
87 |
|
|
|
88 |
|
## desired: pdf = np.prod(stats.norm.pdf(sampling_plan_G) / std, axis=1) |
|
89 |
|
pdf = stats.norm.pdf(sampling_plan_G) |
|
90 |
|
pdf = np.divide(pdf, std, out=pdf) |
|
91 |
|
pdf = np.prod(pdf, axis=1) |
|
92 |
|
|
|
93 |
|
|
|
94 |
|
#č a teď pustíme se do výpočtu souřadnic |
|
95 |
|
## desired: sampling_plan_N = (sampling_plan_G * std) + mean |
|
96 |
|
sampling_plan_N = sampling_plan_G; del(sampling_plan_G) |
|
97 |
|
sampling_plan_N = np.multiply(sampling_plan_N, std, out=sampling_plan_N) |
|
98 |
|
sampling_plan_N = np.add(sampling_plan_N, mean, out=sampling_plan_N) |
|
99 |
|
|
|
100 |
|
# tady musíme provést jeden trik |
|
101 |
|
# totež jako v IS_like - ve výsledku dycky dostaneme f_model |
|
102 |
|
to_sample = f.new_sample(sampling_plan_N, sampling_space) #č naše N-ko smerdžíme ako G-čko |
|
103 |
|
w = to_sample.pdf(sampling_space) / pdf # snad je to správně |
|
104 |
|
|
|
105 |
|
# vahy máme |
|
106 |
|
# zabalme do boxu |
|
107 |
|
# zbytek už nejsou naši starosti |
|
108 |
|
return CandyBox(to_sample, w=w) |
71 |
109 |
|
|
72 |
110 |
|
|
73 |
111 |
|
|
|
... |
... |
def IS_like(f_plan, sampling_space='G', weights=None, nis=1000, d=1): |
117 |
155 |
return CandyBox(h_plan, w=w) |
return CandyBox(h_plan, w=w) |
118 |
156 |
|
|
119 |
157 |
|
|
120 |
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
|
# for simplex: d = nvar+2 |
|
128 |
|
# for cell: d = base_r**2 |
|
129 |
|
#def sample_like(plan, weights=None, nis=1000, d=1): |
|
130 |
|
# """ |
|
131 |
|
# takes coordinates and returns coordinates of the same cov sampling plan |
|
132 |
|
# covariance matrix we'll divide by d |
|
133 |
|
# """ |
|
134 |
|
# |
|
135 |
|
# |
|
136 |
|
# S_bc = np.cov(plan, rowvar=False, bias=True, aweights=weights) |
|
137 |
|
# barycenter = np.average(plan, axis=0, weights=weights) |
|
138 |
|
# |
|
139 |
|
# |
|
140 |
|
# # matika |
|
141 |
|
# w, v = np.linalg.eig(S_bc) |
|
142 |
|
# |
|
143 |
|
# # use IS sampling density with center equal to the simplex's barycenter |
|
144 |
|
# # set the minimum distance as the standard deviation of IS densisty |
|
145 |
|
# # u stats.norm zadáváme směrodatnou odchylku, to je asi správné |
|
146 |
|
# sigmas = np.sqrt(w/d) |
|
147 |
|
# h_i = [stats.norm(0, sigma) for sigma in sigmas] |
|
148 |
|
# # rozdělení ve vlastním prostoru |
|
149 |
|
# # select nis = 100 points from IS density |
|
150 |
|
# h_L = f_models.UnCorD(h_i)(nis) |
|
151 |
|
# |
|
152 |
|
# # здесь уже так легко не отделаемся. Трансформовать кароно. |
|
153 |
|
# h_plan_bc = (v @ h_L.R.T).T |
|
154 |
|
# h_plan_sing = h_plan_bc + barycenter |
|
155 |
|
# |
|
156 |
|
# |
|
157 |
|
# return h_plan_sing |
|
158 |
|
|
|
159 |
|
|
|
160 |
|
|
|
161 |
158 |
|
|
162 |
159 |
|
|
163 |
160 |
|
|