# Stastic Python, Kindly share the code in python

1. Suppose a variable X has a bell-shaped distribution with a mean of 150 and a standard deviation of 20.
a. What percentage of X values lies between 130 and 170?
b. What percentage of X values lies between 110 and 190?
c. What percentage of X values lies above 190?
2. Variable X has a mean of 15 and a standard deviation of 2.
a. What percentage of X values will lie within 1.5 standard deviation of the mean?
b. What is the minimum percentage of X values that lie between 8 and 17?

We do not provide complete solutions here. The purpose of homework is
that you learn things by doing them, and solving problems is something
questions like these try to teach.

If you’re just after precooked things you can put in an exam answer,
that is just cheating. I will not enable it.

Instead, how would you solve these problems? Find the definition of
these mathematical terms. Write in English (or whatever you usual
language is) how you might approach them if you were doing it by hand.
Then write little bits of code that do that for each piece.

Come here with questions, but bring your own code or specific questions
which prevent you trying to write code.

Cheers,
Cameron Simpson cs@cskk.id.au

import numpy as np
import math
import scipy.stats
import dill
x = scipy.stats.norm.pdf(130,150,20)

I am tried the above code, however, i am not sure.

This is a start. I’ve never used scipy myself, so we will learn
together.

The documentation for the scipy.stats package is here:
https://scipy.github.io/devdocs/reference/stats.html

Keep it to hand.

All of that said, I’m not sure you need scipy for this, although it
looks like it has functions to answer these questions. You might do
better to consult the Wikipedia article about the normal distribution:

and for what a standard deviation is:

The docs for `norm` are here:
https://scipy.github.io/devdocs/reference/generated/scipy.stats.norm.html#scipy.stats.norm
and it looks like the `pdf` function is a probably desnity function.

It isn’t clear to me that initialising a pdf with (130,150,20) is a
sensible thing to do. In particular, I’ve no idea what the first
parameter should be. The second parameter looks to be the location of
the mean, so 150 seems correct. The third parameter is means to be a
scale; it is possible that that this represents the size of the standard
deviation, but the docs are not clear to me.

It seems that the `interval(alpha,loc=0,scale=1)` is a better way to
scipy.stats.norm.html page mentioned above). It takes:

``````interval(alpha, loc=0, scale=1)
``````

where `alpha` is a fraction of the distribution. So if `loc=150` and
`scale=20` you could try various values for `alpha` between 0.0 and 1.0
until you get the endpoints you want, such as `130,170` for your
question 1a.

Cheers,
Cameron Simpson cs@cskk.id.au

Which bell-shaped distribution?

It can make a very big difference between a normal (Gaussian)
distribution, a t-distribution, Cauchy distribution, etc.

But let’s use the normal distribution. There is no need for numpy or
scipy to solve this problem!

https://docs.python.org/3/library/statistics.html#normaldist-objects

``````>>> from statistics import NormalDist
>>> bell = NormalDist(mu=150, sigma=20)
>>> bell.cdf(170) - bell.cdf(130)
0.6826894921370859
``````

That’s 68.3% of values are between 170 and 130, or another way of saying
this that is within one standard deviation of the mean.

(The mean, mu=150, plus or minus the stdev, sigma=20, is 170 and 130.)

``````>>> bell.cdf(190) - bell.cdf(170)
0.13590512198327787
``````

13.6% of values are between 190 and 170.

``````>>> 1 - bell.cdf(190)
0.02275013194817921
``````

2.3% of values are above 190.

I will leave Q2 to you.