import numpy as np
from scipy.stats import beta
import matplotlib.pyplot as plt

def quadratic(x):
    return 4*x*(1-x)

N = 100000
x = np.empty(N)

# arbitary irrational starting point
x[0] = 1/np.sqrt(3) 
for i in range(1, N):
    x[i] = quadratic( x[i-1] )

plt.plot(x[0:100])
plt.xlabel("iteration index")
plt.show()

t = np.linspace(0, 1, 100)
plt.hist(x, bins=t, normed=True)
plt.xlabel("bins")
plt.ylabel("counts")

plt.plot(t, beta(0.5,0.5).pdf(t), linewidth=3)
plt.legend(["beta(1/2, 1/2)"])
plt.show()

"""
Explanation: In Chaos and the beta distribution", John Cook shows that the distribution of values generated by the logistic map fits a beta distribution.  He shows that graphically by comparing the PMF of the generated values with the PDF of a beta distribution with parameters (1/2, 1/2).
Looking at that picture, it was not immediately clear whether the beta distribution fit the data at the extremes, so I wanted to compare the CDFs, which can sometimes show differences between distributions more clearly.
So here's John's code:
End of explanation
"""


from thinkstats2 import Cdf
import thinkplot

plt.plot(t, beta(0.5,0.5).cdf(t), color='orange')

thinkplot.Cdf(Cdf(x), color='blue', linewidth=1)

plt.show()

"""
Explanation: And here's my code, using CDFs.
End of explanation
"""


