ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dvect25.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include <fvar.hpp>
12 
18  const dvector& p)
19  // Fils a dvector with random numbers drawn from a multinomial distribution
20  {
21  double sum=mean(p)*p.size();
22  int pmin=p.indexmin();
23  int pmax=p.indexmax();
24  dvector tmp(pmin,pmax);
25  dvector tmp1(pmin,pmax);
26  dvector choose(indexmin(),indexmax());
27  choose.fill_randu(rng);
28  tmp=p/sum;
29  tmp1(pmin)=tmp(pmin);
30  int j;
31  for (j=pmin+1;j<=pmax-1;j++)
32  {
33  tmp1(j)=tmp1(j-1)+tmp(j);
34  }
35  tmp1(pmax)=1.0;
36 
37  for (int i=indexmin();i<=indexmax();i++)
38  {
39  j=pmin;
40  while (choose(i)>tmp1(j))
41  {
42  j++;
43  }
44  (*this)(i)=j;
45  }
46  }
47 
53  const dvector& p)
54  // Fills a dvector with random numbers drawn from a multinomial distribution
55  {
56  double sum=mean(p)*p.size();
57  int pmin=p.indexmin();
58  int pmax=p.indexmax();
59  dvector tmp(pmin,pmax);
60  dvector tmp1(pmin,pmax);
61  dvector choose(indexmin(),indexmax());
62  choose.fill_randu(rng);
63  tmp=p/sum;
64  tmp1(pmin)=tmp(pmin);
65  int j;
66  for (j=pmin+1;j<=pmax-1;j++)
67  {
68  tmp1(j)=tmp1(j-1)+tmp(j);
69  }
70  tmp1(pmax)=1.0;
71 
72  for (int i=indexmin();i<=indexmax();i++)
73  {
74  j=pmin;
75  while (choose(i)>tmp1(j))
76  {
77  j++;
78  }
79  (*this)(i)=j;
80  }
81  }
82 
88  const dvector& p)
89  // Fils a dvector with random numbers drawn from a multinomial distribution
90  {
91  double sum=mean(p)*p.size();
92  int pmin=p.indexmin();
93  int pmax=p.indexmax();
94  dvector tmp(pmin,pmax);
95  dvector tmp1(pmin,pmax);
96  dvector choose(indexmin(),indexmax());
97  choose.fill_randu(rng);
98  tmp=p/sum;
99  tmp1(pmin)=tmp(pmin);
100  int j;
101  for (j=pmin+1;j<=pmax-1;j++)
102  {
103  tmp1(j)=tmp1(j-1)+tmp(j);
104  }
105  tmp1(pmax)=1.0;
106 
107  for (int i=indexmin();i<=indexmax();i++)
108  {
109  j=pmin;
110  while (choose(i)>tmp1(j))
111  {
112  j++;
113  }
114  (*this)(i)=j;
115  }
116  }
void fill_multinomial(const int &seed, const dvector &p)
Description not yet available.
Definition: dvect11.cpp:83
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
int indexmin() const
Definition: fvar.hpp:676
void fill_multinomial(const int &seed, const dvector &p)
Description not yet available.
Definition: dvect11.cpp:50
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
void fill_randu(long int &n)
Fill vector with random numbers.
Definition: ranfill.cpp:190
double mean(const dvector &vec)
Returns computed mean of vec.
Definition: cranfill.cpp:43
int indexmax() const
Definition: fvar.hpp:681
Description not yet available.
Definition: fvar.hpp:7951
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int indexmin() const
Definition: ivector.h:99
int indexmax() const
Definition: ivector.h:104
unsigned int size() const
Get number of elements in array.
Definition: dvector.h:209
void fill_multinomial(const int &seed, const dvector &p)
Description not yet available.
Definition: dvect11.cpp:17