Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

6 Question 6 6.1 Up to now, all the functions we have written are stand-alone fu

ID: 3884333 • Letter: 6

Question

6 Question 6 6.1 Up to now, all the functions we have written are stand-alone functions. Hence both the functions price from.yield..) and yield from price(..) carry cumbersome baggage, in the sense of input arguments (F,c, n) for the face, coupon and number of coupons, respectively. The values of these parameters are not relevant to a bisection algorithm, and do not change during iteration Nevertheless, they must be passed as inputs to yield from price(.., so that they can be passed to price from.yield. to value the bond 6.2 This suggests the use of object-oriented programming. We can encapsulate the values of (F,c,n) in a "Bond" object. The functions price from yield) and yield from price...) can be methods of that Bond class we shall also support bonds which are not newly issued, so to 0. 6.3 Let us design a simple Bond class. What are its internal data members? Clearly we must have the face. We also need the maturity. (In real life the maturity would be a date, but here we store it as a double, measured in years.) To make things more interesting, let us consider a bond with variable coupons ci, paid on dates (tunes) ti, i = 1, 2, , n. Both ci and ti are of type double Hence our data members are: 1. double face; 2. double maturity; 3. std:vector coupon ; std:: vector coupon-date; 4. You can also include a data member of type int to store the number of coupons, if you wish 6.4 Because the coupons are variable, and also to 0, the formula for the bond price B in terms of the yield y is more complicated Cn (1 + 2(ti-to) 2lta " (1 +2(12 . Here T is the maturity date. In all test examples of coupon bearing bonds, the last coupon date tn will be equal to T. .However, the Bond class must also work for a ZERO COUPON BOND The Bond class must function even if there are no coupons c and dates t Hence we cannot assume T = tn, because tn might not exist

Explanation / Answer

C++ CODE FOR BOND CLASS:

class Bond

{

                double face, maturity;

                vector<double> coupon;

                vector<double> coupon_date;

               

                public:

                                Bond(double F, double T)

                                {

                                                face = F;

                                                maturity = T;

                                }

                               

                                ~Bond()               {coupon.clear();coupon_date.clear();}

                               

                                void set_flows(int n, const double c[],const double d[]);

                               

                                void price(double yield, double t0, double &B) const;

                               

                                int yield(double target, double t0, double tol, int max_iter, double &y, int &num_iter) const;

};

                                void Bond::set_flows(int n, const double c[],const double d[])

                                {

                                                for(int i=0;i<n;i++)

                                                {

                                                                coupon[i]=c[i];

                                                                coupon_date[i]=d[i];

                                                }

                                }

                               

                                void Bond::price(double yield, double t0, double &B) const

                                {

                                                double s;

                                                int n = coupon.size();

                                                for(int i=0;i<n;i++)

                                                {

                                                                s+=( coupon[i]/pow((1+yield/2.0),2*(coupon_date[i]-t0)) );

                                                }

                                               

                                                s+=( face/pow((1+yield/2.0),2*(maturity-t0)) );

                                               

                                                B=s;

                                }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote