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 import numpy as np
35 from .domains import _POSITIVE,_NEGATIVE, _BOUNDED
36 import weakref
37
38 import sys
39
40 _log_lim_val = np.log(np.finfo(np.float64).max)
41 _exp_lim_val = np.finfo(np.float64).max
42 _lim_val = 36.0
43 epsilon = np.finfo(np.float64).resolution
44
45
46
47 __fixed__ = "fixed"
48 FIXED = False
49 UNFIXED = True
50
51
52
102
104 domain = _POSITIVE
107
109 return np.where(f>_lim_val, f, np.log(np.expm1(f)))
111 return df*np.where(f>_lim_val, 1., - np.expm1(-f))
113 if np.any(f < 0.):
114 print("Warning: changing parameters to satisfy constraints")
115 return np.abs(f)
117 return np.where(model_param>_lim_val, model_param, np.log(np.expm1(model_param))) - model_param
119 return 1./(np.expm1(model_param))
122
124 domain = _POSITIVE
130 return np.einsum('i,i->i', df, f)
132 if np.any(f < 0.):
133 print("Warning: changing parameters to satisfy constraints")
134 return np.abs(f)
136 return np.log(model_param)
138 return 1./model_param
141
142
143
145 "Do not use, not officially supported!"
146 _instances = []
147 - def __new__(cls, mu_indices=None, var_indices=None):
148 "Do not use, not officially supported!"
149 if cls._instances:
150 cls._instances[:] = [instance for instance in cls._instances if instance()]
151 for instance in cls._instances:
152 if np.all(instance().mu_indices==mu_indices, keepdims=False) and np.all(instance().var_indices==var_indices, keepdims=False):
153 return instance()
154 o = super(Transformation, cls).__new__(cls, mu_indices, var_indices)
155 cls._instances.append(weakref.ref(o))
156 return cls._instances[-1]()
157
158 - def __init__(self, mu_indices, var_indices):
159 self.mu_indices = mu_indices
160 self.var_indices = var_indices
161
162 - def f(self, theta):
163
164
165
166
167 theta[self.var_indices] = np.abs(-.5/theta[self.var_indices])
168
169 theta[self.mu_indices] *= theta[self.var_indices]
170 return theta
171
172 - def finv(self, muvar):
173
174 varp = muvar[self.var_indices]
175 muvar[self.mu_indices] /= varp
176 muvar[self.var_indices] = -.5/varp
177
178
179 return muvar
180
182 mu = muvar[self.mu_indices]
183 var = muvar[self.var_indices]
184
185
186
187 dmuvar[self.mu_indices] *= var
188 dmuvar[self.var_indices] *= 2*(var)**2
189 dmuvar[self.var_indices] += 2*dmuvar[self.mu_indices]*mu
190
191
192 return dmuvar
193
195 if np.any(f[self.var_indices] < 0.):
196 print("Warning: changing parameters to satisfy constraints")
197 f[self.var_indices] = np.abs(f[self.var_indices])
198 return f
199
202
204 return [self.mu_indices, self.var_indices]
205
207 self.mu_indices = state[0]
208 self.var_indices = state[1]
209
211 "Do not use, not officially supported!"
212 _instances = []
213 - def __new__(cls, mu_indices=None, var_indices=None):
214 "Do not use, not officially supported!"
215 if cls._instances:
216 cls._instances[:] = [instance for instance in cls._instances if instance()]
217 for instance in cls._instances:
218 if np.all(instance().mu_indices==mu_indices, keepdims=False) and np.all(instance().var_indices==var_indices, keepdims=False):
219 return instance()
220 o = super(Transformation, cls).__new__(cls, mu_indices, var_indices)
221 cls._instances.append(weakref.ref(o))
222 return cls._instances[-1]()
223
224 - def __init__(self, mu_indices, var_indices):
225 self.mu_indices = mu_indices
226 self.var_indices = var_indices
227
229 mu = muvar[self.mu_indices]
230 var = muvar[self.var_indices]
231
232
233
234
235 dmuvar[self.mu_indices] *= var
236 dmuvar[self.var_indices] *= 2*var**2
237
238
239 return dmuvar
240
242 if np.any(f[self.var_indices] < 0.):
243 print("Warning: changing parameters to satisfy constraints")
244 f[self.var_indices] = np.abs(f[self.var_indices])
245 return f
246
249
251 "Do not use, not officially supported!"
252 _instances = []
253 - def __new__(cls, mu_indices=None, var_indices=None):
254 "Do not use, not officially supported!"
255 if cls._instances:
256 cls._instances[:] = [instance for instance in cls._instances if instance()]
257 for instance in cls._instances:
258 if np.all(instance().mu_indices==mu_indices, keepdims=False) and np.all(instance().var_indices==var_indices, keepdims=False):
259 return instance()
260 o = super(Transformation, cls).__new__(cls, mu_indices, var_indices)
261 cls._instances.append(weakref.ref(o))
262 return cls._instances[-1]()
263
264 - def __init__(self, mu_indices, var_indices):
265 self.mu_indices = mu_indices
266 self.var_indices = var_indices
267
268 - def f(self, theta):
269 theta[self.var_indices] = np.abs(theta[self.var_indices] - theta[self.mu_indices]**2)
270 return theta
271
272 - def finv(self, muvar):
273 muvar[self.var_indices] += muvar[self.mu_indices]**2
274 return muvar
275
277 mu = muvar[self.mu_indices]
278
279
280 dmuvar[self.mu_indices] -= 2*mu*dmuvar[self.var_indices]
281
282 return dmuvar
283
285 if np.any(f[self.var_indices] < 0.):
286 print("Warning: changing parameters to satisfy constraints")
287 f[self.var_indices] = np.abs(f[self.var_indices])
288 return f
289
292
294 "Do not use, not officially supported!"
295 _instances = []
296 - def __new__(cls, mu_indices=None, var_indices=None):
297 "Do not use, not officially supported!"
298 if cls._instances:
299 cls._instances[:] = [instance for instance in cls._instances if instance()]
300 for instance in cls._instances:
301 if np.all(instance().mu_indices==mu_indices, keepdims=False) and np.all(instance().var_indices==var_indices, keepdims=False):
302 return instance()
303 o = super(Transformation, cls).__new__(cls, mu_indices, var_indices)
304 cls._instances.append(weakref.ref(o))
305 return cls._instances[-1]()
306
307 - def __init__(self, mu_indices, var_indices):
308 self.mu_indices = mu_indices
309 self.var_indices = var_indices
310
312 mu = muvar[self.mu_indices]
313 var = muvar[self.var_indices]
314
315
316
317 dmuvar[self.mu_indices] -= 2*mu*dmuvar[self.var_indices]
318
319
320
321
322
323
324
325 return dmuvar
326
328 mu = muvar[self.mu_indices]
329 var = muvar[self.var_indices]
330
331
332
333 dmuvar[self.mu_indices] *= var
334 dmuvar[self.var_indices] *= 2*(var)**2
335 dmuvar[self.var_indices] += 2*dmuvar[self.mu_indices]*mu
336
337
338 return dmuvar
339
342
343
345 "Do not use, not officially supported!"
346 _instances = []
347 - def __new__(cls, mu_indices=None, var_indices=None):
348 "Do not use, not officially supported!"
349 if cls._instances:
350 cls._instances[:] = [instance for instance in cls._instances if instance()]
351 for instance in cls._instances:
352 if np.all(instance().mu_indices==mu_indices, keepdims=False) and np.all(instance().var_indices==var_indices, keepdims=False):
353 return instance()
354 o = super(Transformation, cls).__new__(cls, mu_indices, var_indices)
355 cls._instances.append(weakref.ref(o))
356 return cls._instances[-1]()
357
358 - def __init__(self, mu_indices, var_indices):
359 self.mu_indices = mu_indices
360 self.var_indices = var_indices
361
363
364
365
366
367
368
369
370
371
372
373
374
375
376 return dmuvar
377
380
382 "Do not use, not officially supported!"
383 _instances = []
384 - def __new__(cls, mu_indices=None, var_indices=None):
385 "Do not use, not officially supported!"
386 if cls._instances:
387 cls._instances[:] = [instance for instance in cls._instances if instance()]
388 for instance in cls._instances:
389 if np.all(instance().mu_indices==mu_indices, keepdims=False) and np.all(instance().var_indices==var_indices, keepdims=False):
390 return instance()
391 o = super(Transformation, cls).__new__(cls, mu_indices, var_indices)
392 cls._instances.append(weakref.ref(o))
393 return cls._instances[-1]()
394
395 - def __init__(self, mu_indices, var_indices):
396 self.mu_indices = mu_indices
397 self.var_indices = var_indices
398
400 mu = muvar[self.mu_indices]
401 var = muvar[self.var_indices]
402
403
404
405 dmuvar[self.mu_indices] *= var
406 dmuvar[self.var_indices] *= 2*(var)**2
407 dmuvar[self.var_indices] += 2*dmuvar[self.mu_indices]*mu
408
409 return dmuvar
410
413
414
416 domain = _POSITIVE
418 return np.where(x>_lim_val, -x, -np.log(1. + np.exp(np.clip(x, -np.inf, _lim_val))))
419
421 return np.where(f>_lim_val, 0, np.log(np.exp(-f) - 1.))
423 return np.einsum('i,i->i', df, np.where(f>_lim_val, -1, -1 + np.exp(-f)))
425 if np.any(f < 0.):
426 print("Warning: changing parameters to satisfy constraints")
427 return np.abs(f)
430
431
445
482
495
508
510 domain = _BOUNDED
511 _instances = []
512 - def __new__(cls, lower=1e-6, upper=1e-6, *args, **kwargs):
513 if cls._instances:
514 cls._instances[:] = [instance for instance in cls._instances if instance()]
515 for instance in cls._instances:
516 if instance().lower == lower and instance().upper == upper:
517 return instance()
518 newfunc = super(Transformation, cls).__new__
519 if newfunc is object.__new__:
520 o = newfunc(cls)
521 else:
522 o = newfunc(cls, lower, upper, *args, **kwargs)
523 cls._instances.append(weakref.ref(o))
524 return cls._instances[-1]()
526 assert lower < upper
527 self.lower, self.upper = float(lower), float(upper)
528 self.difference = self.upper - self.lower
530 if (x<-300.).any():
531 x = x.copy()
532 x[x<-300.] = -300.
533 return self.lower + self.difference / (1. + np.exp(-x))
535 return np.log(np.clip(f - self.lower, 1e-10, np.inf) / np.clip(self.upper - f, 1e-10, np.inf))
537 return np.einsum('i,i->i', df, (f - self.lower) * (self.upper - f) / self.difference)
539 if np.any(np.logical_or(f < self.lower, f > self.upper)):
540 print("Warning: changing parameters to satisfy constraints")
541
542
543 return np.where(np.logical_or(f < self.lower, f > self.upper), self.f(np.zeros_like(f)), f)
545 return '{},{}'.format(self.lower, self.upper)
546