Group Abstract Group Abstract

Message Boards Message Boards

0
|
479 Views
|
9 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Syntax error using RecurrenceTable function simulatin Stock-Flow Consistent model

See attached NB. A manipulate module using the RecurrenceTable function detects a syntax error which I have been unable to see. More eyes on what might be obvious to others might help.

POSTED BY: Lawrence Winkler
9 Replies

Thanks to all for the help. My code is now working nicely. I plan to make some changes to the equations by converting them to equating the left sides == 0, and see if RecurrenceTable can handle it.

There is one inconvenience which I would like to eliminate. When I change the parameters of the simulation, the plot reverts back to choosing the first 5 variables as selected rather than remembering the last variables selected for display. I'm not clear on how that can be accomplished. I'm not even sure how to prompt AI to give me some hints.

Attachments:
POSTED BY: Lawrence Winkler

Here is my attempt:

DynamicModule[{params, eqns, constraints, inits, vars, sim,
  Y, YD, Td, Ts, Cd, Cs, Hh, Hs, Gd, Gs, WBd, WBs, c1, c2,
  consumeRate, consumeWealth, taxRate, t, g1, g2, g3},
 vars = {Y, YD, Td, Ts, Cd, Cs, Hh, Hs, Gd, Gs, WBd, WBs};
 Manipulate[
  eqns = {
    Gd[t] == 
     Piecewise[{{g1, 1 <= t < 5}, {g2, 5 <= t < 10}, {g3, t >= 10}}],
    Y[t] == Cs[t] + Gs[t], Y[t] == WBd[t],
    Hs[t] == Gd[t] - Td[t] + Hs[t - 1],
    Hh[t] == YD[t] - Cd[t] + Hh[t - 1],
    Cd[t] == consumeRate YD[t] + consumeWealth Hh[t - 1],
    Td[t] == taxRate WBs[t], YD[t] == WBs[t] - Ts[t]};
  constraints =
    {Cs[t] == Cd[t], Gs[t] == Gd[t],
    Ts[t] == Td[t], WBs[t] == WBd[t]};
  inits = {Y[0] == 0, Cs[0] == 0, Cd[0] == 0,
    Ts[0] == 0, Td[0] == 0, YD[0] == 0,
    Gd[0] == 0, Gs[0] == 0, WBs[0] == 0,
    WBd[0] == 0, Hh[0] == 0, Hs[0] == 0};
  sim = RecurrenceTable[
    Join[constraints, eqns, inits],
         vars,
         {t, 1, 10}];
  ListLinePlot[sim[[All, {1, 2, 3, 5, 7, 8, 9}]] // Transpose, 
   AxesLabel -> {"Time", "Value"}, 
   PlotLegends -> {"Y", "YD", "Td", "Cd", "Hh", "Hs", "Gd"}],
  {g1, {20, 30}}, {g2, {20}}, {g3, {20}},
  {{taxRate, 0.2}, 0.1, 1.0, .1, Appearance -> "Labeled"}, 
  {{consumeRate, 0.6}, 0.1, 1.0, .1, Appearance -> "Labeled"}, 
  {{consumeWealth, 0.4}, 0.1, 1.0, .1, Appearance -> "Labeled"},
  ContinuousAction -> False, SynchronousUpdating -> False]]

It seems that Y == YD, so that only 6 curves are showing.

POSTED BY: Gianluca Gorni

Progress in simulating Stock-Flow economic model.

Now however, RecurrenceTable aborts when the number of iterations is 30, but succeeds with 25 iterations. There isn't an indication of what limit has been exceeded.

The process is fairly slow, and given the more complex models promise to need 100 or more equations, handling and estimating the time limits will be necessary.

Attachments:
POSTED BY: Lawrence Winkler

I think that it is not RecurrenceTable that aborts, but Manipulate. I asked the AI help, and it suggested to add to Manipulate the following options:

ContinuousAction -> False, SynchronousUpdating -> False
POSTED BY: Gianluca Gorni
Posted 25 days ago

sanity check

ClearAll[y, yd, td, cd, hs, cs, gs, ts, hh, dHs, dHh];
eqns = {y[t] == cs[t] + gs[t],dHs[t] == hs[t] - hs[t - 1],dHh[t] == hh[t] - hh[t - 1],cs[t] == cd[t],gs[t] ==     gd[t],ts[t] == td[t],cd[t] == c1 * yd[t] + c2 * hh[t - 1],td[t] == \[Theta] * y[t - 1],yd[t] == y[t] - \[Theta] * y[t - 1],y[0] == 0, cs[0] == 0,yd[0] == 0, hh[0] == 0,hs[0] == 0, td[0] == 0, gs[0] == 0,gs[1] == g };(*all your recurrences*)
vars = {y, yd, td, cd, hs, cs, gs, ts, hh, dHs, dHh};
init = {y[0] == 0, cs[0] == 0, yd[0] == 0, hh[0] == 0, hs[0] == 0, td[0] == 0, gs[0] == 0, dHh[0] == 0, gs[1] == 20};
RecurrenceTable[Join[eqns, init], vars, {t, 1, 10}]

