ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dvect11.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 
17 void dvector::fill_multinomial(const int& seed, const dvector& p)
18  // Fils a dvector with random numbers drawn from a multinomial distribution
19  {
20  double sum=mean(p)*p.size();
21  int pmin=p.indexmin();
22  int pmax=p.indexmax();
23  dvector tmp(pmin,pmax);
24  dvector tmp1(pmin,pmax);
25  dvector choose(indexmin(),indexmax());
26  choose.fill_randu(seed);
27  tmp=p/sum;
28  tmp1(pmin)=tmp(pmin);
29  for (int j=pmin+1;j<=pmax-1;j++)
30  {
31  tmp1(j)=tmp1(j-1)+tmp(j);
32  }
33  tmp1(pmax)=1.0;
34 
35  for (int i=indexmin();i<=indexmax();i++)
36  {
37  int j=pmin;
38  while (choose(i)>tmp1(j))
39  {
40  j++;
41  }
42  (*this)(i)=j;
43  }
44  }
45 
50 void ivector::fill_multinomial(const int& seed, const dvector& p)
51  // Fills a dvector with random numbers drawn from a multinomial distribution
52  {
53  double sum=mean(p)*p.size();
54  int pmin=p.indexmin();
55  int pmax=p.indexmax();
56  dvector tmp(pmin,pmax);
57  dvector tmp1(pmin,pmax);
58  dvector choose(indexmin(),indexmax());
59  choose.fill_randu(seed);
60  tmp=p/sum;
61  tmp1(pmin)=tmp(pmin);
62  for (int j=pmin+1;j<=pmax-1;j++)
63  {
64  tmp1(j)=tmp1(j-1)+tmp(j);
65  }
66  tmp1(pmax)=1.0;
67 
68  for (int i=indexmin();i<=indexmax();i++)
69  {
70  int j=pmin;
71  while (choose(i)>tmp1(j))
72  {
73  j++;
74  }
75  (*this)(i)=j;
76  }
77  }
78 
83 void lvector::fill_multinomial(const int& seed, const dvector& p)
84  // Fils a dvector with random numbers drawn from a multinomial distribution
85  {
86  double sum=mean(p)*p.size();
87  int pmin=p.indexmin();
88  int pmax=p.indexmax();
89  dvector tmp(pmin,pmax);
90  dvector tmp1(pmin,pmax);
91  dvector choose(indexmin(),indexmax());
92  choose.fill_randu(seed);
93  tmp=p/sum;
94  tmp1(pmin)=tmp(pmin);
95  for (int j=pmin+1;j<=pmax-1;j++)
96  {
97  tmp1(j)=tmp1(j-1)+tmp(j);
98  }
99  tmp1(pmax)=1.0;
100 
101  for (int i=indexmin();i<=indexmax();i++)
102  {
103  int j=pmin;
104  while (choose(i)>tmp1(j))
105  {
106  j++;
107  }
108  (*this)(i)=j;
109  }
110  }
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
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