Skip to content

Commit 80d6628

Browse files
committed
Update normal
1 parent 6e886e5 commit 80d6628

File tree

4 files changed

+193
-143
lines changed

4 files changed

+193
-143
lines changed

bayesml/bernoulli/_bernoulli.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ class GenModel(base.Generative):
1818
Parameters
1919
----------
2020
theta : float, optional
21-
a real number in :math:`[0, 1]`, 0.5 by default.
21+
a real number in :math:`[0, 1]`, by default 0.5.
2222
h_alpha : float, optional
23-
a positive real number, 0.5 by default.
23+
a positive real number, by default 0.5.
2424
h_beta : float, optional
25-
a positibe real number, 0.5 by default.
25+
a positibe real number, by default 0.5.
2626
seed : {None, int}, optional
2727
A seed to initialize numpy.random.default_rng(),
28-
None by default.
28+
by default None.
2929
"""
30-
def __init__(self,*,theta=0.5,h_alpha=0.5,h_beta=0.5,seed=None):
30+
def __init__(self,theta=0.5,h_alpha=0.5,h_beta=0.5,seed=None):
3131
self.rng = np.random.default_rng(seed)
3232

3333
# params
@@ -58,9 +58,9 @@ def set_h_params(self,h_alpha=None,h_beta=None):
5858
Parameters
5959
----------
6060
h_alpha : float, optional
61-
a positive real number, None by default.
61+
a positive real number, by default None.
6262
h_beta : float, optional
63-
a positibe real number, None by default.
63+
a positibe real number, by default None.
6464
"""
6565
if h_alpha is not None:
6666
self.h_alpha = _check.pos_float(h_alpha,'h_alpha',ParameterFormatError)
@@ -93,7 +93,7 @@ def set_params(self,theta=None):
9393
Parameters
9494
----------
9595
theta : float, optional
96-
a real number :math:`\theta \in [0, 1]`, None by default.
96+
a real number :math:`\theta \in [0, 1]`, by default None.
9797
"""
9898
if theta is not None:
9999
self.theta = _check.float_in_closed01(theta,'theta',ParameterFormatError)
@@ -150,9 +150,9 @@ def visualize_model(self,sample_size=20,sample_num=5):
150150
Parameters
151151
----------
152152
sample_size : int, optional
153-
A positive integer, 20 by default.
153+
A positive integer, by default 20.
154154
sample_num : int, optional
155-
A positive integer, 5 by default.
155+
A positive integer, by default 5.
156156
157157
Examples
158158
--------
@@ -195,9 +195,9 @@ class LearnModel(base.Posterior,base.PredictiveMixin):
195195
Parameters
196196
----------
197197
h0_alpha : float, optional
198-
a positive real number, 0.5 by default.
198+
a positive real number, by default 0.5.
199199
h0_beta : float, optional
200-
a positibe real number, 0.5 by default.
200+
a positibe real number, by default 0.5.
201201
202202
Attributes
203203
----------
@@ -240,9 +240,9 @@ def set_h0_params(self,h0_alpha=None,h0_beta=None):
240240
Parameters
241241
----------
242242
h0_alpha : float, optional
243-
a positive real number, None by default.
243+
a positive real number, by default None.
244244
h0_beta : float, optionanl
245-
a positibe real number, None by default.
245+
a positibe real number, by default None.
246246
"""
247247
if h0_alpha is not None:
248248
self.h0_alpha = _check.pos_float(h0_alpha,'h0_alpha',ParameterFormatError)
@@ -262,15 +262,15 @@ def get_h0_params(self):
262262
"""
263263
return {"h0_alpha":self.h0_alpha, "h0_beta":self.h0_beta}
264264

265-
def set_hn_params(self,hn_alpha,hn_beta):
265+
def set_hn_params(self,hn_alpha=None,hn_beta=None):
266266
"""Set updated values of the hyperparameter of the posterior distribution.
267267
268268
Parameters
269269
----------
270270
hn_alpha : float, optional
271-
a positive real number, None by default.
271+
a positive real number, by default None.
272272
hn_beta : float, optional
273-
a positibe real number, None by default.
273+
a positibe real number, by default None.
274274
"""
275275
if hn_alpha is not None:
276276
self.hn_alpha = _check.pos_float(hn_alpha,'hn_alpha',ParameterFormatError)
@@ -304,7 +304,7 @@ def update_posterior(self,x):
304304
return self
305305

306306
def _update_posterior(self,x):
307-
"""Update opsterior withou input check."""
307+
"""Update opsterior without input check."""
308308
self.hn_alpha += np.count_nonzero(x==1)
309309
self.hn_beta += np.count_nonzero(x==0)
310310
return self
@@ -315,10 +315,10 @@ def estimate_params(self,loss="squared",dict_out=False):
315315
Parameters
316316
----------
317317
loss : str, optional
318-
Loss function underlying the Bayes risk function, \"squared\" by default.
318+
Loss function underlying the Bayes risk function, by default \"squared\".
319319
This function supports \"squared\", \"0-1\", \"abs\", and \"KL\".
320320
dict_out : bool, optional
321-
If ``True``, output will be a dict, by ``False`` default.
321+
If ``True``, output will be a dict, by default ``False``.
322322
323323
Returns
324324
-------
@@ -376,7 +376,7 @@ def estimate_interval(self,credibility=0.95):
376376
Parameters
377377
----------
378378
credibility : float, optional
379-
A posterior probability that the interval conitans the paramter, 0.95 by default.
379+
A posterior probability that the interval conitans the paramter, by default 0.95.
380380
381381
Returns
382382
-------
@@ -430,7 +430,7 @@ def make_prediction(self,loss="squared"):
430430
Parameters
431431
----------
432432
loss : str, optional
433-
Loss function underlying the Bayes risk function, \"squared\" by default.
433+
Loss function underlying the Bayes risk function, by default \"squared\".
434434
This function supports \"squared\", \"0-1\", \"abs\", and \"KL\".
435435
436436
Returns
@@ -462,7 +462,7 @@ def pred_and_update(self,x,loss="squared"):
462462
x : int
463463
It must be 0 or 1
464464
loss : str, optional
465-
Loss function underlying the Bayes risk function, \"squared\" by default.
465+
Loss function underlying the Bayes risk function, by default \"squared\".
466466
This function supports \"squared\", \"0-1\", \"abs\", and \"KL\".
467467
468468
Returns

bayesml/exponential/_exponential.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ class GenModel(base.Generative):
2121
Parameters
2222
----------
2323
lambda_ : float, optional
24-
a positive real number, 1.0 by default.
24+
a positive real number, by default 1.0.
2525
h_alpha : float, optional
26-
a positive real number, 1.0 by default.
26+
a positive real number, by default 1.0.
2727
h_beta : float, optional
28-
a positive real number, 1.0 by default.
28+
a positive real number, by default 1.0.
2929
seed : {None, int}, optional
3030
A seed to initialize numpy.random.default_rng(),
3131
by default None
3232
"""
33-
def __init__(self,*,lambda_=1.0,h_alpha=1.0,h_beta=1.0,seed=None):
33+
def __init__(self,lambda_=1.0,h_alpha=1.0,h_beta=1.0,seed=None):
3434
self.rng = np.random.default_rng(seed)
3535

