def test_lognormal(): mean = Symbol('mu', real=True, finite=True) std = Symbol('sigma', positive=True, real=True, finite=True) X = LogNormal('x', mean, std) # The sympy integrator can't do this too well #assert E(X) == exp(mean+std**2/2) #assert variance(X) == (exp(std**2)-1) * exp(2*mean + std**2) # Right now, only density function and sampling works # Test sampling: Only e^mean in sample std of 0 for i in range(3): X = LogNormal('x', i, 0) assert S(sample(X)) == N(exp(i)) # The sympy integrator can't do this too well #assert E(X) == mu = Symbol("mu", real=True) sigma = Symbol("sigma", positive=True) X = LogNormal('x', mu, sigma) assert density(X)(x) == (sqrt(2)*exp(-(-mu + log(x))**2 /(2*sigma**2))/(2*x*sqrt(pi)*sigma)) X = LogNormal('x', 0, 1) # Mean 0, standard deviation 1 assert density(X)(x) == sqrt(2)*exp(-log(x)**2/2)/(2*x*sqrt(pi))
def test_sample_continuous(): z = Symbol('z') Z = ContinuousRV(z, exp(-z), set=Interval(0, oo)) assert sample(Z) in Z.pspace.domain.set sym, val = list(Z.pspace.sample().items())[0] assert sym == Z and val in Interval(0, oo) assert density(Z)(-1) == 0
def test_given(): X = Die('X', 6) assert density(X, X > 5) == {S(6): S.One} assert where(X > 2, X > 5).as_boolean() == Eq(X.symbol, 6) scipy = import_module('scipy') if not scipy: skip('Scipy is not installed. Abort tests') with ignore_warnings( UserWarning): ### TODO: Restore tests once warnings are removed assert next(sample(X, X > 5)) == 6
def test_sample_pymc3(): distribs_pymc3 = [ MultivariateNormal("M", [5, 2], [[1, 0], [0, 1]]), MultivariateBeta("B", [0.4, 5, 15]), Multinomial("N", 4, [0.3, 0.2, 0.1, 0.4]) ] size = 3 pymc3 = import_module('pymc3') if not pymc3: skip('PyMC3 is not installed. Abort tests for _sample_pymc3.') else: with ignore_warnings(UserWarning): for X in distribs_pymc3: samps = next(sample(X, size=size, library='pymc3')) for sam in samps: assert tuple(sam.flatten()) in X.pspace.distribution.set N_c = NegativeMultinomial('N', 3, 0.1, 0.1, 0.1) raises(NotImplementedError, lambda: next(sample(N_c, library='pymc3')))
def test_sample_numpy(): distribs_numpy = [Geometric('G', 0.5), Poisson('P', 1), Zeta('Z', 2)] size = 3 numpy = import_module('numpy') if not numpy: skip('Numpy is not installed. Abort tests for _sample_numpy.') else: with ignore_warnings( UserWarning ): ### TODO: Restore tests once warnings are removed for X in distribs_numpy: samps = next(sample(X, size=size, library='numpy')) for sam in samps: assert sam in X.pspace.domain.set raises(NotImplementedError, lambda: next(sample(Skellam('S', 1, 1), library='numpy'))) raises( NotImplementedError, lambda: Skellam('S', 1, 1).pspace.distribution. sample(library='tensorflow'))
def test_prefab_sampling(): N = Normal('X', 0, 1) L = LogNormal('L', 0, 1) E = Exponential('Ex', 1) P = Pareto('P', 1, 3) W = Weibull('W', 1, 1) U = Uniform('U', 0, 1) B = Beta('B', 2, 5) G = Gamma('G', 1, 3) variables = [N, L, E, P, W, U, B, G] niter = 10 size = 5 for var in variables: for i in range(niter): assert sample(var) in var.pspace.domain.set samps = sample(var, size=size) for samp in samps: assert samp in var.pspace.domain.set
def test_Sample(): X = Die(6) Y = Normal(0,1) z = Symbol('z') assert sample(X) in [1,2,3,4,5,6] assert sample(X+Y).is_Float P(X+Y>0, Y<0, numsamples=10).is_number assert E(X+Y, numsamples=10).is_number assert variance(X+Y, numsamples=10).is_number raises(ValueError, lambda: P(Y>z, numsamples=5)) assert P(sin(Y)<=1, numsamples=10) == 1 assert P(sin(Y)<=1, cos(Y)<1, numsamples=10) == 1 # Make sure this doesn't raise an error E(Sum(1/z**Y, (z,1,oo)), Y>2, numsamples=3)
def test_Sample(): X = Die('X', 6) Y = Normal('Y', 0, 1) z = Symbol('z') assert sample(X) in [1, 2, 3, 4, 5, 6] assert sample(X + Y).is_Float P(X + Y > 0, Y < 0, numsamples=10).is_number assert E(X + Y, numsamples=10).is_number assert variance(X + Y, numsamples=10).is_number raises(ValueError, lambda: P(Y > z, numsamples=5)) assert P(sin(Y) <= 1, numsamples=10) == 1 assert P(sin(Y) <= 1, cos(Y) < 1, numsamples=10) == 1 # Make sure this doesn't raise an error E(Sum(1/z**Y, (z, 1, oo)), Y > 2, numsamples=3)
def test_sample_numpy(): distribs_numpy = [ Geometric('G', 0.5), Poisson('P', 1), Zeta('Z', 2) ] size = 3 numpy = import_module('numpy') if not numpy: skip('Numpy is not installed. Abort tests for _sample_numpy.') else: for X in distribs_numpy: samps = sample(X, size=size, library='numpy') for sam in samps: assert sam in X.pspace.domain.set raises(NotImplementedError, lambda: sample(Skellam('S', 1, 1), library='numpy')) raises(NotImplementedError, lambda: Skellam('S', 1, 1).pspace.distribution.sample(library='tensorflow'))
def test_sample_numpy(): distribs_numpy = [ MultivariateNormal("M", [3, 4], [[2, 1], [1, 2]]), MultivariateBeta("B", [0.4, 5, 15, 50, 203]), Multinomial("N", 50, [0.3, 0.2, 0.1, 0.25, 0.15]) ] size = 3 numpy = import_module('numpy') if not numpy: skip('Numpy is not installed. Abort tests for _sample_numpy.') else: with ignore_warnings(UserWarning): for X in distribs_numpy: samps = next(sample(X, size=size, library='numpy')) for sam in samps: assert tuple(sam) in X.pspace.distribution.set N_c = NegativeMultinomial('N', 3, 0.1, 0.1, 0.1) raises(NotImplementedError, lambda: next(sample(N_c, library='numpy')))
def test_sampling_gaussian_inverse(): scipy = import_module('scipy') if not scipy: skip( 'Scipy not installed. Abort tests for sampling of Gaussian inverse.' ) X = GaussianInverse("x", 1, 1) with ignore_warnings( UserWarning): ### TODO: Restore tests once warnings are removed assert next(sample(X, library='scipy')) in X.pspace.domain.set
def test_sample_scipy(): distribs_scipy = [ MatrixNormal('M', [[5, 6]], [4], [[2, 1], [1, 2]]), Wishart('W', 5, [[1, 0], [0, 1]]) ] size = 5 scipy = import_module('scipy') if not scipy: skip('Scipy not installed. Abort tests for _sample_scipy.') else: with ignore_warnings( UserWarning ): ### TODO: Restore tests once warnings are removed for X in distribs_scipy: samps = next(sample(X, size=size)) for sam in samps: assert Matrix(sam) in X.pspace.distribution.set M = MatrixGamma('M', 1, 2, [[1, 0], [0, 1]]) raises(NotImplementedError, lambda: next(sample(M, size=3)))
def test_sample_numpy(): distribs_numpy = [ Binomial("B", 5, 0.4), ] size = 3 numpy = import_module('numpy') if not numpy: skip('Numpy is not installed. Abort tests for _sample_numpy.') else: with ignore_warnings( UserWarning ): ### TODO: Restore tests once warnings are removed for X in distribs_numpy: samps = next(sample(X, size=size, library='numpy')) for sam in samps: assert sam in X.pspace.domain.set raises(NotImplementedError, lambda: next(sample(Die("D"), library='numpy'))) raises(NotImplementedError, lambda: Die("D").pspace.sample(library='tensorflow'))
def test_sample_pymc3(): distribs_pymc3 = [ MatrixNormal('M', [[5, 6], [3, 4]], [[1, 0], [0, 1]], [[2, 1], [1, 2]]), Wishart('W', 7, [[2, 1], [1, 2]]) ] size = 3 pymc3 = import_module('pymc3') if not pymc3: skip('PyMC3 is not installed. Abort tests for _sample_pymc3.') else: with ignore_warnings( UserWarning ): ### TODO: Restore tests once warnings are removed for X in distribs_pymc3: samps = next(sample(X, size=size, library='pymc3')) for sam in samps: assert Matrix(sam) in X.pspace.distribution.set M = MatrixGamma('M', 1, 2, [[1, 0], [0, 1]]) raises(NotImplementedError, lambda: next(sample(M, size=3)))
def test_sample_pymc3(): distribs_pymc3 = [ Geometric('G', 0.5), Poisson('P', 1), NegativeBinomial('N', 5, 0.4) ] size = 3 pymc3 = import_module('pymc3') if not pymc3: skip('PyMC3 is not installed. Abort tests for _sample_pymc3.') else: with ignore_warnings( UserWarning ): ### TODO: Restore tests once warnings are removed for X in distribs_pymc3: samps = next(sample(X, size=size, library='pymc3')) for sam in samps: assert sam in X.pspace.domain.set raises(NotImplementedError, lambda: next(sample(Skellam('S', 1, 1), library='pymc3')))
def test_sample_scipy(): distribs_scipy = [ MultivariateNormal("M", [0, 0], [[0.1, 0.025], [0.025, 0.1]]), MultivariateBeta("B", [0.4, 5, 15]), Multinomial("N", 8, [0.3, 0.2, 0.1, 0.4]) ] size = 3 scipy = import_module('scipy') if not scipy: skip('Scipy not installed. Abort tests for _sample_scipy.') else: for X in distribs_scipy: samps = sample(X, size=size) samps2 = sample(X, size=(2, 2)) for sam in samps: assert tuple(sam) in X.pspace.distribution.set for i in range(2): for j in range(2): assert tuple(samps2[i][j]) in X.pspace.distribution.set N_c = NegativeMultinomial('N', 3, 0.1, 0.1, 0.1) raises(NotImplementedError, lambda: sample(N_c))
def test_prefab_sampling(): scipy = import_module('scipy') if not scipy: skip('Scipy is not installed. Abort tests') N = Normal('X', 0, 1) L = LogNormal('L', 0, 1) E = Exponential('Ex', 1) P = Pareto('P', 1, 3) W = Weibull('W', 1, 1) U = Uniform('U', 0, 1) B = Beta('B', 2, 5) G = Gamma('G', 1, 3) variables = [N, L, E, P, W, U, B, G] niter = 10 size = 5 for var in variables: for _ in range(niter): assert sample(var) in var.pspace.domain.set samps = sample(var, size=size) for samp in samps: assert samp in var.pspace.domain.set
def __init__(self): self.is_focused = True self.focus_loss_density = Normal('focus_loss', config.FOCUS_LENGTH_MEAN, config.FOCUS_LENGTH_STD) self.start_time = time.time() + sample(self.focus_loss_density) self.click_interval_density_focused = Normal('focused_click', 50, 20) self.click_interval_density_unfocused = Normal('unfocused_click', 200, 50) self.movement_speed_density = Normal('mouse_movement', 0.5, 0.10)
def test_Sample(): X = Die("X", 6) Y = Normal("Y", 0, 1) z = Symbol("z") assert sample(X) in [1, 2, 3, 4, 5, 6] assert sample(X + Y).is_Float P(X + Y > 0, Y < 0, numsamples=10).is_number assert E(X + Y, numsamples=10).is_number assert variance(X + Y, numsamples=10).is_number raises(ValueError, lambda: P(Y > z, numsamples=5)) assert P(sin(Y) <= 1, numsamples=10) == 1 assert P(sin(Y) <= 1, cos(Y) < 1, numsamples=10) == 1 # Make sure this doesn't raise an error E(Sum(1 / z ** Y, (z, 1, oo)), Y > 2, numsamples=3) assert all(i in range(1, 7) for i in density(X, numsamples=10)) assert all(i in range(4, 7) for i in density(X, X > 3, numsamples=10))
def test_sample_scipy(): distribs_scipy = [ FiniteRV('F', { 1: S.Half, 2: Rational(1, 4), 3: Rational(1, 4) }), DiscreteUniform("Y", list(range(5))), Die("D"), Bernoulli("Be", 0.3), Binomial("Bi", 5, 0.4), BetaBinomial("Bb", 2, 1, 1), Hypergeometric("H", 1, 1, 1), Rademacher("R") ] size = 3 numsamples = 5 scipy = import_module('scipy') if not scipy: skip('Scipy not installed. Abort tests for _sample_scipy.') else: with ignore_warnings( UserWarning ): ### TODO: Restore tests once warnings are removed h_sample = list( sample(Hypergeometric("H", 1, 1, 1), size=size, numsamples=numsamples)) assert len(h_sample) == numsamples for X in distribs_scipy: samps = next(sample(X, size=size)) samps2 = next(sample(X, size=(2, 2))) for sam in samps: assert sam in X.pspace.domain.set for i in range(2): for j in range(2): assert samps2[i][j] in X.pspace.domain.set
def test_sample_discrete(): X = Geometric('X', S.Half) scipy = import_module('scipy') if not scipy: skip('Scipy not installed. Abort tests') assert sample(X) in X.pspace.domain.set samps = sample(X, size=2) # This takes long time if ran without scipy for samp in samps: assert samp in X.pspace.domain.set libraries = ['scipy', 'numpy', 'pymc3'] for lib in libraries: try: imported_lib = import_module(lib) if imported_lib: s0, s1, s2 = [], [], [] s0 = sample(X, size=10, library=lib, seed=0) s1 = sample(X, size=10, library=lib, seed=0) s2 = sample(X, size=10, library=lib, seed=1) assert all(s0 == s1) assert not all(s1 == s2) except NotImplementedError: continue
def test_sample_scipy(): distribs_scipy = [ Beta("B", 1, 1), BetaPrime("BP", 1, 1), Cauchy("C", 1, 1), Chi("C", 1), Normal("N", 0, 1), Gamma("G", 2, 7), GammaInverse("GI", 1, 1), GaussianInverse("GUI", 1, 1), Exponential("E", 2), LogNormal("LN", 0, 1), Pareto("P", 1, 1), StudentT("S", 2), ChiSquared("CS", 2), Uniform("U", 0, 1) ] size = 3 numsamples = 5 scipy = import_module('scipy') if not scipy: skip('Scipy is not installed. Abort tests for _sample_scipy.') else: with ignore_warnings( UserWarning ): ### TODO: Restore tests once warnings are removed g_sample = list( sample(Gamma("G", 2, 7), size=size, numsamples=numsamples)) assert len(g_sample) == numsamples for X in distribs_scipy: samps = next(sample(X, size=size, library='scipy')) samps2 = next(sample(X, size=(2, 2), library='scipy')) for sam in samps: assert sam in X.pspace.domain.set for i in range(2): for j in range(2): assert samps2[i][j] in X.pspace.domain.set
def test_prefab_sampling(): N = Normal("X", 0, 1) L = LogNormal("L", 0, 1) E = Exponential("Ex", 1) P = Pareto("P", 1, 3) W = Weibull("W", 1, 1) U = Uniform("U", 0, 1) B = Beta("B", 2, 5) G = Gamma("G", 1, 3) variables = [N, L, E, P, W, U, B, G] niter = 10 for var in variables: for i in range(niter): assert sample(var) in var.pspace.domain.set
def test_prefab_sampling(): N = Normal(0, 1) L = LogNormal(0, 1) E = Exponential(1) P = Pareto(1, 3) W = Weibull(1, 1) U = Uniform(0, 1) B = Beta(2, 5) G = Gamma(1, 3) variables = [N, L, E, P, W, U, B, G] niter = 10 for var in variables: for i in xrange(niter): assert sample(var) in var.pspace.domain.set
def test_prefab_sampling(): N = Normal('X', 0, 1) L = LogNormal('L', 0, 1) E = Exponential('Ex', 1) P = Pareto('P', 1, 3) W = Weibull('W', 1, 1) U = Uniform('U', 0, 1) B = Beta('B', 2, 5) G = Gamma('G', 1, 3) variables = [N, L, E, P, W, U, B, G] niter = 10 for var in variables: for i in xrange(niter): assert sample(var) in var.pspace.domain.set
def test_sample_numpy(): distribs_numpy = [ Beta("B", 1, 1), Normal("N", 0, 1), Gamma("G", 2, 7), Exponential("E", 2), LogNormal("LN", 0, 1), Pareto("P", 1, 1), ChiSquared("CS", 2), Uniform("U", 0, 1) ] size = 3 numpy = import_module('numpy') if not numpy: skip('Numpy is not installed. Abort tests for _sample_numpy.') else: for X in distribs_numpy: samps = sample(X, size=size, library='numpy') for sam in samps: assert sam in X.pspace.domain.set raises(NotImplementedError, lambda: sample(Chi("C", 1), library='numpy')) raises(NotImplementedError, lambda: Chi("C", 1).pspace.distribution.sample(library='tensorflow'))
def test_sample_pymc3(): distribs_pymc3 = [ Beta("B", 1, 1), Cauchy("C", 1, 1), Normal("N", 0, 1), Gamma("G", 2, 7), GaussianInverse("GI", 1, 1), Exponential("E", 2), LogNormal("LN", 0, 1), Pareto("P", 1, 1), ChiSquared("CS", 2), Uniform("U", 0, 1) ] size = 3 pymc3 = import_module('pymc3') if not pymc3: skip('PyMC3 is not installed. Abort tests for _sample_pymc3.') else: for X in distribs_pymc3: samps = sample(X, size=size, library='pymc3') for sam in samps: assert sam in X.pspace.domain.set raises(NotImplementedError, lambda: sample(Chi("C", 1), library='pymc3'))
def sample( self, n: int, variables: Optional[Sequence[Hashable]] = None, seed: Optional[Union[int, np.random.Generator]] = None, ): """ Sample method to generate data for the given variables. If no list of variables is supplied, the method will simply generate data for all variables. Setting the seed guarantees reproducibility. Parameters ---------- n: int, number of samples variables: list, the variable names to consider for sampling. If None, all variables will be sampled. seed: int, the seeding for the noise generators Returns ------- pd.DataFrame, the dataframe containing the samples of all the variables needed for the selection. """ samples = dict() if seed is None: seed = self.rng_state for node in self._causal_iterator(variables): node_attr = self.dag.nodes[node] predecessors = list(self.dag.predecessors(node)) named_args = dict() for pred in predecessors: named_args[pred] = samples[pred] noise_gen = node_attr[self.noise_key] if noise_gen is not None: named_args[Assignment.noise_argname] = np.array(list( sample(noise_gen, numsamples=n, seed=seed)), dtype=float) data = node_attr[self.assignment_key](**named_args) samples[node] = data return pd.DataFrame.from_dict(samples)
def test_lognormal(): mean = Symbol('mu', real=True) std = Symbol('sigma', positive=True) X = LogNormal('x', mean, std) # The sympy integrator can't do this too well #assert E(X) == exp(mean+std**2/2) #assert variance(X) == (exp(std**2)-1) * exp(2*mean + std**2) # Right now, only density function and sampling works for i in range(3): X = LogNormal('x', i, 1) assert sample(X) in X.pspace.domain.set # The sympy integrator can't do this too well #assert E(X) == mu = Symbol("mu", real=True) sigma = Symbol("sigma", positive=True) X = LogNormal('x', mu, sigma) assert density(X)(x) == (sqrt(2)*exp(-(-mu + log(x))**2 /(2*sigma**2))/(2*x*sqrt(pi)*sigma)) X = LogNormal('x', 0, 1) # Mean 0, standard deviation 1 assert density(X)(x) == sqrt(2)*exp(-log(x)**2/2)/(2*x*sqrt(pi))
def test_sampling_gamma_inverse(): scipy = import_module('scipy') if not scipy: skip('Scipy not installed. Abort tests for sampling of gamma inverse.') X = GammaInverse("x", 1, 1) assert sample(X) in X.pspace.domain.set
def test_sample(): z = Symbol('z') Z = ContinuousRV(z, exp(-z), set=Interval(0, oo)) assert sample(Z) in Z.pspace.domain.set sym, val = list(Z.pspace.sample().items())[0] assert sym == Z and val in Interval(0, oo)
def test_given(): X = Die('X', 6) assert density(X, X > 5) == {S(6): S(1)} assert where(X > 2, X > 5).as_boolean() == Eq(X.symbol, 6) assert sample(X, X > 5) == 6
def test_given(): X = Die(6) density(X, X > 5) == {S(6): S(1)} where(X > 2, X > 5).as_boolean() == Eq(X.symbol, 6) sample(X, X > 5) == 6
def test_sample(): z = Symbol('z') Z = ContinuousRV(z, exp(-z), set=Interval(0, oo)) assert sample(Z) in Z.pspace.domain.set sym, val = Z.pspace.sample().items()[0] assert sym == Z and val in Interval(0, oo)
def sample(self): return {k: next(stats.sample(v)) for k, v in self.marginals.items()}
def test_sample_continuous(): Z = ContinuousRV(z, exp(-z), set=Interval(0, oo)) assert sample(Z) in Z.pspace.domain.set sym, val = list(Z.pspace.sample().items())[0] assert sym == Z and val in Interval(0, oo) assert density(Z)(-1) == 0
def test_given_sample(): X = Die('X', 6) scipy = import_module('scipy') if not scipy: skip('Scipy is not installed. Abort tests') assert sample(X, X > 5) == 6