dvect11.cpp
Go to the documentation of this file.
```00001 /*
00002  * \$Id: dvect11.cpp 1112 2013-07-12 21:41:41Z johnoel \$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <fvar.hpp>
00012
00017 void dvector::fill_multinomial(const int& seed, const dvector& p)
00018   // Fils a dvector with random numbers drawn from a multinomial distribution
00019   {
00020     double sum=mean(p)*p.size();
00021     int pmin=p.indexmin();
00022     int pmax=p.indexmax();
00023     dvector tmp(pmin,pmax);
00024     dvector tmp1(pmin,pmax);
00025     dvector choose(indexmin(),indexmax());
00026     choose.fill_randu((const long&)seed);
00027     tmp=p/sum;
00028     tmp1(pmin)=tmp(pmin);
00029     int j;
00030     for (j=pmin+1;j<=pmax-1;j++)
00031     {
00032       tmp1(j)=tmp1(j-1)+tmp(j);
00033     }
00034     tmp1(pmax)=1.0;
00035
00036     for (int i=indexmin();i<=indexmax();i++)
00037     {
00038       j=pmin;
00039       while (choose(i)>tmp1(j))
00040       {
00041         j++;
00042       }
00043       (*this)(i)=j;
00044     }
00045   }
00046
00051 void ivector::fill_multinomial(const int& seed, const dvector& p)
00052   // Fills a dvector with random numbers drawn from a multinomial distribution
00053   {
00054     double sum=mean(p)*p.size();
00055     int pmin=p.indexmin();
00056     int pmax=p.indexmax();
00057     dvector tmp(pmin,pmax);
00058     dvector tmp1(pmin,pmax);
00059     dvector choose(indexmin(),indexmax());
00060     choose.fill_randu((const long&)seed);
00061     tmp=p/sum;
00062     tmp1(pmin)=tmp(pmin);
00063     int j;
00064     for (j=pmin+1;j<=pmax-1;j++)
00065     {
00066       tmp1(j)=tmp1(j-1)+tmp(j);
00067     }
00068     tmp1(pmax)=1.0;
00069
00070     for (int i=indexmin();i<=indexmax();i++)
00071     {
00072       j=pmin;
00073       while (choose(i)>tmp1(j))
00074       {
00075         j++;
00076       }
00077       (*this)(i)=j;
00078     }
00079   }
00080
00085 void lvector::fill_multinomial(const int& seed, const dvector& p)
00086   // Fils a dvector with random numbers drawn from a multinomial distribution
00087   {
00088     double sum=mean(p)*p.size();
00089     int pmin=p.indexmin();
00090     int pmax=p.indexmax();
00091     dvector tmp(pmin,pmax);
00092     dvector tmp1(pmin,pmax);
00093     dvector choose(indexmin(),indexmax());
00094     choose.fill_randu((const long&)seed);
00095     tmp=p/sum;
00096     tmp1(pmin)=tmp(pmin);
00097     int j;
00098     for (j=pmin+1;j<=pmax-1;j++)
00099     {
00100       tmp1(j)=tmp1(j-1)+tmp(j);
00101     }
00102     tmp1(pmax)=1.0;
00103
00104     for (int i=indexmin();i<=indexmax();i++)
00105     {
00106       j=pmin;
00107       while (choose(i)>tmp1(j))
00108       {
00109         j++;
00110       }
00111       (*this)(i)=j;
00112     }
00113   }
```