patternpythonMinor
Creating a special Toeplitz matrix in R
Viewed 0 times
creatingmatrixspecialtoeplitz
Problem
I'd like to know if there is a standard way of creating a matrix like the one shown below using the R programming language.
$$
\begin{bmatrix}
1 & \rho & \rho^2 & \cdots & \rho^{T-1}\\
\rho & 1 & \rho & \cdots & \rho^{T-2}\\
\rho^2 & \rho & 1 & \cdots & \rho^{T-3}\\
\vdots &\vdots &\vdots & \ddots & \vdots\\
\rho^{T-1} & \rho^{T-2} & \rho^{T-3} & \cdots & 1
\end{bmatrix}
$$
The code I've been using to create matrices like this one is shown below.
For illustrative purposes, let rho = 0.8 and T = 20.
Although the output of this code does, indeed, produce the matrix of interest, I get the feeling that there's probably a better way of doing it.
Can this code snippet be improved - in the sense that there is a more elegant, more standard, more correct, or more efficient way of creating the matrix?
$$
\begin{bmatrix}
1 & \rho & \rho^2 & \cdots & \rho^{T-1}\\
\rho & 1 & \rho & \cdots & \rho^{T-2}\\
\rho^2 & \rho & 1 & \cdots & \rho^{T-3}\\
\vdots &\vdots &\vdots & \ddots & \vdots\\
\rho^{T-1} & \rho^{T-2} & \rho^{T-3} & \cdots & 1
\end{bmatrix}
$$
The code I've been using to create matrices like this one is shown below.
For illustrative purposes, let rho = 0.8 and T = 20.
rho <- 0.8
t <- 20
toeplitz(c(1, poly(rho, t-1, raw=TRUE)))Although the output of this code does, indeed, produce the matrix of interest, I get the feeling that there's probably a better way of doing it.
Can this code snippet be improved - in the sense that there is a more elegant, more standard, more correct, or more efficient way of creating the matrix?
Solution
In this case,
poly is probably too much trouble. You can just calculate the polynomial yourself with rho^seq.int(0, t-1). Altogether that givestoeplitz(rho^seq.int(0, t-1))Code Snippets
toeplitz(rho^seq.int(0, t-1))Context
StackExchange Code Review Q#75306, answer score: 3
Revisions (0)
No revisions yet.