-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhgf.py
1196 lines (966 loc) · 35.2 KB
/
hgf.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# hgf.py
#"""The HGF time series model."""
import numpy as np
import warnings
# Turn warnings into exceptions
#warnings.simplefilter('error')
# Generic HGF model
class Model(object):
"""Generic HGF model"""
def __init__(self):
self._nodes = []
@property
def nodes(self):
return self._nodes
@property
def input_nodes(self):
input_nodes = []
for node in self.nodes:
if (isinstance(node, InputNode) or
isinstance(node, BinaryInputNode)):
input_nodes.append(node)
return input_nodes
@property
def params(self):
params = []
for node in self.nodes:
params.extend(node.params)
return params
@property
def var_params(self):
var_params = []
for param in self.params:
tpp = param.trans_prior_precision
if tpp is not None and tpp is not np.inf:
var_params.append(param)
return var_params
@property
def param_values(self):
return [param.value for param in self.params]
@param_values.setter
def param_values(self, values):
for i, param in enumerate(self.params):
param.value = values[i]
@property
def var_param_values(self):
return [var_param.value for var_param in self.var_params]
@var_param_values.setter
def var_param_values(self, values):
for i, var_param in enumerate(self.var_params):
var_param.value = values[i]
@property
def param_trans_values(self):
return [param.trans_value for param in self.params]
@param_trans_values.setter
def param_trans_values(self, trans_values):
for i, param in enumerate(self.params):
param.trans_value = trans_values[i]
@property
def var_param_trans_values(self):
return [var_param.trans_value for var_param in self.var_params]
@var_param_trans_values.setter
def var_param_trans_values(self, trans_values):
for i, var_param in enumerate(self.var_params):
var_param.trans_value = trans_values[i]
def add_state_node(self,
*,
initial_mu,
initial_pi,
rho=0,
phi=0,
m=0,
omega=0):
node = StateNode(initial_mu=initial_mu,
initial_pi=initial_pi,
rho=rho,
phi=phi,
m=m,
omega=omega)
self._nodes.append(node)
return node
def add_binary_node(self):
node = BinaryNode()
self._nodes.append(node)
return node
def add_input_node(self, *, omega=0):
node = InputNode(omega=omega)
self._nodes.append(node)
return node
def add_binary_input_node(self, *, pihat=np.inf, eta0=0, eta1=1):
node = BinaryInputNode(pihat=pihat, eta0=eta0, eta1=eta1)
self._nodes.append(node)
return node
def reset(self):
for input_node in self.input_nodes:
input_node.reset_hierarchy()
def undo_last_reset(self):
for input_node in self.input_nodes:
input_node.undo_last_reset_hierarchy()
def recalculate(self):
for input_node in self.input_nodes:
input_node.recalculate()
def surprise(self):
surprise = 0
for input_node in self.input_nodes:
surprise += sum(input_node.surprises)
return surprise
def log_prior(self):
if self.var_params:
log_prior = 0
for var_param in self.var_params:
log_prior += var_param.log_prior()
return log_prior
else:
raise ModelConfigurationError(
'No variable (i.e., non-fixed) parameters.')
def log_joint(self):
return -self.surprise() + self.log_prior()
def neg_log_joint_function(self):
def f(trans_values):
trans_values_backup = self.var_param_trans_values
self.var_param_trans_values = trans_values
try:
self.recalculate()
return -self.log_joint()
except HgfUpdateError:
self.var_param_trans_values = trans_values_backup
return np.inf
return f
# Standard 2-level HGF for continuous inputs
class StandardHGF(Model):
"""The standard 2-level HGF for continuous inputs"""
def __init__(self,
*,
initial_mu1,
initial_pi1,
initial_mu2,
initial_pi2,
omega1,
kappa1,
omega2,
omega_input,
rho1=0,
rho2=0,
phi1=0,
m1=0,
phi2=0,
m2=0):
# Superclass initialization
super().__init__()
# Set up nodes and their relationships
self.x2 = self.add_state_node(initial_mu=initial_mu2,
initial_pi=initial_pi2,
omega=omega2,
rho=rho2,
phi=phi2,
m=m2)
self.x1 = self.add_state_node(initial_mu=initial_mu1,
initial_pi=initial_pi1,
omega=omega1,
rho=rho1,
phi=phi1,
m=m1)
self.xU = self.add_input_node(omega=omega_input)
self.x1.add_volatility_parent(parent=self.x2, kappa=kappa1)
self.xU.set_value_parent(parent=self.x1)
# Input method
def input(self, inputs):
self.xU.input(inputs)
# Standard 3-level HGF for binary inputs
class StandardBinaryHGF(Model):
"""The standard 3-level HGF for binary inputs"""
def __init__(self,
*,
initial_mu2,
initial_pi2,
initial_mu3,
initial_pi3,
omega2,
kappa2,
omega3,
pihat_input=np.inf,
eta0=0,
eta1=1,
rho2=0,
rho3=0,
phi2=0,
m2=0,
phi3=0,
m3=0):
# Superclass initialization
super().__init__()
# Set up nodes and their relationships
self.x3 = self.add_state_node(initial_mu=initial_mu3,
initial_pi=initial_pi3,
omega=omega3,
rho=rho3,
phi=phi3,
m=m3)
self.x2 = self.add_state_node(initial_mu=initial_mu2,
initial_pi=initial_pi2,
omega=omega2,
rho=rho2,
phi=phi2,
m=m2)
self.x1 = self.add_binary_node()
self.xU = self.add_binary_input_node(pihat=pihat_input,
eta0=eta0,
eta1=eta1)
self.x2.add_volatility_parent(parent=self.x3, kappa=kappa2)
self.x1.set_parent(parent=self.x2)
self.xU.set_parent(parent=self.x1)
# Input method
def input(self, inputs):
self.xU.input(inputs)
# HGF continuous state node
class StateNode(object):
"""HGF continuous state node"""
def __init__(self,
*,
initial_mu,
initial_pi,
rho=0,
phi=0,
m=0,
omega=0):
# Sanity check
if rho and phi:
raise NodeConfigurationError(
'hgf.StateNode: rho (drift) and phi (AR(1) parameter) may ' +
'not be non-zero at the same time.')
# Initialize parameter attributes
self.initial_mu = Parameter(value=initial_mu)
self.initial_pi = Parameter(value=initial_pi, space='log')
self.rho = Parameter(value=rho)
self.phi = Parameter(value=phi, space='logit')
self.m = Parameter(value=m)
self.omega = Parameter(value=omega)
self.psis = []
self.kappas = []
# Initialize parents
self.va_pas = []
self.vo_pas = []
# Initialize time series
self.times = [0]
self.pihats = [None]
self.pis = [self.initial_pi.value]
self.muhats = [None]
self.mus = [self.initial_mu.value]
self.nus = [None]
@property
def parents(self):
parents = []
parents.extend(self.va_pas)
parents.extend(self.vo_pas)
return parents
@property
def params(self):
params = [self.initial_mu,
self.initial_pi,
self.rho,
self.phi,
self.m,
self.omega]
params.extend(self.psis)
params.extend(self.kappas)
return params
def reset(self):
self._times_backup = self.times
self.times = [0]
self._pihats_backup = self.pihats
self.pihats = [None]
self._pis_backup = self.pis
self.pis = [self.initial_pi.value]
self._muhats_backup = self.muhats
self.muhats = [None]
self._mus_backup = self.mus
self.mus = [self.initial_mu.value]
self._nus_backup = self.nus
self.nus = [None]
def undo_last_reset(self):
self.times = self._times_backup
self.pihats = self._pihats_backup
self.pis = self._pis_backup
self.muhats = self._muhats_backup
self.mus = self._mus_backup
self.nus = self._nus_backup
def reset_hierarchy(self):
self.reset()
for pa in self.parents:
pa.reset_hierarchy()
def undo_last_reset_hierarchy(self):
self.undo_last_reset()
for pa in self.parents:
pa.undo_last_reset_hierarchy()
def add_value_parent(self, *, parent, psi):
self.va_pas.append(parent)
self.psis.append(Parameter(value=psi))
def add_volatility_parent(self, *, parent, kappa):
self.vo_pas.append(parent)
self.kappas.append(Parameter(value=kappa, space='log'))
def new_muhat(self, time):
t = time - self.times[-1]
driftrate = self.rho.value
for i, va_pa in enumerate(self.va_pas):
driftrate += self.psis[i].value * self.va_pas[i].mus[-1]
return self.mus[-1] + t * driftrate
def _new_nu(self, time):
t = time - self.times[-1]
logvol = self.omega.value
for i, vo_pa in enumerate(self.vo_pas):
logvol += self.kappas[i].value * self.vo_pas[i].mus[-1]
nu = t * np.exp(logvol)
if nu > 1e-128:
return nu
else:
raise HgfUpdateError(
'Nu is zero. Parameters values are in region where model\n' +
'assumptions are violated.')
def new_pihat_nu(self, time):
new_nu = self._new_nu(time)
return [1 / (1 / self.pis[-1] + new_nu), new_nu]
def vape(self):
return self.mus[-1] - self.muhats[-1]
def vope(self):
return ((1 / self.pis[-1] + self.vape()**2) *
self.pihats[-1] - 1)
def update_parents(self, time):
va_pas = self.va_pas
vo_pas = self.vo_pas
if not va_pas and not vo_pas:
return
pihat = self.pihats[-1]
# Update value parents
psis = self.psis
vape = self.vape()
for i, va_pa in enumerate(va_pas):
pihat_pa, nu_pa = va_pa.new_pihat_nu(time)
pi_pa = pihat_pa + psis[i].value**2 * pihat
muhat_pa = va_pa.new_muhat(time)
mu_pa = muhat_pa + psis[i].value * pihat / pi_pa * vape
va_pa.update(time, pihat_pa, pi_pa, muhat_pa, mu_pa, nu_pa)
# Update volatility parents
nu = self.nus[-1]
kappas = self.kappas
vope = self.vope()
for i, vo_pa in enumerate(vo_pas):
pihat_pa, nu_pa = vo_pa.new_pihat_nu(time)
pi_pa = pihat_pa + 0.5 * (kappas[i].value * nu * pihat)**2 * \
(1 + (1 - 1 / (nu * self.pis[-2])) * vope)
if pi_pa <= 0:
raise HgfUpdateError(
'Negative posterior precision. Parameters values are\n' +
'in a region where model assumptions are violated.')
muhat_pa = vo_pa.new_muhat(time)
mu_pa = (muhat_pa +
0.5 * kappas[i].value * nu * pihat / pi_pa * vope)
vo_pa.update(time, pihat_pa, pi_pa, muhat_pa, mu_pa, nu_pa)
def update(self, time, pihat, pi, muhat, mu, nu):
self.times.append(time)
self.pihats.append(pihat)
self.pis.append(pi),
self.muhats.append(muhat)
self.mus.append(mu)
self.nus.append(nu)
self.update_parents(time)
# HGF binary state node
class BinaryNode(object):
"""HGF binary state node"""
def __init__(self):
# Initialize parent
self.pa = None
# Initialize time series
self.times = [0]
self.pihats = [None]
self.pis = [None]
self.muhats = [None]
self.mus = [None]
@property
def parents(self):
parents = []
if self.pa:
parents.append(self.pa)
return parents
@property
def params(self):
return []
def reset(self):
self._times_backup = self.times
self.times = [0]
self._pihats_backup = self.pihats
self.pihats = [None]
self._pis_backup = self.pis
self.pis = [None]
self._muhats_backup = self.muhats
self.muhats = [None]
self._mus_backup = self.mus
self.mus = [None]
def undo_last_reset(self):
self.times = self._times_backup
self.pihats = self._pihats_backup
self.pis = self._pis_backup
self.muhats = self._muhats_backup
self.mus = self._mus_backup
def reset_hierarchy(self):
self.reset()
for pa in self.parents:
pa.reset_hierarchy()
def undo_last_reset_hierarchy(self):
self.undo_last_reset()
for pa in self.parents:
pa.undo_last_reset_hierarchy()
def set_parent(self, *, parent):
self.pa = parent
def new_muhat_pihat(self, time):
muhat_pa = self.pa.new_muhat(time)
muhat = sgm(muhat_pa)
pihat = 1 / (muhat * (1 - muhat))
return [muhat, pihat]
def vape(self):
return self.mus[-1] - self.muhats[-1]
def update_parent(self, time):
pa = self.pa
if not pa:
return
pihat = self.pihats[-1]
# Update parent
vape = self.vape()
pihat_pa, nu_pa = pa.new_pihat_nu(time)
pi_pa = pihat_pa + 1 / pihat
muhat_pa = pa.new_muhat(time)
mu_pa = muhat_pa + vape / pi_pa
pa.update(time, pihat_pa, pi_pa, muhat_pa, mu_pa, nu_pa)
def update(self, time, pihat, pi, muhat, mu):
self.times.append(time)
self.pihats.append(pihat)
self.pis.append(pi),
self.muhats.append(muhat)
self.mus.append(mu)
self.update_parent(time)
# HGF continuous input nodes
class InputNode(object):
"""An HGF node that receives input on a continuous scale"""
def __init__(self, *, omega):
# Incorporate parameter attributes
self.omega = Parameter(value=omega)
self.kappa = None
# Initialize parents
self.va_pa = None
self.vo_pa = None
# Initialize time series
self.times = [0]
self.inputs = [None]
self.inputs_with_times = [(None, 0)]
self.surprises = [0]
@property
def parents(self):
parents = []
if self.va_pa:
parents.append(self.va_pa)
if self.vo_pa:
parents.append(self.vo_pa)
return parents
@property
def params(self):
params = [self.omega]
if self.kappa is not None:
params.append(self.kappa)
return params
def reset(self):
self._times_backup = self.times
self.times = [0]
self._inputs_backup = self.inputs
self.inputs = [None]
self._inputs_with_times_backup = self.inputs_with_times
self.inputs_with_times = [(None, 0)]
self._surprises_backup = self.surprises
self.surprises = [0]
def undo_last_reset(self):
self.times = self._times_backup
self.inputs = self._inputs_backup
self.inputs_with_times = self._inputs_with_times_backup
self.surprises = self._surprises_backup
def reset_hierarchy(self):
self.reset()
for pa in self.parents:
pa.reset_hierarchy()
def undo_last_reset_hierarchy(self):
self.undo_last_reset()
for pa in self.parents:
pa.undo_last_reset_hierarchy()
def recalculate(self):
iwt = list(self.inputs_with_times[1:])
self.reset_hierarchy()
try:
self.input(iwt)
except HgfUpdateError as e:
self.undo_last_reset_hierarchy()
raise e
def set_value_parent(self, *, parent):
self.va_pa = parent
def set_volatility_parent(self, *, parent, kappa):
self.vo_pa = parent
self.kappa = Parameter(value=kappa, space='log')
# Update parents and return surprise
def update_parents(self, value, time):
va_pa = self.va_pa
vo_pa = self.vo_pa
if not vo_pa and not va_pa:
return
lognoise = self.omega.value
kappa = None
if self.kappa is not None:
kappa = self.kappa.value
if kappa is not None:
lognoise += kappa * vo_pa.mu[-1]
pihat = 1 / np.exp(lognoise)
# Update value parent
pihat_va_pa, nu_va_pa = va_pa.new_pihat_nu(time)
pi_va_pa = pihat_va_pa + pihat
muhat_va_pa = va_pa.new_muhat(time)
vape = value - muhat_va_pa
mu_va_pa = muhat_va_pa + pihat / pi_va_pa * vape
va_pa.update(time, pihat_va_pa, pi_va_pa, muhat_va_pa,
mu_va_pa, nu_va_pa)
# Update volatility parent
if vo_pa is not None:
vope = (1 / pi_va_pa + (value - mu_va_pa)**2) * pihat - 1
pihat_vo_pa, nu_vo_pa = vo_pa.new_pihat_nu(time)
pi_vo_pa = pihat_vo_pa + 0.5 * kappa**2 * (1 + vope)
if pi_vo_pa <= 0:
raise HgfUpdateError(
'Negative posterior precision. Parameters values are\n' +
'in a region where model assumptions are violated.')
muhat_vo_pa = vo_pa.new_muhat(time)
mu_vo_pa = muhat_vo_pa + 0.5 * kappa / pi_vo_pa * vope
vo_pa.update(time, pihat_vo_pa, pi_vo_pa, muhat_vo_pa,
mu_vo_pa, nu_vo_pa)
return gaussian_surprise(value, muhat_va_pa, pihat)
def _single_input(self, value, time):
self.times.append(time)
self.inputs.append(value)
self.inputs_with_times.append((value, time))
self.surprises.append(self.update_parents(value, time))
def input(self, inputs):
try:
for input in inputs:
try:
value = input[0]
time = input[1]
except IndexError:
value = input
time = self.times[-1] + 1
self._single_input(value, time)
except TypeError:
value = inputs
time = self.times[-1] + 1
self._single_input(value, time)
# HGF binary input nodes
class BinaryInputNode(object):
"""An HGF node that receives binary input"""
def __init__(self,
*,
pihat=np.inf,
eta0=0,
eta1=1):
# Incorporate parameter attributes
self.pihat = Parameter(value=pihat, space='log')
self.eta0 = Parameter(value=eta0)
self.eta1 = Parameter(value=eta1)
# Initialize parent
self.pa = None
# Initialize time series
self.times = [0]
self.inputs = [None]
self.inputs_with_times = [(None, 0)]
self.surprises = [0]
@property
def parents(self):
parents = []
if self.pa is not None:
parents.append(self.pa)
return parents
@property
def params(self):
return [self.pihat,
self.eta0,
self.eta1]
def reset(self):
self._times_backup = self.times
self.times = [0]
self._inputs_backup = self.inputs
self.inputs = [None]
self._inputs_with_times_backup = self.inputs_with_times
self.inputs_with_times = [(None, 0)]
self._surprises_backup = self.surprises
self.surprises = [0]
def undo_last_reset(self):
self.times = self._times_backup
self.inputs = self._inputs_backup
self.inputs_with_times = self._inputs_with_times_backup
self.surprises = self._surprises_backup
def reset_hierarchy(self):
self.reset()
for pa in self.parents:
pa.reset_hierarchy()
def undo_last_reset_hierarchy(self):
self.undo_last_reset()
for pa in self.parents:
pa.undo_last_reset_hierarchy()
def recalculate(self):
iwt = list(self.inputs_with_times[1:])
self.reset_hierarchy()
try:
self.input(iwt)
except HgfUpdateError as e:
self.undo_last_reset_hierarchy()
raise e
def set_parent(self, *, parent):
self.pa = parent
def update_parent(self, value, time):
pa = self.pa
if not pa:
return
surprise = 0
pihat = self.pihat.value
muhat_pa, pihat_pa = pa.new_muhat_pihat(time)
if pihat == np.inf:
# Just pass the value through in the absence of noise
mu_pa = value
pi_pa = np.inf
surprise = binary_surprise(value, muhat_pa)
else:
eta1 = self.eta1.value
eta0 = self.eta0.value
# Likelihood under eta1
und1 = np.exp(-pihat / 2 * (value - eta1)**2)
# Likelihood under eta0
und0 = np.exp(-pihat / 2 * (value - eta0)**2)
# Eq. 39 in Mathys et al. (2014) (i.e., Bayes)
mu_pa = muhat_pa * und1 / (muhat_pa * und1 + (1 - muhat_pa) * und0)
pi_pa = 1 / (mu_pa * (1 - mu_pa))
# Surprise
surprise = (-np.log(muhat_pa * gaussian(value, eta1, pihat) +
(1 - muhat_pa) * gaussian(value, eta0, pihat)))
pa.update(time, pihat_pa, pi_pa, muhat_pa, mu_pa)
return surprise
def _single_input(self, value, time):
self.times.append(time)
self.inputs.append(value)
self.inputs_with_times.append((value, time))
self.surprises.append(self.update_parent(value, time))
def input(self, inputs):
try:
for input in inputs:
try:
value = input[0]
time = input[1]
except IndexError:
value = input
time = self.times[-1] + 1
finally:
self._single_input(value, time)
except TypeError:
value = inputs
time = self.times[-1] + 1
self._single_input(value, time)
class Parameter(object):
"""Parameters of nodes"""
def __init__(self,
*,
space='native',
lower_bound=None,
upper_bound=None,
value=None,
trans_value=None,
prior_mean=None,
trans_prior_mean=None,
trans_prior_precision=None):
# Initialize attributes
self.space = space
if lower_bound is not None:
self.lower_bound = lower_bound
if upper_bound is not None:
self.upper_bound = upper_bound
if value is not None and trans_value is not None:
raise ParameterConfigurationError(
'Only one of value and trans_value can be given.')
elif value is not None:
self.value = value
elif trans_value is not None:
self.trans_value = trans_value
else:
raise ParameterConfigurationError(
'One of value and trans_value must be given.')
if prior_mean is not None and trans_prior_mean is not None:
raise ParameterConfigurationError(
'Only one of prior_mean and trans_prior_mean can be given.')
elif prior_mean is not None:
self.prior_mean = prior_mean
else:
self.trans_prior_mean = trans_prior_mean
if (trans_prior_precision is None and
(prior_mean is not None or
trans_prior_mean is not None)):
raise ParameterConfigurationError(
'trans_prior_precision must be given if prior_mean ' +
'or trans_prior_mean is given')
else:
self.trans_prior_precision = trans_prior_precision
@property
def space(self):
return self._space
@space.setter
def space(self, space):
if space == 'native':
self._space = space
self._lower_bound = None
self._upper_bound = None
elif space == 'log':
self._space = space
self._lower_bound = 0
self._upper_bound = None
elif space == 'logit':
self._space = space
self._lower_bound = 0
self._upper_bound = 1
else:
raise ParameterConfigurationError(
"Space must be one of 'native, 'log', or 'logit'")
# Recalculate trans_value
try:
self.value = self._value
except AttributeError:
pass
# Recalculate trans_prior_mean
try:
self.prior_mean = self._prior_mean
except AttributeError:
pass
@property
def lower_bound(self):
return self._lower_bound
@lower_bound.setter
def lower_bound(self, lower_bound):
space = self.space
if lower_bound is not None and space == 'native':
raise ParameterConfigurationError(
"lower_bound must be None if space == 'native'.")
elif lower_bound is not None and lower_bound > self.value:
raise ParameterConfigurationError(
'lower_bound may not be greater than current value')
elif space == 'log':
self._lower_bound = lower_bound
self._upper_bound = None
else:
self._lower_bound = lower_bound
# Recalculate trans_value
try:
self.value = self._value
except AttributeError:
pass
# Recalculate trans_prior_mean
try:
self.prior_mean = self._prior_mean
except AttributeError:
pass
@property
def upper_bound(self):
return self._upper_bound
@upper_bound.setter
def upper_bound(self, upper_bound):
space = self.space
if upper_bound is not None and space == 'native':
raise ParameterConfigurationError(
"upper_bound must be None if space == 'native'.")
elif upper_bound is not None and upper_bound < self.value:
raise ParameterConfigurationError(
'upper_bound may not be less than current value')
elif space == 'log':
self._lower_bound = None
self._upper_bound = upper_bound
else:
self._upper_bound = upper_bound
# Recalculate trans_value
try:
self.value = self._value
except AttributeError:
pass
# Recalculate trans_prior_mean
try:
self.prior_mean = self._prior_mean
except AttributeError:
pass
@property
def value(self):
return self._value
@value.setter
def value(self, value):
if self.lower_bound is not None and value < self.lower_bound:
raise ParameterConfigurationError(
'value may not be less than current lower_bound')
elif self.upper_bound is not None and value > self.upper_bound:
raise ParameterConfigurationError(
'value may not be greater than current upper_bound')
else:
self._value = value
space = self.space
if space == 'native':
self._trans_value = value
elif space == 'log':
self._trans_value = log(value,
lower_bound=self.lower_bound,
upper_bound=self.upper_bound)