How to visualize 95% confidence interval in matplotlib?

后端 未结 2 799
渐次进展
渐次进展 2021-02-04 14:01

I have learned how to find the 95% confidence interval with scipy.stats.t like so

In [1]: from scipy.stats import t
In [2]: t.interval(0.95, 10, loc         


        
相关标签:
2条回答
  • 2021-02-04 14:17

    you need to divide by standard deviation, and, second, if your data is two-sided (as plot suggests), you need to allow 2.5% of misses on each side of Gaussian, that is:

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

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

    0 讨论(0)
  • 2021-02-04 14:36

    You don't need .interval method, to get the size of 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 vectorize way to get the (half) size of interval, given the degrees of freedom and standard deviation.

    enter image description here

    0 讨论(0)
提交回复
热议问题