Skip to content
DeepForgeby svx
All problems
MediumTime Seriests-034

AR(1) Process Generation

Problem

Generate a seeded AR(1) process. Call random.seed(seed), draw n standard normal innovations, and initialize x[0] = c / (1 - phi) + e[0] when abs(phi) < 1 (otherwise x[0] = e[0]); then iterate x[t] = c + phi * x[t-1] + e[t].

Return the generated series; n <= 0 gives an empty list.

Hint

Start from the stationary mean c / (1 - phi) so burn-in is unnecessary.

Test cases

case 1
in: [5,0.5,0.5,42]
expected: [0.8559096704220717,0.7550512348795165,0.7662097558720958,1.5850886030349112,1.1649560177345686]
case 2
in: [4,1,0,7]
expected: [0.7441197115523996,1.511431512516514,0.7739038352168953,0.6849315776688145]
case 3
in: [3,0.2,-0.5,1]
expected: [1.4215180864887962,0.9386865654553731,-0.20300747378942463]
case 4
in: [0,0.5,0.5,3]
expected: []
Starter codePython
import random
def ar1_generate(n, c, phi, seed):
    # Your code here
    pass

4 test cases · run your code against them in the editor