In [1]:
import scipy.stats
import numpy as np
import matplotlib.pyplot as plt
In [7]:
fig, axes = plt.subplots(figsize=(20, 8), ncols=4)
# Create a random variable
rv = scipy.stats.poisson(5)
q = np.linspace(0, 1, num=50)
x = np.arange(0, 20)
sample = rv.rvs(100000)
# inverse of the pdf
axes[0].plot(q, rv.ppf(q), drawstyle='steps-mid')
axes[0].set_title('PPF x as function of quantile')
axes[1].plot(x, rv.cdf(x), drawstyle='steps')
axes[1].set_title('CDF quantile as function x')
axes[2].vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=3, label='frozen pmf')
axes[2].set_title('PMF density as function of x')
axes[3].hist(sample, bins=np.arange(20))
axes[3].set_title('Sample of x')
rv.mean()
Out[7]:
In [8]:
fig, axes = plt.subplots(figsize=(20, 8), ncols=4)
# Create a random variable
# Mean of exponential variable is per definition 1
# You can shift it to the right with loc
# You can also use the lambda parameter
# In scipy this is implemented with the more general scale (scale = 1 / lambda)
# The mean of the distribution 1/lambda so you can use scale = 5
rv = scipy.stats.expon(scale=5)
q = np.linspace(0, 1, num=50)
x = np.linspace(0, 10, num=50)
sample = rv.rvs(100000)
# inverse of the pdf
axes[0].plot(q, rv.ppf(q))
axes[0].set_title('PPF x as function of quantile')
axes[1].plot(x, rv.cdf(x))
axes[1].set_title('CDF quantile as function x')
axes[2].plot(x, rv.pdf(x), 'k-')
axes[2].set_title('PMF density as function of x')
axes[3].hist(sample, bins=np.arange(20))
axes[3].set_title('Sample of x')
rv.mean()
Out[8]:
In [4]:
import sympy
import sympy.stats
from sympy.stats import Exponential, density, cdf, E, Normal, variance
from sympy.abc import x, z
sympy.init_printing()
In [5]:
l = sympy.Symbol("lambda", positive=True)
X = Exponential("x1", l)
# what is the expected value
E(X)
Out[5]:
In [6]:
# expected value with lambda = 1/5
E(X).subs({l: 1/5})
Out[6]:
In [7]:
# Make another distribution
N = Normal('x2', 5, 1)
In [8]:
# Expected value of normal distribution + exponential distribution
E(N + X)
Out[8]:
In [9]:
variance(N+X)
Out[9]:
In [ ]: