stat.

971 days ago by chlee

# integral (expression, variable, range) integral(exp(-1/2*x^2),x,-oo,oo) 
       
def f(x,a,b,c,d): return a*x^3 + b*x^2 + c*x + d #define vector var("x a b c d") #solve equation w/ each constraints solve([f(0,a,b,c,d)==1/2, f(1,a,b,c,d)==1/3, f(2,a,b,c,d)==1/12, f(3,a,b,c,d)==1/12], a,b,c,d) 
       
#plot example plot (x^3, (x,-1,1), color='red' ) 
       
f(x) = exp(-1*x^2);f 
       
plot(f,(x,-4,4),color="black") 
       
# sqrt(pi)로 나누어주어 전체 적분값이 1이 되도록 맞춘다 integral(exp(-1*x^2),x,-oo,oo) integral(f,x,-oo,oo) 
       
# 평균=0이므로 분산은 integral(1/sqrt(pi)*f*(x-0)^2, x,-oo,oo) 
       
# mean=0, var=1 이면서 전체적분값이 1인 pdf로 만들어주어야함 # integral f dx = 1 / integral f*x dx = 0 / integral f*(x-mean)^2 dx = 1 
       
#mixed normal distribution def fNormal(x, mu, sd1): return 1/sqrt(2*pi*sd1^2)*exp(-1/2*((x-mu)/sd1)^2) def fN_summed(x, mu1, mu2, sd1, sd2): return fNormal(x, mu1+mu2, sqrt(sd1^2 + sd2^2)) def fN_mixed(x, mu1, mu2, sd1, sd2, p1): return p1*fNormal(x, mu1, sd1) + (1-p1)*fNormal(x, mu2, sd2) a = plot(fNormal(x,1,1), (x,-3,3), linestyle=":", color="blue") #dashline b = plot(fNormal(x,-1,1), (x,-3,3), linestyle="-.", color="red") c = plot(fN_summed(x, -1,1,1,1), (x,-3,3), linestyle="-", color="black") show(a+b+c) 
       
a = plot(fNormal(x,1,1), (x,-3,3), linestyle=":", color="blue") #dashline b = plot(fNormal(x,-1,1), (x,-3,3), linestyle="--", color="red") c = plot(fN_mixed(x, -1,1,1,1,1/2), (x,-3,3), linestyle="-", color="black") show(a+b+c) 
       
# normal distribution def f1(x,mu1,sigma1): return 1/sqrt(2*pi*sigma1^2)*exp(-1/2*((x-mu1)/sigma1)^2) plot(f1(x,0,1),x,-4,4) 
       
# Laplace distribution def f2(x,mu2,sigma2): return 1/sqrt(2*pi*sigma2^2)*exp(-sqrt(2)*abs((x-mu2)/sigma2)) plot(f2(x,0,1),x,-4,4) 
       
var("mu sigma") assume(sigma>0) integral(x*f1(x,mu,sigma),x,-oo,oo) 
       
factor(sqrt(pi)*sqrt(1/2)*sqrt(2)*mu/(sqrt(pi*sigma^2)*sqrt(sigma^(-2))))