17
17
from preliz .internal .distribution_helper import get_distributions
18
18
19
19
20
- def backfitting (prior , p_model , var_info2 ):
20
+ def back_fitting_pymc (prior , preliz_model , untransformed_var_info ):
21
21
"""
22
22
Fit the samples from prior into user provided model's prior.
23
23
from the perspective of ppe "prior" is actually an approximated posterior
24
24
but from the users perspective is its prior.
25
- We need to "backfitted " because we can not use arbitrary samples as priors.
25
+ We need to "backfit " because we can not use arbitrary samples as priors.
26
26
We need probability distributions.
27
27
"""
28
28
new_priors = {}
29
- for key , size_inf in var_info2 .items ():
29
+ for key , size_inf in untransformed_var_info .items ():
30
30
if not size_inf [2 ]:
31
31
size = size_inf [1 ]
32
32
if size > 1 :
33
33
params = []
34
34
for i in range (size ):
35
35
value = prior [f"{ key } __{ i } " ]
36
- dist = p_model [key ]
36
+ dist = preliz_model [key ]
37
37
dist ._fit_mle (value )
38
38
params .append (dist .params )
39
39
dist ._parametrization (* [np .array (x ) for x in zip (* params )])
40
40
else :
41
41
value = prior [key ]
42
- dist = p_model [key ]
42
+ dist = preliz_model [key ]
43
43
dist ._fit_mle (value )
44
44
45
45
new_priors [key ] = dist
@@ -81,7 +81,7 @@ def get_pymc_to_preliz():
81
81
return pymc_to_preliz
82
82
83
83
84
- def get_guess (model , free_rvs ):
84
+ def get_initial_guess (model , free_rvs ):
85
85
"""
86
86
Get initial guess for optimization routine.
87
87
"""
@@ -104,17 +104,32 @@ def get_guess(model, free_rvs):
104
104
105
105
def get_model_information (model ): # pylint: disable=too-many-locals
106
106
"""
107
- Get information from the PyMC model.
108
-
109
- This needs some love. We even have a variable named var_info,
110
- and another one var_info2!
107
+ Get information from a PyMC model.
108
+
109
+ Parameters
110
+ ----------
111
+ model : a PyMC model
112
+
113
+ Returns
114
+ -------
115
+ bounds : a list of tuples with the support of each marginal distribution in the model
116
+ prior : a dictionary with a key for each marginal distribution in the model and an empty
117
+ list as value. This will be filled with the samples from a backfitting procedure.
118
+ preliz_model : a dictionary with a key for each marginal distribution in the model and the
119
+ corresponding PreliZ distribution as value
120
+ transformed_var_info : a dictionary with a key for each transformed variable in the model
121
+ and a tuple with the shape, size and the indexes of the non-constant parents as value
122
+ untransformed_var_info : same as `transformed_var_info` but the keys are untransformed
123
+ variable names
124
+ num_draws : the number of observed samples
125
+ free_rvs : a list with the free random variables in the model
111
126
"""
112
127
113
128
bounds = []
114
129
prior = {}
115
- p_model = {}
116
- var_info = {}
117
- var_info2 = {}
130
+ preliz_model = {}
131
+ transformed_var_info = {}
132
+ untransformed_var_info = {}
118
133
free_rvs = []
119
134
pymc_to_preliz = get_pymc_to_preliz ()
120
135
rvs_to_values = model .rvs_to_values
@@ -128,13 +143,13 @@ def get_model_information(model): # pylint: disable=too-many-locals
128
143
r_v .owner .op .name if r_v .owner .op .name else str (r_v .owner .op ).split ("RV" , 1 )[0 ].lower ()
129
144
)
130
145
dist = copy (pymc_to_preliz [name ])
131
- p_model [r_v .name ] = dist
146
+ preliz_model [r_v .name ] = dist
132
147
if nc_parents :
133
148
idxs = [free_rvs .index (var_ ) for var_ in nc_parents ]
134
149
# the keys are the name of the (transformed) variable
135
- var_info [rvs_to_values [r_v ].name ] = (shape , size , idxs )
150
+ transformed_var_info [rvs_to_values [r_v ].name ] = (shape , size , idxs )
136
151
# the keys are the name of the (untransformed) variable
137
- var_info2 [r_v .name ] = (shape , size , idxs )
152
+ untransformed_var_info [r_v .name ] = (shape , size , idxs )
138
153
else :
139
154
free_rvs .append (r_v )
140
155
@@ -147,13 +162,21 @@ def get_model_information(model): # pylint: disable=too-many-locals
147
162
prior [r_v .name ] = []
148
163
149
164
# the keys are the name of the (transformed) variable
150
- var_info [rvs_to_values [r_v ].name ] = (shape , size , nc_parents )
165
+ transformed_var_info [rvs_to_values [r_v ].name ] = (shape , size , nc_parents )
151
166
# the keys are the name of the (untransformed) variable
152
- var_info2 [r_v .name ] = (shape , size , nc_parents )
153
-
154
- draws = model .observed_RVs [0 ].eval ().size
155
-
156
- return bounds , prior , p_model , var_info , var_info2 , draws , free_rvs
167
+ untransformed_var_info [r_v .name ] = (shape , size , nc_parents )
168
+
169
+ num_draws = model .observed_RVs [0 ].eval ().size
170
+
171
+ return (
172
+ bounds ,
173
+ prior ,
174
+ preliz_model ,
175
+ transformed_var_info ,
176
+ untransformed_var_info ,
177
+ num_draws ,
178
+ free_rvs ,
179
+ )
157
180
158
181
159
182
def write_pymc_string (new_priors , var_info ):
0 commit comments