Example #1
0
Type objective_function<Type>::operator() () {
  // data:
  DATA_MATRIX(age);
  DATA_VECTOR(len);
  DATA_SCALAR(CV_e);
  DATA_INTEGER(num_reads);
  
  // parameters:
  PARAMETER(r0); // reference value
  PARAMETER(b); // growth displacement
  PARAMETER(k); // growth rate
  PARAMETER(m); // slope of growth
  PARAMETER(CV_Lt);
  
  PARAMETER(gam_shape);
  PARAMETER(gam_scale);
  
  PARAMETER_VECTOR(age_re);
  
  // procedures:
  Type n = len.size();
  
  Type nll = 0.0; // Initialize negative log-likelihood
  
  Type eps = 1e-5;
  
  CV_e = CV_e < 0.05 ? 0.05 : CV_e;
  
  for (int i = 0; i < n; i++) {
    Type x = age_re(i);
    if (!isNA(x) && isFinite(x)) {
      Type len_pred = pow(r0 + b * exp(k * x), m);
      
      Type sigma_e = CV_e * x + eps;
      Type sigma_Lt = CV_Lt * (len_pred + eps);
      
      nll -= dnorm(len(i), len_pred, sigma_Lt, true);
      nll -= dgamma(x + eps, gam_shape, gam_scale, true);
      
      for (int j = 0; j < num_reads; j++) {
        if (!isNA(age(j, i)) && isFinite(age(j, i)) && age(j, i) >= 0) {
          nll -= dnorm(age(j, i), x, sigma_e, true); 
        }
      }  
    }
  }
  
  return nll;
}
Type objective_function<Type>::operator() () {
  // data:
  DATA_MATRIX(age);
  DATA_VECTOR(len);
  DATA_SCALAR(CV_e);
  DATA_INTEGER(num_reads);
  
  // parameters:
  PARAMETER(a); // upper asymptote
  PARAMETER(b); // growth range
  PARAMETER(k); // growth rate
  PARAMETER(CV_Lt);
  
  PARAMETER(beta);
  
  PARAMETER_VECTOR(age_re);
  
  // procedures:
  Type n = len.size();
  
  Type nll = 0.0; // Initialize negative log-likelihood
  
  Type eps = 1e-5;

  
  CV_e = CV_e < 0.05 ? 0.05 : CV_e;
  
  for (int i = 0; i < n; i++) {
    Type x = age_re(i);
    if (!isNA(x) && isFinite(x)) {
      Type len_pred = a / (1 + b * exp(-k * x));
      
      Type sigma_e = CV_e * x + eps;
      Type sigma_Lt = CV_Lt * (len_pred + eps);
      
      nll -= dnorm(len(i), len_pred, sigma_Lt, true);
      nll -= dexp(x, beta, true);
      
      for (int j = 0; j < num_reads; j++) {
        if (!isNA(age(j, i)) && isFinite(age(j, i)) && age(j, i) >= 0) {
          nll -= dnorm(age(j, i), x, sigma_e, true); 
        }
      } 
    }
  }
  
  return nll;
}
Type objective_function<Type>::operator()()
{
  DATA_VECTOR(y);                  // Observations
  DATA_VECTOR_INDICATOR(keep, y);  // For one-step predictions

  DATA_SCALAR(huge);
  PARAMETER_VECTOR(x);
  PARAMETER(mu);
  PARAMETER(logsigma);
  PARAMETER(logs);

  // Initial condition
  Type nll = -dnorm(x(0), Type(0), huge, true);

  // Increments
  for (int i = 1; i < x.size(); ++i)
    nll -= dnorm(x(i), x(i - 1) + mu, exp(logsigma), true);

  // Observations
  for (int i = 0; i < y.size(); ++i)
    nll -= keep(i) * dnorm(y(i), x(i), exp(logs), true);

  return nll;
}
Example #4
0
Type objective_function<Type>::operator() ()
{
  DATA_INTEGER(minAge);         
  DATA_INTEGER(maxAge);         
  DATA_INTEGER(minYear);        
  DATA_INTEGER(maxYear);        
  DATA_ARRAY(catchNo);        
  DATA_ARRAY(stockMeanWeight);
  DATA_ARRAY(propMature);     
  DATA_ARRAY(M);              
  DATA_INTEGER(minAgeS);        
  DATA_INTEGER(maxAgeS);        
  DATA_INTEGER(minYearS);       
  DATA_INTEGER(maxYearS);       
  DATA_SCALAR(surveyTime);     
  DATA_ARRAY(Q1);  

  PARAMETER_VECTOR(logN1Y);
  PARAMETER_VECTOR(logN1A);
  PARAMETER_VECTOR(logFY);
  PARAMETER_VECTOR(logFA);
  PARAMETER_VECTOR(logVarLogCatch);
  PARAMETER_VECTOR(logQ);
  PARAMETER(logVarLogSurvey);  

  int na=maxAge-minAge+1;
  int ny=maxYear-minYear+1;
  int nas=maxAgeS-minAgeS+1;
  int nys=maxYearS-minYearS+1;

  // setup F
  matrix<Type> F(ny,na);
  for(int y=0; y<ny; ++y){
    for(int a=0; a<na; ++a){
      F(y,a)=exp(logFY(y))*exp(logFA(a));
    }
  }
  // setup logN
  matrix<Type> logN(ny,na);
  for(int a=0; a<na; ++a){
    logN(0,a)=logN1Y(a);
  } 
  for(int y=1; y<ny; ++y){
    logN(y,0)=logN1A(y-1);
    for(int a=1; a<na; ++a){
      logN(y,a)=logN(y-1,a-1)-F(y-1,a-1)-M(y-1,a-1);
      if(a==(na-1)){
        logN(y,a)=log(exp(logN(y,a))+exp(logN(y,a-1)-F(y-1,a)-M(y-1,a)));
      }
    }
  }
  matrix<Type> predLogC(ny,na);
  for(int y=0; y<ny; ++y){
    for(int a=0; a<na; ++a){
      predLogC(y,a)=log(F(y,a))-log(F(y,a)+M(y,a))+log(Type(1.0)-exp(-F(y,a)-M(y,a)))+logN(y,a);
    }
  }

  Type ans=0; 
  for(int y=0; y<ny; ++y){
    for(int a=0; a<na; ++a){
      if(a==0){
        ans+= -dnorm(log(catchNo(y,a)),predLogC(y,a),exp(Type(0.5)*logVarLogCatch(0)),true);
      }else{
        ans+= -dnorm(log(catchNo(y,a)),predLogC(y,a),exp(Type(0.5)*logVarLogCatch(1)),true);
      }
    }
  }

  matrix<Type> predLogS(nys,nas);
  for(int y=0; y<nys; ++y){
    for(int a=0; a<nas; ++a){
			int sa        = a+(minAgeS-minAge);
			int sy        = y+(minYearS-minYear);
			predLogS(y,a) = logQ(a)-(F(sy,sa)+M(sy,sa))*surveyTime+logN(sy,sa);
			ans          += -dnorm(log(Q1(y,a)),predLogS(y,a),exp(Type(0.5)*logVarLogSurvey),true);
    }
  }

  vector<Type> ssb(ny);
  ssb.setZero();
  for(int y=0; y<=ny; ++y){
    for(int a=0; a<na; ++a){
    	std::cout<<y<<" "<<a<<" "<<"\n";
      ssb(y)+=exp(logN(y,a))*stockMeanWeight(y,a)*propMature(y,a);
    }
  }

  ADREPORT(ssb);
  return ans;
}