@@ -73,6 +73,9 @@ class PyMCModel(pm.Model):
73
73
def default_priors (self ):
74
74
return {}
75
75
76
+ def priors_from_data (self , X , y ) -> Dict [str , Any ]:
77
+ return {}
78
+
76
79
def __init__ (
77
80
self ,
78
81
sample_kwargs : Optional [Dict [str , Any ]] = None ,
@@ -122,6 +125,8 @@ def fit(self, X, y, coords: Optional[Dict[str, Any]] = None) -> None:
122
125
# sample_posterior_predictive() if provided in sample_kwargs.
123
126
random_seed = self .sample_kwargs .get ("random_seed" , None )
124
127
128
+ self .priors = {** self .priors_from_data (X , y ), ** self .priors }
129
+
125
130
self .build_model (X , y , coords )
126
131
with self :
127
132
self .idata = pm .sample (** self .sample_kwargs )
@@ -295,16 +300,22 @@ class WeightedSumFitter(PyMCModel):
295
300
"y_hat" : Prior ("Normal" , sigma = Prior ("HalfNormal" , sigma = 1 ), dims = "obs_ind" ),
296
301
}
297
302
303
+ def priors_from_data (self , X , y ) -> Dict [str , Any ]:
304
+ n_predictors = X .shape [1 ]
305
+
306
+ return {
307
+ "beta" : Prior ("Dirichlet" , a = np .ones (n_predictors ), dims = "coeffs" ),
308
+ }
309
+
298
310
def build_model (self , X , y , coords ):
299
311
"""
300
312
Defines the PyMC model
301
313
"""
302
314
with self :
303
315
self .add_coords (coords )
304
- n_predictors = X .shape [1 ]
305
316
X = pm .Data ("X" , X , dims = ["obs_ind" , "coeffs" ])
306
317
y = pm .Data ("y" , y [:, 0 ], dims = "obs_ind" )
307
- beta = pm . Dirichlet ( "beta" , a = np . ones ( n_predictors ), dims = "coeffs " )
318
+ beta = self . priors [ "beta" ]. create_variable ( "beta " )
308
319
mu = pm .Deterministic ("mu" , pm .math .dot (X , beta ), dims = "obs_ind" )
309
320
self .priors ["y_hat" ].create_likelihood_variable ("y_hat" , mu = mu , observed = y )
310
321
0 commit comments