Last active
January 6, 2022 08:57
-
-
Save waterrmalann/347faa9dff2f82a0dad4d581f29cf5bd to your computer and use it in GitHub Desktop.
Calculate Standard Deviation (Continuous Series)
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
# Calculate Standard Deviation (Continuous Series | Actual & Assumed Mean Methods) | |
# Simple python program I wrote to automate a huge portion of my homework. | |
# Economics is a nightmare :√ | |
from prettytable import PrettyTable | |
def number(num): | |
"""Returns any string containing number (int/float) in a cleaner way.""" | |
#num = str(round(float(num), 2)) | |
return num[:num.rindex('.')] if num.endswith('.0') else num | |
def get_input(prompt): | |
"""Function to collect input from the user in two different ways.""" | |
_a = [] | |
print(prompt) | |
while True: | |
_i = input(">> ").strip() | |
if ',' in _i: | |
_a = _i.replace(' ', '').split(',') | |
break | |
elif not _i: | |
break | |
else: | |
_a.append(_i) | |
return _a | |
X = get_input("Enter X (individually or comma separated):") | |
print() | |
f = get_input("Enter F (individually or comma separated):") | |
print() | |
# Test Values | |
#X = ['0-5', '5-10', '10-15', '15-20', '20-25', '25-30', '30-35', '35-40'] | |
#f = ['2', '5', '7', '13', '21', '16', '8', '3'] | |
if len(X) != len(f): | |
print("Error: Number of items do not match.") | |
print("(1) Actual Mean Method | (2) Assumed Mean Method") | |
method = input(">> ").strip() | |
if method in {'2', 'assumed'}: | |
# Assumed Mean Method: σ = √(Σfdx2/N - (Σfdx/N)2) | |
m = [] | |
for i in X: | |
_nums = i.split('-') | |
m.append(number(str(round((int(_nums[0]) + int(_nums[1])) / 2, 2)))) | |
# Assume a mean value. | |
A = m[len(m) // 2] | |
dx = [number(str(round(float(i) - float(A), 2))) for i in m] # m - A | |
dx2 = [number(str(round(float(i) * float(i), 2))) for i in dx] # d * d | |
fdx = [number(str(round(float(_f) * float(_dx), 2))) for _f, _dx in zip(f, dx)] # f * dx | |
fdx2 = [number(str(round(float(_f) * float(_dx2), 2))) for _f, _dx2 in zip(f, dx2)] # f * dx2 | |
Σf = number(str(round(sum(float(i) for i in f), 2))) | |
Σfdx = number(str(round(sum(float(i) for i in fdx), 2))) | |
Σfdx2 = number(str(round(sum(float(i) for i in fdx2), 2))) | |
σ = round(((float(Σfdx2)/float(Σf)) - ((float(Σfdx)/float(Σf)) ** 2)) ** 0.5, 2) | |
m[len(m) // 2] = f'({A})A' # little visual indicator for assumed mean | |
# Prepare a table to represent the numbers. | |
table = PrettyTable(['X', 'f', 'm', 'dx', 'dx2', 'fdx', 'fdx2']) | |
for i in range(len(X)): | |
table.add_row([X[i], f[i], m[i], dx[i], dx2[i], fdx[i], fdx2[i]]) | |
table.add_row([' ', '= ' + Σf, ' ', ' ', ' ', '= ' + Σfdx, '= ' + Σfdx2]) | |
print() | |
print(table) | |
print() | |
# Break down the equations when printing so that it is easier to understand. | |
print("σ = √Σfdx2/N - (Σfdx/N)2") | |
print(f"\t = √{Σfdx2}/{Σf} - ({Σfdx}/{Σf})2") | |
print(f"\t = √{round(float(Σfdx2)/float(Σf), 2)} - ({round(float(Σfdx)/float(Σf), 2)})2") | |
print(f"\t = √{round(float(Σfdx2)/float(Σf), 2)} - {round(float(Σfdx)/float(Σf), 2) ** 2}") | |
print(f"\t = √{round(float(Σfdx2)/float(Σf), 2) - (round(float(Σfdx)/float(Σf), 2) ** 2)}") | |
print(f"\t = {σ}") | |
else: | |
# Actual Mean Method: σ = √(Σfx2/N) | |
m = [] | |
for i in X: | |
_nums = i.split('-') | |
m.append(number(str(round((int(_nums[0]) + int(_nums[1])) / 2, 2)))) # X1+X2 / 2 | |
fm = [number(str(round(float(_f) * float(_m), 2))) for _f, _m in zip(f, m)] # f * m | |
Σf = number(str(round(sum(float(i) for i in f), 2))) | |
Σfm = number(str(round(sum(float(i) for i in fm), 2))) | |
x̄ = round(float(Σfm)/float(Σf), 2) # x̄ = Σfm/Σf | |
x = [number(str(round(float(i) - float(x̄), 2))) for i in m] # x = m - x̄ | |
x2 = [number(str(round(float(i) * float(i), 2))) for i in x] # x2 = x * x | |
fx2 = [number(str(round(float(_f) * float(_x2), 2))) for _f, _x2 in zip(f, x2)] # fx2 = f * x2 | |
Σfx2 = number(str(round(sum(float(i) for i in fx2), 2))) | |
σ = round((float(Σfx2) / float(Σf)) ** 0.5, 2) | |
# Prepare a table. | |
table = PrettyTable(['X', 'f', 'm', 'fm', 'x', 'x2', 'fx2']) | |
for i in range(len(X)): | |
table.add_row([X[i], f[i], m[i], fm[i], x[i], x2[i], fx2[i]]) | |
table.add_row([' ', '= ' + Σf, ' ', '= ' + Σfm, ' ', ' ', '= ' + Σfx2]) | |
print() | |
print(table) | |
print() | |
print("x̄ = Σfm/N") | |
print(f"\t = {Σfm}/{Σf}") | |
print(f"\t = {x̄}") | |
print() | |
print("σ = √Σfx2/N") | |
print(f"\t = √{Σfx2}/{Σf}") | |
print(f"\t = √{round(float(Σfx2) / float(Σf), 2)}") | |
print(f"\t = {σ}") | |
# 06-01-2022 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment