Serving the Quantitative Finance Community

  • 1
  • 6
  • 7
  • 8
  • 9
  • 10
 
User avatar
Cuchulainn
Posts: 20204
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz --- generic programming

February 8th, 2017, 5:04 pm

std::array instead std::vector
alloc storage: 23103
init  storage: 9613
algo  time   : 6462562
flip  time   : 672761
That's a nice state machine. The idea could be done for any problem using MWM?
http://www.boost.org/doc/libs/1_62_0/li ... index.html

That would be a nice Boost library?? just stick in your #pragma, run the stuff and it churns out a report as above.
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz --- generic programming

February 8th, 2017, 5:22 pm

I try to stick to standard C++ as much as possible now, it's very rich afaik, the only thing missing is math.
F
I think your bug is that you copy back into vold inside the inner space loop. Maybe a remnant from a binary tree where you would only use even/odd nodes? I don't know, but it can't be right?
 
User avatar
Cuchulainn
Posts: 20204
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz --- generic programming

February 8th, 2017, 8:26 pm

IMO Maths does not belong in C++. It's too big and not everyone needs it. The huge majority of C++ developers are not quants. So, it's just extra baggage.

Fortran got it right (use NAG, IMSL libraries which you pay for but they work).
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz --- generic programming

February 8th, 2017, 9:09 pm

And Python got it right. "pip install <something>" and you're set. 
 
User avatar
Cuchulainn
Posts: 20204
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz --- generic programming

March 16th, 2017, 5:28 pm

template <typename T, template <typename T> class Dist>
class Distribution
{
private:
 Dist<T> dist;
public:
 template<typename... Args>
 Distribution(Args... args) : dist(Dist<T>(args...)) {}

 T operator () (T x) const
 { // pdf, as function object

 return boost::math::pdf(dist, x);
 }

 T pdf(T x) const
 { // pdf

 return (*this)(x);
 }

 T cdf(T x) const
 { // cdf

 return boost::math::cdf(dist, x);
 }

 // etc.
};
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz --- generic programming

March 16th, 2017, 5:47 pm

I think the general consensus is that the non-member functions version improve encapsulation. Similar to container.begin() having the newly-ish introduced alternative std::begin(container)
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz --- generic programming

March 16th, 2017, 6:30 pm

and
template <typename T1, typename T2>
void wrapper(T1 e1, T2 e2) {
    func(e1, e2);
}

vs
template <typename T1, typename T2>
void wrapper(T1&& e1, T2&& e2) {
    func(forward<T1>(e1), forward<T2>(e2));
}
 
User avatar
Cuchulainn
Posts: 20204
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz --- generic programming

March 16th, 2017, 7:30 pm

I think the general consensus is that the non-member functions version improve encapsulation. 
That's what Boost Stats does. But it is hard-coded per distribution type. The approach is not general, I tried it.
It is (also) generally accepted that adapters (HAS A) is good practice. Boost does it as well.

Variadics ctors is the main advantage here.
Last edited by Cuchulainn on March 16th, 2017, 7:38 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Posts: 20204
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz --- generic programming

March 16th, 2017, 7:38 pm

Similar to container.begin() having the newly-ish introduced alternative std::begin(container)

It depends. When doing matrix algebra the world has been using indexes since the early days. Iterators are useful, but not for all cases. Just imagine doing Cholesky decompositoin with iterators. It is torture. But if someone wants to do it, go for it.
 
User avatar
Cuchulainn
Posts: 20204
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Re: C++ quiz --- generic programming

March 16th, 2017, 7:54 pm

template <typename T, template <typename T> class Dist>
class Distribution
{
private:
 Dist<T> dist;
public:
 template<typename... Args>
 Distribution(Args... args) : dist(Dist<T>(args...)) {}

 T operator () (T x) const
 { // pdf, as function object

 return boost::math::pdf(dist, x);
 }

 T pdf(T x) const
 { // pdf

 return (*this)(x);
 }

 T cdf(T x) const
 { // cdf

 return boost::math::cdf(dist, x);
 }

 // etc.
};
The Boost maths developers were brainstorming the any_distribution class, which has the same rationale as above.
http://www.boost.org/doc/libs/1_63_0/li ... uture.html

Actually they create an adapter class (like I do) and then have non-member functions for cdf() etc. for *any* distribution. It's a variation on a theme.
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz --- generic programming

March 16th, 2017, 8:24 pm

This discussion is not about iterators, about member vs non-member functions, it was an example of a design flaw fix introduced in C++11.

Your code  changes non-member functions into member functions. Why? Going back to the std::begin example, the non-member based std::begin() design you can implement (add to you design) begin(carray) or begin(myNewType) or begin(SomeOtherDevelopersType) and all your generic algorithm that's are build on top of begin(T) will then also be able to operate on carrays, myNewType, SomeOtherDevelopersType.  I've run into this design issue when working on bindings of algorithms to containers.

Cholesky is a good example. The member function version would be M.cholesky(), the non-member function chlolesky(M). Now suppose you want to generate correlated random numbers with some generic design, -using cholesky-, but your correlation matrix is stored in a 3rd party container, from some math lib. How are you going to call it's that cholesky member function in your generic algorithm??

Member vs Non-member is of course not an arbitrary fashionable choice ("variation on a theme"), but a choice to be made on rational arguments,  and one with design consequences. 

Going back to the distribution case you've posted: what rational process let to you not using the non-member boost function and write code to convert it to member functions versions? What benefits does this member version design have to justify this, ..what problem is being solved?

A classic is this article: http://www.drdobbs.com/cpp/how-non-memb ... /184401197
 
User avatar
outrun
Posts: 4573
Joined: January 1st, 1970, 12:00 am

Re: C++ quiz --- generic programming

May 4th, 2017, 1:08 pm

What era is this coming from?
Attachments
20170504_121739.jpg