# Normal Distribution

#### [Normal Distribution](https://en.wikipedia.org/wiki/Normal_distribution)

The probability density of *normal distribution* is:

$$\huge N(\mu, \sigma^2) = \frac{1}{\sigma \sqrt{2\pi}}e^{- \frac{(x-\mu)^2}{2\sigma^2}}$$

Here,

* $$\mu$$ is the mean (or expectation) of the distribution. It is also equal to median and mode of the distribution.
* $$\sigma^2$$ is the variance.
* $$\sigma$$ is the standard deviation.&#x20;

#### [Standard Normal Distribution](https://en.wikipedia.org/wiki/Normal_distribution#Standard_normal_distribution)

If $$\mu = 0$$ and $$\sigma =1$$, then the normal distribution is known as *standard normal distribution*:\
\
$$\large \phi(x) = \frac{e^{-\frac{x^2}{2}}}{\sqrt{2\pi}}$$

Every normal distribution can be represented as standard normal distribution:

$$\large N(\mu, \sigma^2) = \frac{1}{\sigma}\phi(\frac{x-\mu}{\sigma})$$

#### [Cumulative Probability](https://en.wikipedia.org/wiki/Normal_distribution#Cumulative_distribution_function)

Consider a real-valued random variable, $$X$$. The *cumulative distribution function* of $$X$$ (or just the distribution function of $$X$$) evaluated at $$x$$ is the probability that $$X$$ will take a value less than or equal to $$x$$:

$$F\_X(x) = P(X\leq x)$$

Also,

$$P(a \leq X \leq b) = P(a < X < b) = F\_X(b) - F\_X(a)$$

The cumulative distribution function for a function with normal distribution is:

$$\huge \Phi(x) = \frac{1}{2}(1+\text{erf}(\frac{x-\mu}{\sigma\sqrt2}))$$

```python
phi = lambda x: 0.5 * (1 + math.erf((x - mean)/(std * (2 ** 0.5))))
```

Where $$\text{erf}$$ is the function:

$$\large \text{erf}(z) = \frac{2}{\sqrt\pi}f\_0^2 e^{-x^2} dx$$

**Task** \
In a certain plant, the time taken to assemble a car is a random variable, $$X$$, having a normal distribution with a mean of hours and a standard deviation of $$2$$ hours. What is the probability that a car can be assembled at this plant in:

1. Less than $$19.5$$ hours?
2. Between $$20$$ and $$22$$ hours?

```python
import math
mean, std = 20, 2
cdf = lambda x: 0.5 * (1 + math.erf((x - mean) / (std * (2 ** 0.5))))

# Less than 19.5
print('{:.3f}'.format(cdf(19.5)))
# Between 20 and 22
print('{:.3f}'.format(cdf(22) - cdf(20)))
```

**Task** \
The final grades for a Physics exam taken by a large group of students have a mean of $$\mu = 70$$ and a standard deviation of $$\sigma = 10$$. If we can approximate the distribution of these grades by a normal distribution, what percentage of the students:

1. Scored higher than $$80$$ (i.e., have a $$\text{grade}>80$$)?
2. Passed the test (i.e., have a $$\text{grade}\geq60$$)?
3. Failed the test (i.e., have a $$\text{grade}<60$$)?

```python
import math

def arg_erf(x, u, o):
    return (x-u)/(o * math.sqrt(2))

def F(x, arg_x):
    return 0.5 * ( 1 + math.erf(arg_x) )

u, o = input().split() # u = 70, o = 10
u, o = float(u), float(o)

a = float(input())   # a = 80
b = float(input())   # b = 60

arg_a = arg_erf(a, u, o)
arg_b = arg_erf(b, u, o)

first_ans = (1 - F(a, arg_a))
second_ans = (1 - F(b, arg_b))
third_ans = F(b, arg_b)

print(round(first_ans*100.0, 2))
print(round(second_ans*100.0, 2))
print(round(third_ans*100.0, 2))
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://stephanosterburg.gitbook.io/scrapbook/math/hackerrank/normal-distribution.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
