ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
dvect25.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 random_number_generator& rng,
00018   const dvector& p)
00019   // Fils a dvector with random numbers drawn from a multinomial distribution
00020   {
00021     double sum=mean(p)*p.size();
00022     int pmin=p.indexmin();
00023     int pmax=p.indexmax();
00024     dvector tmp(pmin,pmax);
00025     dvector tmp1(pmin,pmax);
00026     dvector choose(indexmin(),indexmax());
00027     choose.fill_randu(rng);
00028     tmp=p/sum;
00029     tmp1(pmin)=tmp(pmin);
00030     int j;
00031     for (j=pmin+1;j<=pmax-1;j++)
00032     {
00033       tmp1(j)=tmp1(j-1)+tmp(j);
00034     }
00035     tmp1(pmax)=1.0;
00036 
00037     for (int i=indexmin();i<=indexmax();i++)
00038     {
00039       j=pmin;
00040       while (choose(i)>tmp1(j))
00041       {
00042         j++;
00043       }
00044       (*this)(i)=j;
00045     }
00046   }
00047 
00052 void ivector::fill_multinomial(const random_number_generator& rng,
00053   const dvector& p)
00054   // Fills a dvector with random numbers drawn from a multinomial distribution
00055   {
00056     double sum=mean(p)*p.size();
00057     int pmin=p.indexmin();
00058     int pmax=p.indexmax();
00059     dvector tmp(pmin,pmax);
00060     dvector tmp1(pmin,pmax);
00061     dvector choose(indexmin(),indexmax());
00062     choose.fill_randu(rng);
00063     tmp=p/sum;
00064     tmp1(pmin)=tmp(pmin);
00065     int j;
00066     for (j=pmin+1;j<=pmax-1;j++)
00067     {
00068       tmp1(j)=tmp1(j-1)+tmp(j);
00069     }
00070     tmp1(pmax)=1.0;
00071 
00072     for (int i=indexmin();i<=indexmax();i++)
00073     {
00074       j=pmin;
00075       while (choose(i)>tmp1(j))
00076       {
00077         j++;
00078       }
00079       (*this)(i)=j;
00080     }
00081   }
00082 
00087 void lvector::fill_multinomial(const random_number_generator& rng,
00088   const dvector& p)
00089   // Fils a dvector with random numbers drawn from a multinomial distribution
00090   {
00091     double sum=mean(p)*p.size();
00092     int pmin=p.indexmin();
00093     int pmax=p.indexmax();
00094     dvector tmp(pmin,pmax);
00095     dvector tmp1(pmin,pmax);
00096     dvector choose(indexmin(),indexmax());
00097     choose.fill_randu(rng);
00098     tmp=p/sum;
00099     tmp1(pmin)=tmp(pmin);
00100     int j;
00101     for (j=pmin+1;j<=pmax-1;j++)
00102     {
00103       tmp1(j)=tmp1(j-1)+tmp(j);
00104     }
00105     tmp1(pmax)=1.0;
00106 
00107     for (int i=indexmin();i<=indexmax();i++)
00108     {
00109       j=pmin;
00110       while (choose(i)>tmp1(j))
00111       {
00112         j++;
00113       }
00114       (*this)(i)=j;
00115     }
00116   }