#!/usr/bin/env python # -*- coding: utf-8 -*- # *************************************************************************** # * Copyright (C) 2014 by Paul Lutus * # * lutusp@arachnoid.com * # * * # * This program is free software; you can redistribute it and/or modify * # * it under the terms of the GNU General Public License as published by * # * the Free Software Foundation; either version 2 of the License, or * # * (at your option) any later version. * # * * # * This program is distributed in the hope that it will be useful, * # * but WITHOUT ANY WARRANTY; without even the implied warranty of * # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * # * GNU General Public License for more details. * # * * # * You should have received a copy of the GNU General Public License * # * along with this program; if not, write to the * # * Free Software Foundation, Inc., * # * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * # *************************************************************************** # Created on Mar 21, 2014 11:29:03 AM import re,sys,os,math if(len(sys.argv) < 2): print('usage: %s [data items] or filename or - for stdin' % sys.argv[0]) quit() data = False flist = [] # in principle, one could present data # in all three ways at once: # from a file # streamed from stdin # as command-line args for arg in sys.argv[1:]: if(arg == '-'): data = sys.stdin.read() elif(os.path.isfile(arg)): with open(arg) as f: data = f.read() else: try: flist.append(float(arg)) except: None if(data): for s in re.split("[^0-9Ee.+-]+",data): try: flist.append(float(s)) except: None if(len(flist) < 2): sys.stderr.write('Error: not enough data.\n') quit(1) # this helps locate median flist.sort() n = len(flist) fsum = sum(flist) mean = fsum / n bias = -1 biased_n = n + bias variance = 0 for val in flist: variance += (val-mean)**2 variance /= biased_n sigma = math.sqrt(variance) stderr = sigma / math.sqrt(n) median = 0 p = n/2 # if even number of samples, median is # average of two central values if(n % 2 == 0): median = (flist[p-1] + flist[p]) / 2 else: median = flist[p] # now show results prompts = [ u'Mean (μ)', u'Standard Deviation (σ)', u'Median', u'Variance', u'Standard Error', u'Samples', u'Sample Bias' ] values = [mean,sigma,median,variance,stderr,n,bias] for i,p in enumerate(prompts): print(u'%-24s: %g' % (p,values[i]))