github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/functions/temporal/holt_winters_test.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package temporal
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/query/executor/transform"
    28  	"github.com/m3db/m3/src/query/test/compare"
    29  
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  var holtWintersTestCases = []testCase{
    34  	{
    35  		name: "holt_winters",
    36  		vals: [][]float64{
    37  			{nan, 1, 2, 3, 4, 0, 1, 2, 3, 4},
    38  			{5, 6, 7, 8, 9, 5, 6, 7, 8, 9},
    39  		},
    40  		expected: [][]float64{
    41  			{nan, nan, 2, 3, 4, 4, 3.64, 3.1824, -4.8224, 4},
    42  			{nan, 6, 7, 8, 9, 9, 8.64, 8.1824, 0.1776, 9},
    43  		},
    44  	},
    45  	{
    46  		name: "holt_winters all NaNs",
    47  		vals: [][]float64{
    48  			{nan, nan, nan, nan, nan, nan, nan, nan, nan, nan},
    49  			{nan, nan, nan, nan, nan, nan, nan, nan, nan, nan},
    50  		},
    51  		expected: [][]float64{
    52  			{nan, nan, nan, nan, nan, nan, nan, nan, nan, nan},
    53  			{nan, nan, nan, nan, nan, nan, nan, nan, nan, nan},
    54  		},
    55  	},
    56  }
    57  
    58  func TestHoltWintersProcess(t *testing.T) {
    59  	opGen := func(t *testing.T, _ testCase) transform.Params {
    60  		op, err := NewHoltWintersOp([]interface{}{5 * time.Minute, 0.2, 0.7})
    61  		require.NoError(t, err)
    62  		return op
    63  	}
    64  
    65  	testTemporalFunc(t, opGen, holtWintersTestCases)
    66  }
    67  
    68  func TestHoltWintersFn(t *testing.T) {
    69  	holtWintersFn := makeHoltWintersFn(0.2, 0.6)
    70  	testData := []float64{nan, 1, nan, 5, 10, 15, nan, nan}
    71  	val := holtWintersFn(testData)
    72  	compare.EqualsWithNansWithDelta(t, 13.6559, val, 0.0001)
    73  }