Python - calculate normal distribution

14,784

Solution 1

A distribution and the cumulative distribution are not the same - the latter is the integral of the former. If the normal distribution looks like a "bell", the cumulative normal distribution looks like a gentle "step" function.

E.g., for the following "bells" enter image description here you'd get the following "steps" enter image description here

If you have an array data, the following will fit it to a normal distribution using scipy.stats.norm:

import numpy as np
from scipy.stats import norm

mu, std = norm.fit(data)

This will return the mean and standard deviation, the combination of which define a normal distribution.

Solution 2

Normal and cumulative distributions are not the same. I'll leave that bit of research to you.

The formula for normal distribution is easy if you have the mean and standard deviation:

enter image description here

Solution 3

The thing that you may look at is the normal distribution not the cumulative normal distribution. You can calculate the frequency of each element that occurs in the array and plot it to visualize the distribution.

Then you can use numpy to calculate mean = numpy.mean(array) and standard deviation as std = numpy.std(array).

Hope this helps.

Share:
14,784
usert4jju7
Author by

usert4jju7

Updated on June 04, 2022

Comments

  • usert4jju7
    usert4jju7 almost 2 years

    I'm quite new to python world. Also, I'm not a statistician. I'm in the need to implementing mathematical models developed by mathematicians in a computer science programming language. I've chosen python after some research. I'm comfortable with programming as such (PHP/HTML/javascript).

    I have a column of values that I've extracted from a MySQL database & in need to calculate the below:

    1. Normal distribution of it. (I don't have the sigma & mu values. These need to be calculated too apparently).
    2. Mixture of normal distribution
    3. Estimate density of normal distribution
    4. Calculate 'Z' score

    The array of values looks similar to the one below ( I've populated sample data)-

    data = [3,3,3,3,3,3,3,9,12,6,3,3,3,3,9,21,3,12,3,6,3,30,12,6,3,3,24,30,3,3,3,12,3,3,3,3,3,3,3,6,9,3,3,3,3,3,3,3,3,3,3,3,3,33,3,3,3,6,3,3,6,6,15,3,3,3,3,6,3,3,3,3,3,3,3,3,12,12,3,3,3,3,3,3,78,9,12,3,6,3,15,6,3,3,3,30,3,6,78,3,9,9,3,78,3,3,3,3,3,12,15,3,3,78,3,3,33,78,15,9,3,3,21,6,3,6,30,6,6,3,3,3,3,12,3,3,3,3,3,12,3,3,3,3,3,3,3,3,3,3,3,3,12,6,3,3,9,3,3,12,3,3,3,3,6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,3,3,3,3,6,3,3,3,3,3,3,3,3,3,3,3,3,3,3,12,6,3,3,3,3,3,3,3,12,3,3,3,6,3,3,3,3,3,3,3,6,3,3,3,3,3,3,3,3,9,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,6,3,3,12,3,3,3,3,3,3,3,3,3,3,3,3,6,3,3,3,3,3,3,6,3,3,3,3,3,3,3,3,3,3,9,3,3,3,3,3,3,3,9,3,3,3,3,3,3,3,3,3,6,3,3,3,3,3,3,3,3,3,3,6,3,3,3,27,3,3,6,3,3,3,3,3,6,3,3,3,3,6,3,3,9,3,3,3,12,3,3,3,3,3,6,9,3,6,3,3]
    

    I've looked around & found quite a bit about cumulative distribution as here (These have the mu & sigma values ready anyway which isn't the case in my scenario). I'm not too sure if cumulative normal distribution & normal distribution are the same. Could I please get some pointers on how to get started with this please?

    I'd very much appreciate any help here please.

  • usert4jju7
    usert4jju7 about 8 years
    Hello Ami - I understand the updated question has a lot of components to it. hence, I've created another question http://stackoverflow.com/questions/35686774/python-statistic‌​al-distribution. I'd really appreciate any help please.
  • Ami Tavory
    Ami Tavory about 8 years
    @usert4jju7 :-) I was going to write you suggest doing that. Will be happy to look at it there.