from IPython.display import Image
Image('../../../python_for_probability_statistics_and_machine_learning.jpg')
from __future__ import division
%pylab inline
from scipy import stats
import sympy as S
d=stats.bernoulli(.1).rvs((10,5000)).mean(0)
d=d[np.logical_not(np.isclose(d,1))]
# print mean(d),var(d)
print mean(d/(1-d)),var(d/(1-d))
ev = lambda p:p/10/(1-p)**3
ev(.5)
S.var('n,p',positive=True)
(p/n*(1-p)**3).subs([(p,0.1),(n,10)])
hist(d/(1-d),30,align='mid');
S.init_printing(use_latex=True)
x,p=S.symbols('x,p',real=True)
g = x/(1-x)
g.series(x=x,x0=p,n=3)
gs=g.series(x=x,x0=p,n=3).removeO().subs(x,x-p).subs(x,x/10)
from sympy.stats import E, Binomial
X = Binomial('X',10,p)
m =S.simplify(E(gs.subs(x,X)))
display(m)
v=S.simplify(E(gs.subs(x,X)**2)-m**2)
display(v)
print m.subs(p,.1),v.subs(p,.1)
print mean(d/(1-d)),var(d/(1-d))
print (gs.subs(p,0.5))
p1=S.plot(gs.subs(p,.5),(x,0,10),show=False,ylim=(0,5),line_color='k')
p2,=S.plot(g.subs(x,x/10),(x,0,10),ylim=(0,5),show=False)
p1.append(p2)
p1.show()
def gen_sample(p=0.1,nsamp=5000):
d=stats.bernoulli(p).rvs((10,nsamp)).mean(axis=0)
d=d[np.logical_not(np.isclose(d,1))]
return mean(d),var(d)
# S.init_printing(use_latex=False)
gen_sample(.1)
# S.init_printing(use_latex=True)
print m.subs(p,0.5),v.subs(p,0.5)
def model(n=3):
gs=g.series(x=x,x0=p,n=n).removeO().subs(x,x-p).subs(x,x/10)
m =S.simplify(E(gs.subs(x,X)))
v=S.simplify(E(gs.subs(x,X)**2)-m**2)
return m,v
m,v = model(3)
print m.subs(p,0.1),v.subs(p,0.1)
g.series(x,x0=p,n=3)