Sunday, March 13, 2016

SIM5: summary and to-do

This post is meant to be a stand-in for an eventual fully fleshed out post tying my previous posts on this subject together: namely SIM, SIM2, Answer for Henry #1, SIM3 and SIM4. A brief summary follows:

SIM: 1st attempt to code up G&L's SIM in a spreadsheet in a discrete time state space format. It worked the 1st time with the sample period (Ts) set to 1. H is the sole state variable producing the system dynamics, and at first I interpreted the exogenous input G and the outputs (system "measurements") all as rates in dollars/original-period (and I named the original period (Ts1) a "year" for convenience). The outputs are Y, T, YD and C. Ramanan, commenting on Jason's blog, suggested scaling the parameter alpha2 by Ts2/Ts1 to change sample periods, where Ts1 = 1 and Ts2 is the new desired sample period. This didn't work for me at first because I was interpreting G, Y, T, YD and C as rates and I wasn't changing their units to match the new sample period. Naturally Ramanan assumed I would think of these as quantities, not rates, so that their values would scale with the sample period (which amounts to keeping them as rates and changing their units to dollars per Ts2). Once I sorted that out, I redid the spreadsheet and found that scaling alpha2 by Ts2/Ts1 was an OK approximation, but not perfect: the system time constant (Tc) does change, and the reason is G&L are doing one of the following:
  1. Assuming the "compounding times" always precisely coincide with the sample times, so Tc does change when the sample period changes (because it means a different compounding rate).
  2. Approximating continuous compounding by approximating exp(-t/Tc) as 1 - t/Tc
Another problem with SIM the way G&L formulate it is that you cannot select a Ts2 >= Ts1*alpha1/alpha2_orig (where alpha2_orig is the the original value of alpha2 before scaling it by Ts2/Ts1). You can set it to what you like of course, but they specify in their text that 0 < theta < 1 and that 0 < alpha2 < alpha1 < 1. This means starting with their default values for the parameters, Ts2 must be < 1.5*Ts1 = 1.5 periods. You can actually make it bigger (of course) and it works OK up until about Ts2 = 6.5... then the resulting A parameter in the difference equation goes 0 and then it goes negative as Ts2 > 6.5. This results in wild oscillations in the curves as Ts2 gets up to values like 10 or 20. But following G&L's restrictions, then varying Ts2 over (0,1.5) brings a variation in time constant (Tc) of about 14% (from a low of about  5.7 at Ts2 = 1.5 to a high of 6.5 as Ts2 -> 0). The default value of Tc with Ts = 1 is about 6 "periods."

SIM2: I started this one when I 1st apparently failed to get SIM to be sample period invariant by using Ramanan's method of scaling alpha2 by Ts2/Ts1. Essentially what I did here was find the equivalent continuous time system and use the time constant from that to find the discrete time state space parameters A and B for Ts2. This amounted to replacing the approximation for exp(-t/Tc) with the exact value. This version did not require changing my interpretation of G, Y, T, YD and C as rates expressed in dollars/(period=Ts1), so naturally I was excited to see that the curves were indeed invariant to the sample period. However, looking at it later and calculating Jason's Γ  = ΔH/(G - T) I realized that Γ = 1 only when Ts=1. So essentially, SIM2 violates stock-flow consistency (SFC), even though H, G, Y, T, YD and C are all sample period invariant. However, I did add an alternative means of calculating the constants CT and DT which are used to produce T: namely T[n] = CT*H[n-1] + DT*G[n]. Using these alternative expressions produces a different T curve, which I added to the plot. It is precisely the same as the original T curve when Ts=1, but differs slightly when Ts ≠ 1. In addition SIM2 has no upper bound on Ts2 (no oscillation problem as in SIM1).

Answer for Henry #1: Not much to say here, except that this is where I introduce  block diagrams of both the discrete and continuous time system equivalents. Note that my naming convention for G and T (and I suppose for the other outputs Y, YD and C) are a bit confusing on the plots up through this one, and in my block diagram as well. That's because of the confusion about G being dollars, or dollars per period=Ts1 or dollars per period=Ts2, etc.

