def test_est_params(): rand_state = np.random.RandomState() rand_state.seed(42) # Generate a target series AR = (np.array([1, 0.3, 0.5]), np.array([3, 1, 1])) MA = (np.array([1, .1]), np.array([2, 1, 1])) arma = ARMA(A=AR, B=MA, C=None, rand_state=rand_state) series = arma.simulate(sampleT=1000) # Estimate parameters by simulated series AR_est = (np.array([1, .01, .01]), np.array([3, 1, 1])) MA_est = (np.array([1, .01]), np.array([2, 1, 1])) arma_est = ARMA(A=AR_est, B=MA_est, C=None) arma_est.fix_constants() arma_est.est_params(y=series) nptest.assert_almost_equal(arma_est.A, arma.A, decimal=1) nptest.assert_almost_equal(arma_est.B, arma.B, decimal=1)
def test_simulate_arma(): AR = (np.array([1, .5, .3, 0, .2, .1, 0, .2, .05, 1, .5, .3]), np.array([3, 2, 2])) MA = (np.array([1, .2, 0, .1, 0, 0, 1, .3]), np.array([2, 2, 2])) arma = ARMA(A=AR, B=MA, C=None) # noise generated with R for comparison # ( setRNG(seed=0); noise <- makeTSnoise(10, 2, 2) ) w0_series0 = np.array([1.2629543, -0.3262334]) w0_series1 = np.array([-1.1476570, -0.2894616]) w_series0 = np.array( [1.329799263, 1.272429321, 0.414641434, -1.539950042, -0.928567035, -0.294720447, -0.005767173, 2.404653389, 0.763593461, -0.799009249]) w_series1 = np.array( [-0.2992151, -0.4115108, 0.2522234, -0.8919211, 0.4356833, -1.2375384, -0.2242679, 0.3773956, 0.1333364, 0.8041895]) noise = (np.vstack([w0_series0, w0_series1]).T, np.vstack([w_series0, w_series1]).T) # R simulation results ( arma <- ARMA(A=AR, B=MA, C=NULL); # R_result <- simulate(arma, noise=noise, sampleT=10) ) series0 = np.array( [1.58239012, 0.85063747, -0.11981462, -1.69017627, -0.19912156, 0.02830831, 0.16284912, 2.42364792, -0.15007052, -1.27531927]) series1 = np.array( [-0.5172168, -0.4261651, 0.2958942, -0.8559883, 0.7033546, -1.0857290, -0.2788928, 0.7393030, -0.2999778, 0.6363970]) R_result = np.vstack([series0, series1]).T result = arma.simulate(sampleT=10, noise=noise) nptest.assert_almost_equal(result, R_result) # R simulations results with trend ( TREND <- c(1,2); # arma_trend <- ARMA(A=AR, B=MA, C=NULL, TREND=TREND); # R_result_trend <- simulate(arma_trend, noise=noise, sampleT=sampleT) ) TREND = np.array([1., 2.]) arma = ARMA(A=AR, B=MA, C=None, TREND=TREND) series0 = np.array( [2.5823901, 0.9506375, 0.2701854, -1.1311763, 0.1139784, 0.4486183, 0.6048481, 2.8091458, 0.2663152, -0.8590244]) series1 = np.array( [1.48278321, 0.37383493, 1.17589420, 0.37601165, 1.67255459, -0.05844899, 0.80133515, 1.76057423, 0.74401876, 1.68619050]) R_result = np.vstack([series0, series1]).T result = arma.simulate(sampleT=10, noise=noise) nptest.assert_almost_equal(result, R_result) # R simulations results with trend and external series # arma_trend_ext <- ARMA(A=AR, B=MA, C=X, TREND=TREND) # input0 <- array(c(0.1, 0.2, 0.15, 0.05), dim=c(2,2)) # input <- array(c(0.1*c(1:10), 0.05*c(1:10)), dim=c(10,2)) # R_result_trend_ext <- simulate(arma_trend_ext, noise=noise, # sampleT=sampleT, input0=input0, input=input) X = (np.array([1, .3, 0, .05, 0, 0.1, 1, .3]), np.array([2, 2, 2])) arma = ARMA(A=AR, B=MA, C=X, TREND=TREND) input0 = np.array([0.1, 0.2, 0.15, 0.05]).reshape((2, 2), order='F') input = np.hstack([0.1*np.arange(1, 11), 0.05*np.arange(1, 11)]) input = input.reshape((10, 2), order='F') series0 = np.array( [2.7273901, 1.0931375, 0.5122354, -0.8113013, 0.4892429, 0.9042891, 1.1312726, 3.4036172, 0.9345114, -0.1201090]) series1 = np.array( [1.5827832, 0.4148349, 1.2723942, 0.5128017, 1.8170296, 0.1212361, 1.0094895, 1.9917395, 1.0044531, 1.9735374]) R_result = np.vstack([series0, series1]).T result = arma.simulate(sampleT=10, noise=noise, u0=input0, u=input) nptest.assert_almost_equal(result, R_result)
def test_simulate_arma(): AR = (np.array([1, .5, .3, 0, .2, .1, 0, .2, .05, 1, .5, .3]), np.array([3, 2, 2])) MA = (np.array([1, .2, 0, .1, 0, 0, 1, .3]), np.array([2, 2, 2])) arma = ARMA(A=AR, B=MA, C=None) # noise generated with R for comparison # ( setRNG(seed=0); noise <- makeTSnoise(10, 2, 2) ) w0_series0 = np.array([1.2629543, -0.3262334]) w0_series1 = np.array([-1.1476570, -0.2894616]) w_series0 = np.array([ 1.329799263, 1.272429321, 0.414641434, -1.539950042, -0.928567035, -0.294720447, -0.005767173, 2.404653389, 0.763593461, -0.799009249 ]) w_series1 = np.array([ -0.2992151, -0.4115108, 0.2522234, -0.8919211, 0.4356833, -1.2375384, -0.2242679, 0.3773956, 0.1333364, 0.8041895 ]) noise = (np.vstack([w0_series0, w0_series1]).T, np.vstack([w_series0, w_series1]).T) # R simulation results ( arma <- ARMA(A=AR, B=MA, C=NULL); # R_result <- simulate(arma, noise=noise, sampleT=10) ) series0 = np.array([ 1.58239012, 0.85063747, -0.11981462, -1.69017627, -0.19912156, 0.02830831, 0.16284912, 2.42364792, -0.15007052, -1.27531927 ]) series1 = np.array([ -0.5172168, -0.4261651, 0.2958942, -0.8559883, 0.7033546, -1.0857290, -0.2788928, 0.7393030, -0.2999778, 0.6363970 ]) R_result = np.vstack([series0, series1]).T result = arma.simulate(sampleT=10, noise=noise) nptest.assert_almost_equal(result, R_result) # R simulations results with trend ( TREND <- c(1,2); # arma_trend <- ARMA(A=AR, B=MA, C=NULL, TREND=TREND); # R_result_trend <- simulate(arma_trend, noise=noise, sampleT=sampleT) ) TREND = np.array([1., 2.]) arma = ARMA(A=AR, B=MA, C=None, TREND=TREND) series0 = np.array([ 2.5823901, 0.9506375, 0.2701854, -1.1311763, 0.1139784, 0.4486183, 0.6048481, 2.8091458, 0.2663152, -0.8590244 ]) series1 = np.array([ 1.48278321, 0.37383493, 1.17589420, 0.37601165, 1.67255459, -0.05844899, 0.80133515, 1.76057423, 0.74401876, 1.68619050 ]) R_result = np.vstack([series0, series1]).T result = arma.simulate(sampleT=10, noise=noise) nptest.assert_almost_equal(result, R_result) # R simulations results with trend and external series # arma_trend_ext <- ARMA(A=AR, B=MA, C=X, TREND=TREND) # input0 <- array(c(0.1, 0.2, 0.15, 0.05), dim=c(2,2)) # input <- array(c(0.1*c(1:10), 0.05*c(1:10)), dim=c(10,2)) # R_result_trend_ext <- simulate(arma_trend_ext, noise=noise, # sampleT=sampleT, input0=input0, input=input) X = (np.array([1, .3, 0, .05, 0, 0.1, 1, .3]), np.array([2, 2, 2])) arma = ARMA(A=AR, B=MA, C=X, TREND=TREND) input0 = np.array([0.1, 0.2, 0.15, 0.05]).reshape((2, 2), order='F') input = np.hstack([0.1 * np.arange(1, 11), 0.05 * np.arange(1, 11)]) input = input.reshape((10, 2), order='F') series0 = np.array([ 2.7273901, 1.0931375, 0.5122354, -0.8113013, 0.4892429, 0.9042891, 1.1312726, 3.4036172, 0.9345114, -0.1201090 ]) series1 = np.array([ 1.5827832, 0.4148349, 1.2723942, 0.5128017, 1.8170296, 0.1212361, 1.0094895, 1.9917395, 1.0044531, 1.9735374 ]) R_result = np.vstack([series0, series1]).T result = arma.simulate(sampleT=10, noise=noise, u0=input0, u=input) nptest.assert_almost_equal(result, R_result)