Bug(s) in HeteroAgentStationaryEqm_Case1

I found two bugs in HeteroAgentStationaryEqm_Case1 when using the options constrainAtoB.

In the main script I set

heteroagentoptions.constrainAtoB = {'ttau_0'};
heteroagentoptions.constrainAtoBlimits.ttau_0 = [0.01,5];

then I call HeteroAgentStationaryEqm_Case1 and I get an error at this line:

GEparamsvec0(pp)=(GEparamsvec0(pp)-caliboptions.constrainAtoBlimits(pp,1))/(caliboptions.constrainAtoBlimits(pp,2)-caliboptions.constrainAtoBlimits(pp,1));

Obviously, caliboptions does not exist.
I replaced caliboptions with heteroagentoptions and the routine works. It calls fminsearch and all is fine, until we reach the following block:

if heteroagentoptions.constrainAtoB(pp)==1
            % Constrain parameter to be A to B
            p_eqm_vec(pp)=heteroagentoptions.constrainAtoBlimits(pp,1)+(heteroagentoptions.constrainAtoBlimits(pp,2)-heteroagentoptions.constrainAtoBlimits(pp,1))*GEprices(pp);
            % Note, this parameter will have first been converted to 0 to 1 already, so just need to further make it A to B
            % y=A+(B-A)*x, converts 0-to-1 x, into A-to-B y
        end

The error is now:

Unrecognized function or variable 'GEprices'.

Error in HeteroAgentStationaryEqm_Case1 (line 591)
            p_eqm_vec(pp)=heteroagentoptions.constrainAtoBlimits(pp,1)+(heteroagentoptions.constrainAtoBlimits(pp,2)-heteroagentoptions.constrainAtoBlimits(pp,1))*GEprices(pp);
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This unfortunately I don’t know how to fix :frowning:

1 Like

I have a related question. If I want to impose that the interest rate is positive, can I do

heteroagentoptions.constrainAtoB = {‘r’};
heteroagentoptions.constrainAtoBlimits.ttau_0 = [0.0,inf];
?

By the way, is there a documentation somewhere explaining how to use the toolkit options? Greatly appreciated

1 Like

There are three constraint types:

Set two numbers as bounds:

heteroagentoptions.constrainAtoB={'r'};
heteroagentoptions.constrainAtoBlimits.r=[0,3];

Bound between 0 and 1:
heteroagentoptions.constrain0to1={'r'};

Constrain to be positive:
heteroagentoptions.constrainpositive={'r'};

There is nowhere with a full list of options (although if you open any command, there are often a bunch of lines near the beginning that are setting the default options so you can see what most options are there).

Between the Workshop and the Intro to Life-Cycle Models, how to use most options are covered somewhere.

1 Like

Should be fixed now. Update toolkit to latest github.

I have switched the ‘transform’ and ‘untransform’ used to change the constrained parameters to unconstrained parameters into two subfunctions:

I have switched the Stationary General Eqm commands over to use these subfunctions. Will do the same for the calibration/estimation commands early next week. Makes it all a bit cleaner and neater internally.

PS. End-user does not need these functions for anything, nor to understand them. I mention them here for those interested in what is happening internally.

1 Like

Great, thanks for fixing this! I will test the GE with bound constraints on my Buera Shin 2013 code and report back.

@jake88: I don’t know what model you are working on, but in Aiayagari for example, the interest rate can be negative in equilibrium.

1 Like

I think there is a typo in your post. The second line should read as

heteroagentoptions.constrainAtoBlimits.r = [0,3];

@jake88 I tried setting heteroagentoptions.constrainAtoBlimits.r = [0,Inf]; as you asked but the code goes crazy. If you want to restrict r to be positive, you should use heteroagentoptions.constrainpositive={'r'};

@robertdkirkby: I think constrainAtoBlimits requires the bounds to be finite numbers, right? I ask this since Matlab built-in minimization routines such as fmincon do accept Inf or -Inf as bounds. (I’m not suggesting to implement this feature).

Fixed typo in my post.

Yes, constrainAtoBlimits must both be finite numbers. [Internally, it will switch x to y=(x-A)/(B-A) and then treat y like it does for numbers that are bound from zero to one.]

At some point I want to go through each of the optimization routines one by one and set it so that those with such bounds already implemented are done using their specific setups, while those without their own way to handle bounds use the current transform/untransform approach. But for the moment I have just taken the easy/lazy approach of using the same transform/untransform for all of them.

Also you can always cobble together an [A,Inf] using the current options. Just replace where ‘r’ appears in model with ‘rmod+A’, and then constrain ‘rmod’ to be [0,Inf]. Once the model is solved in terms of ‘rmod’ you can get ‘r’ back from ‘r=rmod+A’. Bit of a pain, but nothing too difficult.

2 Likes

Yes, I just wanted to learn how to set bounds on general equilibrium objects

1 Like