3636
# params
@@ -61,9 +61,9 @@ def set_h_params(self,h_alpha=None,h_beta=None):
6161
Parameters
6262
----------
6363
h_alpha : float, optional
64-
a positive real number, None by default.
64+
a positive real number, by default None.
6565
h_beta : float, optional
66-
a positive real number, None by default.
66+
a positive real number, by default None.
6767
"""
6868
if h_alpha is not None:
6969
self.h_alpha = _check.pos_float(h_alpha,'h_alpha',ParameterFormatError)
@@ -96,7 +96,7 @@ def set_params(self,lambda_=None):
9696
Parameters
9797
----------
9898
lambda_ : float, optional
99-
a positive real number, None by default.
99+
a positive real number, by default None.
100100
"""
101101
if lambda_ is not None:
102102
self.lambda_ = _check.pos_float(lambda_, 'lambda_', ParameterFormatError)
@@ -153,9 +153,9 @@ def visualize_model(self,sample_size=100,hist_bins=10):
153153
Parameters
154154
----------
155155
sample_size : int, optional
156-
A positive integer, 100 by default.
156+
A positive integer, by default 100.
157157
hist_bins : float, optional
158-
A positive float, 10 by default.
158+
A positive float, by default 10.
159159
160160
Examples
161161
--------
@@ -194,9 +194,9 @@ class LearnModel(base.Posterior,base.PredictiveMixin):
194194
Parameters
195195
----------
196196
h0_alpha : float, optional
197-
a positive real number, 1.0 by default.
197+
a positive real number, by default 1.0.
198198
h0_beta : float, optional
199-
a positibe real number, 1.0 by default.
199+
a positibe real number, by default 1.0.
200200
201201
Attributes
202202
----------
@@ -242,9 +242,9 @@ def set_h0_params(self,h0_alpha=None,h0_beta=None):
242242
Parameters
243243
----------
244244
h0_alpha : float, optional
245-
a positive real number, None by default.
245+
a positive real number, by default None.
246246
h0_beta : float, optional
247-
a positibe real number, None by default.
247+
a positibe real number, by default None.
248248
"""
249249
if h0_alpha is not None:
250250
self.h0_alpha = _check.pos_float(h0_alpha, 'h0_alpha', ParameterFormatError)
@@ -270,9 +270,9 @@ def set_hn_params(self,hn_alpha=None,hn_beta=None):
270270
Parameters
271271
----------
272272
hn_alpha : float, optional
273-
a positive real number, None by default.
273+
a positive real number, by default None.
274274
hn_beta : float, optional
275-
a positibe real number, None by default.
275+
a positibe real number, by default None.
276276
"""
277277
if hn_alpha is not None:
278278
self.hn_alpha = _check.pos_float(hn_alpha, 'hn_alpha', ParameterFormatError)
@@ -301,8 +301,17 @@ def update_posterior(self,x):
301301
All the elements must be positive real numbers.
302302
"""
303303
_check.pos_floats(x, 'x', DataFormatError)
304+
try:
305+
self.hn_alpha += x.size
306+
except:
307+
self.hn_alpha += 1
308+
self.hn_beta += np.sum(x)
309+
return self
310+
311+
def _update_posterior(self,x):
312+
"""Update opsterior without input check."""
304313
self.hn_alpha += x.size
305-
self.hn_beta += x.sum()
314+
self.hn_beta += np.sum(x)
306315
return self
307316

308317
def estimate_params(self,loss="squared",dict_out=False):
@@ -311,10 +320,10 @@ def estimate_params(self,loss="squared",dict_out=False):
311320
Parameters
312321
----------
313322
loss : str, optional
314-
Loss function underlying the Bayes risk function, \"squared\" by default.
323+
Loss function underlying the Bayes risk function, by default \"squared\".
315324
This function supports \"squared\", \"0-1\", \"abs\", and \"KL\".
316325
dict_out : bool, optional
317-
If ``True``, output will be a dict, ``False`` by default.
326+
If ``True``, output will be a dict, by default ``False``.
318327
319328
Returns
320329
-------
@@ -361,7 +370,7 @@ def estimate_interval(self,credibility=0.95):
361370
Parameters
362371
----------
363372
credibility : float, optional
364-
A posterior probability that the interval conitans the paramter, 0.95 by default.
373+
A posterior probability that the interval conitans the paramter, by default 0.95.
365374
366375
Returns
367376
-------
@@ -417,7 +426,7 @@ def make_prediction(self,loss="squared"):
417426
Parameters
418427
----------
419428
loss : str, optional
420-
Loss function underlying the Bayes risk function, \"squared\" by default.
429+
Loss function underlying the Bayes risk function, by default \"squared\".
421430
This function supports \"squared\", \"0-1\", \"abs\", and \"KL\".
422431
423432
Returns
@@ -451,7 +460,7 @@ def pred_and_update(self,x,loss="squared"):
451460
x : float
452461
a positive real number
453462
loss : str, optional
454-
Loss function underlying the Bayes risk function, \"squared\" by default.
463+
Loss function underlying the Bayes risk function, by default \"squared\".
455464
This function supports \"squared\", \"0-1\", \"abs\", and \"KL\".
456465
457466
Returns

0 commit comments

Comments
 (0)