Converting between a z-score and p-value - Weblogs at Harvard

Converting between a z-score and p-value

June 28, 2020

[9]: # The following code will ask the user for a p-value # on a normal distribution curve and return the # corresponding z-score.

# Reference: how-to-calculate-the-inverse-of-the-normal-cumulative-distribution-function-in-p

import scipy.stats from scipy.stats import norm

print('1 = Convert a z-score to p-value on a normal curve') print('2 = Convert a p-value to z-score on a normal curve')

number = input('Enter your choice: ') choice = int(number)

if choice == 1: b = input("What is the z-score? ") z_score = float(b) one_sided_pvalue = round(scipy.stats.norm.sf(abs(z_score)),5) if z_score > 0.5: print('The corresponding one-sided (right-tailed) p-value

is',one_sided_pvalue,'.') elif z_score < 0.5: print('The corresponding one-sided (left-tailed) p-value

is',one_sided_pvalue,'.') else: print('The corresponding one-sided p-value is',one_sided_pvalue,'.')

elif choice == 2: a = input("What is the p-value? ") p_value = float(a) # ppf stands for "percent point function" z_score = norm.ppf(p_value) print('The corresponding z-score is',z_score,'.')

else: print('Invalid selection.')

1 = Convert a z-score to p-value on a normal curve

1

2 = Convert a p-value to z-score on a normal curve Enter your choice: 2 What is the p-value? 0.005 The corresponding z-score is -2.575829303548901 . [ ]:

2

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download