bpp-core3  3.0.0
ContingencyTableGenerator.cpp
Go to the documentation of this file.
1 //
2 // File: ContingencyTableGenerator.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2010-12-10 16:19:00
6 //
7 
8 /*
9  Copyright or © or Copr. CNRS, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for numerical calculus.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #include <iostream>
42 
43 #include "../VectorTools.h"
45 
46 using namespace bpp;
47 using namespace std;
48 
49 /**************************************************************************/
50 
52  const std::vector<size_t>& nrowt,
53  const std::vector<size_t>& ncolt) :
54  nrowt_(nrowt),
55  ncolt_(ncolt),
56  nrow_(nrowt.size()),
57  ncol_(ncolt.size()),
58  nrowm_(0),
59  ncolm_(0),
60  jwork_(ncolt.size()),
61  ntot_(0),
62  fact_(0)
63 {
64  if (nrow_ < 2 || ncol_ < 2)
65  throw Exception("ContingencyTableGenerator. Input marginals must have size greater than 1.");
68  throw Exception("ContingencyTableGenerator. Marginal do not sum to the same value.");
69  nrowm_ = nrow_ - 1;
70  ncolm_ = ncol_ - 1;
71  fact_.resize(ntot_ + 1);
72  double x = 0.;
73  fact_[0] = 0.;
74  for (unsigned int i = 1; i <= ntot_; i++)
75  {
76  x = x + log(static_cast<double>(i));
77  fact_[i] = x;
78  }
79 }
80 
81 /* Algorithm AS 159 Applied Statistics (1981), vol. 30, no. 1
82  original (C) Royal Statistical Society 1981
83 
84  Generate random two-way table with given marginal totals.
85 
86  Heavily pretty edited by Martin Maechler, Dec 2003
87  use double precision for integer multiplication (against overflow);
88 
89  Taken from R source file rcont.c and adapted by Julien Dutheil, Dec 2010
90  */
92 {
93  RowMatrix<size_t> table(nrow_, ncol_); // Result
94  size_t j, l, m, ia, ib, ic, jc, id, ie, ii, nll, nlm, nr_1, nc_1;
95  long double x, y, dummy, sumprb;
96  bool lsm, lsp;
97 
98  nr_1 = nrow_ - 1;
99  nc_1 = ncol_ - 1;
100 
101  ib = 0; /* -Wall */
102 
103  /* Construct random matrix */
104  for (j = 0; j < nc_1; ++j)
105  {
106  jwork_[j] = ncolt_[j];
107  }
108 
109  jc = ntot_;
110 
111  for (l = 0; l < nr_1; ++l) /* ----- matrix[ l, * ] ----- */
112  {
113  ia = nrowt_[l];
114  ic = jc;
115  jc -= ia;/* = n_tot - sum(nr[0:l]) */
116 
117  for (m = 0; m < nc_1; ++m)
118  {
119  id = jwork_[m];
120  ie = ic;
121  ic -= id;
122  ib = ie - ia;
123  ii = ib - id;
124 
125  if (ie == 0) /* Row [l,] is full, fill rest with zero entries */
126  {
127  for (j = m; j < nc_1; ++j)
128  {
129  table(l, j) = 0;
130  }
131  ia = 0;
132  break;
133  }
134 
135  /* Generate pseudo-random number */
136  dummy = generator.drawNumber();
137 
138  do/* Outer Loop */
139 
140  /* Compute conditional expected value of MATRIX(L, M) */
141 
142  {
143  nlm = static_cast<size_t>(ia * (static_cast<long double>(id) / static_cast<long double>(ie)) + 0.5);
144  x = exp(fact_[ia] + fact_[ib] + fact_[ic] + fact_[id]
145  - fact_[ie] - fact_[nlm]
146  - fact_[id - nlm] - fact_[ia - nlm] - fact_[ii + nlm]);
147  if (x >= dummy)
148  break;
149 
150  sumprb = x;
151  y = x;
152  nll = nlm;
153 
154  do
155  {
156  /* Increment entry in row L, column M */
157  j = static_cast<size_t>((id - nlm) * static_cast<long double>(ia - nlm));
158  lsp = (j == 0);
159  if (!lsp)
160  {
161  ++nlm;
162  x = x * j / (static_cast<long double>(nlm) * (ii + nlm));
163  sumprb += x;
164  if (sumprb >= dummy)
165  goto L160;
166  }
167 
168  do
169  {
170  /* Decrement entry in row L, column M */
171  j = nll * (ii + nll);
172  lsm = (j == 0);
173  if (!lsm)
174  {
175  --nll;
176  y = y * j / (static_cast<long double>(id - nll) * (ia - nll));
177  sumprb += y;
178  if (sumprb >= dummy)
179  {
180  nlm = nll;
181  goto L160;
182  }
183  /* else */
184  if (!lsp)
185  break; /* to while (!lsp) */
186  }
187  }
188  while (!lsm);
189  }
190  while (!lsp);
191 
192  dummy = sumprb * generator.drawNumber();
193  }
194  while (true);
195 
196 L160:
197  table(l, m) = nlm;
198  ia -= nlm;
199  jwork_[m] -= nlm;
200  }
201  table(l, nc_1) = ia;/* last column in row l */
202  }
203 
204  /* Compute entries in last row of MATRIX */
205  for (m = 0; m < nc_1; ++m)
206  {
207  table(nr_1, m) = jwork_[m];
208  }
209 
210  table(nr_1, nc_1) = ib - table(nr_1, nc_1 - 1);
211 
212  return table;
213 }
214 
215 /**************************************************************************/
RowMatrix< size_t > rcont2(const RandomFactory &generator= *RandomTools::DEFAULT_GENERATOR)
ContingencyTableGenerator(const std::vector< size_t > &nrowt, const std::vector< size_t > &ncolt)
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
This is the interface for the Random Number Generators.
Definition: RandomFactory.h:55
virtual double drawNumber() const =0
Return a random number.
Matrix storage by row.
Definition: Matrix.h:131
static T sum(const std::vector< T > &v1)
Definition: VectorTools.h:624