comparison python/utils.py @ 855:2277ab1a8141

added utility for lognorm estimation
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 27 Sep 2016 17:37:56 -0400
parents 33d296984dd8
children e310577cc0b8
comparison
equal deleted inserted replaced
854:33d296984dd8 855:2277ab1a8141
3 ''' Generic utilities.''' 3 ''' Generic utilities.'''
4 4
5 import matplotlib.pyplot as plt 5 import matplotlib.pyplot as plt
6 from datetime import time, datetime 6 from datetime import time, datetime
7 from math import sqrt, ceil, floor 7 from math import sqrt, ceil, floor
8 from scipy.stats import kruskal, shapiro 8 from scipy.stats import kruskal, shapiro, lognorm
9 from scipy.spatial import distance 9 from scipy.spatial import distance
10 from scipy.sparse import dok_matrix 10 from scipy.sparse import dok_matrix
11 from numpy import zeros, array, exp, sum as npsum, int as npint, arange, cumsum, median, isnan, ones, convolve, dtype, isnan, NaN, mean, ma, isinf, savez, load as npload 11 from numpy import zeros, array, exp, sum as npsum, int as npint, arange, cumsum, median, isnan, ones, convolve, dtype, isnan, NaN, mean, ma, isinf, savez, load as npload, log
12 12
13 13
14 datetimeFormat = "%Y-%m-%d %H:%M:%S" 14 datetimeFormat = "%Y-%m-%d %H:%M:%S"
15 15
16 ######################### 16 #########################
43 43
44 same as lognorm.stats(scale, 0, exp(loc))''' 44 same as lognorm.stats(scale, 0, exp(loc))'''
45 mean = exp(loc+(scale**2)/2) 45 mean = exp(loc+(scale**2)/2)
46 var = (exp(scale**2)-1)*exp(2*loc+scale**2) 46 var = (exp(scale**2)-1)*exp(2*loc+scale**2)
47 return mean, var 47 return mean, var
48
49 def fitLogNormal(x):
50 'returns the fitted location and scale of the lognormal (general definition)'
51 shape, loc, scale = lognorm.fit(x, floc=0.)
52 return log(scale), shape
48 53
49 def sampleSize(stdev, tolerance, percentConfidence, printLatex = False): 54 def sampleSize(stdev, tolerance, percentConfidence, printLatex = False):
50 from scipy.stats.distributions import norm 55 from scipy.stats.distributions import norm
51 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200. 56 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200.
52 if printLatex: 57 if printLatex: