Generating non-zero mean AR(2) samples in python

45 Views Asked by At

I am trying to generate non-zero mean AR(2) samples using statsmodels package. But it seems , by default we can't generate non-zero mean samples.

Is there any workaround, in python. I want to generate only positive samples.

My current code is

import numpy as np
from statsmodels.tsa.arima_process import ArmaProcess

rng = np.random.default_rng(12345)
ar_1 = np.array([2, -0.25, -0.25])    
ar_2 = np.array([2, -0.5, -0.25])  
ma1 = np.array([1])
ar1_proc = ArmaProcess(ar_1, ma1)
ar1_dat = ar1_proc.generate_sample(nsample=2*60*60, distrvs=rng.lognormal)
ar2_proc = ArmaProcess(ar_2, ma1)
ar2_dat = ar2_proc.generate_sample(nsample=2*60*60, distrvs=rng.lognormal)
1

There are 1 best solutions below

1
Robert Long On

i'm a bit confused by your question. Since you are using istrvs=rng.lognormal that should be enough to ensure no negative values.

To check this I have wrapped up your code in a Monte-Carlo simulation loop.

import numpy as np
from statsmodels.tsa.arima_process import ArmaProcess

n_sim = 100000  # Number of simulations
nsample = 2*60*60

rng = np.random.default_rng(15)

# ARMA process parameters
ar_1 = np.array([2, -0.25, -0.25])
ar_2 = np.array([2, -0.5, -0.25])
ma1 = np.array([1])

# Initialize variables to store the minimum value from each simulation
min_values = []

for _ in range(n_sim):
    # AR(1) process
    ar1_proc = ArmaProcess(ar_1, ma1)
    ar1_dat = ar1_proc.generate_sample(nsample=nsample, distrvs=rng.lognormal)
    
    # AR(2) process
    ar2_proc = ArmaProcess(ar_2, ma1)
    ar2_dat = ar2_proc.generate_sample(nsample=nsample, distrvs=rng.lognormal)
    
    min_values.append(min(ar1_dat.min(), ar2_dat.min()))

print(min(min_values))

which took just over a minute on my machine and the result was: 0.004882878764670938