pointStats.py

From GM-RKB
Revision as of 01:24, 23 May 2012 by Gmelli (talk | contribs) (Created page with "pointStats.py is a Python-based point estimation system. * <B><U>Context</U>:</B> ** It uses a Python Function. * <B><U>Counter-Example(s)</U>:</B> ** [[point...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

pointStats.py is a Python-based point estimation system.



References

##############################################
# pointStat.py (v1.0) - Calculate some point statistics.
# return results in a hash {Average}{Min}{Max}{Median}{SquareSum}{StdDev}
# References: http://szabgab.com/talks/fundamentals_of_perl/solution-median.html

def pointStats( Numlist ):
   "This Python function returns several well known population statistics. It assumes input of a non-empty numeric list."

   # ideally should test if non-empty ...

   cnt_val = len(Numlist) ;

   sum_val = 0;
   for val in Numlist:
   	 sum_val += val 
   # sum_val = sum(Numlist)
    
   avg_val = float(sum_val) / cnt_val
   # avg_val = float(sum(Numlist)) / len(Numlist)
   
   SortedNumlist = sorted (Numlist)
   med_val = 0
   if cnt_val % 2 == 1:
     med_val = SortedNumlist[(cnt_val+1)/2-1]
   else:
     lower = SortedNumlist[cnt_val/2-1]
     upper = SortedNumlist[cnt_val/2]
     med_val = (float(lower + upper)) / 2  
   
   min_val = SortedNumlist[0]
   max_val = SortedNumlist[cnt_val-1] ;

   varS_val = 0
   for val in Numlist:
     varS_val += (avg_val - val) ** 2
   stddev_val = (varS_val / cnt_val) ** 0.5 ;

   Result = {}
   Result["Count"]       = cnt_val ;
   Result["Sum"]         = sum_val ;
   Result["Average"]     = avg_val ;
   Result["Min"]         = min_val ;
   Result["Max"]         = max_val ;
   Result["Median"]      = med_val ;
   Result["VarSquSum"]   = varS_val ;
   Result["StdDev"]      = stddev_val ;
   
   return Result 
   #return cnt_val, min_val, max_val, avg_val, med_val, stddev_val ;

# Call the pointStats function
NumList = [2, 20, 24, 19]
Result = pointStats( NumList );
print Result

# {'Count': 4, 'Min': 2, 'Max': 24, 'Sum': 65, 'Median': 19.5, 'VarSquSum': 284.75, 'StdDev': 8.4372685, 'Average': 16.25}