Login  Register

RE: random generation of constrained portfolio allocation weights

Posted by Hurd, Matthew on Sep 08, 2004; 4:40pm
URL: http://quantlib.414.s1.nabble.com/random-generation-of-constrained-portfolio-allocation-weights-tp3309p3312.html

Mario,

As I mentioned to "randomize the bias" you can shuffle an indirection.

This is what I mentioned in a later mail:  
  "Intuitively, you can see why as the final measure is likely to be a
   biased number as it has to fit into the initial choices.  A simple way
   of improving the randomness is to shuffle the tuple, i.e. randomly
   select the axes to constrain, before allocating the weights.  That way
   the bias is randomly distributed and hopefully a little nicer."

Code below produces:

    0.333351/0.333322/0.333327
    Press any key to continue . . .

On my machine, YMMV.

Probably good enough for what you're after.  Though disguising bias and eliminating bias aren't necessarily the same thing ;-)

Hope this helps,

Matt.
[hidden email]
+61.419.602148
__________________________________________

#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>


const int nn = 10000000;

int main() {

    std::vector<double> ss(3,0.0);
    std::vector<std::size_t> axis(3);
    std::vector<double> uu(3);

    axis[0] = 0;
    axis[1] = 1;
    axis[2] = 2;

    std::vector<std::size_t>::iterator b = axis.begin();
    std::vector<std::size_t>::iterator e = axis.end();

    for(int j=0; j<nn; j++) {

        // shuffle the indirection, as the rules are the same you could just
        // shuffle the values instead which would be quicker

        std::random_shuffle( b, e);

        uu[axis[0]] = 0.2 + 0.2*(double)(rand()/(RAND_MAX+1.0));
        uu[axis[1]] = (0.6-uu[axis[0]])
                        +(uu[axis[0]]-0.2)
                        *(double)(rand()/(RAND_MAX+1.0));
        uu[axis[2]] = 1.0-(uu[axis[0]] + uu[axis[1]] );
       
        ss[0] += uu[0];
        ss[1] += uu[1];
        ss[2] += uu[2];
    }

    std::cout << "\n"
        << ss[0]/nn
        << "/" << ss[1]/nn
        << "/" << ss[2]/nn
        << "\n";

    system("pause");
}




