Skip to content

Instantly share code, notes, and snippets.

@kupp1
Created September 23, 2019 13:23
Show Gist options
  • Save kupp1/c323a99e7e2026ba7203eb4af2423b48 to your computer and use it in GitHub Desktop.
Save kupp1/c323a99e7e2026ba7203eb4af2423b48 to your computer and use it in GitHub Desktop.
calculate expected value & standard deviation of sequence of numbers
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))
@burevestnik-png
Copy link

u r best)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment