Mercurial > gemma
annotate pkg/common/nashsutcliffe.go @ 3334:8c96b7379aea
client: fairway availability: made store action work with both types of feature objects
author | Markus Kottlaender <markus@intevation.de> |
---|---|
date | Mon, 20 May 2019 17:14:47 +0200 |
parents | 247e78ba2bf7 |
children | 8c5df0f3562e |
rev | line source |
---|---|
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2019 by via donau |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package common |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
17 "fmt" |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "time" |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 ) |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 |
3087
014be2194bd1
Prepare new Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2878
diff
changeset
|
21 type TimedValue struct { |
014be2194bd1
Prepare new Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2878
diff
changeset
|
22 When time.Time |
014be2194bd1
Prepare new Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2878
diff
changeset
|
23 Value float64 |
014be2194bd1
Prepare new Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2878
diff
changeset
|
24 } |
014be2194bd1
Prepare new Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2878
diff
changeset
|
25 |
014be2194bd1
Prepare new Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2878
diff
changeset
|
26 type TimedValues []TimedValue |
014be2194bd1
Prepare new Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2878
diff
changeset
|
27 |
3099
f516ac26f4db
"Sharp" match for predicted and measured values in nash sutcliffe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3098
diff
changeset
|
28 func epsEquals(a, b time.Time) bool { |
f516ac26f4db
"Sharp" match for predicted and measured values in nash sutcliffe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3098
diff
changeset
|
29 d := a.Sub(b) |
f516ac26f4db
"Sharp" match for predicted and measured values in nash sutcliffe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3098
diff
changeset
|
30 return -10*time.Millisecond < d && d < 10*time.Millisecond |
f516ac26f4db
"Sharp" match for predicted and measured values in nash sutcliffe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3098
diff
changeset
|
31 } |
3087
014be2194bd1
Prepare new Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2878
diff
changeset
|
32 |
3099
f516ac26f4db
"Sharp" match for predicted and measured values in nash sutcliffe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3098
diff
changeset
|
33 func (mvs TimedValues) Find(when time.Time) (float64, bool) { |
f516ac26f4db
"Sharp" match for predicted and measured values in nash sutcliffe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3098
diff
changeset
|
34 for i := range mvs { |
f516ac26f4db
"Sharp" match for predicted and measured values in nash sutcliffe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3098
diff
changeset
|
35 if epsEquals(when, mvs[i].When) { |
f516ac26f4db
"Sharp" match for predicted and measured values in nash sutcliffe.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3098
diff
changeset
|
36 return mvs[i].Value, true |
3097
e6ba32b060df
Nash Sutcliffe: Treat last prediction date as valid afterwards. Small optimizsations.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3096
diff
changeset
|
37 } |
3087
014be2194bd1
Prepare new Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2878
diff
changeset
|
38 } |
014be2194bd1
Prepare new Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2878
diff
changeset
|
39 return 0, false |
014be2194bd1
Prepare new Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2878
diff
changeset
|
40 } |
014be2194bd1
Prepare new Nash-Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2878
diff
changeset
|
41 |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
42 func NashSutcliffe(predicted, observed []float64) float64 { |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
44 if len(predicted) != len(observed) { |
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
45 panic(fmt.Sprintf( |
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
46 "NashSutcliffe: predicted and observed len differ: %d != %d", |
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
47 len(predicted), |
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
48 len(observed))) |
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
49 } |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
51 if len(observed) == 0 { |
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
52 return 0 |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 } |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 var mo float64 |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
56 for _, v := range observed { |
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
57 mo += v |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 } |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
60 mo /= float64(len(observed)) |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 var num, denom float64 |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
63 for i, o := range observed { |
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
64 d1 := predicted[i] - o |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 num += d1 * d1 |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
66 d2 := o - mo |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 denom += d2 * d2 |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 } |
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 |
3091
cec9d4af5f03
Simplified and fixed Nash Sutcliffe calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3088
diff
changeset
|
70 return 1 - num/denom |
2741
87aed4f9b1b8
Added calculation of Nash Sutcliffe efficiency coefficents.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 } |