I am trying to find the yield of a bond given the bond price, coupon, and semiannual three yr bond using newtons method. The value for B in the output should about equal 101. I have checked everywhere for errors but cant find any. Any ideas?
And yes I am aware about my vectors but dont make fun, i dont know how to automate them.

// bondyield.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>

using namespace std;

double b_est(double x);
double f_deriv(double y);

int _tmain(int argc, _TCHAR* argv[])
{
	double B = 101.0;
	double n = 6.0;

	vector<double> t_cash_flow;
	double arrayOne[]={0.5, 1.0, 1.5, 2.0, 2.5, 3.0};
	t_cash_flow.assign(arrayOne, arrayOne+6);

	vector<double> v_cash_flow;
	double arrayTwo[]={2, 2, 2, 2, 2, 102};
	v_cash_flow.assign(arrayTwo, arrayTwo+6);

	double tol = 10E-6;
	double x0 = 0.03;
	double xnew = x0;
	double xold = x0 - 1.0;
	
	while(abs(xnew - xold) > tol)
	{
		xold = xnew;		
		xnew = xnew + ((b_est(xold) - B)/f_deriv(xold));
	}
	cout << setprecision (12) << xnew << endl;
	double Bp = 0.0;
	double D = 0.0;
	double C = 0.0;
	double disc[6];
	for(int i=0; i<n; ++i)
	{
		disc[i] = exp(-t_cash_flow[i]*xnew);
		Bp = Bp + v_cash_flow[i]*disc[i];
		D = D + t_cash_flow[i]*v_cash_flow[i]*disc[i];
		C = C + pow(t_cash_flow[i], 2)*v_cash_flow[i]*disc[i];
	}
	D = D/Bp;
	C = C/Bp;
	
	cout << setprecision (16) << "Bond Price= " << Bp << endl;
	cout << setprecision (16) << "Duration= " << D << endl;
	cout << setprecision (16) << "Convexity= " << C << endl << endl;


	system ("pause");

	return 0;
}

double b_est(double x)
{	
	double n = 6.0;

	vector<double> t_cash_flow;
	double arrayOne[]={0.5, 1.0, 1.5, 2.0, 2.5, 3.0};
	t_cash_flow.assign(arrayOne, arrayOne+6);

	vector<double> v_cash_flow;
	double arrayTwo[]={2, 2, 2, 2, 2, 102};
	v_cash_flow.assign(arrayTwo, arrayTwo+6);

	double best;
	for(int i=0; i<n; ++i)
		{ 
			best = v_cash_flow[i]*exp(-x*t_cash_flow[i]);
		}
	return best;
}

double f_deriv(double y)
{
	double n = 6.0;

	vector<double> t_cash_flow;
	double arrayOne[]={0.5, 1.0, 1.5, 2.0, 2.5, 3.0};
	t_cash_flow.assign(arrayOne, arrayOne+6);

	vector<double> v_cash_flow;
	double arrayTwo[]={2, 2, 2, 2, 2, 102};
	v_cash_flow.assign(arrayTwo, arrayTwo+6);

	double fderiv;
	for(int i=0; i<n; ++i)
		{ 
			fderiv = t_cash_flow[i]*v_cash_flow[i]*exp(-y*t_cash_flow[i]);
		}
	return fderiv;
}

But are you also aware of the inherent problems of floating point?
http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

Your code may be algorithmically correct, but from a computational view, the accumulation of inaccuracies is destroying your results.

Look for places where you're trying to use a very large number with a very small number.
Eg.
1 + 0.00000000001
could still be just 1 when you're done, even if you put it in a loop.
The tiny fraction is of no significance to the larger value, so it just disappears.

Over time, you might expect your 1 to grow in size towards 2 say.
But it will forever be stuck at 1 as a float.

OK but I dont have a thing defined as float. Everything is double except for for(int i=0.....

double stands for double precision floating point number in C++

Well then I am using float but I don't think its the problem because I have similar programs using double and they work fine.

> double n = 6.0;
Why are you using a double to limit the range of an integer for loop?

> but I don't think its the problem because I have similar programs using double and they work fine.
Yes, it's a common argument. Deeply flawed, but common.
"I made program x work therefore program y must be flawless" doesn't wash. We often see code here which works purely by luck, rather than down to any skill of the programmer.


Start using a debugger.
- step the code one line at a time,
- do the same calculations on paper (or on a calculator)
- examine the program variables involved, and your hand results
- where they differ, then work out which approach has the bug.

How do you step the code one line at a time? I dont entirely understand what that means.

I tried changing the n to int, didn't change anytthing.

Found the problem, the b_best function wasn't set to summate. needed to be best = best + ...

Thanks everyone for your help

Better check out line 98 as well. It looks very similar to the bug you have identified.

commented: It's the catch of the day :) +19

Yes you are right, it was right to 10E-5 now its dead on. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.