Skip to content

Commit

Permalink
added more formulae
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronmore committed Dec 17, 2024
1 parent 09f46db commit 6426ea2
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/statsy/Methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,102 @@ 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)))

def trimmed_mean(trim:Number,data:list)->Number:
"""
#Returns the trimmed mean of some data.
"""
partial_trim = (trim/100)/2
# Find how many elements to remove from each side of the
number_to_slice_off = round((len(data) * partial_trim))
trimmed_data = data[int(number_to_slice_off):len(data)-number_to_slice_off]
return arithmetic_mean(trimmed_data)

def vrange(data:list)->Number:
"""
#Returns the range variability of a set of data.
"""
return abs(data[0] - data[int(len(data)-1)])

def interquartile_range(data:list)->Number:
"""
#Also known as the H-spread. \n
#Returns the interquartile range of a set of data.
"""
total_length = len(data)
if total_length & 1:
# If the data is of odd length
# Remove the median value and pass the data to the rest of the function.
temporary_IQR_function_data = data[:]
del temporary_IQR_function_data[int((total_length+1)/2)-1]
total_length = total_length - 1
return abs( median(temporary_IQR_function_data[:int(total_length//2)]) - median(temporary_IQR_function_data[int(total_length//2):]))

def variance(data:list,type:str,rooted:Optional[bool]=None)->Number:
"""
type = 'sample' returns the variance of a sample \n
type = 'population' returns the variance of a population \n
Returns the variance of a normally distributed set of data points. \n
"""
n = len(data)
if type == 'sample':
n = len(data) - 1
m = arithmetic_mean(data)
varian = ((( summation(None,None,lambda x: ((x - m)**2) ,[data]) / n )))
if rooted is True:
varian = root(2,varian)
return varian

def standard_deviation(data:list,type:str):
"""
type = 'sample' returns the variance of a sample \n
type = 'population' returns the variance of a population \n
Returns the standard deviation of a normally distributed set of data points. \n
"""
return variance(data,type,True)

def skewness(data:list,type:str)->Number:
"""
type = 'sample' or 'population" \n
Return the skew of a set of data. \n
"""
mew = arithmetic_mean(data)
n = len(data)
sd = standard_deviation(data,'sample')
fron =(n / ((n-1) * (n-2)))
if type == "population":
sd = standard_deviation(data,'population')
fron = (1/n)
skew = fron * summation(None,None,lambda x: (((x - mew)/ sd) ** 3) ,[data])
return skew

def kurtosis(data:list,type:str)->Number:
"""
type = 'sample' or 'population" \n
Return the kurtosis of a set of data. \n
"""
mew = arithmetic_mean(data)
n = len(data)
sd = standard_deviation(data,'sample')
fron = ( (n * (n + 1)) / ((n-1) * (n-2) * (n-3)))
end = 3 * ( ((n-1)**2) / ((n-2)*(n-3)) )
sm = summation(None,None,lambda x: (((x - mew)/ sd) ** 4) ,[data])
if type == "population":
sd = standard_deviation(data,'population')
fron = (1/n)
end = 3
sm = summation(None,None,lambda x: (((x - mew)/ sd) ** 4) ,[data])
kurt = (fron * sm) - end
return kurt

def Pearson_correlation_coefficient_r(data:List[list])->Number:
"""
Returns Pearson's correlation coefficient of r.
"""
n = len(data[0])
t1 = summation(None,None,lambda x, y: x*y,data)
t2 = ( summation(None,None,None,[data[0]]) * summation(None,None,None,[data[1]]) ) / n
b1 = root(2,(summation(None,None,lambda x: x**2,[data[0]]) - ((summation(None,None,None,[data[0]])**2) / n)))
b2 = root(2,(summation(None,None,lambda x: x**2,[data[1]]) - ((summation(None,None,None,[data[1]])**2) / n)))
r = (t1-t2) / (b1 * b2)
return r

0 comments on commit 6426ea2

Please sign in to comment.