I am trying to fit a linear 1-order autoregressive model to some multivariate time-series data. The model I am using is of the form
$$x_t = Ax_{t-1}+\xi_{t-1}$$
and I am solving it in R using the mAr package which can solve this easily. However, I notice that there are negative entries in the matrix $A$. According to my model assumptions there should be no negative entries, so I was wondering if there is a way to impose this restriction into to the autoregressive model and find the best solution given that constraint. I am also aware that there are algorithms for performing non-negative least squares but how should I implement this in R.
Additionally, if you know of any libraries in R, Python or Matlab that can solve this please let me know, but of course, I would like to understand the mathematical formulation of the optimisation problem.
Thanks in advance.
I think I have found an answer. Rather than using a built in AR model, I can write my own and use the built-in non-negative least squares optimisation.
I have to solve a separate problem for each variable $i$. We set up the problem as $$\min_{x}||Ax-b||^2$$ which can be misleading due to the different use of $A$ and $x$ in the model. So we define, the problem to be $$\min_{y}||By-b||^2$$ where we can set $$B_{t,n} = x_{t,n}$$ i.e. the value of variable $x_{n}$ at time $t$ in the time-series for $t=0,...,T-1$. We set $y$ to be the column vector $(a_{i,1},...,a_{i,N})^{\top}$ and $b$ to be the column vector $(x_{1,i},...,x_{T,i})^{\top}$. Then we solve using the NNLS R package and set the $i$-th row of matrix $A$ to be the vector $y$ (as a row vector). We repeat this for all $i$ in turn and we get the full matrix $A$. NNLS also returns the residuals $\xi$ for each variable, so we concatenate them to get the time-series of residuals.