Changeset 978 in lmdz_wrf


Ignore:
Timestamp:
Aug 8, 2016, 5:13:30 PM (8 years ago)
Author:
lfita
Message:

Adding `std_stats2Val': two variables standard Statistics class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r957 r978  
    21662166    return
    21672167
     2168class std_stats2Val(object):
     2169  """two variables standard Statistics class providing:
     2170  vals1 = variable 1
     2171  vals2 = variable 2
     2172  self.bias=mean(var1-var2)
     2173  self.mae=mean(abs(var1-var2))
     2174  self.rmse=sqrt((var1-var2)**2)
     2175  self.explvar = stddev1/stddev2
     2176  self.correlation (and p-value)
     2177  """
     2178
     2179  def __init__(self, vals1, vals2):
     2180    import numpy as np
     2181    from scipy import stats as sts
     2182
     2183    fname = 'std_stats2Val'
     2184
     2185    if vals1 is None:
     2186      self.bias = None
     2187      self.mae = None
     2188      self.rmse = None
     2189      self.explvar = None
     2190      self.corr = None
     2191    else:
     2192      values1 = vals1.flatten()
     2193      values2 = vals2.flatten()
     2194
     2195      Nvals1 = len(values1)
     2196      Nvals2 = len(values2)
     2197
     2198
     2199      if not len(values1) == len(values2):
     2200        print errormsg
     2201        print '    ' + fname + ': lengths of variables differ!! Lvar1: ', Nvals1,    \
     2202          ' Lvar2: ', Nvals2,' statistics between them can not be computed!'
     2203        quit(-1)
     2204
     2205      self.bias = 0.
     2206      self.mae = 0.
     2207      self.rmse = 0.
     2208      self.explvar = 0.
     2209      self.corr = np.zeros(2, dtype=np.float)
     2210
     2211      for inum in range(Nvals1):
     2212          Sval = values1[inum] - values2[inum]
     2213          self.mae = self.mae + abs(Sval)
     2214          self.rmse = self.rmse + Sval**2
     2215
     2216      self.bias = np.mean(vals1) - np.mean(vals2)
     2217      self.mae = self.mae/Nvals1
     2218      self.rmse = np.sqrt(self.rmse/Nvals1)
     2219      self.explvar = np.var(vals1)/np.var(vals2)
     2220
     2221      self.corr[0], self.corr[1] = sts.pearsonr(values1, values2)
     2222
     2223    return
     2224
    21682225class stats2Val(object):
    21692226  """two variables Statistics class providing:
     
    22132270      self.mean2v1Pv2 = None
    22142271      self.stdv1Pv2 = None
     2272      self.bias = None
    22152273      self.mae = None
    22162274      self.rmse = None
     
    22512309      self.mean2v1Pv2=0.
    22522310      self.stdv1Pv2=0.
     2311      self.bias = 0.
    22532312      self.mae = 0.
    22542313      self.rmse = 0.
    2255       self.corr = np.array([0., 0.])
     2314      self.corr = np.zeros(2, dtype=np.float)
    22562315      self.linRegress = np.zeros(5, float)
    22572316      self.polRegress = np.zeros(power+1, float)
     
    22842343            Dval = values1[inum] / values2[inum]
    22852344
     2345          self.bias = self.bias + Sval
    22862346          self.mae = self.mae + abs(Sval)
    22872347          self.rmse = self.rmse + Sval**2
     
    23352395      self.stdv1Pv2 = np.sqrt(self.mean2v1Pv2-self.meanv1Pv2*self.meanv1Pv2)
    23362396
     2397      self.bias = self.bias/self.Nokvalues12
    23372398      self.mae = self.mae/self.Nokvalues12
    23382399      self.rmse = np.sqrt(self.rmse/self.Nokvalues12)
    23392400
    2340       self.corr = sts.pearsonr(values1, values2)
    2341 
     2401      self.corr[0], self.corr[1] = sts.pearsonr(values1, values2)
    23422402      self.linRegress[0], self.linRegress[1], self.linRegress[2], self.linRegress[3], self.linRegress[4] = sts.linregress(values1, values2)
    23432403
Note: See TracChangeset for help on using the changeset viewer.