Exercise 4.3

This forum is to discuss the book "C++ design patterns and derivatives pricing."

Exercise 4.3

Postby akbar » Fri Jan 25, 2008 12:52 am

There are various ways to implement the piecewise-constant parameter class asked page 65, but I am curious to know what would be the most efficient approach in terms of code duplication and speed. Can anyone suggest a possible implementation (mj, maybe)?
akbar
 
Posts: 28
Joined: Fri Aug 10, 2007 7:12 pm

Postby mj » Fri Jan 25, 2008 5:03 am

As far as I can recall, I just used an array of times, an array of integrated values and an array of integrated values squared.

The value between two points was then read and off and interpolated as necessary.
mj
Site Admin
 
Posts: 1335
Joined: Fri Jul 27, 2007 7:21 am

Postby akbar » Sat Jan 26, 2008 3:38 pm

Does your constructor take an array of times and an array of values as input (size of times vector = 1 + size of values vector), or does it take values individually?
There can be some consistency problems (time values in growing order for example) to define the function properly. Are they better solved within the class definition (throwing error messages) or at the main() level, e.g. at input (I opted for the second option)?
akbar
 
Posts: 28
Joined: Fri Aug 10, 2007 7:12 pm

Postby mj » Sat Jan 26, 2008 8:31 pm

i generally do checking in the constructor -- this is the place that will always persist with the class

definitely took in two arrays
mj
Site Admin
 
Posts: 1335
Joined: Fri Jul 27, 2007 7:21 am

Re: Exercise 4.3

Postby pzling » Fri Jan 20, 2012 8:31 am

My attempt was just to copy the ParametersConstant class and add another input for time3 in the Integral function so that there are two time domains (and called it ParametersPWConstant).

My problem is that the compiler keeps thinking ParametersPWConstant is an abstract class. As I understand, an abstract class becomes concrete once all its undefined functions are defined. Having done so, I still don't understand why the class is still coming up as abstract.

Thanks for your help.

Header
======
class ParametersPWConstant : public ParametersInner
{
public:
ParametersPWConstant(double constant);
virtual ParametersInner* clone() const;

virtual double Integral(
double time1,
double time2,
double time3) const;
virtual double IntegralSquare(
double time1,
double time2) const;

private:
double Constant;
double ConstantDoubled;
double ConstantSquare;
};

Source
======
ParametersPWConstant::ParametersPWConstant(double constant)
{
Constant = constant;
ConstantDoubled = 2*constant;
ConstantSquare = constant*constant;
}

ParametersInner* ParametersPWConstant::clone() const
{
return new ParametersPWConstant(*this);
}

double ParametersPWConstant::Integral(double time1, double time2, double time3) const
{
return (time2 - time1)*Constant + (time3 - time1)*ConstantDoubled;
}

double ParametersPWConstant::IntegralSquare(double time1, double time2) const
{
return (time2 - time1)*ConstantSquare;
}
pzling
 
Posts: 41
Joined: Sat Jan 14, 2012 8:52 pm

Re: Exercise 4.3

Postby mj » Wed Jan 25, 2012 2:03 am

you have defined

double ParametersPWConstant::Integral(double time1, double time2, double time3) const

the compiler is looking for

double ParametersPWConstant::Integral(double time1, double time2) const

Generally overloading virtual functions is dangerous and best avoided.
mj
Site Admin
 
Posts: 1335
Joined: Fri Jul 27, 2007 7:21 am

Re: Exercise 4.3

Postby pzling » Wed Jan 25, 2012 4:56 am

i see, thanks
pzling
 
Posts: 41
Joined: Sat Jan 14, 2012 8:52 pm


Return to C++ design patterns and derivatives pricing

Who is online

Users browsing this forum: No registered users and 1 guest

cron