Roll-your-own stats module

Name this file "stats.py", then import it as a module named "stats".

# -*- coding: utf-8 -*-
"""
Created on Fri Mar 26 11:15:25 2021

Module for descriptive statistics
 2021-03-26
 
@author: rmontant
"""

def mean(datalist):
    m = sum(datalist) / len(datalist)
    return m
#--------

def median(datalist):
    sdl = sorted(datalist)
    length = len(sdl)
    indexUpper = length // 2    # INTEGER division !
    if length % 2 == 1:
        medianValue = sdl[ indexUpper ]
    else:
        indexLower = indexUpper - 1
        medianValue = (sdl[ indexUpper ] + sdl[ indexLower ]) / 2
    return medianValue
#--------

def geometric_mean(datalist):
    from math import prod
    p = prod(datalist)
    return p ** (1/len(datalist))
#--------

def harmonic_mean(datalist):
    return 1                  # just a stub, for now.
#--------

def span(datalist):
    return max(datalist) - min(datalist)
#--------

def variance(datalist):
    n = len(datalist)
    squares = [x**2 for x in datalist]
    ssq = sum(squares)
    sqs = sum(datalist)**2
    return (ssq - sqs/n) / (n - 1)
#--------

def stdev(datalist):
    return variance(datalist)**0.5
#--------