Skip to content

Commit a99eead

Browse files
committed
revise h_params of autoregressive
1 parent 6259fff commit a99eead

File tree

2 files changed

+40
-95
lines changed

2 files changed

+40
-95
lines changed

bayesml/autoregressive/_autoregressive.py

Lines changed: 39 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -114,44 +114,25 @@ def __init__(
114114
self.h_alpha = _check.pos_float(h_alpha,'h_alpha',ParameterFormatError)
115115
self.h_beta = _check.pos_float(h_beta,'h_beta',ParameterFormatError)
116116
self.rng = np.random.default_rng(seed)
117-
self._H_PARAM_KEYS = {'h_mu_vec','h_lambda_mat','h_alpha','h_beta'}
118-
self._H0_PARAM_KEYS = {'h0_mu_vec','h0_lambda_mat','h0_alpha','h0_beta'}
119-
self._HN_PARAM_KEYS = {'hn_mu_vec','hn_lambda_mat','hn_alpha','hn_beta'}
120117

121-
def set_h_params(self,**kwargs):
118+
def set_h_params(self,h_mu_vec,h_lambda_mat,h_alpha,h_beta):
122119
"""Set the hyperparameters of the prior distribution.
123120
124121
Parameters
125122
----------
126-
**kwargs
127-
a python dictionary {'h_mu_vec':ndarray, 'h_lambda_mat':ndarray, 'h_alpha':float, 'h_beta':float} or
128-
{'h0_mu_vec':ndarray, 'h0_lambda_mat':ndarray, 'h0_alpha':float, 'h0_beta':float}
129-
or {'hn_mu_vec':ndarray, 'hn_lambda_mat':ndarray, 'hn_alpha':float, 'hn_beta':float}
130-
They are obtained by ``get_h_params()`` of GenModel,
131-
``get_h0_params`` of LearnModel or ``get_hn_params`` of LearnModel.
123+
h_mu_vec : numpy ndarray
124+
a vector of real numbers
125+
h_lambda_mat : numpy ndarray
126+
a positibe definate matrix
127+
h_alpha : float
128+
a positive real number
129+
h_beta : float
130+
a positibe real number
132131
"""
133-
if kwargs.keys() == self._H_PARAM_KEYS:
134-
self.h_mu_vec = _check.float_vec(kwargs['h_mu_vec'],'h_mu_vec',ParameterFormatError)
135-
self.h_lambda_mat = _check.pos_def_sym_mat(kwargs['h_lambda_mat'],'h_lambda_mat',ParameterFormatError)
136-
self.h_alpha = _check.pos_float(kwargs['h_alpha'],'h_alpha',ParameterFormatError)
137-
self.h_beta = _check.pos_float(kwargs['h_beta'],'h_beta',ParameterFormatError)
138-
elif kwargs.keys() == self._H0_PARAM_KEYS:
139-
self.h_mu_vec = _check.float_vec(kwargs['h0_mu_vec'],'h_mu_vec',ParameterFormatError)
140-
self.h_lambda_mat = _check.pos_def_sym_mat(kwargs['h0_lambda_mat'],'h_lambda_mat',ParameterFormatError)
141-
self.h_alpha = _check.pos_float(kwargs['h0_alpha'],'h_alpha',ParameterFormatError)
142-
self.h_beta = _check.pos_float(kwargs['h0_beta'],'h_beta',ParameterFormatError)
143-
elif kwargs.keys() == self._HN_PARAM_KEYS:
144-
self.h_mu_vec = _check.float_vec(kwargs['hn_mu_vec'],'h_mu_vec',ParameterFormatError)
145-
self.h_lambda_mat = _check.pos_def_sym_mat(kwargs['hn_lambda_mat'],'h_lambda_mat',ParameterFormatError)
146-
self.h_alpha = _check.pos_float(kwargs['hn_alpha'],'h_alpha',ParameterFormatError)
147-
self.h_beta = _check.pos_float(kwargs['hn_beta'],'h_beta',ParameterFormatError)
148-
else:
149-
raise(ParameterFormatError(
150-
"The input of this function must be a python dictionary with keys:"
151-
+str(self._H_PARAM_KEYS)+" or "
152-
+str(self._H0_PARAM_KEYS)+" or "
153-
+str(self._HN_PARAM_KEYS)+".")
154-
)
132+
self.h_mu_vec = _check.float_vec(h_mu_vec,'h_mu_vec',ParameterFormatError)
133+
self.h_lambda_mat = _check.pos_def_sym_mat(h_lambda_mat,'h_lambda_mat',ParameterFormatError)
134+
self.h_alpha = _check.pos_float(h_alpha,'h_alpha',ParameterFormatError)
135+
self.h_beta = _check.pos_float(h_beta,'h_beta',ParameterFormatError)
155136

156137
if (self.h_mu_vec.shape[0] != self.h_lambda_mat.shape[0]):
157138
raise(ParameterFormatError(
@@ -397,47 +378,27 @@ def __init__(
397378
self.p_lambda = self.hn_alpha / self.hn_beta / (1.0 + _explanatory_vec @ np.linalg.solve(self.hn_lambda_mat,_explanatory_vec))
398379
self.p_nu = 2.0 * self.hn_alpha
399380

400-
self._H_PARAM_KEYS = {'h_mu_vec','h_lambda_mat','h_alpha','h_beta'}
401-
self._H0_PARAM_KEYS = {'h0_mu_vec','h0_lambda_mat','h0_alpha','h0_beta'}
402-
self._HN_PARAM_KEYS = {'hn_mu_vec','hn_lambda_mat','hn_alpha','hn_beta'}
403-
404-
def set_h0_params(self,**kwargs):
381+
def set_h0_params(self,h0_mu_vec,h0_lambda_mat,h0_alpha,h0_beta):
405382
"""Set initial values of the hyperparameter of the posterior distribution.
406383
407384
Note that the parameters of the predictive distribution are also calculated from
408385
``self.h0_mu_vec``, ``slef.h0_lambda_mat``, ``self.h0_alpha`` and ``self.h0_beta``.
409386
410387
Parameters
411388
----------
412-
**kwargs
413-
a python dictionary {'h_mu_vec':ndarray, 'h_lambda_mat':ndarray, 'h_alpha':float, 'h_beta':float} or
414-
{'h0_mu_vec':ndarray, 'h0_lambda_mat':ndarray, 'h0_alpha':float, 'h0_beta':float}
415-
or {'hn_mu_vec':ndarray, 'hn_lambda_mat':ndarray, 'hn_alpha':float, 'hn_beta':float}
416-
They are obtained by ``get_h_params()`` of GenModel,
417-
``get_h0_params`` of LearnModel or ``get_hn_params`` of LearnModel.
389+
h0_mu_vec : numpy ndarray
390+
a vector of real numbers
391+
h0_lambda_mat : numpy ndarray
392+
a positibe definate matrix
393+
h0_alpha : float
394+
a positive real number
395+
h0_beta : float
396+
a positibe real number
418397
"""
419-
if kwargs.keys() == self._H_PARAM_KEYS:
420-
self.h0_mu_vec = _check.float_vec(kwargs['h_mu_vec'],'h0_mu_vec',ParameterFormatError)
421-
self.h0_lambda_mat = _check.pos_def_sym_mat(kwargs['h_lambda_mat'],'h0_lambda_mat',ParameterFormatError)
422-
self.h0_alpha = _check.pos_float(kwargs['h_alpha'],'h0_alpha',ParameterFormatError)
423-
self.h0_beta = _check.pos_float(kwargs['h_beta'],'h0_beta',ParameterFormatError)
424-
elif kwargs.keys() == self._H0_PARAM_KEYS:
425-
self.h0_mu_vec = _check.float_vec(kwargs['h0_mu_vec'],'h0_mu_vec',ParameterFormatError)
426-
self.h0_lambda_mat = _check.pos_def_sym_mat(kwargs['h0_lambda_mat'],'h0_lambda_mat',ParameterFormatError)
427-
self.h0_alpha = _check.pos_float(kwargs['h0_alpha'],'h0_alpha',ParameterFormatError)
428-
self.h0_beta = _check.pos_float(kwargs['h0_beta'],'h0_beta',ParameterFormatError)
429-
elif kwargs.keys() == self._HN_PARAM_KEYS:
430-
self.h0_mu_vec = _check.float_vec(kwargs['hn_mu_vec'],'h0_mu_vec',ParameterFormatError)
431-
self.h0_lambda_mat = _check.pos_def_sym_mat(kwargs['hn_lambda_mat'],'h0_lambda_mat',ParameterFormatError)
432-
self.h0_alpha = _check.pos_float(kwargs['hn_alpha'],'h0_alpha',ParameterFormatError)
433-
self.h0_beta = _check.pos_float(kwargs['hn_beta'],'h0_beta',ParameterFormatError)
434-
else:
435-
raise(ParameterFormatError(
436-
"The input of this function must be a python dictionary with keys:"
437-
+str(self._H_PARAM_KEYS)+" or "
438-
+str(self._H0_PARAM_KEYS)+" or "
439-
+str(self._HN_PARAM_KEYS)+".")
440-
)
398+
self.h0_mu_vec = _check.float_vec(h0_mu_vec,'h0_mu_vec',ParameterFormatError)
399+
self.h0_lambda_mat = _check.pos_def_sym_mat(h0_lambda_mat,'h0_lambda_mat',ParameterFormatError)
400+
self.h0_alpha = _check.pos_float(h0_alpha,'h0_alpha',ParameterFormatError)
401+
self.h0_beta = _check.pos_float(h0_beta,'h0_beta',ParameterFormatError)
441402

442403
self.degree = self.h0_mu_vec.shape[0]-1
443404
if (self.h0_mu_vec.shape[0] != self.h0_lambda_mat.shape[0]):
@@ -472,43 +433,27 @@ def get_hn_params(self):
472433
"""
473434
return {"hn_mu_vec":self.hn_mu_vec, "hn_lambda_mat":self.hn_lambda_mat, "hn_alpha":self.hn_alpha, "hn_beta":self.hn_beta}
474435

475-
def set_hn_params(self,**kwargs):
436+
def set_hn_params(self,hn_mu_vec,hn_lambda_mat,hn_alpha,hn_beta):
476437
"""Set updated values of the hyperparameter of the posterior distribution.
477438
478439
Note that the parameters of the predictive distribution are also calculated from
479440
``self.hn_mu_vec``, ``slef.hn_lambda_mat``, ``self.hn_alpha`` and ``self.hn_beta``.
480441
481442
Parameters
482443
----------
483-
**kwargs
484-
a python dictionary {'h_mu_vec':ndarray, 'h_lambda_mat':ndarray, 'h_alpha':float, 'h_beta':float} or
485-
{'h0_mu_vec':ndarray, 'h0_lambda_mat':ndarray, 'h0_alpha':float, 'h0_beta':float}
486-
or {'hn_mu_vec':ndarray, 'hn_lambda_mat':ndarray, 'hn_alpha':float, 'hn_beta':float}
487-
They are obtained by ``get_h_params()`` of GenModel,
488-
``get_h0_params`` of LearnModel or ``get_hn_params`` of LearnModel.
444+
hn_mu_vec : numpy ndarray
445+
a vector of real numbers
446+
hn_lambda_mat : numpy ndarray
447+
a positibe definate matrix
448+
hn_alpha : float
449+
a positive real number
450+
hn_beta : float
451+
a positibe real number
489452
"""
490-
if kwargs.keys() == self._H_PARAM_KEYS:
491-
self.hn_mu_vec = _check.float_vec(kwargs['h_mu_vec'],'hn_mu_vec',ParameterFormatError)
492-
self.hn_lambda_mat = _check.pos_def_sym_mat(kwargs['h_lambda_mat'],'hn_lambda_mat',ParameterFormatError)
493-
self.hn_alpha = _check.pos_float(kwargs['h_alpha'],'hn_alpha',ParameterFormatError)
494-
self.hn_beta = _check.pos_float(kwargs['h_beta'],'hn_beta',ParameterFormatError)
495-
elif kwargs.keys() == self._H0_PARAM_KEYS:
496-
self.hn_mu_vec = _check.float_vec(kwargs['h0_mu_vec'],'hn_mu_vec',ParameterFormatError)
497-
self.hn_lambda_mat = _check.pos_def_sym_mat(kwargs['h0_lambda_mat'],'hn_lambda_mat',ParameterFormatError)
498-
self.hn_alpha = _check.pos_float(kwargs['h0_alpha'],'hn_alpha',ParameterFormatError)
499-
self.hn_beta = _check.pos_float(kwargs['h0_beta'],'hn_beta',ParameterFormatError)
500-
elif kwargs.keys() == self._HN_PARAM_KEYS:
501-
self.hn_mu_vec = _check.float_vec(kwargs['hn_mu_vec'],'hn_mu_vec',ParameterFormatError)
502-
self.hn_lambda_mat = _check.pos_def_sym_mat(kwargs['hn_lambda_mat'],'hn_lambda_mat',ParameterFormatError)
503-
self.hn_alpha = _check.pos_float(kwargs['hn_alpha'],'hn_alpha',ParameterFormatError)
504-
self.hn_beta = _check.pos_float(kwargs['hn_beta'],'hn_beta',ParameterFormatError)
505-
else:
506-
raise(ParameterFormatError(
507-
"The input of this function must be a python dictionary with keys:"
508-
+str(self._H_PARAM_KEYS)+" or "
509-
+str(self._H0_PARAM_KEYS)+" or "
510-
+str(self._HN_PARAM_KEYS)+".")
511-
)
453+
self.hn_mu_vec = _check.float_vec(hn_mu_vec,'hn_mu_vec',ParameterFormatError)
454+
self.hn_lambda_mat = _check.pos_def_sym_mat(hn_lambda_mat,'hn_lambda_mat',ParameterFormatError)
455+
self.hn_alpha = _check.pos_float(hn_alpha,'hn_alpha',ParameterFormatError)
456+
self.hn_beta = _check.pos_float(hn_beta,'hn_beta',ParameterFormatError)
512457

513458
self.degree = self.hn_mu_vec.shape[0]-1
514459
if (self.hn_mu_vec.shape[0] != self.hn_lambda_mat.shape[0]):

doc/devdoc/examples/h_params_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from bayesml import linearregression as bayesml_model
1+
from bayesml import autoregressive as bayesml_model
22
import numpy as np
33

44
h0_params = {'h0_mu_vec':np.ones(3),'h0_lambda_mat':np.eye(3)*2,"h0_alpha":2.1, "h0_beta":2.34}

0 commit comments

Comments
 (0)