AMBHAS
errlib.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 """
00004 Created on Thu Jan 20 15:36:37 2011
00005 @ author:                  Sat Kumar Tomer 
00006 @ author's webpage:        http://civil.iisc.ernet.in/~satkumar/
00007 @ author's email id:       satkumartomer@gmail.com
00008 @ author's website:        www.ambhas.com
00009 
00010 A libray with Python functions for calculations of 
00011 micrometeorological parameters and some miscellaneous
00012 utilities.
00013 
00014 functions:
00015     pc_bias : percentage bias
00016     apb :     absolute percent bias
00017     rmse :    root mean square error
00018     mae :     mean absolute error
00019     bias :    bias
00020     NS :      Nash-Sutcliffe Coefficient
00021     L:        likelihood estimation
00022     correlation: correlation
00023     
00024 """
00025 
00026 # import required modules
00027 import numpy as np
00028 
00029 def filter_nan(s,o):
00030     """
00031     this functions removed the data  from simulated and observed data
00032     whereever the observed data contains nan
00033     
00034     this is used by all other functions, otherwise they will produce nan as 
00035     output
00036     """
00037     data = np.array([s,o])
00038     data = np.transpose(data)
00039     data = data[~np.isnan(data).any(1)]
00040     return data[:,0],data[:,1]
00041 
00042 def pc_bias(s,o):
00043     """
00044     Percent Bias
00045     input:
00046         s: simulated
00047         o: observed
00048     output:
00049         pc_bias: percent bias
00050     """
00051     s,o = filter_nan(s,o)
00052     return 100.0*sum(s-o)/sum(o)
00053 
00054 def apb(s,o):
00055     """
00056     Absolute Percent Bias
00057     input:
00058         s: simulated
00059         o: observed
00060     output:
00061         apb_bias: absolute percent bias
00062     """
00063     s,o = filter_nan(s,o)
00064     return 100.0*sum(abs(s-o))/sum(o)
00065 
00066 def rmse(s,o):
00067     """
00068     Root Mean Squared Error
00069     input:
00070         s: simulated
00071         o: observed
00072     output:
00073         rmses: root mean squared error
00074     """
00075     s,o = filter_nan(s,o)
00076     return np.sqrt(np.mean((s-o)**2))
00077 
00078 def mae(s,o):
00079     """
00080     Mean Absolute Error
00081     input:
00082         s: simulated
00083         o: observed
00084     output:
00085         maes: mean absolute error
00086     """
00087     s,o = filter_nan(s,o)
00088     return np.mean(abs(s-o))
00089 
00090 def bias(s,o):
00091     """
00092     Bias
00093     input:
00094         s: simulated
00095         o: observed
00096     output:
00097         bias: bias
00098     """
00099     s,o = filter_nan(s,o)
00100     return np.mean(s-o)
00101 
00102 def NS(s,o):
00103     """
00104     Nash Sutcliffe efficiency coefficient
00105     input:
00106         s: simulated
00107         o: observed
00108     output:
00109         ns: Nash Sutcliffe efficient coefficient
00110     """
00111     s,o = filter_nan(s,o)
00112     return 1 - sum((s-o)**2)/sum((o-np.mean(o))**2)
00113 
00114 def L(s,o, N=5):
00115     """
00116     Likelihood 
00117     input:
00118         s: simulated
00119         o: observed
00120     output:
00121         L: likelihood
00122     """
00123     s,o = filter_nan(s,o)
00124     return np.exp(-N*sum((s-o)**2)/sum((o-np.mean(o))**2))
00125 
00126 def correlation(s,o):
00127     """
00128     correlation coefficient
00129     input:
00130         s: simulated
00131         o: observed
00132     output:
00133         correlation: correlation coefficient
00134     """
00135     s,o = filter_nan(s,o)
00136     return np.corrcoef(o, s)[0,1]
00137 
 All Classes Namespaces Files Functions Variables