ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
multifan.cpp
Go to the documentation of this file.
1 #include "statsLib.h"
2 
14 dvariable multifan(const dmatrix oprop,const dvar_matrix& pprop, const int& Nsamp)
15 { //Vivian Haist.
16  dvariable extra=0.1/14.;
17  dvar_matrix resid=elem_div((oprop-pprop),sqrt((elem_prod(pprop,1.-pprop)+extra)/Nsamp));
18  return sum(0.5*log(elem_prod(pprop,1. -pprop)+extra) -log(mfexp(-0.5*elem_prod(resid,resid))+0.01));
19 }
20 
21 dvariable multifan(const int& n, const dmatrix obs, const dvar_matrix pred,double& nef)
22 {
23  int A=obs.colmax()-obs.colmin()+1;
24  //dvar_matrix xi=(elem_prod(1.-pred,pred)+0.1/A)/n; //variance from Fourniers paper.
25  dvar_matrix xi=(elem_prod(1.-obs,obs)+0.1/A)/n; //variance from the multifanCL manual.
26  dvar_matrix resid=obs-pred;
27  nef=value(sum(elem_prod(1.-pred,pred))/sum(elem_prod(resid,resid)));
28  return sum(0.5*log(2.*M_PI*xi)-log(mfexp(-0.5*elem_div(elem_prod(resid,resid),xi))+0.01));
29 }
30 
31 dvariable multifan(const double& s,const dvector obsQ,const dvar_vector& preQ, double& nmle)
32 {
33  //using Fournier's robust likelihood for length frequency data.
34  //s is the sample size
35  //neff is the sample size limit This seems to be fucked...
36  //RETURN_ARRAYS_INCREMENT();
37  dvariable like;
38  dvariable tau;
39  int lb=obsQ.indexmin();
40  int nb=obsQ.indexmax();
41 
42  dvar_vector epsilon(lb,nb);
43  dvar_vector Q=obsQ/sum(obsQ);
44  dvar_vector Qhat=preQ/sum(preQ);
45 
46  //dvariable nmle; //effective sample size
47  nmle=value(sum(elem_prod(Qhat,1.-Qhat))/norm2(Q-Qhat));
48  cout<<nmle<<endl;
49  tau=1./s;
50  epsilon=elem_prod(1.-Qhat,Qhat);
51 
52  like=0.5*sum(log(2.*M_PI*(epsilon+0.1/nb)))+nb*log(sqrt(tau));
53  like+= -1.*sum(log(mfexp(-1.*elem_div(square(Q-Qhat),2.*tau*(epsilon+0.1/nb)))+0.01));
54  //RETURN_ARRAYS_DECREMENT();
55  return like;
56 }
57 
d3_array elem_prod(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements product of a(i, j, k) * b(i, j, k).
Definition: d3arr2a.cpp:92
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
d3_array elem_div(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements division of a(i, j, k) / b(i, j, k).
Definition: d3arr2a.cpp:112
dvariable multifan(const dmatrix oprop, const dvar_matrix &pprop, const int &Nsamp)
Definition: multifan.cpp:14
ADMB variable vector.
Definition: fvar.hpp:2172
d3_array mfexp(const d3_array &m)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr7.cpp:10
prnstream & endl(prnstream &)
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
int colmin(void) const
Definition: fvar.hpp:2939
Description not yet available.
Definition: fvar.hpp:2819
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
Library of statistic functions.
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
#define M_PI
Definition: fvar.hpp:92
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
int colmax(void) const
Definition: fvar.hpp:2943