Skip to content

Commit 6259fff

Browse files
committed
revise h_params of linearregression
1 parent b77e0a8 commit 6259fff

File tree

2 files changed

+41
-96
lines changed

2 files changed

+41
-96
lines changed

bayesml/linearregression/_linearregression.py

Lines changed: 39 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -111,44 +111,25 @@ def __init__(
111111
self.h_alpha = _check.pos_float(h_alpha,'h_alpha',ParameterFormatError)
112112
self.h_beta = _check.pos_float(h_beta,'h_beta',ParameterFormatError)
113113
self.rng = np.random.default_rng(seed)
114-
self._H_PARAM_KEYS = {'h_mu_vec','h_lambda_mat','h_alpha','h_beta'}
115-
self._H0_PARAM_KEYS = {'h0_mu_vec','h0_lambda_mat','h0_alpha','h0_beta'}
116-
self._HN_PARAM_KEYS = {'hn_mu_vec','hn_lambda_mat','hn_alpha','hn_beta'}
117114

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

153134
if (self.h_mu_vec.shape[0] != self.h_lambda_mat.shape[0]):
154135
raise(ParameterFormatError(
@@ -434,47 +415,27 @@ def __init__(
434415
self.p_lambda = self.hn_alpha / self.hn_beta / (1.0 + _explanatory_vec @ np.linalg.solve(self.hn_lambda_mat,_explanatory_vec))
435416
self.p_nu = 2.0 * self.hn_alpha
436417

437-
self._H_PARAM_KEYS = {'h_mu_vec','h_lambda_mat','h_alpha','h_beta'}
438-
self._H0_PARAM_KEYS = {'h0_mu_vec','h0_lambda_mat','h0_alpha','h0_beta'}
439-
self._HN_PARAM_KEYS = {'hn_mu_vec','hn_lambda_mat','hn_alpha','hn_beta'}
440-
441-
def set_h0_params(self,**kwargs):
418+
def set_h0_params(self,h0_mu_vec,h0_lambda_mat,h0_alpha,h0_beta):
442419
"""Set initial values of the hyperparameter of the posterior distribution.
443420
444421
Note that the parameters of the predictive distribution are also calculated from
445422
``self.h0_mu_vec``, ``slef.h0_lambda_mat``, ``self.h0_alpha`` and ``self.h0_beta``.
446423
447424
Parameters
448425
----------
449-
**kwargs
450-
a python dictionary {'h_mu_vec':ndarray, 'h_lambda_mat':ndarray, 'h_alpha':float, 'h_beta':float} or
451-
{'h0_mu_vec':ndarray, 'h0_lambda_mat':ndarray, 'h0_alpha':float, 'h0_beta':float}
452-
or {'hn_mu_vec':ndarray, 'hn_lambda_mat':ndarray, 'hn_alpha':float, 'hn_beta':float}
453-
They are obtained by ``get_h_params()`` of GenModel,
454-
``get_h0_params`` of LearnModel or ``get_hn_params`` of LearnModel.
426+
h0_mu_vec : numpy ndarray
427+
a vector of real numbers
428+
h0_lambda_mat : numpy ndarray
429+
a positibe definate matrix
430+
h0_alpha : float
431+
a positive real number
432+
h0_beta : float
433+
a positibe real number
455434
"""
456-
if kwargs.keys() == self._H_PARAM_KEYS:
457-
self.h0_mu_vec = _check.float_vec(kwargs['h_mu_vec'],'h0_mu_vec',ParameterFormatError)
458-
self.h0_lambda_mat = _check.pos_def_sym_mat(kwargs['h_lambda_mat'],'h0_lambda_mat',ParameterFormatError)
459-
self.h0_alpha = _check.pos_float(kwargs['h_alpha'],'h0_alpha',ParameterFormatError)
460-
self.h0_beta = _check.pos_float(kwargs['h_beta'],'h0_beta',ParameterFormatError)
461-
elif kwargs.keys() == self._H0_PARAM_KEYS:
462-
self.h0_mu_vec = _check.float_vec(kwargs['h0_mu_vec'],'h0_mu_vec',ParameterFormatError)
463-
self.h0_lambda_mat = _check.pos_def_sym_mat(kwargs['h0_lambda_mat'],'h0_lambda_mat',ParameterFormatError)
464-
self.h0_alpha = _check.pos_float(kwargs['h0_alpha'],'h0_alpha',ParameterFormatError)
465-
self.h0_beta = _check.pos_float(kwargs['h0_beta'],'h0_beta',ParameterFormatError)
466-
elif kwargs.keys() == self._HN_PARAM_KEYS:
467-
self.h0_mu_vec = _check.float_vec(kwargs['hn_mu_vec'],'h0_mu_vec',ParameterFormatError)
468-
self.h0_lambda_mat = _check.pos_def_sym_mat(kwargs['hn_lambda_mat'],'h0_lambda_mat',ParameterFormatError)
469-
self.h0_alpha = _check.pos_float(kwargs['hn_alpha'],'h0_alpha',ParameterFormatError)
470-
self.h0_beta = _check.pos_float(kwargs['hn_beta'],'h0_beta',ParameterFormatError)
471-
else:
472-
raise(ParameterFormatError(
473-
"The input of this function must be a python dictionary with keys:"
474-
+str(self._H_PARAM_KEYS)+" or "
475-
+str(self._H0_PARAM_KEYS)+" or "
476-
+str(self._HN_PARAM_KEYS)+".")
477-
)
435+
self.h0_mu_vec = _check.float_vec(h0_mu_vec,'h0_mu_vec',ParameterFormatError)
436+
self.h0_lambda_mat = _check.pos_def_sym_mat(h0_lambda_mat,'h0_lambda_mat',ParameterFormatError)
437+
self.h0_alpha = _check.pos_float(h0_alpha,'h0_alpha',ParameterFormatError)
438+
self.h0_beta = _check.pos_float(h0_beta,'h0_beta',ParameterFormatError)
478439

479440
self.degree = self.h0_mu_vec.shape[0]
480441
if (self.h0_mu_vec.shape[0] != self.h0_lambda_mat.shape[0]):
@@ -496,43 +457,27 @@ def get_h0_params(self):
496457
"""
497458
return {"h0_mu_vec":self.h0_mu_vec, "h0_lambda_mat":self.h0_lambda_mat, "h0_alpha":self.h0_alpha, "h0_beta":self.h0_beta}
498459

499-
def set_hn_params(self,**kwargs):
460+
def set_hn_params(self,hn_mu_vec,hn_lambda_mat,hn_alpha,hn_beta):
500461
"""Set updated values of the hyperparameter of the posterior distribution.
501462
502463
Note that the parameters of the predictive distribution are also calculated from
503464
``self.hn_mu_vec``, ``slef.hn_lambda_mat``, ``self.hn_alpha`` and ``self.hn_beta``.
504465
505466
Parameters
506467
----------
507-
**kwargs
508-
a python dictionary {'h_mu_vec':ndarray, 'h_lambda_mat':ndarray, 'h_alpha':float, 'h_beta':float} or
509-
{'h0_mu_vec':ndarray, 'h0_lambda_mat':ndarray, 'h0_alpha':float, 'h0_beta':float}
510-
or {'hn_mu_vec':ndarray, 'hn_lambda_mat':ndarray, 'hn_alpha':float, 'hn_beta':float}
511-
They are obtained by ``get_h_params()`` of GenModel,
512-
``get_h0_params`` of LearnModel or ``get_hn_params`` of LearnModel.
468+
hn_mu_vec : numpy ndarray
469+
a vector of real numbers
470+
hn_lambda_mat : numpy ndarray
471+
a positibe definate matrix
472+
hn_alpha : float
473+
a positive real number
474+
hn_beta : float
475+
a positibe real number
513476
"""
514-
if kwargs.keys() == self._H_PARAM_KEYS:
515-
self.hn_mu_vec = _check.float_vec(kwargs['h_mu_vec'],'hn_mu_vec',ParameterFormatError)
516-
self.hn_lambda_mat = _check.pos_def_sym_mat(kwargs['h_lambda_mat'],'hn_lambda_mat',ParameterFormatError)
517-
self.hn_alpha = _check.pos_float(kwargs['h_alpha'],'hn_alpha',ParameterFormatError)
518-
self.hn_beta = _check.pos_float(kwargs['h_beta'],'hn_beta',ParameterFormatError)
519-
elif kwargs.keys() == self._H0_PARAM_KEYS:
520-
self.hn_mu_vec = _check.float_vec(kwargs['h0_mu_vec'],'hn_mu_vec',ParameterFormatError)
521-
self.hn_lambda_mat = _check.pos_def_sym_mat(kwargs['h0_lambda_mat'],'hn_lambda_mat',ParameterFormatError)
522-
self.hn_alpha = _check.pos_float(kwargs['h0_alpha'],'hn_alpha',ParameterFormatError)
523-
self.hn_beta = _check.pos_float(kwargs['h0_beta'],'hn_beta',ParameterFormatError)
524-
elif kwargs.keys() == self._HN_PARAM_KEYS:
525-
self.hn_mu_vec = _check.float_vec(kwargs['hn_mu_vec'],'hn_mu_vec',ParameterFormatError)
526-
self.hn_lambda_mat = _check.pos_def_sym_mat(kwargs['hn_lambda_mat'],'hn_lambda_mat',ParameterFormatError)
527-
self.hn_alpha = _check.pos_float(kwargs['hn_alpha'],'hn_alpha',ParameterFormatError)
528-
self.hn_beta = _check.pos_float(kwargs['hn_beta'],'hn_beta',ParameterFormatError)
529-
else:
530-
raise(ParameterFormatError(
531-
"The input of this function must be a python dictionary with keys:"
532-
+str(self._H_PARAM_KEYS)+" or "
533-
+str(self._H0_PARAM_KEYS)+" or "
534-
+str(self._HN_PARAM_KEYS)+".")
535-
)
477+
self.hn_mu_vec = _check.float_vec(hn_mu_vec,'hn_mu_vec',ParameterFormatError)
478+
self.hn_lambda_mat = _check.pos_def_sym_mat(hn_lambda_mat,'hn_lambda_mat',ParameterFormatError)
479+
self.hn_alpha = _check.pos_float(hn_alpha,'hn_alpha',ParameterFormatError)
480+
self.hn_beta = _check.pos_float(hn_beta,'hn_beta',ParameterFormatError)
536481

537482
self.degree = self.hn_mu_vec.shape[0]
538483
if (self.hn_mu_vec.shape[0] != self.hn_lambda_mat.shape[0]):

doc/devdoc/examples/h_params_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from bayesml import exponential as bayesml_model
1+
from bayesml import linearregression as bayesml_model
22
import numpy as np
33

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

66
print('Gen to Learn 1')
77
model = bayesml_model.GenModel()

0 commit comments

Comments
 (0)