Still gives -

There are more dependent variables than equations, so the system is underdetermined.

So I rewrote it, think I got it correct (, but hope it helps).

ClearAll[y, taxes, yd, c, h, hg, g];
Manipulate[
 Module[{sim},
  sim =
   RecurrenceTable[{
     g[t] == Piecewise[{{g1, t <= switchTime}, {g2, t > switchTime}}],
     taxes[t] == taxRate y[t],
     yd[t] == y[t] - taxes[t],
     c[t] == income yd[t] + wealth h[t - 1],
     y[t] == c[t] + g[t],
     h[t] == h[t - 1] + yd[t] - c[t],
     hg[t] == hg[t - 1] + g[t] - taxes[t],
     h[0] == 0,
     hg[0] == 0},
    {y, c, yd, taxes, h, hg, g},
    {t, 1, periods}];
  ListLinePlot[
   sim[[All, #]] & /@ {1, 2, 5, 7},
   PlotLegends -> {"y", "c", "h", "g"},
   PlotRange -> All,
   AxesLabel -> {"Time", "Value"},
   ImageSize -> Large]]
 , {{g1, 20, "initial g"}, 1, 30, 1, Appearance -> "Labeled"},
      {{g2, 30, "new g"}, 1, 50, 1, Appearance -> "Labeled"},
      {{taxRate, 0.2, "tax rate \[Theta]"}, 0., 1., 0.05, 
  Appearance -> "Labeled"},
      {{income, 0.6, "income \[Alpha]1"}, 0., 0.99, 0.05, 
  Appearance -> "Labeled"},
      {{wealth, 0.4, "wealth \[Alpha]2"}, 0., 1., 0.05, 
  Appearance -> "Labeled"},
      {{switchTime, 20, "switch time"}, 1, 49, 1, 
  Appearance -> "Labeled"}, {{periods, 50, "periods"}, 20, 100, 1, 
  Appearance -> "Labeled"}]
POSTED BY: Karl Fraser

I fixed most errors but still have issues.

The key is: I want to use RecurrenceTable (and other functions) to simulate the models in Monetary Economics by Wynne Godley, et al. These models define what is called Stock-flow consistent models. The Levy economic institute, part of Bard College, which is associated with Columbia University continue to research this area.

From what I've seen, there are packaged solutions in R (and some other modeling systems like EViews) simulate these models using the typical array and do loop constructions which, in my mind, hide the basic models behind these constructs, or are black-boxes. It seems like Mathematica should be able to simulate these models with relative ease using RecurrenceTable.

I've cleaned up some of the coding thus far, but still mathematica is unhappy with the result.

In this latest interation, Mathematica results in dumping some RecurrenceTable without generating a computation. (and giving no reason for its failure).

Attachments:
POSTED BY: Lawrence Winkler

Thanks to all. I haven't been using Show Expression. This gives me some critical information I haven't used before, not only this issue but some from past modules where I fat-fingered some entry.

POSTED BY: Lawrence Winkler
Posted 27 days ago

Hello,

Looking VERY closely at the ClearAll line, one can see that comma after gd is smaller than the other commas. If you use Show Expression from Cell Menu, you see the offending section as:

RowBox[{
    SubscriptBox["g", 
     RowBox[{"d", ",", " "}]],

Clearly there is a corruption after the gs Subscript and in the gd Subscript.

The small comma is part of the Subscript, so delete it and add the comma after the Subscript.

POSTED BY: Karl Fraser

You use g as a function in RecurrenceTable and as a number in Manipulate. I tried this:

RecurrenceTable[{y[t] == Subscript[c, s][t] + Subscript[g, s][t], 
    Subscript[\[CapitalDelta]h, s][t] == 
     Subscript[h, s][t] - Subscript[h, s][t - 1], 
    Subscript[\[CapitalDelta]h, h][t] == 
     Subscript[h, h][t] - Subscript[h, h][t - 1], 
    Subscript[c, s][t] == Subscript[c, d][t], 
    Subscript[g, s][t] == Subscript[g, d][t], 
    Subscript[c, d][t] == c1 yd[t] + c2 Subscript[h, h][t - 1], 
    td[t] == \[Theta] y[t - 1], yd[t] == y[t] - \[Theta] y[t - 1], 
    y[0] == 0, Subscript[c, s][0] == 0, yd[0] == 0, 
    Subscript[h, h][0] == 0, Subscript[h, s][0] == 0, 
    td[0] == 0} /. {Subscript[g, s] -> g, \[Theta] -> taxRate, 
    c1 -> consumeRate, c2 -> overspend} /. {taxRate -> 1, 
   consumeRate -> 1, overspend -> 1}, {g, td, y, yd, Subscript[c, d], 
  Subscript[c, s], Subscript[g, d], Subscript[h, h], Subscript[h, s], 
  Subscript[\[CapitalDelta]h, h], Subscript[\[CapitalDelta]h, s]},
 {t, 1, 50}]

and it gives this warning:

RecurrenceTable::underdet: There are more dependent variables than equations, so the system is underdetermined.
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard