def test_identical_conds_issue(): from sympy.stats import Uniform, density u1 = Uniform('u1', 0, 1) u2 = Uniform('u2', 0, 1) # Result is quite big, so not really important here (and should ideally be # simpler). Should not give an exception though. density(u1 + u2)
def generate_decreasing_pattern_rollouts(base_price, rollout_probability): prices = [] rate = 0.9 rate -= Uniform("dec_rate_init", 0.0, 0.05) for k in range(12): prices.append(rate * base_price) rate -= 0.03 rate -= Uniform("dec_rate_%d" % k, 0.0, 0.02) return [Rollout(rollout_probability, "Decreasing", np.array(prices))]
def test_uniform(): l = Symbol('l', real=True, bounded=True) w = Symbol('w', positive=True, bounded=True) X = Uniform('x', l, l + w) assert simplify(E(X)) == l + w / 2 assert simplify(variance(X)) == w**2 / 12 # With numbers all is well X = Uniform('x', 3, 5) assert P(X < 3) == 0 and P(X > 5) == 0 assert P(X < 4) == P(X > 4) == S.Half
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_characteristic_function(): X = Uniform('x', 0, 1) cf = characteristic_function(X) assert cf(1) == -I * (-1 + exp(I)) Y = Normal('y', 1, 1) cf = characteristic_function(Y) assert cf(0) == 1 assert cf(1) == exp(I - S(1) / 2) Z = Exponential('z', 5) cf = characteristic_function(Z) assert cf(0) == 1 assert cf(1).expand() == S(25) / 26 + 5 * I / 26 X = GaussianInverse('x', 1, 1) cf = characteristic_function(X) assert cf(0) == 1 assert cf(1) == exp(1 - sqrt(1 - 2 * I)) X = ExGaussian('x', 0, 1, 1) cf = characteristic_function(X) assert cf(0) == 1 assert cf(1) == (1 + I) * exp(-S(1) / 2) / 2
def test_characteristic_function(): X = Uniform('x', 0, 1) cf = characteristic_function(X) assert cf(1) == -I * (-1 + exp(I)) Y = Normal('y', 1, 1) cf = characteristic_function(Y) assert cf(0) == 1 assert cf(1) == exp(I - S.Half) Z = Exponential('z', 5) cf = characteristic_function(Z) assert cf(0) == 1 assert cf(1).expand() == Rational(25, 26) + I * Rational(5, 26) X = GaussianInverse('x', 1, 1) cf = characteristic_function(X) assert cf(0) == 1 assert cf(1) == exp(1 - sqrt(1 - 2 * I)) X = ExGaussian('x', 0, 1, 1) cf = characteristic_function(X) assert cf(0) == 1 assert cf(1) == (1 + I) * exp(Rational(-1, 2)) / 2 L = Levy('x', 0, 1) cf = characteristic_function(L) assert cf(0) == 1 assert cf(1) == exp(-sqrt(2) * sqrt(-I))
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 scipy = import_module('scipy') if not scipy: skip('Scipy is not installed. Abort tests for _sample_scipy.') else: for X in distribs_scipy: samps = sample(X, size=size, library='scipy') samps2 = 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_precomputed_characteristic_functions(): import mpmath def test_cf(dist, support_lower_limit, support_upper_limit): pdf = density(dist) t = Symbol('t') x = Symbol('x') # first function is the hardcoded CF of the distribution cf1 = lambdify([t], characteristic_function(dist)(t), 'mpmath') # second function is the Fourier transform of the density function f = lambdify([x, t], pdf(x)*exp(I*x*t), 'mpmath') cf2 = lambda t: mpmath.quad(lambda x: f(x, t), [support_lower_limit, support_upper_limit], maxdegree=10) # compare the two functions at various points for test_point in [2, 5, 8, 11]: n1 = cf1(test_point) n2 = cf2(test_point) assert abs(re(n1) - re(n2)) < 1e-12 assert abs(im(n1) - im(n2)) < 1e-12 test_cf(Beta('b', 1, 2), 0, 1) test_cf(Chi('c', 3), 0, mpmath.inf) test_cf(ChiSquared('c', 2), 0, mpmath.inf) test_cf(Exponential('e', 6), 0, mpmath.inf) test_cf(Logistic('l', 1, 2), -mpmath.inf, mpmath.inf) test_cf(Normal('n', -1, 5), -mpmath.inf, mpmath.inf) test_cf(RaisedCosine('r', 3, 1), 2, 4) test_cf(Rayleigh('r', 0.5), 0, mpmath.inf) test_cf(Uniform('u', -1, 1), -1, 1) test_cf(WignerSemicircle('w', 3), -3, 3)
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: 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(Chi("C", 1), library='pymc3')))
def test_uniform_P(): """ This stopped working because SingleContinuousPSpace.compute_density no longer calls integrate on a DiracDelta but rather just solves directly. integrate used to call UniformDistribution.expectation which special-cased subsed out the Min and Max terms that Uniform produces I decided to regress on this class for general cleanliness (and I suspect speed) of the algorithm. """ l = Symbol('l', real=True, finite=True) w = Symbol('w', positive=True, finite=True) X = Uniform('x', l, l + w) assert P(X < l) == 0 and P(X > l + w) == 0
def test_uniform(): l = Symbol('l', real=True, finite=True) w = Symbol('w', positive=True, finite=True) X = Uniform('x', l, l + w) assert simplify(E(X)) == l + w/2 assert simplify(variance(X)) == w**2/12 # With numbers all is well X = Uniform('x', 3, 5) assert P(X < 3) == 0 and P(X > 5) == 0 assert P(X < 4) == P(X > 4) == S.Half z = Symbol('z') p = density(X)(z) assert p.subs(z, 3.7) == S(1)/2 assert p.subs(z, -1) == 0 assert p.subs(z, 6) == 0 c = cdf(X) assert c(2) == 0 and c(3) == 0 assert c(S(7)/2) == S(1)/4 assert c(5) == 1 and c(6) == 1
def test_uniform(): l = Symbol('l', real=True) w = Symbol('w', positive=True) X = Uniform('x', l, l + w) assert E(X) == l + w / 2 assert variance(X).expand() == w**2 / 12 # With numbers all is well X = Uniform('x', 3, 5) assert P(X < 3) == 0 and P(X > 5) == 0 assert P(X < 4) == P(X > 4) == S.Half z = Symbol('z') p = density(X)(z) assert p.subs(z, 3.7) == S.Half assert p.subs(z, -1) == 0 assert p.subs(z, 6) == 0 c = cdf(X) assert c(2) == 0 and c(3) == 0 assert c(Rational(7, 2)) == Rational(1, 4) assert c(5) == 1 and c(6) == 1
def test_characteristic_function(): X = Uniform('x', 0, 1) cf = characteristic_function(X) assert cf(1) == -I*(-1 + exp(I)) Y = Normal('y', 1, 1) cf = characteristic_function(Y) assert cf(0) == 1 assert simplify(cf(1)) == exp(I - S(1)/2) Z = Exponential('z', 5) cf = characteristic_function(Z) assert cf(0) == 1 assert simplify(cf(1)) == S(25)/26 + 5*I/26
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_bernoulli_CompoundDist(): X = Beta('X', 1, 2) Y = Bernoulli('Y', X) assert density(Y).dict == {0: S(2)/3, 1: S(1)/3} assert E(Y) == P(Eq(Y, 1)) == S(1)/3 assert variance(Y) == S(2)/9 assert cdf(Y) == {0: S(2)/3, 1: 1} # test issue 8128 a = Bernoulli('a', S(1)/2) b = Bernoulli('b', a) assert density(b).dict == {0: S(1)/2, 1: S(1)/2} assert P(b > 0.5) == S(1)/2 X = Uniform('X', 0, 1) Y = Bernoulli('Y', X) assert E(Y) == S(1)/2 assert P(Eq(Y, 1)) == E(Y)
def generate_all_turnip_patterns(previous_pattern_prior): assert(previous_pattern_prior.shape == (4,)) assert(np.allclose(np.sum(previous_pattern_prior), 1.)) all_rollouts = [] # Random integer base price on [90, 110] base_price = Uniform("base_price", 90., 110.) # Probability of being in each pattern: pattern_probs = np.dot(pattern_transition_matrix, previous_pattern_prior) for next_pattern_k in range(4): all_rollouts += pattern_rollout_generators[next_pattern_k]( base_price=base_price, rollout_probability=pattern_probs[next_pattern_k]) return all_rollouts
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 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_issue_13324(): X = Uniform('X', 0, 1) assert E(X, X > S.Half) == Rational(3, 4) assert E(X, X > 0) == S.Half
def test_issue_13324(): X = Uniform('X', 0, 1) assert E(X, X > Rational(1, 2)) == Rational(3, 4) assert E(X, X > 0) == Rational(1, 2)
def test_moment_generating_function(): t = symbols('t', positive=True) # Symbolic tests a, b, c = symbols('a b c') mgf = moment_generating_function(Beta('x', a, b))(t) assert mgf == hyper((a, ), (a + b, ), t) mgf = moment_generating_function(Chi('x', a))(t) assert mgf == sqrt(2)*t*gamma(a/2 + S.Half)*\ hyper((a/2 + S.Half,), (Rational(3, 2),), t**2/2)/gamma(a/2) +\ hyper((a/2,), (S.Half,), t**2/2) mgf = moment_generating_function(ChiSquared('x', a))(t) assert mgf == (1 - 2 * t)**(-a / 2) mgf = moment_generating_function(Erlang('x', a, b))(t) assert mgf == (1 - t / b)**(-a) mgf = moment_generating_function(ExGaussian("x", a, b, c))(t) assert mgf == exp(a * t + b**2 * t**2 / 2) / (1 - t / c) mgf = moment_generating_function(Exponential('x', a))(t) assert mgf == a / (a - t) mgf = moment_generating_function(Gamma('x', a, b))(t) assert mgf == (-b * t + 1)**(-a) mgf = moment_generating_function(Gumbel('x', a, b))(t) assert mgf == exp(b * t) * gamma(-a * t + 1) mgf = moment_generating_function(Gompertz('x', a, b))(t) assert mgf == b * exp(b) * expint(t / a, b) mgf = moment_generating_function(Laplace('x', a, b))(t) assert mgf == exp(a * t) / (-b**2 * t**2 + 1) mgf = moment_generating_function(Logistic('x', a, b))(t) assert mgf == exp(a * t) * beta(-b * t + 1, b * t + 1) mgf = moment_generating_function(Normal('x', a, b))(t) assert mgf == exp(a * t + b**2 * t**2 / 2) mgf = moment_generating_function(Pareto('x', a, b))(t) assert mgf == b * (-a * t)**b * uppergamma(-b, -a * t) mgf = moment_generating_function(QuadraticU('x', a, b))(t) assert str(mgf) == ( "(3*(t*(-4*b + (a + b)**2) + 4)*exp(b*t) - " "3*(t*(a**2 + 2*a*(b - 2) + b**2) + 4)*exp(a*t))/(t**2*(a - b)**3)") mgf = moment_generating_function(RaisedCosine('x', a, b))(t) assert mgf == pi**2 * exp(a * t) * sinh(b * t) / (b * t * (b**2 * t**2 + pi**2)) mgf = moment_generating_function(Rayleigh('x', a))(t) assert mgf == sqrt(2)*sqrt(pi)*a*t*(erf(sqrt(2)*a*t/2) + 1)\ *exp(a**2*t**2/2)/2 + 1 mgf = moment_generating_function(Triangular('x', a, b, c))(t) assert str(mgf) == ("(-2*(-a + b)*exp(c*t) + 2*(-a + c)*exp(b*t) + " "2*(b - c)*exp(a*t))/(t**2*(-a + b)*(-a + c)*(b - c))") mgf = moment_generating_function(Uniform('x', a, b))(t) assert mgf == (-exp(a * t) + exp(b * t)) / (t * (-a + b)) mgf = moment_generating_function(UniformSum('x', a))(t) assert mgf == ((exp(t) - 1) / t)**a mgf = moment_generating_function(WignerSemicircle('x', a))(t) assert mgf == 2 * besseli(1, a * t) / (a * t) # Numeric tests mgf = moment_generating_function(Beta('x', 1, 1))(t) assert mgf.diff(t).subs(t, 1) == hyper((2, ), (3, ), 1) / 2 mgf = moment_generating_function(Chi('x', 1))(t) assert mgf.diff(t).subs(t, 1) == sqrt(2) * hyper( (1, ), (Rational(3, 2), ), S.Half) / sqrt(pi) + hyper( (Rational(3, 2), ), (Rational(3, 2), ), S.Half) + 2 * sqrt(2) * hyper( (2, ), (Rational(5, 2), ), S.Half) / (3 * sqrt(pi)) mgf = moment_generating_function(ChiSquared('x', 1))(t) assert mgf.diff(t).subs(t, 1) == I mgf = moment_generating_function(Erlang('x', 1, 1))(t) assert mgf.diff(t).subs(t, 0) == 1 mgf = moment_generating_function(ExGaussian("x", 0, 1, 1))(t) assert mgf.diff(t).subs(t, 2) == -exp(2) mgf = moment_generating_function(Exponential('x', 1))(t) assert mgf.diff(t).subs(t, 0) == 1 mgf = moment_generating_function(Gamma('x', 1, 1))(t) assert mgf.diff(t).subs(t, 0) == 1 mgf = moment_generating_function(Gumbel('x', 1, 1))(t) assert mgf.diff(t).subs(t, 0) == EulerGamma + 1 mgf = moment_generating_function(Gompertz('x', 1, 1))(t) assert mgf.diff(t).subs(t, 1) == -e * meijerg(((), (1, 1)), ((0, 0, 0), ()), 1) mgf = moment_generating_function(Laplace('x', 1, 1))(t) assert mgf.diff(t).subs(t, 0) == 1 mgf = moment_generating_function(Logistic('x', 1, 1))(t) assert mgf.diff(t).subs(t, 0) == beta(1, 1) mgf = moment_generating_function(Normal('x', 0, 1))(t) assert mgf.diff(t).subs(t, 1) == exp(S.Half) mgf = moment_generating_function(Pareto('x', 1, 1))(t) assert mgf.diff(t).subs(t, 0) == expint(1, 0) mgf = moment_generating_function(QuadraticU('x', 1, 2))(t) assert mgf.diff(t).subs(t, 1) == -12 * e - 3 * exp(2) mgf = moment_generating_function(RaisedCosine('x', 1, 1))(t) assert mgf.diff(t).subs(t, 1) == -2*e*pi**2*sinh(1)/\ (1 + pi**2)**2 + e*pi**2*cosh(1)/(1 + pi**2) mgf = moment_generating_function(Rayleigh('x', 1))(t) assert mgf.diff(t).subs(t, 0) == sqrt(2) * sqrt(pi) / 2 mgf = moment_generating_function(Triangular('x', 1, 3, 2))(t) assert mgf.diff(t).subs(t, 1) == -e + exp(3) mgf = moment_generating_function(Uniform('x', 0, 1))(t) assert mgf.diff(t).subs(t, 1) == 1 mgf = moment_generating_function(UniformSum('x', 1))(t) assert mgf.diff(t).subs(t, 1) == 1 mgf = moment_generating_function(WignerSemicircle('x', 1))(t) assert mgf.diff(t).subs(t, 1) == -2*besseli(1, 1) + besseli(2, 1) +\ besseli(0, 1)