程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Calculate the confidence interval of t distribution with Python

編輯:Python

Set random seeds

Ensure the randomness of the array

np.random.seed(1)

Generate a normal distribution

norm_dist = stats.norm(loc, scale)

among , loc = Array mean , scale = Array variance

Generate random arrays

sample = norm_dist.rvs(size=10)
size Is the number of samples

Calculation t The confidence interval of the distribution

When the number of samples is relatively small <30, Use t Distribution

stats.t.interval(0.95,df,mu,se)
df= len(sample)-1 # freedom
mu= np.mean(sample) # mean value
std = np.std(sample,ddof=1) # Sample standard variance
se = std/ np.sqrt(len(sample) # Standard error
0.95 = Confidence level

The library used
np = import numpy as np
stats = from scipy import stats
np. Distribution name .rvs: Generate random numbers that obey the specified distribution

Complete code

# Introduce libraries for calculation
import numpy as np
import pandas as pd
import scipy as sp
from scipy import stats
# Set random seeds
np.random.seed(1)
# Set normal distribution
norm_dist=stats.norm(loc=5,scale=0.09)
# Take samples
sample = norm_dist.rvs(size=20)
# freedom
df=len(sample)-1
# mean value
mu=np.mean(sample)
# Standard deviation , Calculate the sample with ddof=1, Overall use ddof=0
std = np.std(sample,ddof=1)
# Standard error
se = std/np.sqrt(len(sample))
# confidence interval
interval = stats.t.interval(0.95,df,mu,se)

Reference resources 《 use python Start learning statistics 》


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved