@property def mean(self): return self.alpha / self.lamb @property def var(self): return self.alpha / self.lamb**2 def _sample(self): raise NotImplementedError def setup_distribution(): ugen = RngStream() dist = Gamma(alpha=1.5, lamb=6, ugen=ugen) return dist if __name__ == "__main__": parser = get_parser() args = parser.parse_args() num_samples = args.num_samples dist = setup_distribution() samples = dist.sample(num_samples) DensityPlotter(dist=dist).make_plots(samples.flatten()) test_mean(dist, samples)
return self._var def _sample(self): u1, u2 = self.ugen1.RandU01(), self.ugen2.RandU01() return self.low + (u1 + u2) * (self.width / 2) def setup_distribution(): ugen1 = RngStream() ugen2 = RngStream() # symm_tri_dist = SymmetricTriangular(ugen1=ugen1, ugen2=ugen2) symm_tri_dist = SymmetricTriangular(low=1, high=10, ugen1=ugen1, ugen2=ugen2) # symm_tri_dist = Gaussian(mu=10, sigma=1, ugen1=ugen1, ugen2=ugen2) return symm_tri_dist if __name__ == "__main__": parser = get_parser() args = parser.parse_args() num_samples = args.num_samples symm_tri_dist = setup_distribution() samples = symm_tri_dist.sample(num_samples) DensityPlotter(dist=symm_tri_dist).make_plots(samples.flatten()) test_mean(symm_tri_dist, samples)
class StdNormal(Gaussian): def __init__(self, ugen1: RngStream, ugen2: RngStream): super().__init__(0, 1, ugen1, ugen2) def _sample(self): try: return self.cache.pop() except IndexError: self.fill_cache() return self.cache.pop() if __name__ == "__main__": parser = get_parser() args = parser.parse_args() num_samples = args.num_samples ugen1 = RngStream() ugen2 = RngStream() normal_dist = StdNormal(ugen1=ugen1, ugen2=ugen2) # normal_dist = Gaussian(mu=10, sigma=1, ugen1=ugen1, ugen2=ugen2) samples = normal_dist.sample(num_samples) DensityPlotter(dist=normal_dist).make_plots(samples.flatten()) test_mean(normal_dist, samples)
from distributions.make_plots import DensityPlotter from distributions.utils import get_parser, test_mean class Exponential(Distribution): def __init__(self, lamb: float, ugen: RngStream): self.lamb = lamb self.mean = 1 / lamb self.var = 1 / lamb**2 self.ugen = ugen def _sample(self): u = self.ugen.RandU01() return -log(u) / self.lamb if __name__ == "__main__": parser = get_parser() args = parser.parse_args() num_samples = args.num_samples ugen = RngStream() exp_dist = Exponential(1, ugen=ugen) samples = exp_dist.sample(num_samples) DensityPlotter(dist=exp_dist).make_plots(samples.flatten()) test_mean(exp_dist, samples)
def sample(self, *shape): num_samples = np.prod(shape, dtype=int) samples = [self._sample() for _ in range(num_samples)] samples = np.reshape(samples, shape) return samples class Uniform01(Uniform): def __init__(self, ugen: RngStream): super().__init__(0, 1, ugen) def _sample(self): return self.ugen.RandU01() if __name__ == "__main__": parser = get_parser() args = parser.parse_args() num_samples = args.num_samples ugen = RngStream() # unif_dist = Uniform01(ugen=ugen) unif_dist = Uniform(100, 500, ugen=ugen) samples = unif_dist.sample(num_samples, 100) DensityPlotter(dist=unif_dist).make_plots(samples.flatten()) test_mean(unif_dist, samples)