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

st: Internal rate of return calculation in Mata

 From Tanja Berg To statalist@hsphsun2.harvard.edu Subject st: Internal rate of return calculation in Mata Date Wed, 23 May 2012 12:09:04 +0200

```Hi,

I really need your help again!
I want to calculate spreads for bonds in Stata.

For each bond I have quarterly data from 2005 until 2011 in the following form

Bond		Date		Price		Cashflow1		Cashflow2		CashflowX		Maturity1		Maturity2		MaturityX		Spot1		Spot2		SpotX
1			31.03.2005	101,3		4,5				4,5				104,5			0,3				1,3				2,3				2,3			2,6			2,8
1			30.06.2005	101,2		4,5				4,5				104,5			0,29			1,29			2,29			2,3			2,55		2,75
..
2			31.03.2005	...
2			30.06.2005	...
..

The Cashflows are the coupon payments each year and the last Cashflow is the coupon payment including the nominal value of the bond.
Maturity defines the time in years until the next Cashflow.
And Spot defines the spot rate which is calculated based on the Svensson parameters dependant on the maturity.

The bond with the longest maturity lasts 16 years. So I have Cashflows from 1 until 16, Maturities from 1 until 16 and Spot rates from 1 until 16.

Now I want to calculate the spread for each bond and each observation, which is defined by the following formula:

How can I find the spread which minimizes the error in this calculation??

I think I have to solve this by using Mata and the mm_root function?! But I am not exactly sure how to do this.

I understood that I first have to start Mata in Stata. But how do I get all the data I need into Mata? Over the putmata command?
And then, how do I define the vectors?

I have really no clue about the syntax to solve this problem.

I hope someone can help!

Thanks!!

*
*   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/
```