Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: Iterative solving of an equation


From   "David Scheuer" <[email protected]>
To   [email protected]
Subject   Re: st: RE: Iterative solving of an equation
Date   Mon, 17 May 2010 12:42:40 +0200

Hello Austin
hello everybody

I took the advice from Austin and tried to implement the model.

As I use st_view, I get the error message 3500  invalid Stata variable name
and <istmt>:     -  function returned error.

. mata
------------------------------------------------- mata (type end to exit) ---------------------------------------------------------
: v=J(1,1,.)

: st_view(v,., "p_0 bps_0 eps_1 eps_2 eps_3 eps_4 eps_5 bps_1 bps_2 bps_3 bps_4 g_CT r_CT")
               st_view():  3500  invalid Stata variable name
                 <istmt>:     -  function returned error

***The following code is:***

function y(a,b,c,d,e,f,g,h,i,j,k,l,m) {
	return(-a+b+(c-m*b)/(1+m)+(d-m*h)/(1+m)^2+(e-m*i)/(1+m)^3+(f-m*j)/(1+m)^4+((g-m*k)*(1+l))/((m-k)*(1+m)^5))
}

for (i=1;i<=rows(v);i++) {
	r=mm_root(r_CT=.,&y(),0,1,1e-9,1000,v[i,2],
v[i,3],v[i,4],v[i,5],v[i,6],v[i,7],v[i,8],v[i,9],v[i,10],v[i,11],v[i,12],v[i,13])
	v[i,1]=r_CT
}
end
l, noo


Do you see how I could fix it? 
Or would you recommend the approach with nl or optimize?

I would appreciate any help.
Regards,
David



-------- Original-Nachricht --------
> Datum: Tue, 20 Apr 2010 17:02:15 -0400
> Von: Austin Nichols <[email protected]>
> An: [email protected]
> Betreff: Re: st: RE: Iterative solving of an equation

> David --
> You could just do some algebra and write out the formula for the solutions
> (I'm guessing you have two complex roots and one positive real root for
> all these problems), or loop over obs in Mata:
> 
> ssc inst moremata
> clear all
> set obs 10
> g Price_0 = 100
> g BookValue_0 = 50
> g EPS_1 = 3
> g BookValue_1 = 52
> g EPS_2 = 4.5
> g g = _n/100
> g x=.
> mata
> v=J(1,1,.)
> st_view(v,.,"x Price_0 BookValue_0 EPS_1 BookValue_1 EPS_2 g")
> function y(x,a,b,c,d,e,f) {
> return(-50*(1+x)^2*(x-f)+(3-50*x)*(1+x)*(x-f)+
> (4-52*x)*(x-.04)+(4.5-54*x)*(1+f))
> }
> for (i=1;i<=rows(v);i++) {
> r=mm_root(x=.,&y(),0,1,1e-9,1000,v[i,2],
> v[i,3],v[i,4],v[i,5],v[i,6],v[i,7])
> v[i,1]=x
> }
> end
> l, noo
> 
> 
> On Tue, Apr 20, 2010 at 8:56 AM, Feiveson, Alan H. (JSC-SK311)
> <[email protected]> wrote:
> > David - One way is to use -nl-
> >
> > See http://www.stata.com/support/faqs/lang/nl.html
> >
> > Al Feiveson
> >
> > -----Original Message-----
> > From: [email protected]
> [mailto:[email protected]] On Behalf Of David Scheuer
> > Sent: Tuesday, April 20, 2010 4:18 AM
> > To: [email protected]
> > Subject: st: Iterative solving of an equation
> >
> > I am rebuilding cost of capital models, which determine cost of capital
> implied as the internal rate of return from a valuation model.
> >
> > For instance, let me assume this is the first line in my dataset and I
> want to solve the following equation.
> >
> > Price_0 = 100
> > BookValue_0 = 50
> > EPS_1 = 3
> > BookValue_1 = 52
> > EPS_2 = 4.5
> > g = 0.04
> > x =?
> >
> >
> 100=50+(3-x*50)/(1+x)+(4-x*52)/((1+x)^2)+((4.5-x*54)*1.04)/((x-0.04)*(1+x)^2)
> >
> > For example, in Excel, I would let the "Solver" find the solution for x.
> > How can I program STATA to find solutions like this for me?
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index