> -----Original Message-----
> From: ML [mailto:[hidden email]]
> Sent: Wednesday, 8 September 2004 7:23 PM
> To: Hurd, Matthew
> Cc: Ferdinando Ametrano
> Subject: Re: [Quantlib-users] random generation of constrained portfolio
> allocation weights
>
> Matthew,
>
> I wrote a small piece of code (cf below) to test your suggestion. After 1
> million runs the first moments of the generated numbers are:
>
> 0.30 / 0.35 / 0.35
>
> However, the problem being symmetrical, the first moments should be equal
> (to 1/3).
>
> -Mario
>
> ===========================================================
> #include <iostream>
> #include <stdlib.h>
>
> using namespace std;
>
> const int nn = 1000000;
>
> int main() {
>   double ss11 = 0.0;
>   double ss21 = 0.0;
>   double ss31 = 0.0;
>   for(int j=0; j<nn; j++) {
>     double uu1 = 0.2+0.2*(double)(random()/(RAND_MAX+1.0));
>     double uu2 = (0.6-uu1)+(uu1-0.2)*(double)(random()/(RAND_MAX+1.0));
>     double uu3 = 1.0-uu1-uu2;
>     //    cout << uu1 << "/" << uu2 << "/" << uu3 << endl;
>     ss11 += uu1;
>     ss21 += uu2;
>     ss31 += uu3;
>   }
>   cout << endl << ss11/nn << "/" << ss21/nn << "/" << ss31/nn << endl;
> }
>
> ========================================================
> ----- Original Message -----
> From: "Hurd, Matthew" <[hidden email]>
> To: "Ferdinando Ametrano" <[hidden email]>;
> <[hidden email]>
> Sent: Tuesday, September 07, 2004 9:05 AM
> Subject: RE: [Quantlib-users] random generation of constrained portfolio
> allocation weights
>
>
> > The below distribution will not be uniformly random (depending on what
> > this means to you), but neither will rejecting illegal cases.
> >
> > Using your [0-40] example:
> >
> > 1. Random first number 20-40.  Has to be at least 20 otherwise no viable
> > solution.
> >
> > 2.  Next random number to give a sum of at least 60, so the last number
> > may be valid (it has to be <= 40).  That is, if the first number is 35,
> > then the second number must be [25-40] to make a viable triple.
> >
> > 3.  Obviously, the third number is 100 - sum of the first two.
> >
> > Trivial to generalise.
> >
> > Is that what you're after?
> >
> > Regards,
> >
> > Matt Hurd.
> > _________________
> >
> > Matt Hurd
> > +61.2.8226.5029
> > [hidden email]
> > Susquehanna
> > _________________
> >
> > > -----Original Message-----
> > > From: [hidden email]
> > [mailto:quantlib-users-
> > > [hidden email]] On Behalf Of Ferdinando Ametrano
> > > Sent: Tuesday, 7 September 2004 12:53 AM
> > > To: QuantLib-users
> > > Subject: [Quantlib-users] random generation of constrained portfolio
> > > allocation weights
> > >
> > > Hi all
> > >
> > > I need to generate random weights for a portfolio allocation, with max
> > > constraints on the weights, no short sale allowed (e.g. 3 assets, each
> > one
> > > in the [0%, 40%] range)
> > >
> > > I have no problem for the unconstrained generation, using
> > (quasi)random
> > > sequences of dimension 3 that I normalize so that their sum is 100%.
> > I'm
> > > not sure this approach is on solid theoretical ground, but it works.
> > >
> > > Anyway when it comes to constrained generation many problems arise,
> > and I
> > > haven't found an efficient generation algorithm. Rejection of the
> > > unconstrained portfolio is inefficient, of course.
> > >
> > > Hints, suggestions, thoughts?
> > >
> > > Even better would be a ready to be coded algorithm ;-)
> > >
> > > ciao -- Nando
> > >
> > >
> > >
> > >
> > > -------------------------------------------------------
> > > This SF.Net email is sponsored by BEA Weblogic Workshop
> > > FREE Java Enterprise J2EE developer tools!
> > > Get your free copy of BEA WebLogic Workshop 8.1 today.
> > > http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click
> > > _______________________________________________
> > > Quantlib-users mailing list
> > > [hidden email]
> > > https://lists.sourceforge.net/lists/listinfo/quantlib-users
> >
> >
> >
> > IMPORTANT: The information contained in this email and/or its attachments
> is confidential. If you are not the intended recipient, please notify the
> sender immediately by reply and immediately delete this message and all its
> attachments.  Any review, use, reproduction, disclosure or dissemination of
> this message or any attachment by an unintended recipient is strictly
> prohibited.  Neither this message nor any attachment is intended as or
> should be construed as an offer, solicitation or recommendation to buy or
> sell any security or other financial instrument.  Neither the sender, his or
> her employer nor any of their respective affiliates makes any warranties as
> to the completeness or accuracy of any of the information contained herein
> or that this message or any of its attachments is free of viruses.
> >
> >
> >
> > -------------------------------------------------------
> > This SF.Net email is sponsored by BEA Weblogic Workshop
> > FREE Java Enterprise J2EE developer tools!
> > Get your free copy of BEA WebLogic Workshop 8.1 today.
> > <a href="http://ads.osdn.com/?ad_idP47&alloc_id808&opÌk">http://ads.osdn.com/?ad_idP47&alloc_id808&opÌk
> > _______________________________________________
> > Quantlib-users mailing list
> > [hidden email]
> > https://lists.sourceforge.net/lists/listinfo/quantlib-users
>