-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathforecast.go
55 lines (43 loc) · 1.57 KB
/
forecast.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
// Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
// Package forecast provides an interface for custom forecasting algorithms.
package forecast
import (
"context"
dataframe "github.com/rocketlaunchr/dataframe-go"
)
// Forecast predicts the next n values of sdf using the forecasting algorithm alg.
// cfg is required to configure the parameters of the algorithm. r is used to select a subset of sdf to
// be the "training set". Values after r form the "validation set". evalFunc can be set to measure the
// quality of the predictions. sdf can be a SeriesFloat64 or a DataFrame. DataFrame input is not yet implemented.
//
// NOTE: You can find basic forecasting algorithms in forecast/algs subpackage.
func Forecast(ctx context.Context, sdf interface{}, r *dataframe.Range, alg ForecastingAlgorithm, cfg interface{}, n uint, evalFunc EvaluationFunc) (interface{}, []Confidence, float64, error) {
switch sdf := sdf.(type) {
case *dataframe.SeriesFloat64:
err := alg.Configure(cfg)
if err != nil {
return nil, nil, 0, err
}
err = alg.Load(ctx, sdf, r)
if err != nil {
return nil, nil, 0, err
}
pred, cnfdnce, err := alg.Predict(ctx, n)
if err != nil {
return nil, nil, 0, err
}
var errVal float64
if evalFunc != nil {
errVal, err = alg.Evaluate(ctx, pred, evalFunc)
if err != nil {
return nil, nil, 0, err
}
}
return pred, cnfdnce, errVal, nil
case *dataframe.DataFrame:
panic("sdf as a DataFrame is not yet implemented")
default:
panic("sdf must be a Series or DataFrame")
}
panic("no reach")
}