jmquintana79
11/29/2016 - 5:05 AM

## scoring functions

Scoring functions

``````import numpy as np
from sklearn.metrics import mean_absolute_error
from sklearn.metrics import mean_squared_error

# calculate general error
error = fcst - obs # "fcst" and "obs" are numpy arrays

# calculate scores
bias = np.mean(error)
mae = np.mean(np.abs(error))
mse = np.mean(error**2)
rmse = np.sqrt(mse)

# other ways
mae = mean_absolute_error(obs,fcst)
mse = mean_squared_error(obs,fcst)``````
``````"""
calculate rmse

inputs: list of numpy arrays with same length

"""
def rmse(y_true, y_pred):
from sklearn.metrics import mean_squared_error
from math import sqrt
# validation input
assert len(y_true) == len(y_pred)
# calculate and return
return sqrt(mean_squared_error(y_true, y_pred))

"""
Root Mean Squared Logarithmic Error
"""
def rmsle(y_true:'array',y_pred:'array'):
import numpy as np
assert len(y_true) == len(y_pred)
return np.square(np.log(y_pred + 1) - np.log(y_true + 1)).mean() ** 0.5

"""
calculate bias

inputs: list of numpy arrays with same length

"""
def bias(predictions, observation):
import numpy as np

# validation input
if len(observation) != len(predictions):
print("ERROR: observation and prediction numpy arraies dont have same length")
return None
# calculate and return
return np.mean([ (ifore - iobs) for ifore,iobs in list(zip(predictions,observation)) ])

"""
calculate mae (mean absolute error)

inputs: list or numpy arrays

"""
def mae(predictions,observation):
from sklearn.metrics import mean_absolute_error

# validation input
if len(observation) != len(predictions):
print("ERROR: observation and prediction numpy arraies dont have same length")
return None

return mean_absolute_error(observation, predictions)

"""
calculate mape

inputs: list of numpy arrays with same length

"""
def mape(predictions, observation):
import numpy as np
# initialize
lmape = list()
for iobs, ifore in list(zip(observation,predictions)):
if float(iobs) != 0.: lmape.append(abs((iobs-ifore) / iobs))
# average and return
return np.mean(lmape)*100

"""
Pearson correlation

input: numpy arrays

"""
def correlation(x1,x2):
from scipy.stats import pearsonr
corr,pvalue = pearsonr(x1,x2)
return corr

"""
Residual Standard Error (standar deviation of epsilon=mean-zero random error: Y = f(X) + epsilon )

units: same than Y.
"""
def RSE(y_obs,y_predict):
import numpy
residue = (y_obs - y_predict)**2