Skip to content

Commit

Permalink
Added solution to A4
Browse files Browse the repository at this point in the history
  • Loading branch information
robjhyndman committed May 21, 2024
1 parent 6137a1d commit f574b07
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
35 changes: 35 additions & 0 deletions assignments/A4.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
generate_arma <- function(n = 100, c = 0, phi = NULL, theta = NULL, sigma = 1) {
# Model parameters
if (is.null(phi)) phi <- numeric()
if (is.null(theta)) theta <- numeric()
p <- length(phi)
q <- length(theta)
if (any(abs(polyroot(c(1, -phi))) <= 1)) {
stop("Model is not stationary")
}
if (any(abs(polyroot(c(1, theta))) <= 1)) {
stop("Model is not invertible")
}

# Series length
if (n <= 0) {
stop("n must be positive")
}
# Add 50 for burn-in period
nplus <- n + max(50, p, q)

# Generate errors
error <- rnorm(nplus, mean = 0, sd = sigma)

# Set up vector for the response with initial values
# equal to the mean of the process for faster burn-in.
y <- rep(c / (1 - sum(phi)), nplus)

# Generate remaining observations
for (i in max(p, q) + seq_len(nplus - max(p, q))) {
y[i] <- c + sum(phi * y[seq(i - 1, by = -1, length.out = p)]) + sum(theta * error[seq(i - 1, by = -1, length.out = q)]) + error[i]
}

# Return last n observations
tail(y, n)
}
6 changes: 6 additions & 0 deletions assignments/A4.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ Please submit your solution as a .R file.
source(here::here("course_info.R"))
submit(schedule, "Assignment 4")
```

---

## Solution

[R code](A4.R)

0 comments on commit f574b07

Please sign in to comment.