masterthesis/codes/partition/variance.py

52 lines
1.6 KiB
Python
Raw Permalink Normal View History

2022-04-19 18:38:01 +02:00
#!/usr/bin/env python3
from math import sqrt
# from itertools import islice
def variance(xs):
n = len(xs)
mu = sum(xs) / n
# mu = 265.8
# print(sum(map(lambda x: (x - mu)**2, xs)))
s = sum(map(lambda x: (x - mu)**2, xs))
var = s / n
deriv = sqrt(var)
# var = sqrt( / len(xs))
print('\\begin{align*}')
print(f' n &= {n} \\\\')
print(f' \\mu &= \\frac{{{sum(xs)}}}{{n}} = {mu:.1f} \\\\')
print(f' s &= \\sum\\limits_{{i=1}}^{{n}} {{(x_i - \\mu)}}^2 \\\\')
# print [mylist[i:i+4] ]
first = True
for i in range(0, n, 3):
symbol = '=' if first else '+'
l = xs[i:i+3]
a = ' + '.join(map(lambda x: f'{{({x} - {mu:.1f})}}^2', l))
first = False
print(f' &{symbol} {a} \\\\')
print(f' &= {s:.1f} \\\\')
# a = ' + '.join(map(lambda x: f'({x} - {mu})^2', xs))
print(f' \\sigma^2 &= \\frac{{s}}{{n}} = {var:.1f} \\\\')
print(f' \\sigma &= \\sqrt{{\\sigma^2}} = {deriv:.1f}')
print('\\end{align*}')
print()
print()
print()
print()
# \mu &= 1595 / 4 = 398.75 \\
# \sigma &= \sqrt{\frac{(403 - 398.75)^2 + (369 - 398.75)^2 + (405 - 398.75)^2 + (418 - 398.75)^2}{4}} = 10.5
def main():
# for xs in [[808, 787], [403, 369, 405, 418], [258, 273, 257, 264, 293, 250], [140, 175, 186, 166, 159, 152, 172, 148, 151, 146]]:
# for xs in [[258, 273, 257, 264, 293, 250]]:
for xs in [[499322, 500678], [249504, 250451, 249818, 250227], [166430, 166861, 166269, 166937, 166623, 166880], [100424, 99650, 99307, 100305, 99403, 100562, 100277, 99875, 99911, 100286]]:
variance(xs)
if __name__ == '__main__':
main()