function y = cross_val(c,obs) %% c is the point being left out, obs is the observations %% cross validation. %% omit one point, estimate the paras, then estimate the point using the %% parameters. Easy case to include, leave out the first observation and %% see what happens. May not be the best one to start with since it should %% carry alot of weight. clear i k samples s_r expected_fails variance_fails recreatd point_left_out=c; basics_f %just to be sure, re sample points from the prior. %run sampling_f display('sampling from prior (again)') sampling_f % now compute the weights associated with each point. display('compute the weights for these prior samples') for k=1:KK l_weight(k)=0; %re calculate the failure rate s_r = samples(k,2) + samples(k,1)*exp(-samples(k,3).*(0:I)); %compute the failure rate for the sampled values FOR ALL POINTS INCLUDING THE ONE ASSOCIATED WITH THE POINT OMMITTED s_r = samples(k,2) + samples(k,1)*exp(-samples(k,3).*(0:I)); %compute the failure rate for the sampled values expected_fails = expected_function(samples(k,1),samples(k,2),samples(k,3)); variance_fails = variances_func(samples(k,1),samples(k,2),samples(k,3)); index=1; for i=2:I %first obs is zero since components are working when they are produced. if i==point_left_out %% ESSENTIALLY, WHAT THIS IS DOING IS COMPUTING THE WEIGHTS THE EXACT SAME WAY BUT IGNORING THE EFFECT THAT POINT 'c' HAS index=index+1; else l_weight(k) = l_weight(k) + log_pnorm(obs(i),expected_fails(i),variance_fails(i)); end end end clear s_r expected_fails variance_fails k display('computing weights') maxt = max(exp(l_weight)); maxt = log(maxt); for k=1:KK temp1(k) = l_weight(k) - maxt; temp2(k) = exp(temp1(k)); end temp3 = sum(temp2); temp4 = maxt+log(temp3); s=exp(temp4); for k=1:KK weight(k) = exp(l_weight(k))/s; end clear temp1 temp2 temp3 temp4 l_weight maxt s_cdf=cumsum(weight); for i=1:MM u=rand(1); sample_x = 1; while (u > s_cdf(sample_x)) sample_x=sample_x+1; end posterior_sim_cross(i,:)=samples(sample_x,:); end clear sample_x u i %% Now, have sampled from the posterior but omitted one point. %% Now, recreate that point using the estimated values of the parameters. p x_post_sim_cross = mean(posterior_sim_cross); display('cross val values') x_post_sim_cross recreatd = expected_function(x_post_sim_cross(1),x_post_sim_cross(2),x_post_sim_cross(3)); var_recreatd = variances_func(x_post_sim_cross(1),x_post_sim_cross(2),x_post_sim_cross(3)); display('obs(c) - expected value 2*SD') [obs(point_left_out) recreatd(point_left_out) 2*sqrt(var_recreatd(point_left_out))] y=recreatd(point_left_out);