Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AllenDowney
GitHub Repository: AllenDowney/bayesian-analysis-recipes
Path: blob/master/incubator/sampling_source.py
411 views
1
import numpy as np
2
from scipy.stats import norm, expon
3
4
5
class Distribution:
6
def __init__(self):
7
return NotImplementedError("Please implement init.")
8
9
def pdf(self, x):
10
"""Evaluate total probability of data x."""
11
return np.prod(self.dist.pdf(x))
12
13
def logpdf(self, x):
14
"""Evaluate total log probability of data x."""
15
return np.sum(self.dist.logpdf(x))
16
17
def draws(self, n):
18
"""Draw n samples from the distribution"""
19
return self.dist.rvs(n)
20
21
22
class Normal(Distribution):
23
def __init__(self, mu, sigma):
24
self.mu = mu
25
self.sigma = sigma
26
self.dist = norm(mu, sigma)
27
28
29
class Exponential(Distribution):
30
def __init__(self, lam):
31
self.lam = lam
32
self.dist = expon(scale=lam)
33
34