bayesml.exponential package#
Module contents#
The exponential distribution with the gamma prior distribution.
The stochastic data generative model is as follows:
\(x \in \mathbb{R}_{\geq 0}\): a data point
\(\lambda \in \mathbb{R}_{>0}\): a parameter
The prior distribution is as follows:
\(\alpha_0 \in \mathbb{R}_{>0}\): a hyperparameter
\(\beta_0 \in \mathbb{R}_{>0}\): a hyperparameter
\(\Gamma(\cdot): \mathbb{R}_{>0} \to \mathbb{R}_{>0}\): the Gamma function
The posterior distribution is as follows:
\(x^n = (x_1, x_2, \dots , x_n) \in \mathbb{R}_{\geq 0}^n\): given data
\(\alpha_n \in \mathbb{R}_{>0}\): a hyperparameter
\(\beta_n \in \mathbb{R}_{>0}\): a hyperparameter
where the updating rule of the hyperparameters is
The predictive distribution is as follows:
\(x_{n+1} \in \mathbb{R}_{\geq 0}\): a new data point
\(\kappa_\mathrm{p} \in \mathbb{R}_{>0}\): the hyperparameter of the posterior
\(\lambda_\mathrm{p} \in \mathbb{R}_{>0}\): the hyperparameter of the posterior
where the parameters are obtained from the hyperparameters of the posterior distribution as follows:
- class bayesml.exponential.GenModel(lambda_=1.0, h_alpha=1.0, h_beta=1.0, seed=None)#
Bases:
Generative
The stochastic data generative model and the prior distribution.
- Parameters:
- lambda_float, optional
a positive real number, by default 1.0.
- h_alphafloat, optional
a positive real number, by default 1.0.
- h_betafloat, optional
a positive real number, by default 1.0.
- seed{None, int}, optional
A seed to initialize numpy.random.default_rng(), by default None
Methods
Generate the parameter from the prior distribution.
gen_sample
(sample_size)Generate a sample from the stochastic data generative model.
Get constants of GenModel.
Get the hyperparameters of the prior distribution.
Get the parameter of the sthocastic data generative model.
load_h_params
(filename)Load the hyperparameters to h_params.
load_params
(filename)Load the parameters saved by
save_params
.save_h_params
(filename)Save the hyperparameters using python
pickle
module.save_params
(filename)Save the parameters using python
pickle
module.save_sample
(filename, sample_size)Save the generated sample as NumPy
.npz
format.set_h_params
([h_alpha, h_beta])Set the hyperparameters of the prior distribution.
set_params
([lambda_])Set the parameter of the sthocastic data generative model.
visualize_model
([sample_size, hist_bins])Visualize the stochastic data generative model and generated samples.
- get_constants()#
Get constants of GenModel.
This model does not have any constants. Therefore, this function returns an emtpy dict
{}
.- Returns:
- constantsan empty dict
- set_h_params(h_alpha=None, h_beta=None)#
Set the hyperparameters of the prior distribution.
- Parameters:
- h_alphafloat, optional
a positive real number, by default None.
- h_betafloat, optional
a positive real number, by default None.
- get_h_params()#
Get the hyperparameters of the prior distribution.
- Returns:
- h_paramsdict of {str: float}
"h_alpha"
: The value ofself.h_alpha
"h_beta"
: The value ofself.h_beta
- gen_params()#
Generate the parameter from the prior distribution.
The generated vaule is set at
self.lambda_
.
- set_params(lambda_=None)#
Set the parameter of the sthocastic data generative model.
- Parameters:
- lambda_float, optional
a positive real number, by default None.
- get_params()#
Get the parameter of the sthocastic data generative model.
- Returns:
- paramsdict of {str:float}
"lambda_"
: The value ofself.lambda_
.
- gen_sample(sample_size)#
Generate a sample from the stochastic data generative model.
- Parameters:
- sample_sizeint
A positive integer
- Returns:
- xnumpy ndarray
1 dimensional array whose size is
sammple_size
and elements are positive real numbers.
- save_sample(filename, sample_size)#
Save the generated sample as NumPy
.npz
format.It is saved as aNpzFile with keyword: “x”.
- Parameters:
- filenamestr
The filename to which the sample is saved.
.npz
will be appended if it isn’t there.- sample_sizeint
A positive integer
See also
- visualize_model(sample_size=100, hist_bins=10)#
Visualize the stochastic data generative model and generated samples.
- Parameters:
- sample_sizeint, optional
A positive integer, by default 100.
- hist_binsfloat, optional
A positive float, by default 10.
Examples
>>> from bayesml import normal >>> model = normal.GenModel() >>> model.visualize_model() lambda_:1.0
- class bayesml.exponential.LearnModel(h0_alpha=1.0, h0_beta=1.0)#
Bases:
Posterior
,PredictiveMixin
The posterior distribution and the predictive distribution.
- Parameters:
- h0_alphafloat, optional
a positive real number, by default 1.0.
- h0_betafloat, optional
a positive real number, by default 1.0.
- Attributes:
- hn_alphafloat
a positive real number
- hn_betafloat
a positive real number
- p_kappafloat
a positive real number
- p_lambdafloat
a positive real number
Methods
Calculate log marginal likelihood
Calculate the parameters of the predictive distribution.
estimate_interval
([credibility])Credible interval of the parameter.
estimate_params
([loss, dict_out])Estimate the parameter of the stochastic data generative model under the given criterion.
Get constants of LearnModel.
Get the initial values of the hyperparameters of the posterior distribution.
Get the hyperparameters of the posterior distribution.
Get the parameters of the predictive distribution.
load_h0_params
(filename)Load the hyperparameters to h0_params.
load_hn_params
(filename)Load the hyperparameters to hn_params.
make_prediction
([loss])Predict a new data point under the given criterion.
overwrite_h0_params
()Overwrite the initial values of the hyperparameters of the posterior distribution by the learned values.
pred_and_update
(x[, loss])Predict a new data point and update the posterior sequentially.
reset_hn_params
()Reset the hyperparameters of the posterior distribution to their initial values.
save_h0_params
(filename)Save the hyperparameters using python
pickle
module.save_hn_params
(filename)Save the hyperparameters using python
pickle
module.set_h0_params
([h0_alpha, h0_beta])Set initial values of the hyperparameter of the posterior distribution.
set_hn_params
([hn_alpha, hn_beta])Set updated values of the hyperparameter of the posterior distribution.
Update the hyperparameters of the posterior distribution using traning data.
Visualize the posterior distribution for the parameter.
- get_constants()#
Get constants of LearnModel.
This model does not have any constants. Therefore, this function returns an emtpy dict
{}
.- Returns:
- constantsan empty dict
- set_h0_params(h0_alpha=None, h0_beta=None)#
Set initial values of the hyperparameter of the posterior distribution.
- Parameters:
- h0_alphafloat, optional
a positive real number, by default None.
- h0_betafloat, optional
a positive real number, by default None.
- get_h0_params()#
Get the initial values of the hyperparameters of the posterior distribution.
- Returns:
- h0_paramsdict of {str: float}
"h0_alpha"
: The value ofself.h0_alpha
"h0_beta"
: The value ofself.h0_beta
- set_hn_params(hn_alpha=None, hn_beta=None)#
Set updated values of the hyperparameter of the posterior distribution.
- Parameters:
- hn_alphafloat, optional
a positive real number, by default None.
- hn_betafloat, optional
a positive real number, by default None.
- get_hn_params()#
Get the hyperparameters of the posterior distribution.
- Returns:
- hn_paramsdict of {str: float}
"hn_alpha"
: The value ofself.hn_alpha
"hn_beta"
: The value ofself.hn_beta
- update_posterior(x)#
Update the hyperparameters of the posterior distribution using traning data.
- Parameters:
- xnumpy.ndarray
All the elements must be positive real numbers.
- estimate_params(loss='squared', dict_out=False)#
Estimate the parameter of the stochastic data generative model under the given criterion.
- Parameters:
- lossstr, optional
Loss function underlying the Bayes risk function, by default “squared”. This function supports “squared”, “0-1”, “abs”, and “KL”.
- dict_outbool, optional
If
True
, output will be a dict, by defaultFalse
.
- Returns:
- estimator{float, None, rv_frozen}
The estimated values under the given loss function. If it is not exist, None will be returned. If the loss function is “KL”, the posterior distribution itself will be returned as rv_frozen object of scipy.stats.
- estimate_interval(credibility=0.95)#
Credible interval of the parameter.
- Parameters:
- credibilityfloat, optional
A posterior probability that the interval conitans the paramter, by default 0.95.
- Returns:
- lower, upper: float
The lower and the upper bound of the interval
- visualize_posterior()#
Visualize the posterior distribution for the parameter.
Examples
>>> from bayesml import exponential >>> gen_model = exponential.GenModel(lambda_=2.0) >>> x = gen_model.gen_sample(20) >>> learn_model = exponential.LearnModel() >>> learn_model.update_posterior(x) >>> learn_model.visualize_posterior()
- get_p_params()#
Get the parameters of the predictive distribution.
- Returns:
- p_paramsdict of {str: float}
"p_kappa"
: The value ofself.p_kappa
"p_lambda"
: The value ofself.p_lambda
- calc_pred_dist()#
Calculate the parameters of the predictive distribution.
- make_prediction(loss='squared')#
Predict a new data point under the given criterion.
- Parameters:
- lossstr, optional
Loss function underlying the Bayes risk function, by default “squared”. This function supports “squared”, “0-1”, “abs”, and “KL”.
- Returns:
- Predicted_value{int, numpy.ndarray}
The predicted value under the given loss function. If the loss function is “KL”, the predictive distribution itself will be returned as numpy.ndarray.
- pred_and_update(x, loss='squared')#
Predict a new data point and update the posterior sequentially.
- Parameters:
- xfloat
a positive real number
- lossstr, optional
Loss function underlying the Bayes risk function, by default “squared”. This function supports “squared”, “0-1”, “abs”, and “KL”.
- Returns:
- Predicted_value{float, numpy.ndarray}
The predicted value under the given loss function. If the loss function is “KL”, the predictive distribution itself will be returned as numpy.ndarray.
- calc_log_marginal_likelihood()#
Calculate log marginal likelihood
- Returns:
- log_marginal_likelihoodfloat
The log marginal likelihood.