Can I solve a nonlinear equation by Rats?

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
T_FIELD
Posts: 44
Joined: Sat May 15, 2010 8:03 pm

Can I solve a nonlinear equation by Rats?

Unread post by T_FIELD »

Dear all,

Can I solve a nonlinear equation for X like the below without reducing it by hands ?

X = a + sqrt(X+b)

The equation I have to solve is more complex, thus I do not want to compute its reduced form.
(I am not good at GAUSS or MATLAB. I am happy if RATS can do it!)

Any comment are welcome.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by TomDoan »

Use FIND ROOT.

compute a=6.0,b=3.0,x=1.0
nonlin x
find root X-(a+sqrt(X+b))
end
T_FIELD
Posts: 44
Joined: Sat May 15, 2010 8:03 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by T_FIELD »

Dear Tom,

Thank you so much for your prompt reply.
This is the first time I know about FIND instruction.
Could you teach me two more questions?

(1) Actually, what I have to solve is like the following system for Z.

X*(a+X)^(1/3) = Y*(a+Y)^(1/3)
X = b - Z
Y = c + Z

Can I obtain the solution for Z WITHOUT substituting the 2nd and 3rd equations to X and Y in the 1st?

(2) How about a simultaneous system for U and V like:

X*(a+X)^(1/3) = Y*(a+Y)^(1/3)
U = sqrt(Y)
X = U - V
Y = c + V

Thanking in advance for your trouble.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by TomDoan »

First, you want to make sure that you write the exponent so 1/3 isn't an integer divide. FIND can do intermediate calculations, so:

compute a=3.0,b=5.0,c=2.0,x=0.0,y=0.0,z=2.0
nonlin z
find root X*(a+X)^(1.0/3)-(Y*(a+Y)^(1.0/3))
comp X = b - Z
comp Y = c + Z
end find

The simultaneous equation is a bit harder, as you have to create a joint expression and minimize it. (FIND ROOT actually minimizes the absolute value of the expression you put in).

nonlin u v
find min abs(X*(a+X)^(1./3)-(Y*(a+Y)^(1./3)))+abs(U-sqrt(Y))
comp X = U - V
comp Y = c + V
end find
T_FIELD
Posts: 44
Joined: Sat May 15, 2010 8:03 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by T_FIELD »

p.s.

One more question.
What I am doing now is simply computing the optimal bond holding in the Euler equation though its tax system is a bit complicated.
I wrote the code below ( by substituting budget constraints, reduced form of consumption, etc.), but I can not obtain any output (no error message as well).
Could you teach me the reason if possible?
Again, thanking in advance.

compute Y0 = 1
compute Y1 = Y0
compute Y2 = Y0
compute lamda = 1
compute delta = 0.3
compute R = 1.2
compute G = 0.2
compute rho = 0.1
compute alpha = 0.5

nonlin B
find(print) root ((1-((G-B)/Y1)-((lamda/2)*(((1/lamda)*(1-sqrt(1-2*lamda*((G-B)/Y1))))**2)))*Y1)**R $
/((1-(((1+rho)*B+G)/Y2)-((lamda/2)*(((1/lamda)*(1-sqrt(1-2*lamda*(((1+rho)*B+G)/Y2))))**2)))*Y2)**R $
= (1+(lamda*((1/lamda)*(1-sqrt(1-2*lamda*((G-B)/Y1))))/sqrt(1-2*lamda*((G-B)/Y1)))) $
/(1+(lamda*((1/lamda)*(1-sqrt(1-2*lamda*(((1+rho)*B+G)/Y2))))/sqrt(1-2*lamda*(((1+rho)*B+G)/Y2))))
T_FIELD
Posts: 44
Joined: Sat May 15, 2010 8:03 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by T_FIELD »

Dear Tom,

Thank you so much for your SO prompt reply.
Please ignore my previous post.

I think I am going to reach the goal.
This is my code.

compute Y0 = 1.0
compute Y1 = Y0
compute Y2 = Y0

compute delta = 0.3
compute R = 1.2
compute G = 0.2
compute rho = 0.1
compute alpha = 0.5

nonlin B
find root (c1/c2)^R = (1+d_gamma_2)/(1+d_gamma_1)
compute xi_1 = (G-B)/Y1
compute xi_2 = ((1+rho)*B+G)/Y2
compute t1 = (1/lamda)*(1-sqrt(1.0-2.0*lamda*xi_1))
compute t2 = (1/lamda)*(1-sqrt(1.0-2.0*lamda*xi_2))
compute gamma_1 = (lamda/2)*(t1**2)
compute gamma_2 = (lamda/2)*(t2**2)
compute d_gamma_1 = lamda*t1/sqrt(1.0-2.0*lamda*xi_1)
compute d_gamma_2 = lamda*t2/sqrt(1.0-2.0*lamda*xi_2)
compute c1 = (1-xi_1-gamma_1)*Y1
compute c2 = (1-xi_2-gamma_2)*Y2
end find

Bu i got the following message:

