SoFunction
Updated on 2025-03-02

How to implement p-value in python

Case:

tt = (sm-m)/(sv/float(n)) # t-statistic for mean
pval = ((tt), n-1)*2 # two-sided pvalue = Prob(abs(t)>tt)
print 't-statistic = %6.3f pvalue = %6.4f' % (tt, pval)
t-statistic = 0.391 pvalue = 0.6955

Link:

/questions/17559897/python-p-value-from-t-statistic

/doc/scipy/reference/tutorial/

Executable code

# coding: utf-8
from __future__ import division
import numpy as np
from scipy import stats
 
means = [0.0539, 4,8,3,6,9,1]
stds = [5,4,8,3,6,7,9]
mu = [0, 4.1, 7, 2, 5, 8, 0]
n = 20
output = []
for sm, std, m in zip(means, stds, mu):
  # print("value:", sm, std)
  tt = (sm-m)/(std/(float(n)))   # t-statistic for mean
  pval = ((tt), n-1)*2 # two-sided pvalue = Prob(abs(t)>tt)
  # print('t-statistic = %6.3f pvalue = %6.4f' % (tt, pval))
  (format(pval))
print("\t".join(output))

The above implementation method of p-value in python is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.