# Re: st: RE: Beginning Programming Advice

 From "Thomas Jacobs" <[email protected]> To [email protected] Subject Re: st: RE: Beginning Programming Advice Date Mon, 15 Oct 2007 08:28:48 -0500

```Dear Nick,

Thanks for the reply.  I was purposely vague on the problem definition
because it is the Stata aspects and not the iterative problem that I
appears that I would be best to somehow load a large matrix and
process each observation with a program and not try to use Gen to call
a function.

Here is a more detailed problem definition with two companies and one
year of trading data for each - I did not populate the sample
Accounting values(Acct), Market Equity (MktEq) or Equity Volatility
(EqVol) variables but they would have a value for each company and

Company, TradeDay, Acct1, Acct2, MktEq, EqVol
1               1
1               2
...
1               252
2               1
...
2               252

On each day I am solving the following non-linear equations each of
which has one or more cumulative normal distribution functions
embedded in the right hand side and I have introduced the variables I
need to solve for: Market Value of Assets (MktAssets) and Asset
Volatility (AssetVol):

MktEq = f(Acct1, Acct2, EqVol, MktAssets, AssetVol)

EqVol = f(Acct1, Acct2, MktEq, MktAssets, AssetVol)

I would then have a file or dataset in memory that appears as:

Company, TradeDay, Acct1, Acct2, MktEq, EqVol, MktAssets, AssetVol

for use in econometric modeling.  I hope this makes the problem more
clear.  I can use either Newton's method, a Quasi-Newton method or
Broyden's method to do the iteration depending upon speed of
convergence/computational time constraints.  For those familiar with
the Finance literature this problem comes from Merton's 1974 paper,
"On the Pricing of Corporate Debt: The Risk Structure of Interest
Rates."

I know how to proceed in Matlab, but was hoping to use this problem to
get into Stata programming.  I have reviewed all the manuals but am
still uncertain how to proceed.  Given your reply, I am inclined to
load the data as I do now, define a large matrix and partially fill it
(column-wise) with the loaded data, then write a program that steps
through each row of the matrix and iteratively solves for the two
variables to populate the remainder of the matrix and either generate
variables from the completed matrix or else perform the econometric
tests on the matrix directly when it is completed.  Whether or not
this is a reasonable approach, I would appreciate any direction as I
am struggling how to define the Stata aspects of the problem.

Sincerely,

Tom
On 10/15/07, Nick Cox <[email protected]> wrote:
> You want definite advice on a fuzzily-stated problem.
>
> That's difficult.
>
> No one except Stata developers can write
> functions that can be called in -generate-
> statements.
>
> I get the impression that you want to produce
> a new variable. Each value of the new variable
> is a function of existing variables. The basic
> calculation is iterative.
>
> It is likely that such a calculation would
> have to be done observation by observation
> (but to me by no means certain).
>
> Whether you write a program to do this
> for individual observations, and then
> loop over observations calling this program
> repeatedly, or whether your program does
> everything, is very difficult to advise on
> without details.
>
> If you say precisely what you want, someone
> may have programmed it already.
>
> Nick
> [email protected]
>
> Thomas Jacobs
>
> > I have the following problem that I am attempting to program in Stata
> > and I am a newbie:
> >
> > I have a collection of companies with trading day data composed of
> > accounting information, market equity values, and equity volatility
> > values which I currently insheet into Stata.  I need to write a
> > routine to perform a 2 variable in 2 unknown newton-raphson or
> > equivalent procedure to iteratively solve for asset value and
> > volatility using non-linear equations.  I know how to produce the
> > routine to do so in a pseudo-code sense, where I am stuck is how to
> > proceed in Stata.
> >
> > Should I be attempting to write a function that I will then call in a
> > generate command so that every company-tradeday instance will have
> > populated values?  If so, do I need to use a function to produce a
> > matrix to bring back multiple values from the function (the target
> > asset value and volatility as well as any other information such as
> > remaining error or iterations completed)?   If so would I then
> > generate a matrix variable which I would set equal to the output of
> > this function?
> >
> > Is there a better way to proceed?  Can this be done in a do file as I
> > bring the raw data in rather than operating on the data in memory
> > after using the insheet command?  Any suggestions would be greatly
> > appreciated.
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

--
Thomas Jacobs
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```