I want to find out the confidence interval of samples which follow a normal distribution.
To test the code, I create a sample first and try to plot a picture of confidence interval in Jupyter notebook[python kernel]
%matplotlib notebook
import pandas as pd
import numpy as np
import statsmodels.stats.api as sms
import matplotlib.pyplot as plt
s= np.random.normal(0,1,2000)
# s= range(10,14) <---this sample has the right CI
# s = (0,0,1,1,1,1,1,2) <---this sample has the right CI
# confidence interval
# I think this is the fucniton I misunderstand
ci=sms.DescrStatsW(s).tconfint_mean()
plt.figure()
_ = plt.hist(s, bins=100)
# cnfidence interval left line
one_x12, one_y12 = [ci[0], ci[0]], [0, 20]
# cnfidence interval right line
two_x12, two_y12 = [ci[1], ci[1]], [0, 20]
plt.plot(one_x12, one_y12, two_x12, two_y12, marker = 'o')
The green and yellow lines suppose to be confidence interval. But they are not at the right position.
I might misunderstand this function :
sms.DescrStatsW(s).tconfint_mean()
But the document says this function will return confidence interval.
This is the figure I expect:
%matplotlib notebook
import pandas as pd
import numpy as np
import statsmodels.stats.api as sms
import matplotlib.pyplot as plt
s= np.random.normal(0,1,2000)
plt.figure()
_ = plt.hist(s, bins=100)
# cnfidence interval left line
one_x12, one_y12 = [np.std(s, axis=0) * -1.96, np.std(s, axis=0) * -1.96], [0, 20]
# cnfidence interval right line
two_x12, two_y12 = [np.std(s, axis=0) * 1.96, np.std(s, axis=0) * 1.96], [0, 20]
plt.plot(one_x12, one_y12, two_x12, two_y12, marker = 'o')