diff --git a/.gitignore b/.gitignore index 4f5df69..908ab0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ dev/ statsy.egg-info -dev.py \ No newline at end of file +dev.py +src/statsy/__pycache__/ \ No newline at end of file diff --git a/src/statsy/Constants.py b/src/statsy/Constants.py new file mode 100644 index 0000000..dcf35c3 --- /dev/null +++ b/src/statsy/Constants.py @@ -0,0 +1,16 @@ +from .main import * + +e = summation(None,None,lambda n: 1/(math.factorial(n)), [[n for n in range (1000)]]) + +def pi()->Number: + """ + Returns pi + """ + return math.pi + +def e_specified(depth:int) -> Number: + """ + A specified depth to calculate the constant e. + """ + depth_list = [n for n in range (depth)] + return summation(None,None,lambda n: 1/(math.factorial(n)), [depth_list]) diff --git a/src/statsy/Conversion.py b/src/statsy/Conversion.py new file mode 100644 index 0000000..de6a796 --- /dev/null +++ b/src/statsy/Conversion.py @@ -0,0 +1,13 @@ +from .main import * + +def Celcius_to_Fahrenheit(C:Number) -> Number: + """ + Returns the Fahrenheit value of a Celcius value. + """ + return (1.8 * C) + 32 + +def Fahrenheit_to_Celcius(F:Number) -> Number: + """ + Returns the Celcius value of a Fahrenheit value. + """ + return (F - 32)/1.8 diff --git a/src/statsy/Logarithms.py b/src/statsy/Logarithms.py new file mode 100644 index 0000000..b85ade9 --- /dev/null +++ b/src/statsy/Logarithms.py @@ -0,0 +1,14 @@ +from .main import * +import math, statistics + +def ln(a:Number)->Number: + """ + Returns the natural log. + """ + return math.log(a) + +def log(base:Number,a:Number)->Number: + """ + Returns any logarithm for base and value. + """ + return (math.log10(a)/math.log10(base)) diff --git a/src/statsy/Methods.py b/src/statsy/Methods.py new file mode 100644 index 0000000..3157cc6 --- /dev/null +++ b/src/statsy/Methods.py @@ -0,0 +1,54 @@ +from .main import * +from .Logarithms import * + +def Sturges_rule(number_of_points:Number)->Number: + """ + Sturges' rule for determining the approx. number of intervals for histograms. + """ + return round(1 + log(2,number_of_points)) + +def Rice_rule(number_of_points:Number)->Number: + """ + Rice rule for determining the approx. number of intervals for histograms. + """ + return round(2 * (root(3,number_of_points))) + +def arithmetic_mean(data:list)->Number: + """ + Returns the arithmetic mean of a list of data points. + """ + return (summation(None,None,None,[data])) / len(data) + +def median(data:list)->Number: + """ + Returns the simple mean of an ordered quantitative list. + """ + total_length = len(data) + if total_length & 1: + return data[int((total_length+1)/2)-1] + else: + return (data[ int((total_length/2)-1)] + data[int((total_length/2)+1)])/2 + +def mode(data:list)->any: + """ + A shortcut for statistics.mode(data) + """ + return statistics.mode(data) + +def trimean(data:list)->Number: + """ + Returns the trimean of a set of data. + Be sure to order list in an increasing order. + """ + p25 = get_value_from_percentile(25,data) + p50 = get_value_from_percentile(50,data) + p75 = get_value_from_percentile(75,data) + print(p25,p50,p75) + + return ( (p25 + (2 * p50) + p75 ) / 4 ) + +def geometric_mean(data:list)->Number: + """ + Returns the geometric mean of a list of data. + """ + return (product_notation(None,None,None,[data]) ** (1/len(data))) diff --git a/src/statsy/__init__.py b/src/statsy/__init__.py index 15b6a64..ad739fb 100644 --- a/src/statsy/__init__.py +++ b/src/statsy/__init__.py @@ -1 +1,5 @@ from .main import * +from .Conversion import * +from .Constants import * +from .Logarithms import * +from .Methods import * \ No newline at end of file diff --git a/src/statsy/main.py b/src/statsy/main.py index 37e4395..835fb37 100644 --- a/src/statsy/main.py +++ b/src/statsy/main.py @@ -24,7 +24,6 @@ def get_value_from_percentile(percentile:Number,data:list) -> Number: value = (diff * fr) + lowest return value - def summation(i:int,range:int,expression:Callable,data:List[List[Number]]) -> Number: """ i = beginning of the range to execute. \n @@ -64,3 +63,56 @@ def summation(i:int,range:int,expression:Callable,data:List[List[Number]]) -> Nu return sum(evaluated_list) +def multiply_elements(data:list)->Number: + """ + Multiplies all elements in a list together. + """ + res = 1 + for element in data: + res = res * element + return res + +def product_notation(i:int,range:int,expression:Callable,data:List[List[Number]]) -> Number: + """ + i = beginning of the range to execute. \n + range = range to execute \n + expression = function to apply to the data \n + data = list of lists that contain values to evaluate \n + This function returns the value of a product notation. + """ + if expression is None: + expression = lambda x: x + if len(inspect.signature(expression).parameters) != len(data): + raise ValueError("There must be the same number of input data lists as expression inputs") + lists_ready_to_eval = [] + # For each list in the data, return the range to be evaluated + for input_list in data: + # If no start and range are specified, pass the entire list to evaluate + if i is None and range is None: + lists_ready_to_eval.append(input_list) + # If a range is given, slice that part of the list and return it to the list of lists to evaluate + else: + lists_ready_to_eval.append(input_list[i-1:range+1]) + # Create a list containing all the evaluated values in the range of interest + evaluated_list = [] + # Check for multi-variable functions by checking the number of input lists. These two numbers should be the same. + if len(data) > 1: + # If multi-variable function is given: + range_of_list_to_eval = len(lists_ready_to_eval[0]) + current_index_of_list_to_eval = 0 + # Use zip to iterate through all lists at the same time and '*' to unpack the lists. This returns a tuple where each item in the tuple has the same index across all lists + for val in zip(*lists_ready_to_eval): + # Use * to unpack the tuple and input into the function and add to evaluated list + evaluated_list.append(expression(*val)) + else: + for val in lists_ready_to_eval[0]: + # Evaluate expression and add to evaluated list + evaluated_list.append(expression(val)) + + return multiply_elements(evaluated_list) + +def root(root_value:Number,root_input:Number)->Number: + """ + Returns the arbitrary root of an input. + """ + return root_input ** (1/root_value)