|
| 1 | +/** |
| 2 | + * Copyright (c) 2016, Cloudera, Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Cloudera, Inc. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"). You may not use this file except in |
| 6 | + * compliance with the License. You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 11 | + * CONDITIONS OF ANY KIND, either express or implied. See the License for |
| 12 | + * the specific language governing permissions and limitations under the |
| 13 | + * License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.cloudera.sparkts.models |
| 17 | + |
| 18 | +import breeze.linalg._ |
| 19 | + |
| 20 | +import org.apache.commons.math3.random.MersenneTwister |
| 21 | +import com.cloudera.sparkts.Lag |
| 22 | +import org.scalatest.FunSuite |
| 23 | +import org.scalatest.Matchers._ |
| 24 | + |
| 25 | +class AutoregressionXSuite extends FunSuite { |
| 26 | + val rand = new MersenneTwister(10L) |
| 27 | + val nRows = 1000 |
| 28 | + val nCols = 2 |
| 29 | + val X = Array.fill(nRows, nCols)(rand.nextGaussian()) |
| 30 | + val intercept = rand.nextGaussian * 10 |
| 31 | + |
| 32 | + // tests an autoregressive model where the exogenous variables are not lagged |
| 33 | + test("fit ARX(1, 0, true)") { |
| 34 | + val xCoeffs = Array(0.8, 0.2) |
| 35 | + val rawY = X.map(_.zip(xCoeffs).map { case (b, v) => b * v }.sum + intercept) |
| 36 | + val arCoeff = 0.4 |
| 37 | + val y = rawY.scanLeft(0.0) { case (priorY, currY) => currY + priorY * arCoeff }.tail |
| 38 | + val dy = new DenseVector(y) |
| 39 | + val dx = new DenseMatrix(rows = X.length, cols = X.head.length, data = X.transpose.flatten) |
| 40 | + val model = AutoregressionX.fitModel(dy, dx, 1, 0, includeOriginalX = true) |
| 41 | + val combinedCoeffs = Array(arCoeff) ++ xCoeffs |
| 42 | + |
| 43 | + model.c should be (intercept +- 1e-4) |
| 44 | + for (i <- combinedCoeffs.indices) { |
| 45 | + model.coefficients(i) should be (combinedCoeffs(i) +- 1e-4) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // tests a model with no autoregressive term but with lagged exogenous variables |
| 50 | + test("fit ARX(0, 1, false) model") { |
| 51 | + val xCoeffs = Array(0.4, 0.15) |
| 52 | + val xLagged = Lag.lagMatTrimBoth(X, 1) |
| 53 | + val y = xLagged.map(_.zip(xCoeffs).map { case (b, v) => b * v }.sum + intercept) |
| 54 | + val dy = new DenseVector(Array(0.0) ++ y) |
| 55 | + // note that we provide the original X matrix to the fitting functiond |
| 56 | + val dx = new DenseMatrix(rows = X.length, cols = X.head.length, data = X.transpose.flatten) |
| 57 | + val model = AutoregressionX.fitModel(dy, dx, 0, 1, includeOriginalX = false) |
| 58 | + |
| 59 | + model.c should be (intercept +- 1e-4) |
| 60 | + for (i <- xCoeffs.indices) { |
| 61 | + model.coefficients(i) should be (xCoeffs(i) +- 1e-4) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // this test simply reduces to a normal regression model |
| 66 | + test("fit ARX(0, 0, true) model") { |
| 67 | + // note that |
| 68 | + val xCoeffs = Array(0.8, 0.2) |
| 69 | + val y = X.map(_.zip(xCoeffs).map { case (b, v) => b * v }.sum + intercept) |
| 70 | + val dy = new DenseVector(y) |
| 71 | + val dx = new DenseMatrix(rows = X.length, cols = X.head.length, data = X.transpose.flatten) |
| 72 | + val model = AutoregressionX.fitModel(dy, dx, 0, 0, includeOriginalX = true) |
| 73 | + |
| 74 | + model.c should be (intercept +- 1e-4) |
| 75 | + for (i <- xCoeffs.indices) { |
| 76 | + model.coefficients(i) should be (xCoeffs(i) +- 1e-4) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // tests a model with no autoregressive term but with lagged exogenous variables |
| 81 | + // of order 2 and inclusive of the original X values |
| 82 | + test("fit ARX(0, 2, true) model") { |
| 83 | + val xLagCoeffs = Array(0.4, 0.15, 0.2, 0.7) |
| 84 | + val xLagged = Lag.lagMatTrimBoth(X, 2) |
| 85 | + val yLaggedPart = xLagged.map(_.zip(xLagCoeffs).map { case (b, v) => b * v }.sum ) |
| 86 | + val xNormalCoeffs = Array(0.3, 0.5) |
| 87 | + val yNormalPart = X.map(_.zip(xNormalCoeffs).map { case (b, v) => b * v }.sum ) |
| 88 | + val y = yLaggedPart.zip(yNormalPart.drop(2)).map { case (l, n) => l + n + intercept } |
| 89 | + |
| 90 | + val dy = new DenseVector(Array(0.0, 0.0) ++ y) |
| 91 | + val dx = new DenseMatrix(rows = X.length, cols = X.head.length, data = X.transpose.flatten) |
| 92 | + val model = AutoregressionX.fitModel(dy, dx, 0, 2, includeOriginalX = true) |
| 93 | + val combinedCoeffs = xLagCoeffs ++ xNormalCoeffs |
| 94 | + |
| 95 | + model.c should be (intercept +- 1e-4) |
| 96 | + for (i <- combinedCoeffs.indices) { |
| 97 | + model.coefficients(i) should be (combinedCoeffs(i) +- 1e-4) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + test("fit ARX(1, 1, false) model") { |
| 102 | + val xCoeffs = Array(0.8, 0.2) |
| 103 | + val xLagged = Lag.lagMatTrimBoth(X, 1) |
| 104 | + val rawY = xLagged.map(_.zip(xCoeffs).map { case (b, v) => b * v }.sum + intercept) |
| 105 | + val arCoeff = 0.4 |
| 106 | + val y = rawY.scanLeft(0.0) { case (priorY, currY) => currY + priorY * arCoeff }.tail |
| 107 | + val dy = new DenseVector(Array(0.0) ++ y) |
| 108 | + val dx = new DenseMatrix(rows = X.length, cols = X.head.length, data = X.transpose.flatten) |
| 109 | + val model = AutoregressionX.fitModel(dy, dx, 1, 1, includeOriginalX = false) |
| 110 | + val combinedCoeffs = Array(arCoeff) ++ xCoeffs |
| 111 | + |
| 112 | + model.c should be (intercept +- 1e-4) |
| 113 | + for (i <- combinedCoeffs.indices) { |
| 114 | + model.coefficients(i) should be (combinedCoeffs(i) +- 1e-4) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + test("predict ARX model") { |
| 119 | + val c = 0 |
| 120 | + val xCoeffs = Array(-1.136026484226831e-08, 8.637677568908233e-07, 15238.143039368977, -7.993535860373772e-09, -5.198597570089805e-07, 1.5691547009557947e-08, 7.409621376205488e-08) |
| 121 | + val yMaxLag = 0 |
| 122 | + val xMaxLag = 0 |
| 123 | + val arxModel = new ARXModel(c, xCoeffs, yMaxLag, xMaxLag, includesOriginalX = true) |
| 124 | + |
| 125 | + val y = new DenseVector(Array(100.0)) |
| 126 | + val x = new DenseMatrix(rows = 1, cols = 7, data = Array(465,1,0.006562479,24,1,0,51)) |
| 127 | + |
| 128 | + val results = arxModel.predict(y, x) |
| 129 | + results.length should be (1) |
| 130 | + results(0) should be (y(0) +- 1e-4) |
| 131 | + } |
| 132 | +} |
| 133 | + |
0 commit comments