In [1]:
import pandas as pd
import numpy as np
from math import *
from ggplot import *
%matplotlib inline
In [4]:
x = np.random.uniform(size=100000)
df = pd.DataFrame({'x':x})
In [5]:
df
Out[5]:
In [6]:
ggplot(aes(x='x'), data=df) + geom_histogram()
Out[6]:
In [9]:
%%latex
x = y
First find the normalizing constant: $$ \begin{align} f_X(X=x) &&= cx^2, 0 \leq x \leq 2 \\ 1 &&= c\int_0^2 x^2 dx \\ &&= c[\frac{1}{3}x^3 + d]_0^2 \\ &&= c[\frac{8}{3} + d - d] \\ &&= c[\frac{8}{3}] \\ f_X(X=x) &&= \frac{3}{8}x^2, 0 \leq x \leq 2 \end{align} $$
Next find the cumulative distribution function:
We can randomly generate values from a standard uniform distribution and set equal to the CDF. Solve for $x$. Plug the randomly generated values into the equation and plot the histogram or density of $x$ to get the shape of the distribution:
In [13]:
%%latex
\begin{align*}
f_X(X=x) &= cx^2, 0 \leq x \leq 2 \\
1 &= c\int_0^2 x^2 dx \\
&= c[\frac{1}{3}x^3 + d]_0^2 \\
&= c[\frac{8}{3} + d - d] \\
&= c[\frac{8}{3}] \\
f_X(X=x) &= \frac{3}{8}x^2, 0 \leq x \leq 2
\end{align*}
In [41]:
u = np.random.uniform(size=100000)
x = 2 * u**.3333
df = pd.DataFrame({'x':x})
print df.describe()
ggplot(aes(x='x'), data=df) + geom_histogram()
Out[41]:
Find the normalizing constant:
In [5]:
x = np.random.uniform(size=10000)
y = np.random.uniform(size=10000)
Find the marginal distribution:
Inversion sampling example:
In [8]:
u = np.random.uniform(size=100000)
x = (-1 + (1 + 24*u)**.5) / 2
df = pd.DataFrame({'x':x})
ggplot(aes(x='x'), data=df) + geom_histogram()
Out[8]:
In [ ]: