Non-Bell Curve Basic Statistics Examples

#!/usr/bin/python3 # =================================================================== # basic statistics (non-bell curve data) # # ---- # # The standard deviation is one particular measure of the # variation. There are several others, Mean Absolute Deviation # is fairly popular. The standard deviation is by no means special. # # What makes it appear special is that the Gaussian distribution # is special. # # ---- # # The sample standard deviation is a measure of the deviance of # the observed values from the mean, in the same units used to # measure the data. Normal distribution, or not. # # Specifically it is the square root of the mean squared deviance # from the mean. # # So the standard deviation tells you how spread out the data # are from the mean, regardless of distribution. # # =================================================================== import random import statistics ##random.seed(1492) # seed the random number generator # ---- create a list of random values (they are not a bell curve) # ---- value range 0 to 29 (inclusive) vlst = [] for _ in range(1000): r = random.randint(0,29) vlst.append(r) # ---- sort the value list # ---- (not required for this example but sometimes) # svlst = sorted(vlst) # ---- create buckets (bins) blst = [0 for i in range(30)] # fill buckets (bins) with zero for v in vlst: blst[v] += 1 # ---- sort the bucket list # ---- (not required for this example but sometimes useful) # sblst = sorted(blst) # ---- display mean, median, mode print() print('---- sorted value list stats') vmea = statistics.mean(svlst) print(f'mean = {vmea:.4}') vmed = statistics.median(svlst) print(f'median = {vmed:.4}') vmod = statistics.mode(vlst) print(f'mode = {vmod}') vsd = statistics.pstdev(vlst,vmea) print(f'sd = {vsd:.4}') print() print('---- sorted bucket list stats') bmea = statistics.mean(sblst) print(f'mean = {bmea:.4}') bmed = statistics.median(sblst) print(f'median = {bmed:.4}') bmod = statistics.mode(sblst) print(f'mode = {bmod}') bsd = statistics.pstdev(sblst,bmea) print(f'sd = {bsd:.4}') # ---- display buckets (bins) print() print('---- buckets') for i,v in enumerate(blst): print(f'bucket[{i:2}] {v}')