Example #1
0
Type objective_function<Type>::operator() ()
{
    DATA_MATRIX(obs);
    DATA_MATRIX(coord);
    DATA_VECTOR(covObs);
    DATA_INTEGER(p);
    DATA_VECTOR(h);

    PARAMETER(logObsSd);
    PARAMETER(logObsTSd);
    PARAMETER(logStatSd);
    PARAMETER_MATRIX(x);

    Type nll = 0.0;
    covafill<Type> cf(coord,covObs,h,p);

    // Contribution from states
    for(int i = 1; i < x.cols(); ++i) {
        nll -= dnorm(x(0,i), x(0,i-1), exp(logStatSd),true);
        nll -= dnorm(x(1,i), x(1,i-1), exp(logStatSd),true);
    }

    // contribution from observations
    for(int i = 0; i < obs.cols(); ++i) {
        nll -= dnorm(obs(0,i), x(0,i), exp(logObsSd),true);
        nll -= dnorm(obs(1,i), x(1,i), exp(logObsSd),true);
        vector<Type> tmp = x.col(i);
        Type val = evalFill((CppAD::vector<Type>)tmp, cf)[0];
        nll -= dnorm(obs(2,i), val, exp(logObsTSd),true);
    }


    return nll;
}
Example #2
0
File: OU.cpp Project: mebrooks/mews
Type objective_function<Type>::operator() ()
{
	DATA_VECTOR(times);
	DATA_VECTOR(obs);
	
	PARAMETER(log_R0);
	PARAMETER(m);
	PARAMETER(log_theta);
	PARAMETER(log_sigma);
	Type theta=exp(log_theta);
	Type sigma=exp(log_sigma);
	Type R0=exp(log_R0);
	
	int n1=times.size();
	int n2=2;//mean and variance
	vector<Type> Dt(n1-1);
	vector<Type> Ex(n1-1);
	vector<Type> Vx(n1-1);
	Type nll=0;
	m=0;
	Dt=diff(times);
	Ex=theta*(Type(1)-exp(-R0*Dt)) + obs.segment(0, n1-1)*exp(-R0*Dt);
	Vx=Type(0.5)*sigma*sigma*(Type(1)-exp(Type(-2)*R0*Dt))/R0;
	
	for(int i=0; i<n1-1; i++)
	{
		nll-= dnorm(obs[i+1], Ex[i], sqrt(Vx[i]), true);
	}
	return nll;
}
Example #3
0
Type objective_function<Type>::operator() ()
{
  DATA_VECTOR(observed);

  PARAMETER_VECTOR(population);
  PARAMETER(log_process_error);
  PARAMETER(log_obs_error);
  
  Type process_error = exp(log_process_error);
  Type obs_error     = exp(log_obs_error);
  
  int n_obs          = observed.size();  // number of observations
  
  Type nll           = 0; // negative log likelihood
  
  // likelihood for state transitions
  for(int y=1; y<n_obs; y++){
    Type m = population[y-1];
    nll   -= dnorm(population(y), m, process_error, true);
  }
  
  // likelihood for observations
  for(int y=0; y<n_obs; y++){
    nll -= dnorm(observed(y), population(y), obs_error, true);
  }
  
  ADREPORT(process_error);
  ADREPORT(obs_error);
  
  return nll;
}
Example #4
0
Type objective_function<Type>::operator() ()
{
  DATA_VECTOR(x);
  PARAMETER(mu);
  PARAMETER(logSigma);
  
  
  Type f;
  f = -sum(dnorm(x,mu,exp(logSigma), true));
  return f;
}
Example #5
0
Type objective_function<Type>::operator() ()
{
  DATA_VECTOR(Y);
  DATA_VECTOR(x);
  PARAMETER(a);
  PARAMETER(b);
  PARAMETER(logSigma);
  parallel_accumulator<Type> nll(this);
  for(int i=0;i<x.size();i++)nll-=dnorm(Y[i],a+b*x[i],exp(logSigma),true);
  return nll;
}
Example #6
0
Type objective_function<Type>::operator() ()
{
  // Data
  DATA_INTEGER( n_data );
  DATA_INTEGER( n_factors );
  DATA_FACTOR( Factor );
  DATA_VECTOR( Y );                
 DATA_VECTOR_INDICATOR(keep, Y);
  
  // Parameters
  PARAMETER( X0 );
  PARAMETER( log_SD0 );
  PARAMETER( log_SDZ );
  PARAMETER_VECTOR( Z );
  
  // Objective funcction
  Type jnll = 0;
  
  // Probability of data conditional on fixed and random effect values
  for( int i=0; i<n_data; i++){
    jnll -= dnorm( Y(i), X0 + Z(Factor(i)), exp(log_SD0), true );
  }
  
  // Probability of random coefficients
  for( int i=0; i<n_factors; i++){
    jnll -= dnorm( Z(i), Type(0.0), exp(log_SDZ), true );
  }
  
  // Reporting
  Type SDZ = exp(log_SDZ);
  Type SD0 = exp(log_SD0);
  ADREPORT( SDZ );
  REPORT( SDZ );
  ADREPORT( SD0 );
  REPORT( SD0 );
  ADREPORT( Z );
  REPORT( Z );
  ADREPORT( X0 );
  REPORT( X0 );
  
  // bias-correction testing
  Type MeanZ = Z.sum() / Z.size();
  Type SampleVarZ = ( (Z-MeanZ) * (Z-MeanZ) ).sum();
  Type SampleSDZ = pow( SampleVarZ + 1e-20, 0.5);
  REPORT( SampleVarZ );
  REPORT( SampleSDZ );  
  ADREPORT( SampleVarZ );
  ADREPORT( SampleSDZ );  
  

  return jnll;
}
Example #7
0
Type objective_function<Type>::operator() ()
{
  DATA_VECTOR(x);
  PARAMETER(mu);
  PARAMETER(logSigma);


  Type f;
  f = -sum(dnorm(x,mu,exp(logSigma), true));
  f /= f / 0.0;
  std::cout << "f " << f << std::endl;
  return f;
}
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;
}
Example #9
0
Type objective_function<Type>::operator() ()
{
  DATA_VECTOR(x);
  DATA_MATRIX(D);
  PARAMETER(phi);
  PARAMETER(kappa);

  matrix<Type> C(D);
  for(int i=0; i<C.rows(); i++)
    for(int j=0; j<C.cols(); j++)
      C(i,j) = matern(D(i,j), phi, kappa);

  Type nll = density::MVNORM_t<Type>(C)(x);

  return nll;
}
Example #10
0
Type objective_function<Type>::operator() ()
{
  
  DATA_FACTOR(y); //categorical response vector
  DATA_INTEGER(S); //number of response categories
  DATA_MATRIX(X); // Fixed effects design matrix
  DATA_FACTOR(group);
  PARAMETER_VECTOR(b); // Fixed effects
  PARAMETER(logsigma);
  PARAMETER_VECTOR(tmpk); // kappa ( category thresholds)
  PARAMETER_VECTOR(u);    // Random effects
  Type sigma = exp(logsigma);
  vector<Type> alpha = tmpk;
  for(int s=1;s<tmpk.size();s++)
    alpha(s) = alpha(s-1) + exp(tmpk(s));
  Type ans=0;
  ans -= sum(dnorm(u,Type(0),Type(1),true));
  vector<Type> eta = X*b;
  for(int i=0; i<y.size(); i++){
    eta(i) += sigma*u(group(i));
    Type P;
    if(y(i)==(S-1)) P = 1.0; else P = Type(1)/(Type(1)+exp(-(alpha(y(i))-eta(i))));
    if(y(i)>0) P -= Type(1)/(Type(1)+exp(-(alpha(y(i)-1)-eta(i))));
    ans -= log(1.e-20+P);
  }
  
  return ans;
}
Example #11
0
Type objective_function<Type>::operator() ()
{
  DATA_VECTOR(y);

  PARAMETER(phi);
  PARAMETER(shape1);
  PARAMETER(shape2);
  PARAMETER(sd);
  PARAMETER_VECTOR(u);

  Type res = 0;
  res += density::AR1(phi)(u);
  vector<Type> unif = pnorm(u, Type(0), Type(1));
  vector<Type> x = qbeta(unif, shape1, shape2);
  res -= dnorm(y, x, sd, true).sum();
  return res;
}
Example #12
0
Type objective_function<Type>::operator() ()
{
  DATA_VECTOR(x);
  DATA_VECTOR(y);
  int n = y.size();

  PARAMETER(b0);
  PARAMETER(b1);
  PARAMETER(logSigma);
  vector<Type> yfit(n);

  Type neglogL = 0.0;

  yfit = b0 + b1*x;
  neglogL = -sum(dnorm(y, yfit, exp(logSigma), true));

  return neglogL;
}
Example #13
0
File: bym.cpp Project: rmp15/models
Type objective_function<Type>::operator() () {
	DATA_VECTOR(E);
	DATA_VECTOR(deaths);
	DATA_SPARSE_MATRIX(P); // precision matrix

	PARAMETER(alpha);
	PARAMETER(log_sigma2_V);
	PARAMETER_VECTOR(V);

	PARAMETER(log_sigma2_U);
	PARAMETER_VECTOR(W);

	int N = E.size();

	vector<Type> log_deaths_pred(N);
	vector<Type> mu(N);

	Type nll = 0;

	Type tau_V = 1 / exp(log_sigma2_V);
	Type tau_U = 1 / exp(log_sigma2_U);

	nll -= dnorm(alpha, Type(0), Type(10), 1);
	nll -= dgamma(tau_V, Type(0.5), Type(2000), 1);
	nll -= dgamma(tau_U, Type(0.5), Type(2000), 1);
	nll -= dnorm(V, Type(0), exp(0.5 * log_sigma2_V), 1).sum();

	vector<Type> tmp = P * W;
	nll -= -0.5 * (W * tmp).sum();
	vector<Type> U = W * exp(0.5 * log_sigma2_U);

	nll -= dnorm(U.sum(), Type(0), Type(0.00001), 1);
	for (size_t i = 0; i < N; i++) log_deaths_pred(i) = log(E(i)) + alpha + V(i) + U(i);
	for (size_t i = 0; i < N; i++) nll -= dpois(deaths(i), exp(log_deaths_pred(i)), 1);
	for (size_t i = 0; i < N; i++) mu(i) = exp(alpha + V(i) + U(i));

	vector<Type> deaths_pred = exp(log_deaths_pred);

	ADREPORT(U);
	ADREPORT(deaths_pred);
	ADREPORT(mu);

	return nll;
}
Example #14
0
Type objective_function<Type>::operator() ()
{
	DATA_VECTOR(times);
	DATA_VECTOR(obs);
	
	PARAMETER(log_R0);
	PARAMETER(log_a);
	PARAMETER(log_theta);
	PARAMETER(log_sigma);
	Type sigma=exp(log_sigma);
	Type theta=exp(log_theta);
	Type R0=exp(log_R0);
	Type a=exp(log_a)+Type(1e-4);
	int n1=times.size();
	int n2=2;//mean and variance
	matrix<Type> xdist(n1,n2); //Ex and Vx
	Type m=(a-Type(1))*R0/times(n1-1);
	Type pen; 

	xdist(0,0)=obs[0];
	xdist(0,1)=Type(0);
	
	Type nll=0;       
	Fun<Type> F;
	F.setpars(R0, m, theta, sigma);

    CppAD::vector<Type> xi(n2);
	xi[1]=Type(0); //Bottinger's code started variance at 0 for all lsoda calls
	Type ti;
	Type tf;
	for(int i=0; i<n1-1; i++)
	{
		xi[0] = Type(obs[i]);
		ti=times[i];
		tf=times[i+1];
		xdist.row(i+1) = vector<Type>(CppAD::Runge45(F, 1, ti, tf, xi));
		xdist(i+1,1)=posfun(xdist(i+1,1), Type(1e-3), pen);//to keep the variance positive
		nll-= dnorm(obs[i+1], xdist(i+1,0), sqrt(xdist(i+1,1)), true);
	}
	nll+=pen; //penalty if the variance is near eps
	return nll;
}	
A661_INTERNAL int a661_com_msg_EditBoxNumeric_A661_VALUE(buffer_el msg[], 
  a661_ushort pid,  a661_float A661_VALUE_Value_){
  int n3 = 0;
  int n2 = 0;
  int n1 = 0;
  PARAMETER("A661_VALUE");
  WRITE_USHORT(msg + 0, pid);
  WRITE_USHORT(msg + 2, 0);
  WRITE_FLOAT(msg + 4, A661_VALUE_Value_);
  return 8 ;
}
A661_INTERNAL int a661_com_msg_GpRectangle_A661_SIZE_X(buffer_el msg[], 
  a661_ushort pid,  a661_ulong A661_SIZE_X_SizeX_){
  int n3 = 0;
  int n2 = 0;
  int n1 = 0;
  PARAMETER("A661_SIZE_X");
  WRITE_USHORT(msg + 0, pid);
  WRITE_USHORT(msg + 2, 0);
  WRITE_ULONG(msg + 4, A661_SIZE_X_SizeX_);
  return 8 ;
}
A661_INTERNAL int a661_com_msg_Label_A661_VISIBLE(buffer_el msg[], 
  a661_ushort pid,  a661_bool A661_VISIBLE_Visible_){
  int n3 = 0;
  int n2 = 0;
  int n1 = 0;
  PARAMETER("A661_VISIBLE");
  WRITE_USHORT(msg + 0, pid);
  msg[2] = GET_BYTE_0(A661_VISIBLE_Visible_);
  msg[3] = GET_BYTE_0(0);
  return 4 ;
}
A661_INTERNAL int a661_com_msg_GpArcCircle_A661_FILL_INDEX(buffer_el msg[], 
  a661_ushort pid,  a661_uchar A661_FILL_INDEX_FillIndex_){
  int n3 = 0;
  int n2 = 0;
  int n1 = 0;
  PARAMETER("A661_FILL_INDEX");
  WRITE_USHORT(msg + 0, pid);
  msg[2] = GET_BYTE_0(A661_FILL_INDEX_FillIndex_);
  msg[3] = GET_BYTE_0(0);
  return 4 ;
}
Example #19
0
Type objective_function<Type>::operator() ()
{
  DATA_ARRAY(wtage);
  DATA_ARRAY(wtcv);

  // matrix<Type> yfit(n);
  int nr = wtage.dim(0);
  int nc = wtage.dim(1);

  PARAMETER(log_sd_coh);
  PARAMETER(log_sd_yr );
  PARAMETER_VECTOR(mnwt );
  PARAMETER_VECTOR(coh_eff); //  (styr-nages-age_st+1,endyr-age_st+3,3);
  PARAMETER_VECTOR( yr_eff); //  yr_eff(styr,endyr+3,3);

  Type nll = 0.0;

  Type sigma_coh = exp(log_sd_coh);
  Type sigma_yr  = exp(log_sd_yr );
  Type wt_pre;
   = mnwt*exp(sigma_yr*yr_eff(i));
Example #20
0
Type objective_function<Type>::operator()()
{
  /* Data section */
  DATA_VECTOR(Y);                  // Counted abundance
  DATA_VECTOR_INDICATOR(keep, Y);  // For one-step predictions

  /* Parameter section */
  PARAMETER_VECTOR(X);  // Latent states. As last as long as Y;
                        // extra elements are not used
  PARAMETER(logr);      // Growth rate
  PARAMETER(logtheta);  // With theta=1, the Ricker model
  PARAMETER(logK);      // Carrying capacity
  PARAMETER(logQ);      // Process noise
  PARAMETER(logS);      // Sample size controlling measurement noise

  /* Procedure section */

  Type r = exp(logr);
  Type theta = exp(logtheta);
  Type K = exp(logK);
  Type Q = exp(logQ);
  Type S = exp(logS);

  int timeSteps = Y.size();
  Type nll = 0;

  // Contributions from state transitions
  for (int i = 1; i < timeSteps; i++) {
    Type m = X[i - 1] + r * (1.0 - pow(exp(X[i - 1]) / K, theta));
    nll -= dnorm(X[i], m, sqrt(Q), true);
  }

  // Contributions from observations
  for (int i = 0; i < timeSteps; i++) {
    nll -= keep(i) * dpois(Y[i], S * exp(X[i]),
                           true);  // keep(i) for one-step predictions
  }

  return nll;
}
Type objective_function<Type>::operator() ()
{
  // Data
  DATA_VECTOR( y_i ); //capital letters - macro - Kasper built into C++ code

  // Parameters
  PARAMETER( mean );
  PARAMETER( log_sd );

  // Objective funcction
  Type sd = exp(log_sd);
  Type jnll = 0;
  int n_data = y_i.size();

  // Probability of data conditional on fixed effect values
  for( int i=0; i<n_data; i++){
    jnll -= dnorm( y_i(i), mean, sd, true );
  }
  
  // Reporting
  return jnll;
}
Example #22
0
Type objective_function<Type>::operator() ()
{
  DATA_VECTOR(x);
  DATA_VECTOR(y);

  int n = y.size();

  PARAMETER(b0);
  PARAMETER(b1);
  PARAMETER(logSigma);
  vector<Type> yfit(n);

  Type neglogL = 0.0;

  //Call triple function
  yfit = b0 + b1*x;
  neglogL = -sum(dnorm(y, yfit, exp(logSigma), true));

  // JIM THORSON JUST ROCK'N TMB
  std::cout << b0<<" "<<b1<<"\n ";

  return neglogL;
}
Example #23
0
Type objective_function<Type>::operator() ()
{
  DATA_VECTOR(y);
  DATA_MATRIX(X)
  DATA_MATRIX(dd)
  PARAMETER_VECTOR(b);
  PARAMETER(a);
  PARAMETER(log_sigma);
  int n = dd.rows();

  // Construct joint negative log-likelihood
  joint_nll<Type> jnll(y, X, dd, b, a, log_sigma);

  // Random effect initial guess
  vector<Type> u(n);
  u.setZero();

  // Calculate Laplace approx (updates u)
  DATA_INTEGER(niter);
  Type res = laplace(jnll, u, niter);
  ADREPORT(u)

  return res;
}
Example #24
0
Type objective_function<Type>::operator() ()
{
  DATA_VECTOR(height);
  DATA_VECTOR(times);
  DATA_IVECTOR(timeidx);
  DATA_IARRAY(trackinfo);
  DATA_VECTOR(weights);

  PARAMETER(logSigma);
  PARAMETER(logSigmaRW);
  PARAMETER(logitp);
  PARAMETER_VECTOR(u);

  int timeSteps=times.size();
  int obsDim=height.size();
  int noTracks=trackinfo.dim[0];

  Type p=ilogit(logitp); 
  
  Type ans=0;
 
  Type sdRW=exp(logSigmaRW);
  for(int i=1;i<timeSteps;i++)
    ans += -dnorm(u(i),u(i-1),sdRW*sqrt(times(i)-times(i-1)),true); 

  Type sdObs=exp(logSigma);
  for(int t=0;t<noTracks;t++){
    vector<Type> sub=height.segment(trackinfo(t,0),trackinfo(t,2));
    vector<Type> subw=weights.segment(trackinfo(t,0),trackinfo(t,2));
    for(int i=0;i<trackinfo(t,2);i++){
      ans += nldens(sub(i),u(timeidx(trackinfo(t,0))-1),sdObs/sqrt(subw(i)),p);
    } 
  }

  return ans;
}
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 #26
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;
}
Example #27
0
static void rmtctl_hw_init(void)
{
	unsigned int st;

	/* Turn off CIR SW reset. */
	REG32_VAL(IRSWRST) = 1;
	REG32_VAL(IRSWRST) = 0;

	REG32_VAL(PARAMETER(0)) = 0x10a;
	REG32_VAL(PARAMETER(1)) = 0x8E;  		
	REG32_VAL(PARAMETER(2)) = 0x42;
	REG32_VAL(PARAMETER(3)) = 0x55;		
	REG32_VAL(PARAMETER(4)) = 0x9; 		
	REG32_VAL(PARAMETER(5)) = 0x13;
	REG32_VAL(PARAMETER(6)) = 0x13;

#ifdef RMT_CFG_REPEAT_KEY
	REG32_VAL(NEC_REPEAT_TIME_OUT_CTRL) = 0x1;
	REG32_VAL(NEC_REPEAT_TIME_OUT_COUNT) = 17965000;//(107.9ms * 1000000)/(1000/166.5)
	REG32_VAL(IRCTL)        =  0X100;//NEC repeat key
#else
       REG32_VAL(IRCTL)        =  0;//NEC repeat key
#endif

#ifdef RMT_CFG_FACTORY_NEC
	REG32_VAL(IRCTL) |= (0x20<<16) ; //BIT16-23->0x20,  BIT 24,25 -> 0
#else  //NEC
	REG32_VAL(IRCTL) |= (0x0<<16) |(0x1<<25);
#endif

#ifdef RMT_CFG_INT_CNT
	REG32_VAL(INT_MASK_CTRL) = 0x1;
	REG32_VAL(INT_MASK_COUNT) =50*1000000*1/3;//0x47868C0/4;//count for 1 sec 0x47868C0
#endif



	/* Set IR remoter vendor type */ 
	/* BIT[0]: IR Circuit Enable. */
	REG32_VAL(IRCTL) |= 0x1; /*  IR_EN */

	/* Read CIR status to clear IR interrupt. */
	st = REG32_VAL(IRSTS);

	
}
Example #28
0
File: ex1.cpp Project: yijay/TMB
Type objective_function<Type>::operator()()
{
 DATA_FACTOR(Sex);
 DATA_VECTOR(Age);
 DATA_VECTOR(Length);
 int n = Length.size();

 // These are the parameters (three are vectors; one is a scalar)
 PARAMETER_VECTOR(Linf);
 PARAMETER_VECTOR(Kappa);
 PARAMETER_VECTOR(t0);
 PARAMETER(LogSigma);
 Type Sigma = exp(LogSigma);
 vector<Type> LengthPred(n);

 // Provide the standard error of Sigma
 ADREPORT(Sigma);

 // Predictions and likelihoods
 for(int i=0;i<n;i++){
  Type Temp = Kappa(Sex(i))*(Age(i)-t0(Sex(i)));
  LengthPred(i) = Linf(Sex(i))*(1.0-exp(-Temp));
  }
 Type nll = -sum(dnorm(Length,LengthPred,Sigma,true));

 // Prediction for sex 1 and age 10
 Type Temp = Kappa(0)*(Type(10)-t0(0));
 Type PredLen10 = Linf(0)*(1.0-exp(-Temp));
 ADREPORT(PredLen10);

 // Predicted growth curve
 matrix<Type>LenPred(2,50);
 for (int Isex=0;Isex<2;Isex++)
  for (int Iage=1;Iage<=50;Iage++)
   {
   Temp = Kappa(Isex)*(Iage*1.0-t0(Isex));
   LenPred(Isex,Iage-1) = Linf(Isex)*(1.0-exp(-Temp));
   }
 REPORT(LenPred);

 return nll;
}
Example #29
0
Type objective_function<Type>::operator() ()
{
  // Data
  DATA_INTEGER( n_y );
  DATA_INTEGER( n_s );
  DATA_IVECTOR( s_i );
  DATA_VECTOR( y_i );

  // Parameters
  PARAMETER( x0 );
  //PARAMETER( log_sdz ); //variability in site effect
  //PARAMETER_VECTOR( z_s );  //site effect

  // Objective funcction
  Type jnll = 0;

  // Probability of data conditional on fixed and random effect values
  vector<Type> ypred_i(n_y);
  for( int i=0; i<n_y; i++){
    ypred_i(i) = exp( x0 );//+ z_s(s_i(i)) );
    jnll -= dpois( y_i(i), ypred_i(i), true );
  }

  // Probability of random coefficients
  //for( int s=0; s<n_s; s++){
  //  jnll -= dnorm( z_s(s), Type(0.0), exp(log_sdz), true );
  //}

  // Reporting
  //Type sdz = exp(log_sdz);

  //REPORT( sdz );
  //REPORT( z_s );
  REPORT( x0 );

  //ADREPORT( sdz );
  //ADREPORT( z_s );
  ADREPORT( x0 );

  return jnll;
}
Example #30
0
int main(int argc, char **argv)
{
  int flag_all = 0;
  int flag_debug = 0;
  char *input_file = "";
  char *log_level = "";
  int i;

  program = argv[0];
  parse_options(&argc, &argv, OPTIONS(
    FLAG('a', "all", flag_all, 1),
    FLAG('d', "debug", flag_debug, 1),
    PARAMETER('i', "input-file", input_file),
    PARAMETER_DEFAULT('l', "log-level", log_level, "error"),
    FLAG_CALLBACK('h', "help", help),
    FLAG_CALLBACK('v', 0, verbose)
  ));

  for (i = 0; i <= argc; ++i) {
    if (argv[i])
      printf("argv[%d] = \"%s\";\n", i, argv[i]);
    else
      printf("argv[%d] = 0;\n", i);
  }

  printf(
    "      All: %s\n"
    "    Debug: %s\n"
    "Verbosity: %d\n"
    "    Input: '%s'\n"
    "Log Level: '%s'\n",
    ON(flag_all),
    ON(flag_debug),
    verbosity,
    input_file,
    log_level
  );

  return 0;
}