Add variance and standard deviation

This commit is contained in:
Ryan Whytsell 2023-12-23 16:45:30 -05:00
parent 3ae519a6bd
commit ad44e727b0
No known key found for this signature in database
GPG Key ID: BD7B18309414DE50
1 changed files with 59 additions and 4 deletions

63
main.py
View File

@ -1,17 +1,18 @@
import ast
import math
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
VERSION = "0.0.0"
sContinue = True
state = []
def show_help():
print("vector [5,6,7...] - set the current dataset to the given vector")
print("stdd - get the standard deviation of the current dataset")
print("stdev - get the standard deviation of the current dataset")
print("pstdev - get the population standard deviation of the current dataset")
print("variance - get the variance of the current dataset")
print("pvariance - get the population variance of the current dataset")
print("mean - get the mean of the current dataset")
print("median - get the median of the current dataset")
print("mode - get the mode of the current dataset")
@ -50,6 +51,48 @@ def get_median():
return median
def get_mode():
"""
:return: The most frequent element in the global variable 'state'.
"""
global state
from collections import Counter
mode = Counter(state).most_common(1)[0][0]
return mode
def get_variance():
global state
n = len(state)
mean = get_mean()
variance = sum((x - mean) ** 2 for x in state) / (n - 1)
return variance
def get_population_variance():
global state
n = len(state)
mean = get_mean()
variance = sum((x - mean) ** 2 for x in state) / n
return variance
def get_standard_deviation():
variance = get_variance()
std_dev = math.sqrt(variance)
return std_dev
def get_population_standard_deviation():
pvariance = get_population_variance()
std_dev = math.sqrt(pvariance)
return std_dev
def start_shell():
global state
state = []
@ -75,6 +118,18 @@ def handle_input():
print(f"Set state to: {value}")
case 'mean':
print(get_mean())
case 'median':
print(get_median())
case 'mode':
print(get_mode())
case 'stdev':
print(get_standard_deviation())
case 'pstdev':
print(get_population_standard_deviation())
case 'variance':
print(get_variance())
case 'pvariance':
print(get_population_variance())
case 'print':
if state is not None:
print(f"{state}")