Search code examples
python-3.xscipystatisticsnormal-distribution

Z-score calculation from mean and st dev


I would like to ask whether any popular package like: numpy, scipy, etc has a built in function to calculate Z-Score if I know already crital value, mean and st dev.

I am doing it usually like:

def Zscore(xcritical, mean, stdev):
    return (xcritical - mean)/stdev

#example:
xcritical = 73.06
mean = 72
stdev = 0.5

zscore = Zscore(xcritical, mean, stdev)

and later I am using scipy.stats.norm.cdf to calculate probability of x being lower than xcritical.

import scipy.stats as st
print(st.norm.cdf(zscore))

I wonder If I can simplify it somehow. I know that there is scipy.stats.zscore function but it takes a sample array and not sample statistics.


Solution

  • Starting Python 3.9, the standard library provides the zscore function on the NormalDist object as part of the statistics module:

    NormalDist(mu=72, sigma=.5).zscore(73.06)
    # 2.1200000000000045