Skip to content

Instantly share code, notes, and snippets.

@Un1Gfn
Last active February 11, 2020 16:45
Show Gist options
  • Save Un1Gfn/4785d48f56a1eddb5037324d99bd04cb to your computer and use it in GitHub Desktop.
Save Un1Gfn/4785d48f56a1eddb5037324d99bd04cb to your computer and use it in GitHub Desktop.
probability and statistics course project
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Run the python code below or refer to 5.png for the plots
Observing how these probability distributions evolve as more measurement values are observed, discuss implications of the obtained results:
The expected value of μ will increase.
The variance of μ will decrease.
rm -fv 5.png
cat <<EOF | python3
from matplotlib import pyplot
from numpy import linspace
from scipy import stats
from math import sqrt
σ=10
x=[1.74, 3.37, 2.64, 3.86, 3.00, 1.29, 3.65, 3.50, 2.73, 2.88, 3.13, 2.29, 2.66, 0.61, 2.03, 3.62, 4.61, 2.50, 3.98, 3.69]
g5=lambda n:(σ**2/(1+n*σ**2))*sum(x[:n])
β=lambda n:sqrt(σ**2/(1+n*σ**2))
μ=lambda n:linspace(g5(n)-3*β(n), g5(n)+3*β(n), 100)
# https://stackoverflow.com/a/22276109/8243991
f=lambda n,c:pyplot.plot(μ(n), stats.norm.pdf(μ(n),g5(n),β(n)),c)
f(1,'r')
f(3,'g')
f(5,'b')
f(10,'y')
f(20,'c')
# https://matplotlib.org/3.1.1/gallery/text_labels_and_annotations/titles_demo.html
pyplot.title('With the first (red 1, green 3, blue 5, yellow 10, cyan 20) measurement values')
# https://stackoverflow.com/a/12444777/8243991
pyplot.xlabel('μ')
pyplot.ylabel('pmf')
# https://stackoverflow.com/a/9012749/8243991
tmp=pyplot.gcf()
pyplot.show()
pyplot.draw()
tmp.savefig('5.png')
EOF
Run the python code below or refer to 6.png for the plots
Discuss the impacts of σ^2 (when it increases):
The expected value of μ will increase.
The variance of μ will increase.
rm -fv 6.png
cat <<EOF | python3
from matplotlib import pyplot
from numpy import linspace
from scipy import stats
from math import sqrt
print()
x=[1.74, 3.37, 2.64, 3.86, 3.00, 1.29, 3.65, 3.50, 2.73, 2.88, 3.13, 2.29, 2.66, 0.61, 2.03, 3.62, 4.61, 2.50, 3.98, 3.69]
print(len(x))
g5=lambda σ:(σ**2/(1+len(x)*σ**2))*sum(x)
β=lambda σ:sqrt(σ**2/(1+len(x)*σ**2))
μ=lambda σ:linspace(g5(σ)-3*β(σ), g5(σ)+3*β(σ), 100)
# https://stackoverflow.com/a/22276109/8243991
f=lambda σ,c:pyplot.plot(μ(σ), stats.norm.pdf(μ(σ),g5(σ),β(σ)),c)
f(0.1,'r')
f(1,'g')
f(10,'b')
# https://matplotlib.org/3.1.1/gallery/text_labels_and_annotations/titles_demo.html
pyplot.title('With σ = (red 0.1, green 1, blue 10)')
# https://stackoverflow.com/a/12444777/8243991
pyplot.xlabel('μ')
pyplot.ylabel('pmf')
# https://stackoverflow.com/a/9012749/8243991
tmp=pyplot.gcf()
pyplot.show()
pyplot.draw()
tmp.savefig('6.png')
EOF
zip 16340238_吳德潤.zip \
1234.pdf\
5.png\
5.txt\
6.png\
6.txt\
https://172.18.166.194
students_prob
https://gist.github.com/Un1Gfn/4785d48f56a1eddb5037324d99bd04cb
okul '/home/darren/Telegram Desktop/中期考核题目.pdf'
(1)
https://en.wikipedia.org/wiki/Sum_of_normally_distributed_random_variables
(2)
https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Bivariate_case
Known σX σY μX μY
Unknown ρ
(5)
https://glowingpython.blogspot.com/2011/04/how-to-plot-function-using-matplotlib.html
import pylab
import numpy
x = numpy.linspace(-15,15,100) # 100 linearly spaced numbers
y = numpy.sin(x)/x # computing the values of sin(x)/x
# compose plot
pylab.plot(x,y) # sin(x)/x
pylab.plot(x,y,'co') # same function with cyan dots
pylab.plot(x,2*y,x,3*y) # 2*sin(x)/x and 3*sin(x)/x
pylab.show() # show the plot
cat <<EOF | python3
from numpy import sqrt,pi,exp
from numpy import linspace,concatenate
from pylab import plot,show
π=pi
σ=10
# μ=concatenate((linspace(-100,-10,91),linspace(-9,9,10000),linspace(10,100,91)))
μ=linspace(2.8,3.0,100)
# μ=2.9
x=[1.74, 3.37, 2.64, 3.86, 3.00, 1.29, 3.65, 3.50, 2.73, 2.88, 3.13, 2.29, 2.66, 0.61, 2.03, 3.62, 4.61, 2.50, 3.98, 3.69]
A = - sum( ((xi-μ)**2)/2 for xi in x) - (μ**2)/(2*σ**2) + sum( (xi**2)/(2*(σ**2+1)) for xi in x )
n=len(x)
y=1/sqrt(2*π) * ((sqrt(σ**2+1))**n)/σ * exp(A)
print(y)
plot(μ,y)
show()
quit()
EOF
Integrate
cat <<EOF | python3
# https://stackoverflow.com/a/25047602/8243991
# https://glowingpython.blogspot.com/2011/04/how-to-plot-function-using-matplotlib.html
# https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html
from numpy import sqrt,pi,exp
from numpy import linspace,concatenate
from pylab import plot,show
from scipy.integrate import quad
π=pi
σ=10
x=[1.74, 3.37, 2.64, 3.86, 3.00, 1.29, 3.65, 3.50, 2.73, 2.88, 3.13, 2.29, 2.66, 0.61, 2.03, 3.62, 4.61, 2.50, 3.98, 3.69]
n=len(x)
# y=lambda x:x**2
# x=3
# print(y(x))
# x=4
# print(y(x))
A=lambda μ:- sum( ((xi-μ)**2)/2 for xi in x) - (μ**2)/(2*σ**2) + sum( (xi**2)/(2*(σ**2+1)) for xi in x )
y=lambda B:1/sqrt(2*π) * ((sqrt(σ**2+1))**n)/σ * exp(B)
# print(y(A(2.9)))
print(quad(lambda x:y(A(x)),2.8,3.0))
EOF
1/(2*π*σ)*e^(-(x-μ)^2/2-μ^2/(2*σ^2))
integrate -inf ~ +inf x
e^(-mu^2/(2*sigma^2))/(sqrt(2)*sqrt(pi)*sigma)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment