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));
Type objective_function<Type>::operator() () { DATA_VECTOR (a); DATA_ARRAY (b); DATA_MATRIX (c); DATA_SPARSE_MATRIX (d); PARAMETER (p); REPORT(a); REPORT(b); REPORT(c); REPORT(d); REPORT(p); //// Vector of anything: vector<matrix<Type> > voa(2); voa[0] = c; voa[1] = c; REPORT(voa); return 0; }
Type objective_function<Type>::operator() () { // Data objects DATA_VECTOR(v1); DATA_MATRIX(m1); DATA_ARRAY(a1); // Parameter objects PARAMETER(p) // Not used in this example // Obtaining dimensions of objects REPORT(a1.dim); vector<int> d2(2); d2(0) = m1.rows(); d2(1) = m1.cols(); REPORT(d2); int d3 = v1.size(); REPORT(d3); // Matrix multiplication versus elementwise multiplication (similar for addition, subtraction,...) matrix<Type> m1m1 = m1*m1; // Matrix multiplication of matrices REPORT(m1m1); array<Type> a1a1 = a1*a1; // Element-wise multiplication of arrays REPORT(a1a1); matrix<Type> m1m1_by_element = (m1.array()*m1.array()).matrix(); // Elementwise multiplication of matrices REPORT(m1m1_by_element); array<Type> a1a1_matrix_mult(2,2); a1a1_matrix_mult = (a1.matrix()*a1.matrix()).array(); // Matrix multiplication of arrays REPORT(a1a1_matrix_mult); // Matrix-vector multiplication REPORT(m1*v1); // matrix-vector product (linear algebra style) Type v1_norm2 = (v1*v1).sum(); // Inner product v1*v1 REPORT(v1_norm2); // Indexing objects m1(1,1); // Element (1,1) of matrix m1 m1.row(1); // 2nd row of matrix m1 m1.col(1); // 2nd col of matrix m1 a1(1,1); // Element (1,1) of array a1 a1.transpose().col(1); // 2nd row of array a1 a1.col(1); //2nd col of array a1 v1(1); // 2nd element of vector v1 // Subsetting matrices and vectors v1.head(1); // First element of v1 v1.tail(1); // Last element of v1 m1.block(0,0,1,1); // Block of m1 consisting of m1(1,1) // Subsetting arrays // See R help pages for "template", i.e. "help(template)" in R // Generic matrix operations that we must ensure compiles m1.transpose(); m1.diagonal(); m1.asDiagonal(); Type ans; return ans; }
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; }