github.com/m3db/m3@v1.5.0/src/query/functions/temporal/linear_regression_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  
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  var linearRegressionTestCases = []testCase{
    33  	{
    34  		name:   "predict_linear",
    35  		opType: PredictLinearType,
    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, 3.6666, 4.6666, 5.6666, 2, 0.1666, 0.1666, 2, 5.6666},
    42  			{nan, 7.6666, 8.6666, 9.6666, 10.6666, 7, 5.1666, 5.1666, 7, 10.6666},
    43  		},
    44  	},
    45  	{
    46  		name:   "deriv",
    47  		opType: DerivType,
    48  		vals: [][]float64{
    49  			{nan, 1, 2, 3, 4, 0, 1, 2, 3, 4},
    50  			{5, 6, 7, 8, 9, 5, 6, 7, 8, 9},
    51  		},
    52  		expected: [][]float64{
    53  			{nan, nan, 0.0166, 0.0166, 0.0166, 0, -0.0083, -0.0083, 0, 0.0166},
    54  			{nan, 0.0166, 0.0166, 0.0166, 0.0166, 0, -0.0083, -0.0083, 0, 0.0166},
    55  		},
    56  	},
    57  	{
    58  		name:   "predict_linear some NaNs",
    59  		opType: PredictLinearType,
    60  		vals: [][]float64{
    61  			{nan, 1, 2, 3, nan, nan, 1, 2, 3, nan},
    62  			{5, 6, nan, 8, 9, 5, 6, nan, 8, 9},
    63  		},
    64  		expected: [][]float64{
    65  			{nan, nan, 3.6666, 4.6666, 5.6666, 6.6666, 0.6153, 0.8461, 4.6666, 5.6666},
    66  			{nan, 7.6666, 8.6666, 9.6666, 10.6666, 7, 3.8333, 2.8333, 7, 10.6666},
    67  		},
    68  	},
    69  	{
    70  		name:   "deriv some NaNs",
    71  		opType: DerivType,
    72  		vals: [][]float64{
    73  			{nan, 1, 2, 3, nan, nan, 1, 2, 3, nan},
    74  			{5, 6, nan, 8, 9, 5, 6, nan, 8, 9},
    75  		},
    76  		expected: [][]float64{
    77  			{nan, nan, 0.0166, 0.0166, 0.0166, 0.0166, -0.0058, -0.0058, 0.0166, 0.0166},
    78  			{nan, 0.0166, 0.0166, 0.0166, 0.0166, 0, -0.0166, -0.0166, 0, 0.0166},
    79  		},
    80  	},
    81  	{
    82  		name:   "predict_linear NaNs",
    83  		opType: PredictLinearType,
    84  		vals: [][]float64{
    85  			{nan, nan, nan, nan, nan, nan, nan, nan, nan, nan},
    86  			{nan, nan, nan, nan, nan, nan, nan, nan, nan, nan},
    87  		},
    88  		expected: [][]float64{
    89  			{nan, nan, nan, nan, nan, nan, nan, nan, nan, nan},
    90  			{nan, nan, nan, nan, nan, nan, nan, nan, nan, nan},
    91  		},
    92  	},
    93  	{
    94  		name:   "deriv NaNs",
    95  		opType: DerivType,
    96  		vals: [][]float64{
    97  			{nan, nan, nan, nan, nan, nan, nan, nan, nan, nan},
    98  			{nan, nan, nan, nan, nan, nan, nan, nan, nan, nan},
    99  		},
   100  		expected: [][]float64{
   101  			{nan, nan, nan, nan, nan, nan, nan, nan, nan, nan},
   102  			{nan, nan, nan, nan, nan, nan, nan, nan, nan, nan},
   103  		},
   104  	},
   105  }
   106  
   107  func TestLinearRegressionBlocks(t *testing.T) {
   108  	opGen := func(t *testing.T, tc testCase) transform.Params {
   109  		if tc.opType == PredictLinearType {
   110  			baseOp, err := NewLinearRegressionOp(
   111  				[]interface{}{5 * time.Minute, 100.0}, tc.opType)
   112  			require.NoError(t, err)
   113  			return baseOp
   114  		}
   115  
   116  		baseOp, err := NewLinearRegressionOp(
   117  			[]interface{}{5 * time.Minute}, tc.opType)
   118  		require.NoError(t, err)
   119  		return baseOp
   120  	}
   121  
   122  	testTemporalFunc(t, opGen, linearRegressionTestCases)
   123  }
   124  
   125  func TestUnknownLinearRegression(t *testing.T) {
   126  	_, err := NewLinearRegressionOp(
   127  		[]interface{}{5 * time.Minute},
   128  		"unknown_linear_regression_func")
   129  	require.Error(t, err)
   130  }