def __new__(cls, mu, omega, symbol=None): mu, omega = sympify(mu), sympify(omega) x = symbol or SingleContinuousPSpace.create_symbol() pdf = 2 * mu**mu / (gamma(mu) * omega**mu) * x**(2 * mu - 1) * exp( -mu / omega * x**2) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) return obj
def __new__(cls, nu, symbol=None): nu = sympify(nu) x = symbol or SingleContinuousPSpace.create_symbol() pdf = 1 / (sqrt(nu) * beta_fn(S(1) / 2, nu / 2)) * (1 + x**2 / nu)**( -(nu + 1) / 2) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def __new__(cls, a, symbol=None): a = sympify(a) x = symbol or SingleContinuousPSpace.create_symbol() pdf = sqrt(2 / pi) * x**2 * exp(-x**2 / (2 * a**2)) / a**3 obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) return obj
def __new__(cls, mu, s, symbol=None): mu, s = sympify(mu), sympify(s) x = symbol or SingleContinuousPSpace.create_symbol() pdf = exp(-(x-mu)/s)/(s*(1+exp(-(x-mu)/s))**2) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def __new__(cls, mu, s, symbol=None): mu, s = sympify(mu), sympify(s) x = symbol or SingleContinuousPSpace.create_symbol() pdf = exp(-(x - mu) / s) / (s * (1 + exp(-(x - mu) / s))**2) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def __new__(cls, R, symbol=None): R = sympify(R) x = symbol or SingleContinuousPSpace.create_symbol() pdf = 2 / (pi * R**2) * sqrt(R**2 - x**2) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(-R, R)) return obj
def __new__(cls, alpha, beta, sigma, symbol = None): alpha, beta, sigma = sympify(alpha), sympify(beta), sympify(sigma) x = symbol or SingleContinuousPSpace.create_symbol() pdf = (exp(-alpha*log(x/sigma)-beta*log(x/sigma)**2) *(alpha/x+2*beta*log(x/sigma)/x)) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set = Interval(sigma, oo)) return obj
def __new__(cls, a, symbol = None): a = sympify(a) x = symbol or SingleContinuousPSpace.create_symbol() pdf = sqrt(2/pi)*x**2*exp(-x**2/(2*a**2))/a**3 obj = SingleContinuousPSpace.__new__(cls, x, pdf, set = Interval(0, oo)) return obj
def __new__(cls, rate, symbol=None): _value_check(rate > 0, "Rate must be positive.") x = symbol or SingleContinuousPSpace.create_symbol() pdf = rate * exp(-rate*x) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) obj.rate = rate return obj
def __new__(cls, R, symbol=None): R = sympify(R) x = symbol or SingleContinuousPSpace.create_symbol() pdf = 2/(pi*R**2)*sqrt(R**2-x**2) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set = Interval(-R, R)) return obj
def __new__(cls, n, symbol=None): n = sympify(n) x = symbol or SingleContinuousPSpace.create_symbol() k = Dummy("k") pdf =1/factorial(n-1)*Sum((-1)**k*binomial(n,k)*(x-k)**(n-1), (k,0,floor(x))) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0,n)) return obj
def __new__(cls, mean, std, symbol=None): mean, std = sympify(mean), sympify(std) x = symbol or SingleContinuousPSpace.create_symbol() pdf = exp(-(log(x)-mean)**2 / (2*std**2)) / (x*sqrt(2*pi)*std) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) obj.mean = mean obj.std = std return obj
def __new__(cls, mean, std, symbol = None): x = symbol or SingleContinuousPSpace.create_symbol() pdf = exp(-(x-mean)**2 / (2*std**2)) / (sqrt(2*pi)*std) obj = SingleContinuousPSpace.__new__(cls, x, pdf) obj.mean = mean obj.std = std obj.variance = std**2 return obj
def __new__(cls, mean, std, symbol=None): _value_check(std > 0, "Standard deviation must be positive") x = symbol or SingleContinuousPSpace.create_symbol() pdf = exp(-(x-mean)**2 / (2*std**2)) / (sqrt(2*pi)*std) obj = SingleContinuousPSpace.__new__(cls, x, pdf) obj.mean = mean obj.std = std obj.variance = std**2 return obj
def __new__(cls, xm, alpha, symbol=None): _value_check(xm > 0, "Xm must be positive") _value_check(alpha > 0, "Alpha must be positive") x = symbol or SingleContinuousPSpace.create_symbol() pdf = alpha * xm**alpha / x**(alpha+1) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(xm, oo)) obj.xm = xm obj.alpha = alpha return obj
def __new__(cls, alpha, beta, sigma, symbol=None): alpha, beta, sigma = sympify(alpha), sympify(beta), sympify(sigma) x = symbol or SingleContinuousPSpace.create_symbol() pdf = (exp(-alpha * log(x / sigma) - beta * log(x / sigma)**2) * (alpha / x + 2 * beta * log(x / sigma) / x)) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(sigma, oo)) return obj
def __new__(cls, n, symbol=None): n = sympify(n) x = symbol or SingleContinuousPSpace.create_symbol() k = Dummy("k") pdf = 1 / factorial(n - 1) * Sum( (-1)**k * binomial(n, k) * (x - k)**(n - 1), (k, 0, floor(x))) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, n)) return obj
def __new__(cls, left, right, symbol=None): left, right = sympify(left), sympify(right) x = symbol or SingleContinuousPSpace.create_symbol() pdf = Piecewise((S.Zero, x < left), (S.Zero, x > right), (S.One / (right - left), True)) obj = SingleContinuousPSpace.__new__(cls, x, pdf) obj.left = left obj.right = right return obj
def __new__(cls, mean, std, symbol=None): mean, std = sympify(mean), sympify(std) x = symbol or SingleContinuousPSpace.create_symbol() pdf = exp(-(log(x) - mean)**2 / (2 * std**2)) / (x * sqrt(2 * pi) * std) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) obj.mean = mean obj.std = std return obj
def __new__(cls, k, theta, symbol=None): _value_check(k > 0, "k must be positive") _value_check(theta > 0, "Theta must be positive") x = symbol or SingleContinuousPSpace.create_symbol() pdf = x**(k-1) * exp(-x/theta) / (gamma(k)*theta**k) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) obj.k = k obj.theta = theta return obj
def __new__(cls, rate, symbol=None): rate = sympify(rate) _value_check(rate > 0, "Rate must be positive.") x = symbol or SingleContinuousPSpace.create_symbol() pdf = rate * exp(-rate * x) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) obj.rate = rate return obj
def __new__(cls, left, right, symbol=None): x = symbol or SingleContinuousPSpace.create_symbol() pdf = Piecewise( (S.Zero, x<left), (S.Zero, x>right), (S.One/(right-left), True)) obj = SingleContinuousPSpace.__new__(cls, x, pdf) obj.left = left obj.right = right return obj
def __new__(cls, a, b, c, symbol=None): a, b, c = sympify(a), sympify(b), sympify(c) x = symbol or SingleContinuousPSpace.create_symbol() pdf = Piecewise( (2*(x-a)/((b-a)*(c-a)), And(a<=x, x<c)), (2/(b-a), Eq(x,c)), (2*(b-x)/((b-a)*(b-c)), And(c<x, x<=b)), (S.Zero, True)) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def __new__(cls, a, b, c, symbol=None): a, b, c = sympify(a), sympify(b), sympify(c) x = symbol or SingleContinuousPSpace.create_symbol() pdf = Piecewise( (2 * (x - a) / ((b - a) * (c - a)), And(a <= x, x < c)), (2 / (b - a), Eq(x, c)), (2 * (b - x) / ((b - a) * (b - c)), And(c < x, x <= b)), (S.Zero, True)) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def __new__(cls, xm, alpha, symbol=None): xm, alpha = sympify(xm), sympify(alpha) _value_check(xm > 0, "Xm must be positive") _value_check(alpha > 0, "Alpha must be positive") x = symbol or SingleContinuousPSpace.create_symbol() pdf = alpha * xm**alpha / x**(alpha + 1) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(xm, oo)) obj.xm = xm obj.alpha = alpha return obj
def __new__(cls, alpha, beta, symbol=None): _value_check(alpha > 0, "Alpha must be positive") _value_check(beta > 0, "Beta must be positive") alpha, beta = sympify(alpha), sympify(beta) x = symbol or SingleContinuousPSpace.create_symbol() pdf = beta * (x/alpha)**(beta-1) * exp(-(x/alpha)**beta) / alpha obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) obj.alpha = alpha obj.beta = beta return obj
def __new__(cls, mean, std, symbol=None): mean, std = sympify(mean), sympify(std) _value_check(std > 0, "Standard deviation must be positive") x = symbol or SingleContinuousPSpace.create_symbol() pdf = exp(-(x - mean)**2 / (2 * std**2)) / (sqrt(2 * pi) * std) obj = SingleContinuousPSpace.__new__(cls, x, pdf) obj.mean = mean obj.std = std obj.variance = std**2 return obj
def __new__(cls, alpha, beta, symbol=None): alpha, beta = sympify(alpha), sympify(beta) _value_check(alpha > 0, "Alpha must be positive") _value_check(beta > 0, "Beta must be positive") x = symbol or SingleContinuousPSpace.create_symbol() pdf = x**(alpha - 1) * (1 - x)**(beta - 1) / beta_fn(alpha, beta) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, 1)) obj.alpha = alpha obj.beta = beta return obj
def __new__(cls, alpha, beta, symbol=None): alpha, beta = sympify(alpha), sympify(beta) _value_check(alpha > 0, "Alpha must be positive") _value_check(beta > 0, "Beta must be positive") x = symbol or SingleContinuousPSpace.create_symbol() pdf = x**(alpha-1) * (1-x)**(beta-1) / beta_fn(alpha, beta) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, 1)) obj.alpha = alpha obj.beta = beta return obj
def __new__(cls, k, theta, symbol=None): k, theta = sympify(k), sympify(theta) _value_check(k > 0, "k must be positive") _value_check(theta > 0, "Theta must be positive") x = symbol or SingleContinuousPSpace.create_symbol() pdf = x**(k - 1) * exp(-x / theta) / (gamma(k) * theta**k) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) obj.k = k obj.theta = theta return obj
def compute_cdf(self, expr, **kwargs): from sympy import Lambda, Min z = Dummy('z', real=True, bounded=True) result = SingleContinuousPSpace.compute_cdf(self, expr, **kwargs) result = result(z).subs({Min(z, self.right): z, Min(z, self.left, self.right): self.left}) return Lambda(z, result)
def __new__(cls, name, mu, omega): mu, omega = sympify(mu), sympify(omega) x = Symbol(name) pdf = 2 * mu**mu / (gamma(mu) * omega**mu) * x**(2 * mu - 1) * exp( -mu / omega * x**2) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) return obj
def __new__(cls, name, nu): nu = sympify(nu) x = Symbol(name) pdf = 1 / (sqrt(nu) * beta_fn(S(1) / 2, nu / 2)) * (1 + x**2 / nu)**( -(nu + 1) / 2) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def ContinuousRV(symbol, density, set=Interval(-oo, oo)): """ Create a Continuous Random Variable given the following: -- a symbol -- a probability density function -- set on which the pdf is valid (defaults to entire real line) Returns a RandomSymbol. Many common continuous random variable types are already implemented. This function should be necessary only very rarely. Examples ======== >>> from sympy import Symbol, sqrt, exp, pi >>> from sympy.stats import ContinuousRV, P, E >>> x = Symbol('x') >>> pdf = sqrt(2)*exp(-x**2/2)/(2*sqrt(pi)) # Normal distribution >>> X = ContinuousRV(x, pdf) >>> E(X) 0 >>> P(X>0) 1/2 """ return SingleContinuousPSpace(symbol, density, set).value
def __new__(cls, name, R): R = sympify(R) x = Symbol(name) pdf = 2/(pi*R**2)*sqrt(R**2-x**2) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set = Interval(-R, R)) return obj
def __new__(cls, name, R): R = sympify(R) x = Symbol(name) pdf = 2/(pi*R**2)*sqrt(R**2 - x**2) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(-R, R)) return obj
def __new__(cls, name, mu, s): mu, s = sympify(mu), sympify(s) x = Symbol(name) pdf = exp(-(x-mu)/s)/(s*(1+exp(-(x-mu)/s))**2) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def __new__(cls, name, a): a = sympify(a) x = Symbol(name) pdf = sqrt(2/pi)*x**2*exp(-x**2/(2*a**2))/a**3 obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) return obj
def __new__(cls, name, mu, s): mu, s = sympify(mu), sympify(s) x = Symbol(name) pdf = exp(-(x - mu)/s)/(s*(1 + exp(-(x - mu)/s))**2) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def __new__(cls, name, a): a = sympify(a) x = Symbol(name) pdf = sqrt(2/pi)*x**2*exp(-x**2/(2*a**2))/a**3 obj = SingleContinuousPSpace.__new__(cls, x, pdf, set = Interval(0, oo)) return obj
def integrate(self, expr, rvs=None, **kwargs): from sympy import Max, Min result = SingleContinuousPSpace.integrate(self, expr, rvs, **kwargs) result = result.subs({ Max(self.left, self.right): self.right, Min(self.left, self.right): self.left }) return result
def compute_cdf(self, expr, **kwargs): from sympy import Lambda, Min z = Dummy('z', real=True, bounded=True) result = SingleContinuousPSpace.compute_cdf(self, expr, **kwargs) result = result(z).subs({ Min(z, self.right): z, Min(z, self.left, self.right): self.left }) return Lambda(z, result)
def __new__(cls, name, left, right): left, right = sympify(left), sympify(right) x = Symbol(name) pdf = Piecewise((S.One / (right - left), And(left <= x, x <= right)), (S.Zero, True)) obj = SingleContinuousPSpace.__new__(cls, x, pdf) obj.left = left obj.right = right return obj
def __new__(cls, name, rate): rate = sympify(rate) _value_check(rate > 0, "Rate must be positive.") x = Symbol(name) pdf = rate * exp(-rate*x) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) obj.rate = rate return obj
def __new__(cls, name, left, right): left, right = sympify(left), sympify(right) x = Symbol(name) pdf = Piecewise( (S.One/(right - left), And(left <= x, x <= right)), (S.Zero, True)) obj = SingleContinuousPSpace.__new__(cls, x, pdf) obj.left = left obj.right = right return obj
def __new__(cls, name, xm, alpha): xm, alpha = sympify(xm), sympify(alpha) _value_check(xm > 0, "Xm must be positive") _value_check(alpha > 0, "Alpha must be positive") x = Symbol(name) pdf = alpha * xm**alpha / x**(alpha + 1) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(xm, oo)) obj.xm = xm obj.alpha = alpha return obj
def __new__(cls, name, alpha, beta): alpha, beta = sympify(alpha), sympify(beta) _value_check(alpha > 0, "Alpha must be positive") _value_check(beta > 0, "Beta must be positive") x = Symbol(name) pdf = beta * (x/alpha)**(beta - 1) * exp(-(x/alpha)**beta) / alpha obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) obj.alpha = alpha obj.beta = beta return obj
def __new__(cls, name, mean, std): mean, std = sympify(mean), sympify(std) _value_check(std > 0, "Standard deviation must be positive") x = Symbol(name) pdf = exp(-(x - mean)**2 / (2*std**2)) / (sqrt(2*pi)*std) obj = SingleContinuousPSpace.__new__(cls, x, pdf) obj.mean = mean obj.std = std obj.variance = std**2 return obj
def __new__(cls, name, k, theta): k, theta = sympify(k), sympify(theta) _value_check(k > 0, "k must be positive") _value_check(theta > 0, "Theta must be positive") x = Symbol(name) pdf = x**(k - 1) * exp(-x/theta) / (gamma(k)*theta**k) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) obj.k = k obj.theta = theta return obj
def __new__(cls, name, alpha, beta): alpha, beta = sympify(alpha), sympify(beta) _value_check(alpha > 0, "Alpha must be positive") _value_check(beta > 0, "Beta must be positive") x = Symbol(name) pdf = beta * (x/alpha)**(beta-1) * exp(-(x/alpha)**beta) / alpha obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) obj.alpha = alpha obj.beta = beta return obj
def __new__(cls, x0, gamma, symbol=None): x0, gamma = sympify(x0), sympify(gamma) x = symbol or SingleContinuousPSpace.create_symbol() pdf = 1 / (pi * gamma * (1 + ((x - x0) / gamma)**2)) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def __new__(cls, mu, b, symbol=None): mu, b = sympify(mu), sympify(b) x = symbol or SingleContinuousPSpace.create_symbol() pdf = 1/(2*b)*exp(-Abs(x-mu)/b) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def __new__(cls, p, a, b, symbol = None): p, a, b = sympify(p), sympify(a), sympify(b) x = symbol or SingleContinuousPSpace.create_symbol() pdf = a*p/x*((x/b)**(a*p)/(((x/b)**a+1)**(p+1))) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def __new__(cls, k, symbol = None): k = sympify(k) x = symbol or SingleContinuousPSpace.create_symbol() pdf = 2**(1-k/2)*x**(k-1)*exp(-x**2/2)/gamma(k/2) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set = Interval(0, oo)) return obj
def __new__(cls, x0, gamma, symbol = None): x0, gamma = sympify(x0), sympify(gamma) x = symbol or SingleContinuousPSpace.create_symbol() pdf = 1/(pi*gamma*(1+((x-x0)/gamma)**2)) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def __new__(cls, mu, b, symbol=None): mu, b = sympify(mu), sympify(b) x = symbol or SingleContinuousPSpace.create_symbol() pdf = 1 / (2 * b) * exp(-Abs(x - mu) / b) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def __new__(cls, p, a, b, symbol=None): p, a, b = sympify(p), sympify(a), sympify(b) x = symbol or SingleContinuousPSpace.create_symbol() pdf = a * p / x * ((x / b)**(a * p) / (((x / b)**a + 1)**(p + 1))) obj = SingleContinuousPSpace.__new__(cls, x, pdf) return obj
def __new__(cls, k, symbol=None): k = sympify(k) x = symbol or SingleContinuousPSpace.create_symbol() pdf = 2**(1 - k / 2) * x**(k - 1) * exp(-x**2 / 2) / gamma(k / 2) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(0, oo)) return obj
def __new__(cls, mu, omega, symbol = None): mu, omega = sympify(mu), sympify(omega) x = symbol or SingleContinuousPSpace.create_symbol() pdf = 2*mu**mu/(gamma(mu)*omega**mu)*x**(2*mu-1)*exp(-mu/omega*x**2) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set = Interval(0, oo)) return obj
def __new__(cls, a, b, symbol=None): a, b = sympify(a), sympify(b) x = symbol or SingleContinuousPSpace.create_symbol() pdf = 1 / (pi * sqrt((x - a) * (b - x))) obj = SingleContinuousPSpace.__new__(cls, x, pdf, set=Interval(a, b)) return obj