# Perfect foresight simulation always failed

**URL:** <https://forum.dynare.org/t/perfect-foresight-simulation-always-failed/17651>\
**Category:** Perfect foresight simulations\
**Created:** [15 March 2021 13:38 UTC](https://forum.dynare.org/t/perfect-foresight-simulation-always-failed/17651 "2021-03-15T13:38:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fuyangzhao](https://forum.dynare.org/letter_avatar_proxy/v4/letter/f/41988e/32.png) [@fuyangzhao](https://forum.dynare.org/u/fuyangzhao)\
**Post date:** [15 March 2021 13:38 UTC](https://forum.dynare.org/t/perfect-foresight-simulation-always-failed/17651/1 "2021-03-15T13:38:27Z")

</div>

Dear Prof Pfeifer,  
I’m trying to simulate a simple model under deterministic case. The model is small. The steady state is easy to solve. And BK condition is satisfied.

However, perfect foresight simulations always fail. And it is driving me crazy.  
Any clue how I can solve it? Thank you in advance!

---

<div class="post-metadata">

**Author:** ![fuyangzhao](https://forum.dynare.org/letter_avatar_proxy/v4/letter/f/41988e/32.png) [@fuyangzhao](https://forum.dynare.org/u/fuyangzhao)\
**Post date:** [15 March 2021 13:50 UTC](https://forum.dynare.org/t/perfect-foresight-simulation-always-failed/17651/2 "2021-03-15T13:50:38Z")

</div>

I provide my mod and m files in the attachment.[GTmodel.zip](https://forum.dynare.org/uploads/short-url/jXwLwic3RFQiL9GaogrVa7gT4jU.zip) (3.6 KB)

---

<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:** [17 March 2021 10:56 UTC](https://forum.dynare.org/t/perfect-foresight-simulation-always-failed/17651/3 "2021-03-17T10:56:34Z")

</div>

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

```auto
beta*C/C(+1)/G*(R(1) + 1 - del) = 1;

```

and the file will work.

---

<div class="post-metadata">

**Author:** ![fuyangzhao](https://forum.dynare.org/letter_avatar_proxy/v4/letter/f/41988e/32.png) [@fuyangzhao](https://forum.dynare.org/u/fuyangzhao)\
**Post date:** [18 March 2021 00:41 UTC](https://forum.dynare.org/t/perfect-foresight-simulation-always-failed/17651/4 "2021-03-18T00:41:09Z")

</div>

Thank you so much! There are more to learn in deterministic models!

---

<div class="post-metadata">

**Author:** ![dqcote](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@dqcote](https://forum.dynare.org/u/dqcote)\
**Post date:** [5 May 2023 14:03 UTC](https://forum.dynare.org/t/perfect-foresight-simulation-always-failed/17651/5 "2023-05-05T14:03:25Z")

</div>

Does `options_.debug=1` **always** flag this type of issue?

---

<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:** [5 May 2023 14:13 UTC](https://forum.dynare.org/t/perfect-foresight-simulation-always-failed/17651/6 "2023-05-05T14:13:22Z")

</div>

It should.

---

<div class="post-metadata">

**Author:** ![dqcote](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@dqcote](https://forum.dynare.org/u/dqcote)\
**Post date:** [5 May 2023 14:51 UTC](https://forum.dynare.org/t/perfect-foresight-simulation-always-failed/17651/7 "2023-05-05T14:51:22Z")

</div>

In a different model I am facing the same problem (Failed to solve perfect foresight model). Basically, the residual does not change at all after a couple of iterations of the solver.

`options_.debug=1` does not provide any information. However, I get the `Matrix is singular to working precision` warning at each iteration as well. Is there a way I can find out where the singularity is coming from?

`model_diagnostics` finds no obvious problems either.

I have already rewritten the model equations so they don’t rely on auxiliary definitions at all. I have also tried with different shock sizes.

Thanks a lot.

---

<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:** [5 May 2023 15:24 UTC](https://forum.dynare.org/t/perfect-foresight-simulation-always-failed/17651/8 "2023-05-05T15:24:06Z")

</div>

It’shard to tell without access to the codes. Is the model BK stable?

---

<div class="post-metadata">

**Author:** ![dqcote](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@dqcote](https://forum.dynare.org/u/dqcote)\
**Post date:** [11 May 2023 16:42 UTC](https://forum.dynare.org/t/perfect-foresight-simulation-always-failed/17651/9 "2023-05-11T16:42:51Z")

</div>

Yes, the model is BK stable. In case it helps, when I try with `linear_approximation` I obtain the following:

MODEL SIMULATION:

The dynamic Jacobian is singular. The problem derives from:

Largest absolute residual at iteration 1: 0.003

Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND =  
7.011148e-21.

> In sim1\_linear (line 208)  
> In perfect\_foresight\_solver\_core (line 97)  
> In perfect\_foresight\_solver (line 63)  
> In model\_PF.driver (line 1681)  
> In dynare (line 281)  
> In PF\_TEST (line 7)

## Iter: 1, Initial err. = 0.0025, err. = 386.959, time = 0.083

Perfect foresight solution found.

Thanks.

---

<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:** [11 May 2023 18:40 UTC](https://forum.dynare.org/t/perfect-foresight-simulation-always-failed/17651/10 "2023-05-11T18:40:55Z")

</div>

Then it must be something else.
