How to visualize 95% confidence interval in matplotlib?

I found out how to find the 95% confidence interval with scipy.stats.t this way

 In [1]: from scipy.stats import t In [2]: t.interval(0.95, 10, loc=1, scale=2) # 95% confidence interval Out[2]: (-3.4562777039298762, 5.4562777039298762) In [3]: t.interval(0.99, 10, loc=1, scale=2) # 99% confidence interval Out[3]: (-5.338545334351676, 7.338545334351676) 

However, visualization is important to me. I am wondering how can I show a confidence interval line on each node of my curve in matplotlib ?

What I expect is something like this

enter image description here

+8
source share
2 answers

You do not need the .interval method to get the size of the confidence interval, you just need the .ppf method.

 import numpy as np import scipy.stats as ss data_m=np.array([1,2,3,4]) #(Means of your data) data_df=np.array([5,6,7,8]) #(Degree-of-freedoms of your data) data_sd=np.array([11,12,12,14]) #(Standard Deviations of your data) import matplotlib.pyplot as plt plt.errorbar([0,1,2,3], data_m, yerr=ss.t.ppf(0.95, data_df)*data_sd) plt.xlim((-1,4)) 

ss.t.ppf(0.95, data_df)*data_sd is a fully vectorized way to get (half) the size of an interval, taking into account degrees of freedom and standard deviation.

enter image description here

+8
source

you need to divide by the standard deviation, and secondly, if your data is two-way (as shown in the graph), you need to allow 2.5% of the passes on each side of the Gaussian, that is:

 ss.t.ppf(0.975, data_df)/np.sqrt(data_df) 

Since you miss 2.5% on both sides, you get only 5%.

0
source

Source: https://habr.com/ru/post/958264/


All Articles