-
Notifications
You must be signed in to change notification settings - Fork 0
/
hbv_fit_measures.go
102 lines (77 loc) · 2.35 KB
/
hbv_fit_measures.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Functions to calculate goodness of fit measure
- Nash-Sutcliffe effciency
- R-squared
- Volume error
- Lindström measure
To be implemented
Tests (also for missing values in obs data)
KGE
log NSE
Spearman rank correlation
*/
package gohbv
import (
"math"
"gonum.org/v1/gonum/floats"
"gonum.org/v1/gonum/stat"
)
func Q_obs_to_array(inData []InputData) []float64 {
var Q_values []float64
for _, value := range inData {
Q_values = append(Q_values, value.Discharge)
}
return Q_values
}
func Q_sim_to_array(mState []ModelState) []float64 {
var Q_values []float64
for _, value := range mState {
Q_values = append(Q_values, value.Q_sim)
}
return Q_values
}
// Element wise power of items in array
// arr**y
func PowArray(arr []float64, y float64) []float64 {
arr_pow := make([]float64, len(arr))
for _, value := range arr {
arr_pow = append(arr_pow, math.Pow(value, y))
}
return arr_pow
}
// Calculate Nash-Sutcliffe efficiency
// 1 - ( sum((obs-sim)**2) / sum((obs-mean_obs)**2) )
func NashSutcliffeEfficiency(sim []float64, obs []float64) float64 {
obs_mean := stat.Mean(obs, nil)
obs_sub_mean := obs
floats.AddConst(-obs_mean, obs_sub_mean)
obs_sub_sim := make([]float64, len(obs))
floats.SubTo(obs_sub_sim, obs, sim)
nse := 1 - (floats.Sum(PowArray(obs_sub_sim, 2)) / floats.Sum(PowArray(obs_sub_mean, 2)))
return nse
}
func RSquared(sim []float64, obs []float64) float64 {
r_sq := stat.RSquaredFrom(sim, obs, nil)
return r_sq
}
// Calculate the relative volume error (i.e. bias)
// This evalulates systematic volume errors over longer periods
// See Lindström, G. (1997). A Simple Automatic Calibration Routine for the HBV Model.
// Nordic Hydrology, 28(3), 153–168. https://doi.org/10.2166/nh.1997.009
// Note: returns single float64, alternativelty return array of dv ?
func VolumeError(sim []float64, obs []float64) float64 {
// Note: here we take the cumulative difference, not accumulating over an array
sim_sub_obs := make([]float64, len(obs))
floats.SubTo(sim_sub_obs, sim, obs)
cum_diff := floats.Sum(sim_sub_obs)
dv := cum_diff / floats.Sum(obs)
return dv
}
// Penalize the NSE measure with remaining weighted volume error
func LindstromMeasure(sim []float64, obs []float64, w float64) float64 {
// NSE - w*abs(dv)
nse := NashSutcliffeEfficiency(sim, obs)
dv := VolumeError(sim, obs)
lm := nse - w*math.Abs(dv)
return lm
}