dvect11.cpp
Go to the documentation of this file.
```00001 /*
00002  * \$Id\$
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(seed);
00027     tmp=p/sum;
00028     tmp1(pmin)=tmp(pmin);
00029     for (int j=pmin+1;j<=pmax-1;j++)
00030     {
00031       tmp1(j)=tmp1(j-1)+tmp(j);
00032     }
00033     tmp1(pmax)=1.0;
00034
00035     for (int i=indexmin();i<=indexmax();i++)
00036     {
00037       int j=pmin;
00038       while (choose(i)>tmp1(j))
00039       {
00040         j++;
00041       }
00042       (*this)(i)=j;
00043     }
00044   }
00045
00050 void ivector::fill_multinomial(const int& seed, const dvector& p)
00051   // Fills a dvector with random numbers drawn from a multinomial distribution
00052   {
00053     double sum=mean(p)*p.size();
00054     int pmin=p.indexmin();
00055     int pmax=p.indexmax();
00056     dvector tmp(pmin,pmax);
00057     dvector tmp1(pmin,pmax);
00058     dvector choose(indexmin(),indexmax());
00059     choose.fill_randu(seed);
00060     tmp=p/sum;
00061     tmp1(pmin)=tmp(pmin);
00062     for (int j=pmin+1;j<=pmax-1;j++)
00063     {
00064       tmp1(j)=tmp1(j-1)+tmp(j);
00065     }
00066     tmp1(pmax)=1.0;
00067
00068     for (int i=indexmin();i<=indexmax();i++)
00069     {
00070       int j=pmin;
00071       while (choose(i)>tmp1(j))
00072       {
00073         j++;
00074       }
00075       (*this)(i)=j;
00076     }
00077   }
00078
00083 void lvector::fill_multinomial(const int& seed, const dvector& p)
00084   // Fils a dvector with random numbers drawn from a multinomial distribution
00085   {
00086     double sum=mean(p)*p.size();
00087     int pmin=p.indexmin();
00088     int pmax=p.indexmax();
00089     dvector tmp(pmin,pmax);
00090     dvector tmp1(pmin,pmax);
00091     dvector choose(indexmin(),indexmax());
00092     choose.fill_randu(seed);
00093     tmp=p/sum;
00094     tmp1(pmin)=tmp(pmin);
00095     for (int j=pmin+1;j<=pmax-1;j++)
00096     {
00097       tmp1(j)=tmp1(j-1)+tmp(j);
00098     }
00099     tmp1(pmax)=1.0;
00100
00101     for (int i=indexmin();i<=indexmax();i++)
00102     {
00103       int j=pmin;
00104       while (choose(i)>tmp1(j))
00105       {
00106         j++;
00107       }
00108       (*this)(i)=j;
00109     }
00110   }
```