00001
00002
00003
00004
00005
00006
00007 #include <admodel.h>
00008
00009 double inv_cumd_norm(const double& x);
00010 double inv_cumd_cauchy(const double& x);
00011 double inv_cumd_norms(const double& x);
00012 double cumd_norm(const double& x);
00013 double cumd_cauchy(const double& x);
00014 double density_cauchy(const double& x);
00015 double myran1(long int&);
00016 double better_rand(long int&);
00017
00018 double log_likelihood_mixture(const double& x);
00019
00020 void multivariate_mixture(const dvector& _mix,int nvar,long int& iseed,
00021 const double& _log_density_normal, const double& _log_density_cauchy,
00022 const double& _log_density_small_normal,int is);
00023
00024 dvector bounded_multivariate_cauchy(int nvar, const dvector& a1,
00025 dvector& b1, const dmatrix& ch,long int& iseed, const double& lprob,
00026 double& log_tprob, const int& outflag);
00027
00028 dvector bounded_robust_multivariate_normal(int nvar, const dvector& a1,
00029 dvector& b1, const dmatrix& ch, const dmatrix& ch3, const dmatrix& chinv,
00030 const dmatrix& ch3inv, double contaminant,long int& iseed,
00031 const double& lprob, const double& lprob3, double& log_tprob,
00032 const int& outflag);
00033
00034 void function_minimizer::monte_carlo_routine(void)
00035 {
00036 initial_params::mc_phase=1;
00037 if (stddev_params::num_stddev_params==0) return;
00038 {
00039 int nvar=initial_params::nvarcalc();
00040 dvector x(1,nvar);
00041 dvector jac(1,nvar);
00042 initial_params::xinit(x);
00043 initial_params::stddev_scale(jac,x);
00044 dvector bmn(1,nvar);
00045 bmn.initialize();
00046
00047 dvector scale(1,nvar);
00048 dvector diag(1,nvar);
00049 dvector v(1,nvar);
00050 dmatrix S(1,nvar,1,nvar);
00051 {
00052 adstring tmpstring="admodel.cov";
00053 if (ad_comm::wd_flag)
00054 tmpstring = ad_comm::adprogram_name + ".cov";
00055 uistream cif((char*)tmpstring);
00056 if (!cif)
00057 {
00058 cerr << "Error trying to open file " << tmpstring
00059 << " for reading" << endl;
00060 }
00061 int tmp_nvar = 0;
00062 cif >> tmp_nvar;
00063 if (nvar !=tmp_nvar)
00064 {
00065 cerr << "Incorrect number of independent variables in file"
00066 << tmpstring << endl;
00067 exit(1);
00068 }
00069 cif >> S;
00070 if (!cif)
00071 {
00072 cerr << "error reading covariance matrix from "
00073 << tmpstring << endl;
00074 exit(1);
00075 }
00076
00077 initial_params::mc_phase=0;
00078 initial_params::stddev_scale(scale,x);
00079 initial_params::mc_phase=1;
00080 {
00081 dmatrix tmp(1,nvar,1,nvar);
00082
00083 int i;
00084 for (i=1;i<=nvar;i++)
00085 {
00086 tmp(i,i)=S(i,i)*scale(i)*scale(i);
00087 for (int j=1;j<i;j++)
00088 {
00089 tmp(i,j)=S(i,j)*scale(i)*scale(j);
00090 tmp(j,i)=tmp(i,j);
00091 }
00092 diag(i)=sqrt(tmp(i,i));
00093 }
00094 S=tmp;
00095
00096
00097
00098 for (i=1;i<=nvar;i++)
00099 {
00100 S(i,i)=S(i,i)/(diag(i)*diag(i));
00101 for (int j=1;j<i;j++)
00102 {
00103 S(i,j)=S(i,j)/(diag(i)*diag(j));
00104 S(j,i)=S(i,j);
00105 }
00106 }
00107 }
00108 }
00109 dmatrix CHD= choleski_decomp(S);
00110 int sgn;
00111 ln_det(S,sgn);
00112
00113
00114
00115 dmatrix chdec(1,nvar,1,nvar);
00116 chdec=choleski_decomp(S);
00117 {
00118 ofstream ofs("chol2");
00119 ofs << chdec << endl;
00120 }
00121
00122
00123
00124 initial_params::montecarlo_scale(scale,x);
00125 {
00126 ofstream ofs("admodel.tmp");
00127 dmatrix covar(1,nvar,1,nvar);
00128 }
00129 double ll=0.0;
00130 initial_params::add_random_vector(jac,bmn,ll,diag);
00131
00132
00133
00134
00135
00136
00137
00138
00139 dmatrix ch3=3.*chdec;
00140 dmatrix chinv=inv(chdec);
00141 dmatrix ch3inv=inv(ch3);
00142
00143
00144 {
00145 long int iseed=0;
00146 int number_sims= 500;
00147
00148 iseed=-33517;
00149 cout << "Enter value for seed" << endl;
00150 cin >> iseed;
00151 cout << "Enter number of simulations" << endl;
00152 cin >> number_sims;
00153 if (iseed>0)
00154 {
00155 iseed=-iseed;
00156 }
00157 better_rand(iseed);
00158
00159
00160
00161
00162
00163 independent_variables parsave(1,nvar);
00164
00165 int ii=1;
00166 initial_params::copy_all_values(parsave,ii);
00167 gradient_structure::set_NO_DERIVATIVES();
00168
00169
00170
00171 double log_tprob_normal=0.0;
00172 double log_tprob_small_normal=0.0;
00173 double log_tprob_cauchy=0.0;
00174
00175 int ndvar=stddev_params::num_stddev_calc();
00176 dvector param_values(1,ndvar);
00177
00178
00179 ii=1;
00180 initial_params::restore_all_values(parsave,ii);
00181
00182
00183
00184 #ifdef USE_ADPVM
00185 if (ad_comm::pvm_manager)
00186 {
00187 switch (ad_comm::pvm_manager->mode)
00188 {
00189 case 1:
00190 pvm_master_get_monte_carlo_value(nvar,x);
00191 break;
00192 case 2:
00193 pvm_slave_get_monte_carlo_value(nvar);
00194 break;
00195 default:
00196 cerr << "error illegal value for pvm_manager->mode" << endl;
00197 exit(1);
00198 }
00199 }
00200 else
00201 #endif
00202 {
00203 get_monte_carlo_value(nvar,x);
00204 }
00205
00206 multivariate_mixture(bmn,nvar,iseed,log_tprob_normal,
00207 log_tprob_cauchy,log_tprob_small_normal,-1);
00208 bmn=elem_div(bmn,scale);
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 dvector y(1,nvar);
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 for (int i=1;i<=number_sims;i++)
00247 {
00248 log_tprob_normal=0.0;
00249 log_tprob_small_normal=0.0;
00250 log_tprob_cauchy=0.0;
00251
00252 ii=1;
00253 initial_params::restore_all_values(parsave,ii);
00254
00255 double mixprob=better_rand(iseed);
00256 int mixswitch;
00257
00258 if (mixprob<.05)
00259 {
00260 mixswitch=1;
00261 }
00262 else if (mixprob<.50)
00263 {
00264 mixswitch=2;
00265 }
00266 else
00267 {
00268 mixswitch=0;
00269 }
00270 multivariate_mixture(bmn,nvar,iseed,log_tprob_normal,
00271 log_tprob_cauchy,log_tprob_small_normal,mixswitch);
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 dvector bmn1=CHD*bmn;
00289
00290
00291 ll=0.0;
00292 initial_params::add_random_vector(jac,bmn1,ll,diag);
00293 initial_params::xinit(y);
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 #ifdef USE_ADPVM
00307 if (ad_comm::pvm_manager)
00308 {
00309 switch (ad_comm::pvm_manager->mode)
00310 {
00311 case 1:
00312 pvm_master_get_monte_carlo_value(nvar,x);
00313 break;
00314 case 2:
00315 pvm_master_get_monte_carlo_value(nvar,x);
00316 break;
00317 default:
00318 cerr << "error illega value for pvm_manager->mode" << endl;
00319 exit(1);
00320 }
00321 }
00322 else
00323 #endif
00324 {
00325 get_monte_carlo_value(nvar,x);
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335 ii=1;
00336 stddev_params::copy_all_values(param_values,ii);
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362 }
00363 initial_params::mc_phase=0;
00364 }
00365 }
00366 }