# Standard Deviation

#### [Expected Values](http://mathworld.wolfram.com/ExpectationValue.html)

The expected value of a discrete random variable, $$X$$, is more or less another way of referring to the mean ($$\mu$$). We can also refer to this as the *mathematical expectation* (or just the *expectation*) of $$X$$.&#x20;

#### [Variance](http://mathworld.wolfram.com/Variance.html) $$\sigma^2$$

This is the average magnitude of fluctuations of $$X$$ from its expected value, $$\mu$$. You can also think of it as the expectation of a random variable's squared deviation from its mean. Given a data set, $$X$$, of size :

$$\sigma^2 = \frac{\sum\_{i=1}^n (x\_i - \mu)^2}{n}$$

where $$x\_i$$ is the $$i^{th}$$ element of the data set and $$\mu$$ is the *mean* of all the elements.&#x20;

#### [Standard Deviation](http://mathworld.wolfram.com/StandardDeviation.html) $$\sigma$$

The standard deviation quantifies the amount of variation in a set of data values. Given a data set, $$X$$, of size :

$$\sigma = \sqrt{\frac{\sum\_{i=1}^n (x\_i - \mu)^2}{n}}$$

where $$x\_i$$ is the $$i^{th}$$ element of the data set and $$\mu$$ is the *mean* of all the elements.&#x20;

```
import math

num = int(input())
values = list(map(int, input().split()))

mean = sum(values)/num

result = 0
for i in values:
    result += (i - mean)**2

print('%.1f' % math.sqrt(result/num))
```

## Interquartile Range

```
num = 5
elements = [10, 40, 30, 50, 20]
freq = [1, 2, 3, 4, 5]

values = []
for k, v in enumerate(elements):
    values.extend([v] * freq[k])
values.sort()

lower = values[:len(values)//2]
upper = values[len(values)//2:]

num = len(values)-1
if num % 2 == 0:
    lower = values[:len(values)//2]
    upper = values[len(values)//2:]
else:
    lower = values[:len(values)//2]
    upper = values[len(values)//2:]
    lower.append(upper[0])

if len(lower) % 2 == 0:
    v1 = lower[int(len(lower)/2)-1]
    v2 = lower[int(len(lower)/2)]
    q1 = (v1+v2)/2
    v1 = upper[int(len(upper)/2)-1]
    v2 = upper[int(len(upper)/2)]
    q3 = (v1+v2)/2
else:
    q1 = lower[int(len(lower)/2)]
    q3 = upper[int(len(upper)/2)]

print(float(q3 - q1))
```


---

# 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/standard-deviation.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.