SIM3: Here I was determined to get the best of both SIM and SIM2: Namely match G&L's SIM with Ts=1, have sample period invariance and satisfy all of G&L's equations over any sample period (or more generally, between any two arbitrary sample times). I accomplished that goal with some qualifications: 1st I restrict Gdot (Gdot a rate of government spending measured in constant units of dollars/(period=Ts1) to be a scaled unit step function: 0 for t < 0, and constant for all t >= 0. Then H takes the form of Hss*(1 - exp(-t/Tc)), where Hss = steady state H = Gdot*B/(1-A). G (now interpreted as the sum from -inf to t = sum from 0 to t of all government spending) is 0 for t < 0 and Gdot*t for t >= 0. Thus I define T (the sum from 0 to t of all taxes collected) as T = G - H. This has the effect of both making the system sample period invariant and forcing it to satisfy G&L's accounting equation (i.e. Γ = 1, once you  use the right version of G and T (the confusion with symbols in this post continues)), and (of course) forcing it to agree with G&L when Ts=1. However, I didn't check Y, YD or C, only H, G and T.

SIM4: This post does not include a spreadsheet, but it does document the math required to extend SIM3 to accept more general G functions and to turn it back into an iterative state space discrete time difference equation. In this post I explicitly show the connection between the continuous time formulas and discrete time formulas for a broad class of G functions (really g functions). Also, I make an attempt to provide consistent variable names: both internally consistent and consistent with G&L. Namely, G and T become quantities again, measured in dollars, and thus dependent on the sample period length. The math is mostly done in relation to g = sum total of all dollars spent by the government, and it's derivative gdot (or g') (and a lower and upper case gamma (γ and Γ respectively), and some Dirac delta components of g', which I refer to by gm, giving us the ability to introduce step functions into g). Like SIM3, this model does not attempt to sort out or check what Y, YD or C are or ensure I have the correct expressions for them so that they always satisfy G&L's accounting equations.


SIM5: Which brings us to the point of this post. The bit I'm missing in all but the non-sample-period-invariant SIM above is ensuring that ALL of G&L's accounting equations are always satisfied between any arbitrary sample times. I can get ΔH = G - T to be satisfied in all of them (using T-alt in SIM2), but not necessarily the rest. This got me thinking about a couple of strategies:
  1.  Starting with SIM 1, which guarantees that all G&L's accounting equations will always be satisfied, it occurred to me that I can adjust alpha2 for a new sample period (Ts2) from the original sample period (Ts1=1) in a way other than scaling it by Ts2/Ts1: Given that A1 is the original feedback parameter (see SIM1), I can calculate a new alpha2 like this: alpha2 = (1 - A1^(T1/T2))*(1 - alpha1*(1 - theta))/theta (where alpha1 and theta are the original values). This preserves the time constant (Tc), but probably messes up the steady state value, so I'd need to adjust B1 as well (this change to alpha2 will produce a new A, which I'll call A2). But this may lead to having to change all the "C" and "D" parameters (the former used to scale H[n-1] and the latter used to scale G[n] such that their sum produce the set of outputs Y, T, YD and C). Thus really, this will probably lead to having to adjust alpha1 and theta as well, so that all the accounting equations are always satisfied. [Update 2016.04.22: Just alpha1 actually. See the update this date in SIM6]
  2. Starting with the alternate variables I added to the SIM2 spreadsheet (alpha2alt, T-alt, CTalt and DTalt), I can actually solve for an alpha1alt and theta1alt [Update 2016.04.22: No, this doesn't actually work for solving for alpha1alt and theta1alt]. I have a feeling doing so will result in the same solution as for strategy 1 above. Note that when Ts=1 these alternative values are the same as the regular values. The original intent was to see if I could find a new CT and a new DT when Ts = Ts2 ≠ 1 that would always satisfy ΔH/(G - T) := Jason's Γ = 1. (Note that Jason's Γ Γ in SIM4). I could by starting with T = G - ΔH and solving for CTalt and DTalt. Then I can use the fact that CTalt = alpha2*DTalt when Ts=1 (since when Ts=1, CT = alpha2*DT) to find a new alpha2alt = CTalt/DTalt when Ts ≠ 1. I can then use other expressions in the table of expressions for coefficients in the lower left of the spreadsheet in SIM to solve for thetaalt in terms of alpha1alt, and then to solve for alpha1alt, and back-substitute to find a value for thetaalt. Essentially I'll be finding a new set of parameters to go with the new sample period (Ts2), and thus all G&L's accounting formulas should continue to be satisfied.
  3. It occurred to me that I could adjust theta, alpha1 and alpha2 for a new sample period Ts2 by assuming they are currently representing the effective rate for continuously compounding over the default sample periods Ts1 = 1. G&L's (Ramanan's) procedure of only scaling alpha2 by Ts2/Ts1 is an approximation to this, assuming continuous compounding. They only scale alpha2 because it's the only one multiplying the total accumulated cash (H) in the economy, while both alpha1 and theta scale quantities which only apply that particular sampler period. Thus for example, if Ts2  = 1/4, then they scale alpha2 by (1/4)/1 = 1/4. But if we assume that exp(x*Ts1) = 1+alpha2, then x = log(1+alpha2)/Ts1, and scaling to Ts2 would give alpha2_new = alpha2(Ts2) = alpha2^(Ts2/Ts1)-1. Scaling theta and alpha1 would be slightly different (they'd maintain values much closer to their starting values), but I'm not sure what that would be yet. I don't have a lot of hope that this strategy (strategy 3) will actually work. [Update 2016.04.22: a lack of hope for this strategy is probably a good thing. See SIM6, SIM7, SIM9 and SIM10]
[Update: it's strategy 1. that actually worked. See Update #1 below for more details and a new post demonstrating this]

I did a little more preliminary work on Strategy 2 above and came up with the following:

Using the equation for the original B, I can put thetaalt in terms of alpha1alt:
thetaalt = [(1-Balt) + alpha1alt*(1-Balt)]/(1 + alpha1alt*(1-Balt))

Also this is useful:
1-thetaalt = Balt/(1 + alpha1alt*(1-Balt))

Then using the equation for the original A, I already have alpha2alt (see Strategy 2 above), and replacing thetaalt with the above expression, I get an expression only in alpha1alt, which looks like a quadratic, which I can then solve for alpha1alt and back-substitute into the above to find thetaalt:

(1-Aalt)*(1+alpha1alt)*(1+alpha1alt*(1-Balt)) = alpha2alt*(1-Balt+alpha1alt*(1-Balt))*(1+alpha1alt*(1-Balt))

Then I should have a complete new set of parameters which give me the same time constant Tc (and thus sample period invariance) and because they are a complete new set, simultaneously cause all variables (H, ΔH, G, Y, T, YD and C) to satisfy all G&L's equations. [Update 2016.04.22: Nope]

I'll leave that for another day! 

Note that Brian Romanchuk has been doing some posts on related issues, here and here.

UPDATE #1: March 15, 2016 10:17 PM
Another day has arrived!
Note that what I crossed out above I think had an error in it (or at least it didn't reduce to a quadratic or any other equation that would uniquely determine the parameters, as I'd anticipated). Also I found that theta and alpha1 were not uniquely determined by the equations. But leaving theta as the original value seemed to work best, so I just adjust alpha1 and alpha2 now [Update 2016.04.22: See the SIM6 update of this date about  why "leaving theta as the original" seemed to work best]. The resulting spreadsheet is a direct modification of SIM called SIM6 and it seems to be successful in being sample period invariant and satisfying all G&L's expressions (which I verified by calculated integrated values for Y, T, YD and C for an arbitrary Ts and comparing those with integrated values for Ts=1).


UPDATE #2: March 17, 2016:

SIM6: A modified version of my SIM post which adjusts alpha1 and alpha2 to keep the time constant fixed when the sample period (Ts) changes. Because  SIM obeys all G&L's expressions by it's construction, then so does SIM6. Also it matches G&L's results with their default parameters (alpha1=0.6, alpha2=0.4, theta=0.2, Ts=1). Tables (that you must scroll all the way to the right to see, using the scroll bar along the bottom of the embedded spreadsheet) integrate Y, T, YD and C so that you can compare these integrated (accumulated) values with a static table for which this was done with G&L's default parameters. This allows you to see that indeed all of G&L's expressions must be satisfied.

To-do: I list some things that could be on a to-do list (for SIM7?) in this comment on Brian Romanchuk's blog: (repeated here for convenience):

"Or you could just keep the continuous time model without any extra terms in b. Then you can do an observability and controllability analysis (easy in this case), do pole and zero placement with a fixed feedback law, add noise to the analysis, design a Kalman filter, design a time varying optimal feedback control law for various objective functions or design a robust control law to achieve a minimum performance level over a whole set of plants that could exist due to our uncertainty about the plant. These things could be done in either discrete or continuous time, but the model is simpler in continuous time"

No comments :

Post a Comment