Serving the Quantitative Finance Community

 
User avatar
isometry
Topic Author
Posts: 0
Joined: April 14th, 2012, 2:53 pm

C++ design question

March 6th, 2013, 10:31 am

I am writing a class to represent a pool of securities to be used in a portfolio selector. Depending on the objective metric and constraints, the class objects will cache some relevant data for performance optimization. This could be done by defining a base class containing the common elements and its sub classes, each optimized for a particular objective metric/constraints.But, I want the ability to cache data for multiple metrics. Is there a standard/neat way to do this?
 
User avatar
Cuchulainn
Posts: 20256
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ design question

March 6th, 2013, 1:27 pm

Yes.
 
User avatar
isometry
Topic Author
Posts: 0
Joined: April 14th, 2012, 2:53 pm

C++ design question

March 6th, 2013, 1:37 pm

Okay then!
 
User avatar
isometry
Topic Author
Posts: 0
Joined: April 14th, 2012, 2:53 pm

C++ design question

March 6th, 2013, 1:37 pm

Please please elaborate.
 
User avatar
Cuchulainn
Posts: 20256
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ design question

March 6th, 2013, 1:56 pm

QuoteOriginally posted by: isometryPlease please elaborate.You first.
 
User avatar
isometry
Topic Author
Posts: 0
Joined: April 14th, 2012, 2:53 pm

C++ design question

March 6th, 2013, 2:45 pm

class Pool { // Contains data/methods for all available securities};class VaRPool : public Pool { // Saves some index information to make calculation of VaR fast};class CVaRPool : public Pool { // Saves some index information to make calculation of CVaR fast};class Selector { operator()(Pool& pool)};This setup is okay if I want to optimize one particular metric (either VaR or CVaR). What I would like to do is to have a class which is optimized for multiple metrices without coding all permutations.
 
User avatar
Cuchulainn
Posts: 20256
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ design question

March 6th, 2013, 3:39 pm

First of all, inheritance is very bad (it's very 90's). To be honest, it's an OOP knee-jerk reaction.Second, you have sketched a possible solution and not the problem description. I don't want to give a brilliant solution to the wrong problem.
Last edited by Cuchulainn on March 5th, 2013, 11:00 pm, edited 1 time in total.
 
User avatar
DevonFangs
Posts: 0
Joined: November 9th, 2009, 1:49 pm

C++ design question

March 6th, 2013, 3:48 pm

QuoteOriginally posted by: isometryclass Pool { // Contains data/methods for all available securities};class VaRPool : public Pool { // Saves some index information to make calculation of VaR fast};class CVaRPool : public Pool { // Saves some index information to make calculation of CVaR fast};class Selector { operator()(Pool& pool)};This setup is okay if I want to optimize one particular metric (either VaR or CVaR). What I would like to do is to have a class which is optimized for multiple metrices without coding all permutations.Composition? Just put in Pool an interface to all the metrics you might need. I think it's called strategy pattern, or something stupid like that.
 
User avatar
Cuchulainn
Posts: 20256
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ design question

March 6th, 2013, 3:54 pm

QuoteComposition? Just put in Pool an interface to all the metrics you might need. I think it's called strategy pattern, or something stupid like that. Yes. But better is a Visitor pattern.
 
User avatar
isometry
Topic Author
Posts: 0
Joined: April 14th, 2012, 2:53 pm

C++ design question

March 6th, 2013, 4:05 pm

QuoteOriginally posted by: CuchulainnFirst of all, inheritance is very bad (it's very 90's). To be honest, it's an OOP knee-jerk reaction.The are several other classes using the abstract Pool class. For example, a class Portfolio which takes Pool as an argument in constructors. Having different implementations of the Pool class allows me to not change all of the existing system. So I am sort of forced to do it this way.But, what has replaced inheritance in the new millennium?QuoteSecond, you have sketched a possible solution and not the problem description.Hmm, I had myself convinced somehow that this is not going to work. Just realized that it would. One of those moments when you start writing down the problem and the answer shows itself!Thanks for making me write it down.
 
User avatar
isometry
Topic Author
Posts: 0
Joined: April 14th, 2012, 2:53 pm

C++ design question

March 6th, 2013, 4:13 pm

QuoteOriginally posted by: DevonFangsQuoteOriginally posted by: isometryclass Pool { // Contains data/methods for all available securities};class VaRPool : public Pool { // Saves some index information to make calculation of VaR fast};class CVaRPool : public Pool { // Saves some index information to make calculation of CVaR fast};class Selector { operator()(Pool& pool)};This setup is okay if I want to optimize one particular metric (either VaR or CVaR). What I would like to do is to have a class which is optimized for multiple metrices without coding all permutations.Composition? Just put in Pool an interface to all the metrics you might need. I think it's called strategy pattern, or something stupid like that.It works. I did a quick check this way only. But is quiet ugly.
 
User avatar
Cuchulainn
Posts: 20256
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ design question

March 6th, 2013, 4:40 pm

QuoteOne of those moments when you start writing down the problem and the answer shows itself!yep.I have that a lot, aka writer's block:-) QuoteBut, what has replaced inheritance in the new millennium?Composition and this. http://www.wilmott.com/messageview.cfm? ... adid=89792
Last edited by Cuchulainn on March 5th, 2013, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20256
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ design question

March 6th, 2013, 4:49 pm

Visitor pattern http://www.wilmott.com/messageview.cfm? ... SGDBTABLE= So, you really want different views/reports/scenarios(e.g. sensitivity) of your portfolio?
Last edited by Cuchulainn on March 5th, 2013, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20256
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

C++ design question

March 13th, 2013, 8:23 am

std::map might be too restrictive. One could use boost bimap to model N:N relationships. Keep your options open.
Last edited by Cuchulainn on March 12th, 2013, 11:00 pm, edited 1 time in total.