ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
newmodm3.cpp
Go to the documentation of this file.
1 
8 #include <admodel.h>
9 
10 int xxxmax(const int x, const int y)
11 {
12  return x < y ? y : x;
13 }
14 int xxxmin(const int x, const int y)
15 {
16  return x < y ? x : y;
17 }
21 void get_confidence_interval(const dvector& _left_bd, const dvector& _right_bd,
22  dmatrix& ms, const dvector& xs, const dvector& siglevel,
23  const int& level_index, int index)
24  {
25  dvector& left_bd=(dvector&) _left_bd;
26  dvector& right_bd=(dvector&) _right_bd;
27  dvector& fval=ms(index);
28  int lb=0;
29  int rb=0;
30  double xdiff=0.0;
31  double isum=0;
32 
33  int mmin=fval.indexmin();
34  int mmax=fval.indexmax();
35  double tmp=fval(mmin);
36  int imax=mmin;
37  int i;
38  for (i=mmin+1;i<=mmax;i++)
39  {
40  if (fval(i)>tmp)
41  {
42  tmp=fval(i);
43  imax=i;
44  }
45  }
46  if (imax>mmin)
47  {
48  lb=imax-1;
49  rb=imax;
50  }
51  else
52  {
53  lb=imax;
54  rb=imax+1;
55  }
56  double integral=0.0;
57 
58  for (i=mmin+1;i<=mmax;i++)
59  {
60  integral+=fval(i)*(xs(i)-xs(i-1));
61  }
62  cout << integral << endl;
63  for (;;)
64  {
65  if (lb <= fval.indexmin() && rb > fval.indexmax())
66  {
67  int lower=xxxmax(lb,fval.indexmin());
68  int upper=xxxmin(rb-1,fval.indexmax());
69  left_bd(level_index)=xs(lower);
70  right_bd(level_index)=xs(upper);
71  break;
72  }
73 
74  if (fval(lb)>=fval(rb) || rb==fval.indexmax())
75  {
76  if (lb>fval.indexmin())
77  {
78  xdiff=xs(lb)-xs(lb-1);
79  }
80  else
81  {
82  int minind=fval.indexmin();
83  xdiff=xs(minind+1)-xs(minind);
84  }
85  double incr=fval(lb)*xdiff/integral;
86  //double incr=fval(lb);
87  if ( incr >= (siglevel(level_index)-isum))
88  {
89  double diff = siglevel(level_index) - isum;
90  double delta=diff/incr;
91  if (lb>fval.indexmin())
92  {
93  left_bd(level_index)=xs(lb)-delta*(xdiff);
94  }
95  else
96  {
97  left_bd(level_index)=xs(fval.indexmin());
98  }
99  right_bd(level_index)=xs(rb);
100  break;
101  }
102  isum+=incr;
103  lb--;
104  }
105  else
106  {
107  xdiff=xs(rb)-xs(rb-1);
108  double incr=fval(rb)*xdiff/integral;
109  //double incr=fval(rb);
110  if ( incr >= (siglevel(level_index)-isum) ||
111  rb == mmax)
112  {
113  double diff = siglevel(level_index) - isum;
114  double delta=diff/incr;
115  left_bd(level_index)=xs(lb);
116  right_bd(level_index)=xs(rb)+delta*(xdiff);
117  break;
118  }
119  isum+=incr;
120  rb++;
121  }
122  }
123  }
124 
125 void get_onesided_intervals(const dvector& _left_bd, const dvector& _right_bd,
126  dmatrix& ms, const dvector& xs, const dvector& siglevel,
127  const int& level_index, int index)
128  {
129  dvector& left_bd=(dvector&) _left_bd;
130  dvector& right_bd=(dvector&) _right_bd;
131  dvector& fval=ms(index);
132  int lb=fval.indexmin()+1;
133  int ub=fval.indexmax();
134  double xdiff=0.0;
135  double isum=0;
136  double tmpsum=0.0;
137  int ii;
138  for (ii=lb+1;ii<=ub;ii++)
139  {
140  tmpsum+=fval(ii)*(xs(ii)-xs(ii-1));
141  }
142 
143  isum=0.0;
144  for (ii=lb+1;ii<=ub;ii++)
145  {
146  xdiff=xs(ii)-xs(ii-1);
147  double incr=fval(ii)*xdiff/tmpsum;
148  double fdiff = 1.-siglevel(level_index) - isum;
149  if ( incr >= fdiff)
150  {
151  double delta=fdiff/incr;
152  left_bd(level_index)=xs(ii)+delta*xdiff;
153  break;
154  }
155  isum+=incr;
156  }
157  //cout << "tmpsum = " << tmpsum << endl;
158  isum=0;
159  for (ii=ub-1;ii>=lb;ii--)
160  {
161  xdiff=xs(ii+1)-xs(ii);
162  double incr=fval(ii)*xdiff/tmpsum;
163  double fdiff = 1.-siglevel(level_index) - isum;
164  if ( incr >= fdiff)
165  {
166  double delta=fdiff/incr;
167  right_bd(level_index)=xs(ii)+delta*xdiff;
168  break;
169  }
170  isum+=incr;
171  }
172  }
void get_onesided_intervals(const dvector &left_bd, const dvector &right_bd, dmatrix &ms, const dvector &xs, const dvector &siglevel, const int &level_index, dvector &xdist, int index)
int xxxmin(const int x, const int y)
Definition: newmodm3.cpp:14
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
void get_confidence_interval(const dvector &left_bd, const dvector &right_bd, dmatrix &ms, const dvector &xs, const dvector &siglevel, const int &level_index, dvector &xdist, int index)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
prnstream & endl(prnstream &)
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Description not yet available.
Description not yet available.
Definition: fvar.hpp:2819
int xxxmax(const int x, const int y)
Definition: newmodm3.cpp:10