Mercurial > gemma
view pkg/common/nashsutcliffe.go @ 3530:e98ea184538e
Improve warning message
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Wed, 29 May 2019 16:56:56 +0200 |
parents | 247e78ba2bf7 |
children | 8c5df0f3562e |
line wrap: on
line source
// This is Free Software under GNU Affero General Public License v >= 3.0 // without warranty, see README.md and license for details. // // SPDX-License-Identifier: AGPL-3.0-or-later // License-Filename: LICENSES/AGPL-3.0.txt // // Copyright (C) 2019 by via donau // – Österreichische Wasserstraßen-Gesellschaft mbH // Software engineering by Intevation GmbH // // Author(s): // * Sascha L. Teichmann <sascha.teichmann@intevation.de> package common import ( "fmt" "time" ) type TimedValue struct { When time.Time Value float64 } type TimedValues []TimedValue func epsEquals(a, b time.Time) bool { d := a.Sub(b) return -10*time.Millisecond < d && d < 10*time.Millisecond } func (mvs TimedValues) Find(when time.Time) (float64, bool) { for i := range mvs { if epsEquals(when, mvs[i].When) { return mvs[i].Value, true } } return 0, false } func NashSutcliffe(predicted, observed []float64) float64 { if len(predicted) != len(observed) { panic(fmt.Sprintf( "NashSutcliffe: predicted and observed len differ: %d != %d", len(predicted), len(observed))) } if len(observed) == 0 { return 0 } var mo float64 for _, v := range observed { mo += v } mo /= float64(len(observed)) var num, denom float64 for i, o := range observed { d1 := predicted[i] - o num += d1 * d1 d2 := o - mo denom += d2 * d2 } return 1 - num/denom }