def test_plot_acf_kwargs(close_figures): # Just test that it runs. fig = plt.figure() ax = fig.add_subplot(111) ar = np.r_[1., -0.9] ma = np.r_[1., 0.9] armaprocess = tsp.ArmaProcess(ar, ma) rs = np.random.RandomState(1234) acf = armaprocess.generate_sample(100, distrvs=rs.standard_normal) buff = BytesIO() plot_acf(acf, ax=ax) fig.savefig(buff, format='rgba') buff_with_vlines = BytesIO() fig_with_vlines = plt.figure() ax = fig_with_vlines.add_subplot(111) vlines_kwargs = {'linestyles': 'dashdot'} plot_acf(acf, ax=ax, vlines_kwargs=vlines_kwargs) fig_with_vlines.savefig(buff_with_vlines, format='rgba') buff.seek(0) buff_with_vlines.seek(0) plain = buff.read() with_vlines = buff_with_vlines.read() assert_(with_vlines != plain)
def trend_plot_items(request,items): xdata1 =[] ydata5 =[] csvdata = FDataFrame.objects.filter(items=str(items),support_threshold='0.01') for l in csvdata: xdata1.append(l.date2str) ydata5.append(l.confidence) ydata5 = map(float,ydata5) xdata1 = map(int,xdata1) extra_serie = {"tooltip": {"y_start": "There are ", "y_end": " calls"}} chartdata = { 'x': xdata1, 'name1': 'confidence of Rule: {% rules %}', 'y1': ydata5, 'extra1': extra_serie, } charttype = "multiBarChart" data = { 'charttype': charttype, 'chartdata': chartdata } ar = np.r_[1., -0.5, -0.2]; ma = np.r_[1., 0.2, -0.2] np.random.seed(123) x = tsp.arma_generate_sample(ar, ma, 20000, burnin=1000) sm.tsa.pacf(x, 5) ap = tsp.ArmaProcess(ar, ma) ap.pacf(5) return render_to_response('blog/multibarchart.html', data)
def test_plotacf(): # Just test that it runs. fig = plt.figure() ax = fig.add_subplot(111) ar = np.r_[1., -0.9] ma = np.r_[1., 0.9] armaprocess = tsp.ArmaProcess(ar, ma) acf = armaprocess.acf(20)[:20] plotacf(acf, ax=ax) plt.close(fig)
def test_plot_pacf(close_figures): # Just test that it runs. fig = plt.figure() ax = fig.add_subplot(111) ar = np.r_[1., -0.9] ma = np.r_[1., 0.9] armaprocess = tsp.ArmaProcess(ar, ma) rs = np.random.RandomState(1234) pacf = armaprocess.generate_sample(100, distrvs=rs.standard_normal) plot_pacf(pacf, ax=ax) plot_pacf(pacf, ax=ax, alpha=None)
def test_plot_acf_irregular(close_figures): # Just test that it runs. fig = plt.figure() ax = fig.add_subplot(111) ar = np.r_[1., -0.9] ma = np.r_[1., 0.9] armaprocess = tsp.ArmaProcess(ar, ma) rs = np.random.RandomState(1234) acf = armaprocess.generate_sample(100, distrvs=rs.standard_normal) plot_acf(acf, ax=ax, lags=np.arange(1, 11)) plot_acf(acf, ax=ax, lags=10, zero=False) plot_acf(acf, ax=ax, alpha=None, zero=False)
def test_plot_pacf(close_figures): # Just test that it runs. fig = plt.figure() ax = fig.add_subplot(111) ar = np.r_[1.0, -0.9] ma = np.r_[1.0, 0.9] armaprocess = tsp.ArmaProcess(ar, ma) rs = np.random.RandomState(1234) pacf = armaprocess.generate_sample(100, distrvs=rs.standard_normal) with pytest.warns(FutureWarning): plot_pacf(pacf, ax=ax) with pytest.warns(FutureWarning, match="The default"): plot_pacf(pacf, ax=ax, alpha=None)
def test_plot_pacf_kwargs(close_figures): # Just test that it runs. fig = plt.figure() ax = fig.add_subplot(111) ar = np.r_[1.0, -0.9] ma = np.r_[1.0, 0.9] armaprocess = tsp.ArmaProcess(ar, ma) rs = np.random.RandomState(1234) pacf = armaprocess.generate_sample(100, distrvs=rs.standard_normal) buff = BytesIO() with pytest.warns(FutureWarning, match="The default"): plot_pacf(pacf, ax=ax) fig.savefig(buff, format="rgba") buff_linestyle = BytesIO() fig_linestyle = plt.figure() ax = fig_linestyle.add_subplot(111) with pytest.warns(FutureWarning, match="The default"): plot_pacf(pacf, ax=ax, ls="-") fig_linestyle.savefig(buff_linestyle, format="rgba") buff_with_vlines = BytesIO() fig_with_vlines = plt.figure() ax = fig_with_vlines.add_subplot(111) vlines_kwargs = {"linestyles": "dashdot"} with pytest.warns(FutureWarning, match="The default"): plot_pacf(pacf, ax=ax, vlines_kwargs=vlines_kwargs) fig_with_vlines.savefig(buff_with_vlines, format="rgba") buff.seek(0) buff_linestyle.seek(0) buff_with_vlines.seek(0) plain = buff.read() linestyle = buff_linestyle.read() with_vlines = buff_with_vlines.read() assert_(plain != linestyle) assert_(with_vlines != plain) assert_(linestyle != with_vlines)
def test_plot_acf_missing(close_figures): # Just test that it runs. fig = plt.figure() ax = fig.add_subplot(111) ar = np.r_[1., -0.9] ma = np.r_[1., 0.9] armaprocess = tsp.ArmaProcess(ar, ma) rs = np.random.RandomState(1234) acf = armaprocess.generate_sample(100, distrvs=rs.standard_normal) acf[::13] = np.nan buff = BytesIO() plot_acf(acf, ax=ax, missing='drop') fig.savefig(buff, format='rgba') buff.seek(0) fig = plt.figure() ax = fig.add_subplot(111) buff_conservative = BytesIO() plot_acf(acf, ax=ax, missing='conservative') fig.savefig(buff_conservative, format='rgba') buff_conservative.seek(0) assert_(buff.read() != buff_conservative.read())
fig = plt.figure(figsize=(8, 13)) fig.suptitle( 'ARMA: Autocorrelation (left) and Partial Autocorrelation (right)') subplotcount = 1 nrows = 4 for arcoef in arcoefs[:-1]: for macoef in macoefs[:-1]: ar = np.r_[1., -arcoef] ma = np.r_[1., macoef] #from statsmodels.sandbox.tsa.fftarma import ArmaFft as FftArmaProcess #y = tsp.arma_generate_sample(ar,ma,nsample, sig, burnin) #armaprocess = FftArmaProcess(ar, ma, nsample) #TODO: make n optional #armaprocess.plot4() armaprocess = tsp.ArmaProcess(ar, ma) acf = armaprocess.acf(20)[:20] pacf = armaprocess.pacf(20)[:20] ax = fig.add_subplot(nrows, 2, subplotcount) plotacf(acf, ax=ax) ## ax.set_title('Autocorrelation \nar=%s, ma=%rs' % (ar, ma), ## size='xx-small') ax.text( 0.7, 0.6, 'ar =%s \nma=%s' % (ar, ma), transform=ax.transAxes, horizontalalignment='left', #'right', size='xx-small') ax.set_xlim(-1, 20) subplotcount += 1
paraAR_mle, paraMA_mle = armaResult.arparams, armaResult.maparams paraSigma2_mle = armaResult.sigma2 break print('\n----3.1 (2)----\n') print(' Coefficents of AR: {0}\n'.format(formatIter(paraAR_mle))) print(' Coefficents of MA: {0}\n'.format(formatIter(paraMA_mle))) print(' Sigma^2: %.4f\n' % paraSigma2_mle) print(' Method:Maximum Likelihood Estimation') ''' # (3) use attributes isstationary and isinvertible of Class arima_process.ArmaProcess ''' armaProcess = arima_process.ArmaProcess(np.append(1, -paraAR_mle), np.append(1, paraMA_mle)) stationary = armaProcess.isstationary invertible = armaProcess.isinvertible print('\n----3.1 (3)----\n') print(' Is stationary: {0}'.format(stationary)) print(' Is invertible: {0}'.format(invertible)) ''' ## (4) 100 repetition of (2) 100 results are stored in a variable data so that Mean, Var, MSE of results can be computed ''' data = np.array([np.append(np.append(paraAR_mle, paraMA_mle), paraSigma2)]) i = 1
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Jun 18 10:19:06 2019 @author: mariaculjak Running LjungBoxtest.py ; plot_acf_pacf.py Run this command in the terminal while making sure your terminal location is in the same directory as .whl file: pip install PythonTsa-1.2-py3-none-any.whl """ import numpy as np from PythonTsa.LjungBoxtest import * from PythonTsa.plot_acf_pacf import * import statsmodels.tsa.arima_process as arp ar = np.array([1., -0.75, 0.25]) ar_process = arp.ArmaProcess(ar=ar, nobs=1000) sample = ar_process.generate_sample() acf_pacf_fig(x=sample) qstatf(x=sample, type="LjungBox", noestimatedcoef=0, nolags=1) acovf(x=sample, unbiased=False, demean=True, fft=False, missing='none') plot_LB_pvalue(x=sample, noestimatedcoef=0, nolags=1) qstatf(x=sample, noestimatedcoef=0, nolags=1, type="LjungBox")
import statsmodels.tsa.arima_process as sm from statsmodels.graphics.tsaplots import * #4. arma = sm.ArmaProcess([-1, -0.6], [1]) sample = arma.generate_sample(200) plot_acf(sample, lags=20) plot_pacf(sample, lags=20) #5. arma = sm.ArmaProcess([-1], [1, 0.4]) sample = arma.generate_sample(200) plot_acf(sample, lags=20) plot_pacf(sample, lags=20) #6. import statsmodels.tsa.arima_process as sm from statsmodels.graphics.tsaplots import * import numpy as np import pandas as pd numbers = np.random.normal(size=100) numbers = pd.Series(numbers) numbers.plot() plot_acf(numbers, lags=20) from statsmodels.tsa import stattools stattools.arma_order_select_ic(numbers.values, max_ma=4) #7.
#-------------------------------------------------------------------------------- # To choose the number of lagged terms, p and q, for ARIMA(p,d,q) processes, use the # Box-Jenkins methodology to look at the pattern in the autocorrelation (acf) and # partial autocorrelation (pacf) functions. # scikits.statsmodels.tsa.arima_process contains a class that provides several properties # of ARMA processes and a random process generator. This allows easy comparison of the # theoretical properties of an ARMA process with their empirical counterparts. # For exmaple, define the lag coefficients for an ARMA(2,2) process, generate a random # process and compare the observed and theoretical pacf: ar = np.r_[1., -0.5, -0.2] ma = np.r_[1., 0.2, -0.2] np.random.seed(123) x = ap.arma_generate_sample(ar, ma, 20000, burnin=1000) print "observed pacf: {0}".format(sm.tsa.pacf(x, 5)) theo_ap = ap.ArmaProcess(ar, ma) print " theo pacf: {0}\n".format(theo_ap.pacf(6)) # We can see that observed and theoretical pacf are very close in a large generated # sample like this. #-------------------------------------------------------------------------------- # We can use Statsmodels Autoregressive Moving Average (ARMA) time-series models to # simulate a series: ar_coef = [1, .75, -.25] ma_coef = [1, -.5] nobs = 100 y = ap.arma_generate_sample(ar_coef, ma_coef, nobs) y += 4 # add in a constant # Estimate an ARMA model of the series mod = tsa.ARMA(y, (2, 1)) res = mod.fit(order=(2, 1), trend='c', method='css-mle', disp=-1) print "Estimated ARMA model params: {0}\n".format(res.params)