def test_arma_acf(): # Check for specific AR(1) N = 20 phi = 0.9 sigma = 1 # rep 1: from module function rep1 = arma_acf([1, -phi], [1], N) # rep 2: manually acovf = np.array([1. * sigma * phi**i / (1 - phi**2) for i in range(N)]) rep2 = acovf / (1. / (1 - phi**2)) assert_allclose(rep1, rep2)
def test_arma_acf(): # Check for specific AR(1) N = 20 phi = 0.9 sigma = 1 # rep 1: from module function rep1 = arma_acf([1, -phi], [1], N) # rep 2: manually acovf = np.array([1. * sigma * phi ** i / (1 - phi ** 2) for i in range(N)]) rep2 = acovf / (1. / (1 - phi ** 2)) assert_almost_equal(rep1, rep2, 8) # 8 is max precision here
def _ACF_ARMA(self, x_max): """Calculate the ACF using an ARMA function (from statsmodels).""" nma = round(self.lambda_avg/self.lambda_s) ma_coeff = numpy.ones((nma)) lag_max = ceil(x_max / self.lambda_s) a1 = exp(-self.lambda_s / self.lambda_p) # Use the statsmodels ARMA function armaAcf = arma_acf([1,-a1], ma_coeff, nobs=lag_max) x = numpy.arange(0, x_max, self.lambda_s) # Set self.acf - a 2-d array of theta/ACF acf = numpy.empty((len(x), 2)) # convert distance x (meters) in the acf to angular scale in degrees - clouds assumed # at 10 km (thus dividing by 10,000) acf[:,0] = x / (10000.0*numpy.pi/180.0) acf[:,1] = armaAcf return acf
def test_arma_acf_compare_R_ARMAacf(): # Test specific cases against output from R's ARMAacf bd_example_3_3_2 = arma_acf([1, -1, 0.25], [1, 1]) assert_allclose(bd_example_3_3_2, results_arma_acf.bd_example_3_3_2) example_1 = arma_acf([1, -1, 0.25], [1, 1, 0.2]) assert_allclose(example_1, results_arma_acf.custom_example_1) example_2 = arma_acf([1, -1, 0.25], [1, 1, 0.2, 0.3]) assert_allclose(example_2, results_arma_acf.custom_example_2) example_3 = arma_acf([1, -1, 0.25], [1, 1, 0.2, 0.3, -0.35]) assert_allclose(example_3, results_arma_acf.custom_example_3) example_4 = arma_acf([1, -1, 0.25], [1, 1, 0.2, 0.3, -0.35, -0.1]) assert_allclose(example_4, results_arma_acf.custom_example_4) example_5 = arma_acf([1, -1, 0.25, -0.1], [1, 1, 0.2]) assert_allclose(example_5, results_arma_acf.custom_example_5) example_6 = arma_acf([1, -1, 0.25, -0.1, 0.05], [1, 1, 0.2]) assert_allclose(example_6, results_arma_acf.custom_example_6) example_7 = arma_acf([1, -1, 0.25, -0.1, 0.05, -0.02], [1, 1, 0.2]) assert_allclose(example_7, results_arma_acf.custom_example_7)
def ACF_ARMA(lambda_p, lambda_avg, lambda_s, x_max): lambda_p = float(lambda_p) lambda_s = float(lambda_s) lambda_avg = float(lambda_avg) nma = round(lambda_avg/lambda_s) ma_coeff = np.ones((nma)) lag_max = round(x_max / lambda_s) a1 = exp(-lambda_s / lambda_p) acf = arma_acf([1,-a1], ma_coeff, nobs=lag_max) x = np.arange(0, x_max, lambda_s) acfArray = np.empty((len(x), 2)) acfArray[:,0] = x acfArray[:,1] = acf return acfArray
('ma2', acovf_ma2), ('arma11', acovf_arma11), ('ar1', acovf_arma11)]) cases = [('ma1', (ar0, ma1)), ('ma2', (ar0, ma2)), ('arma11', (ar1, ma1)), ('ar1', (ar1, ma0))] for c, args in cases: ar, ma = args print('') print(c, ar, ma) myacovf = arma_acovf(ar, ma, nobs=10) myacf = arma_acf(ar, ma, nobs=10) if c[:2]=='ma': othacovf = comparefn[c](ma) else: othacovf = comparefn[c](ar, ma) print(myacovf[:5]) print(othacovf[:5]) #something broke again, #for high persistence case eg ar=0.99, nobs of IR has to be large #made changes to arma_acovf assert_array_almost_equal(myacovf, othacovf,10) assert_array_almost_equal(myacf, othacovf/othacovf[0],10) #from nitime.utils def ar_generator(N=512, sigma=1.):
ma2 = [1., 0.4, 0.6] ma0 = [1., 0.] comparefn = dict([('ma1', acovf_ma1), ('ma2', acovf_ma2), ('arma11', acovf_arma11), ('ar1', acovf_arma11)]) cases = [('ma1', (ar0, ma1)), ('ma2', (ar0, ma2)), ('arma11', (ar1, ma1)), ('ar1', (ar1, ma0))] for c, args in cases: ar, ma = args print('') print(c, ar, ma) myacovf = arma_acovf(ar, ma, nobs=10) myacf = arma_acf(ar, ma, nobs=10) if c[:2] == 'ma': othacovf = comparefn[c](ma) else: othacovf = comparefn[c](ar, ma) print(myacovf[:5]) print(othacovf[:5]) #something broke again, #for high persistence case eg ar=0.99, nobs of IR has to be large #made changes to arma_acovf assert_array_almost_equal(myacovf, othacovf, 10) assert_array_almost_equal(myacf, othacovf / othacovf[0], 10) #from nitime.utils def ar_generator(N=512, sigma=1.):
ma2 = [1., 0.4, 0.6] ma0 = [1., 0.] comparefn = dict([('ma1', acovf_ma1), ('ma2', acovf_ma2), ('arma11', acovf_arma11), ('ar1', acovf_arma11)]) cases = [('ma1', (ar0, ma1)), ('ma2', (ar0, ma2)), ('arma11', (ar1, ma1)), ('ar1', (ar1, ma0))] for c, args in cases: ar, ma = args print('') print(c, ar, ma) myacovf = arma_acovf(ar, ma, nobs=10) myacf = arma_acf(ar, ma, lags=10) if c[:2] == 'ma': othacovf = comparefn[c](ma) else: othacovf = comparefn[c](ar, ma) print(myacovf[:5]) print(othacovf[:5]) #something broke again, #for high persistence case eg ar=0.99, nobs of IR has to be large #made changes to arma_acovf assert_array_almost_equal(myacovf, othacovf, 10) assert_array_almost_equal(myacf, othacovf / othacovf[0], 10) #from nitime.utils def ar_generator(N=512, sigma=1.):