Created
September 23, 2019 13:23
-
-
Save kupp1/c323a99e7e2026ba7203eb4af2423b48 to your computer and use it in GitHub Desktop.
calculate expected value & standard deviation of sequence of numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
values = {} | |
N = 0 | |
while True: | |
inpt = input() | |
if inpt == 'stop': | |
break | |
N += 1 | |
inpt = float(inpt) | |
try: | |
values[inpt] += 1 | |
except KeyError: | |
values[inpt] = 1 | |
probs = {} | |
for v in values: | |
probs[v] = values[v] / N | |
exp_v = 0 | |
for p in probs: | |
exp_v += p * probs[p] | |
print('expected value: ' + str(exp_v)) | |
sq_s = 0 | |
str_dev = 0 | |
for v in values: | |
sq_s += (v - exp_v)**2 | |
sq_s = math.sqrt( (1/(N-1)) * sq_s) | |
print('standard deviation: ' + str(sq_s)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
u r best)