## SX11. Identifier C1 is Not Recognizable. Incorrect Option Field or Parameter Order?
>>>>find root (c1/c<<<<
Is it possible you meant
Y1
If the name isn't mistyped, it's possible that you have a poorly formatted instruction
Common errors are
* a space before the ( in an option field
* a missing space before = in a SET or FRML
* a missing $ at the end of a long line which continues to the next

Could you teach me why?
I really appreciate your kindness.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by TomDoan »

Because you never defined C1 (or C2). That's what the message is telling you.
T_FIELD
Posts: 44
Joined: Sat May 15, 2010 8:03 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by T_FIELD »

Thank you for your reply.

>Because you never defined C1 (or C2). That's what the message is telling you.

I wrote:

compute c2 = (1-xi_2-gamma_2)*Y2

at the end of Find-End loop.
Do you mean the order of my code is wrong?
Or, simply I should add,

compute C1 = 0.0
compute C2 = 0.0

before FIND ?
T_FIELD
Posts: 44
Joined: Sat May 15, 2010 8:03 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by T_FIELD »

Dear Tom,

Thanks to your warm kindness, I finally obtain the solution.
My collections are as follows.

(1) I simply add
compute C1 = 1.0
etc..

(2) I modify

compute gamma_1 = (lamda/2)*(t1**2)

to

compute gamma_2 = (lamda/2)*(t2^2.0)

Again, I deeply appreciate your kindness.

Sincerely,
T_FIELD
Posts: 44
Joined: Sat May 15, 2010 8:03 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by T_FIELD »

Dear Tom,

Thank you for your kind suggestion yesterday.
Unfortunately, I face the next priblem.

I am now computeing a simple optimal borrowing problem of a governmnet with 2 period, CES utirity, like:

compute Y1=1.0, Y2=1.0
nonlin B
find max (1/1-R)*c1^(1-R)+(1/(1+r))*(1/1-R)*c2^(1-R)
compute T_1 = G - B
compute T_2 = (1+r)*B + G
compute c1 = Y1-T_1
compute c2 = Y2-T_2
end find

As the results, I can get B=0.
However, B<>0 when I use the Euler quation like:

nonlin B
find root (c2/c1)^R = 1
compute T_1 = G - B
compute T_2 = (1+r)*B + G
compute c1 = Y1-T_1
compute c2 = Y2-T_2
end find

The result is:

Variable Coeff
**********************************************
1. B 0.7272753906


Could you teach me what I mistake?
Thanking in advance for your trouble.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by TomDoan »

find root (c2/c1)^R = 1

should be

find root (c2/c1)^R-1
T_FIELD
Posts: 44
Joined: Sat May 15, 2010 8:03 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by T_FIELD »

Dear Tom,

It seems that I need a bit more time to use FIND well....
Anyway, thank you so much for your kind assistance!

Sincerely,
T_FIELD
Posts: 44
Joined: Sat May 15, 2010 8:03 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by T_FIELD »

Dear Tom,

I'm terribly sorry to bother you over and over again.

Could you teach me if it is possible to maximize a expected value like below ?

max log(C1) + S log(C2+e) f(e) de
s.t. C1 + 0.9*C2 = 1 where e ~ uniform(-1.0, 1.0).

( "S" above means integral here.)

Yours Sincerely.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by TomDoan »

You would have to use FIND MAX and use quadrature to approximate the integral (such as with the %ISIMPSON function).
T_FIELD
Posts: 44
Joined: Sat May 15, 2010 8:03 pm

Re: Can I solve a nonlinear equation by Rats?

Unread post by T_FIELD »

Dear Tom,

Thank you so much for your comments on December 19, 2017.
I am still working on this topic.

This time, I attach my code. My question is simple, but let me first explain what I am doing.

On this code:
The attached is a code to replicate Aizenman et al (2000) which investigates a relationship between an income shock and an optimal level of the government
bond issues. They consider a simple two-period setting where an income shock in period 2 is uncertain between [-limit, limit].
As is well-known, if we have a negative income shock in period 1, the government will issue their bonds for consumption smoothing. However, in this model, the borrowing constraint is set depending on tax revenue in period 2 which in turn depends on the expected income shocks in period 2. Therefore, they may unable to accomplish their optimization perfectly. In a polar case, their bond issue will be fixed at a certain level.The attached code is to depict the situation above. When you run it, you can see the figure the bond issue is restricted at a certain level.

My question:
Let me turn to my question. The revenue in period 2 depends on tax efficiency parameter lambda as well. When lambda is, for example, 1.1, this code exactly replicates Aizenman (2000). However, when lambda = 1.55, we see weird horizontal part of the graph which is not emerged in Aizenman (2000) (Run my code, please). Actually, when I changed the method from Simplex to, for example, to generic, I have a different figure again. Thus, I expect the reason why I got the different figure from Aizenman (2000) is my inferior technique for Rats.
So, could you tell me if you have any idea?
thanking in advance for your trouble. I am looking forward to your assistance.

Reference:
Joshua Aizenman, Michael Gavin & Ricardo Hausmann (2000) Optimal tax and debt policy with endogenously imperfect creditworthiness, The Journal of International Trade & Economic Development, 9:4, 367-395, DOI: 10.1080/096381900750056830

To link to this article:
https://doi.org/10.1080/096381900750056830
Attachments
Inquiry.RPF
(5.56 KiB) Downloaded 1029 times
Post Reply