# Matrix is singular to working precision problem

**URL:** <https://forum.dynare.org/t/matrix-is-singular-to-working-precision-problem/18094>\
**Category:** Perfect foresight simulations\
**Created:** [14 May 2021 14:47 UTC](https://forum.dynare.org/t/matrix-is-singular-to-working-precision-problem/18094 "2021-05-14T14:47:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![undergrad](https://forum.dynare.org/letter_avatar_proxy/v4/letter/u/d9b06d/32.png) [@undergrad](https://forum.dynare.org/u/undergrad)\
**Post date:** [14 May 2021 14:47 UTC](https://forum.dynare.org/t/matrix-is-singular-to-working-precision-problem/18094/1 "2021-05-14T14:47:43Z")

</div>

Hi, I’m currently working on the model by Kirchner & Van Wijnbergen (2016), and I intend to adapt it to the context of epidemics/Covid-19 for which I have included an external process from Acurio-Vasconez (2021) that exogenously determines the labor supply. Hence, the model is deterministic and is solved w/ perfect foresight. I’ve solved the steady-state analytically and followed the original code closely, yet somehow this problem keeps occurring. I’ve double-checked the equations and can’t seem to find anything wrong.

MODEL SIMULATION:

Warning: Matrix is singular to working precision.

> In sim1\_linear (line 199)  
> In perfect\_foresight\_solver\_core (line 97)  
> In perfect\_foresight\_solver (line 63)  
> In Dynare\_forum.driver (line 873)  
> In dynare (line 293)

## Iter: 1, Initial err. = 0.4515, err. = NaN, time = 0.046

Simulation terminated with NaN or Inf in the residuals or endogenous variables.  
There is most likely something wrong with your model. Try model\_diagnostics or another simulation method.

Here’s the .mod file

[Dynare\_forum.mod](https://forum.dynare.org/uploads/short-url/ngWN5l7mXcJLIAKX5j9Dq1rTe57.mod) (11.8 KB)

---

<div class="post-metadata">

**Author:** ![undergrad](https://forum.dynare.org/letter_avatar_proxy/v4/letter/u/d9b06d/32.png) [@undergrad](https://forum.dynare.org/u/undergrad)\
**Post date:** [15 May 2021 16:28 UTC](https://forum.dynare.org/t/matrix-is-singular-to-working-precision-problem/18094/2 "2021-05-15T16:28:39Z")

</div>

Update: after debugging I seem to find the culprit, apparently Om (variable for measuring portfolio allocation between government bonds & loans among banks) has a derivative of 0. However, I do not understand why this is the case, and why this worked in a stochastic simulation but not a deterministic one

Simulation terminated with NaN or Inf in the residuals or endogenous variables.  
There is most likely something wrong with your model. Try model\_diagnostics or another simulation method.

Simulation of the perfect foresight model failed!Switching to a homotopy method…  
Iter. | Lambda | status | Max. residual  
++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
The dynamic Jacobian is singular. The problem derives from:  
The derivative with respect to the lag of Om being 0 for all equations.

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [16 May 2021 11:36 UTC](https://forum.dynare.org/t/matrix-is-singular-to-working-precision-problem/18094/3 "2021-05-16T11:36:25Z")

</div>

Have a look at

```auto
(Rp) = (Rk)*(Om(-1))+(Rb)*(1-(Om(-1))); 

```

As `Rk=Rb`, this equation reduces to `Rp=Rk=Rb`. This is a problem for perfect foresight simulations as `Om(-1)` will drop out.

---

<div class="post-metadata">

**Author:** ![undergrad](https://forum.dynare.org/letter_avatar_proxy/v4/letter/u/d9b06d/32.png) [@undergrad](https://forum.dynare.org/u/undergrad)\
**Post date:** [16 May 2021 12:58 UTC](https://forum.dynare.org/t/matrix-is-singular-to-working-precision-problem/18094/4 "2021-05-16T12:58:25Z")

</div>

@jpfeifer Thank you Prof. I thought that was the problem as well. Although I am at a loss as to how to modify it, that equation determines the return on portfolio for banks and at steady state Rk must equal Rb

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [16 May 2021 14:48 UTC](https://forum.dynare.org/t/matrix-is-singular-to-working-precision-problem/18094/5 "2021-05-16T14:48:49Z")

</div>

It seems the fundamental problem is the same one as at

> [@Perfect foresight simulation always failed](https://forum.dynare.org/t/perfect-foresight-simulation-always-failed/17651/3):
>
> The problem is the equation Lam(1)\*(R(1) + 1 - del) = 1; All variables here are dated t+1. Thus, in the last period, the derivative with respect to all variables will be 0. This is no problem for stochastic simulation. For deterministic ones, do not rely on auxiliary definitions. Use beta\*C/C(+1)/G\*(R(1) + 1 - del) = 1; and the file will work.

Have a look at the attached file where I reduced the crazy tolerance you set, increased `periods` and substituted in for the Lagrange multiplier.  
[Dynare\_forum.mod](https://forum.dynare.org/uploads/short-url/wkZ0qjz2qLRsVeejYYYcu7OgzKj.mod) (11.9 KB)

---

<div class="post-metadata">

**Author:** ![undergrad](https://forum.dynare.org/letter_avatar_proxy/v4/letter/u/d9b06d/32.png) [@undergrad](https://forum.dynare.org/u/undergrad)\
**Post date:** [16 May 2021 17:02 UTC](https://forum.dynare.org/t/matrix-is-singular-to-working-precision-problem/18094/6 "2021-05-16T17:02:37Z")

</div>

Dear Professor, thank you so much for your help, it’s now working properly. You just single-handedly saved my thesis 😊

---

<div class="post-metadata">

**Author:** ![tonyxia324](https://forum.dynare.org/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@tonyxia324](https://forum.dynare.org/u/tonyxia324)\
**Post date:** [12 May 2024 03:47 UTC](https://forum.dynare.org/t/matrix-is-singular-to-working-precision-problem/18094/7 "2024-05-12T03:47:11Z")

</div>

Dear Professor,

I know this is a very old post, but the problem I encountered in my mod file is very similar to the one that you solved here. My understanding is that you adjusted the mod file by replacing (U\_c(+1)/U\_c) with the Lagrangian multiplier Lambda(+1). Can you help me understand the intuition in how this resolves the numerical issue? The equation (Rp) = (Rk)_(Om(-1))+(Rb)_(1-(Om(-1))) is still the same, wouldn’t that lead to Om(-1) drop out just as before?

Best regards

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [13 May 2024 08:29 UTC](https://forum.dynare.org/t/matrix-is-singular-to-working-precision-problem/18094/8 "2024-05-13T08:29:04Z")

</div>

The problem derives from the internal representation of the model. The inclusion of an auxiliary variable and corresponding equation can cause numerical problems as it may introduce spurious terminal conditions.
