├── Bayesian Model Updating Frame Strucure with M.Jhon's experimental data.zip ├── myrand.m ├── README.md ├── problemA_p_theta_rnd.m ├── problemA_p_theta_pdf.m ├── Likelihood_Ferson_Challenge.m ├── Ferson_Challenge_Updating.m └── problemA_tmcmc.m /Bayesian Model Updating Frame Strucure with M.Jhon's experimental data.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Roberock/Bayesian_Model_Updating/HEAD/Bayesian Model Updating Frame Strucure with M.Jhon's experimental data.zip -------------------------------------------------------------------------------- /myrand.m: -------------------------------------------------------------------------------- 1 | function Vrand = myrand(Nsample) 2 | %UNTITLED Summary of this function goes here 3 | % Detailed explanation goes here 4 | load('ranstream.mat'); 5 | 6 | stream = RandStream('mt19937ar','Seed',0); 7 | stream.State = savedState; 8 | 9 | Vrand = rand(stream,Nsample,1); 10 | 11 | end 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bayesian_Model_Updating 2 | Bayesian Framework for Updating 3 | 4 | 5 | Data for cracked suspension arm can be downloaded here 6 | [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4678117.svg)](https://doi.org/10.5281/zenodo.4678117) 7 | 8 | 9 | 10 | [] Roberto Rocchetta, Matteo Broggi, Quentin Huchet, Edoardo Patelli, On-line Bayesian model updating for structural health monitoring, 11 | Mechanical Systems and Signal Processing, Volume 103, 2018, Pages 174-195, ISSN 0888-3270, 12 | https://doi.org/10.1016/j.ymssp.2017.10.015. 13 | (https://www.sciencedirect.com/science/article/pii/S088832701730540X) 14 | -------------------------------------------------------------------------------- /problemA_p_theta_rnd.m: -------------------------------------------------------------------------------- 1 | function p = problemA_p_theta_rnd(lb, ub, N) 2 | % Sampling from the prior PDF for the example in problemA.m 3 | % 4 | % USAGE: 5 | % p = problemA_p_theta_rnd(lb, ub, N) 6 | % 7 | % INPUTS: 8 | % lb, ub = lower and upper bounds of the uniform PDF 1 x dim_theta 9 | % N = number of samples to generate 10 | % 11 | % OUTPUTS: 12 | % p = samples N x dim_theta 13 | % 14 | % EXAMPLE: 15 | %{ 16 | p = problemA_p_theta_rnd([1 2 3 4 5], [2 3 4 5 6], 10) 17 | %} 18 | 19 | %-------------------------------------------------------------------------- 20 | % who when observations 21 | %-------------------------------------------------------------------------- 22 | % Diego Andres Alvarez Jul-24-2013 First algorithm 23 | %-------------------------------------------------------------------------- 24 | % Diego Andres Alvarez - daalvarez@unal.edu.co 25 | 26 | % Here an uniform non informative prior is employed 27 | 28 | dim_theta = length(lb); 29 | 30 | p = zeros(N, dim_theta); 31 | for i = 1:dim_theta 32 | p(:,i) = unifrnd(lb(i), ub(i), N, 1); 33 | end 34 | 35 | return; 36 | -------------------------------------------------------------------------------- /problemA_p_theta_pdf.m: -------------------------------------------------------------------------------- 1 | function p = problemA_p_theta_pdf(theta, lb, ub) 2 | % Definition of the prior PDF for the example in problemA.m 3 | % 4 | % USAGE: 5 | % p = problemA_p_theta_pdf(theta, lb, ub) 6 | % 7 | % INPUTS: 8 | % theta = samples N x dim_theta 9 | % lb, ub = lower and upper bounds of the uniform PDF 1 x dim_theta 10 | % 11 | % OUTPUTS: 12 | % p = p_theta(theta) N x 1 13 | % 14 | % EXAMPLE: 15 | %{ 16 | rnd = randn(100,3); 17 | p = problemA_p_theta_pdf(rnd, [-1 -1 -1], [1 1 1]); 18 | %} 19 | 20 | %-------------------------------------------------------------------------- 21 | % who when observations 22 | %-------------------------------------------------------------------------- 23 | % Diego Andres Alvarez Jul-24-2013 First algorithm 24 | %-------------------------------------------------------------------------- 25 | % Diego Andres Alvarez - daalvarez@unal.edu.co 26 | 27 | % Here an uniform non informative prior is employed 28 | 29 | [n, dim_theta] = size(theta); 30 | 31 | marginal_PDF = zeros(n, dim_theta); 32 | for i = 1:dim_theta 33 | marginal_PDF(:,i) = unifpdf(theta(:,i), lb(i), ub(i)); 34 | end 35 | 36 | p = prod(marginal_PDF,2); 37 | 38 | return; 39 | -------------------------------------------------------------------------------- /Likelihood_Ferson_Challenge.m: -------------------------------------------------------------------------------- 1 | function logL = Likelihood_Ferson_Challenge(D, theta) 2 | % Calculation of the log_likelihood for the example in problemA.m 3 | % 4 | % USAGE: 5 | % logL = problemA_log_p_D_theta(D, theta) 6 | % 7 | % INPUTS: 8 | % D = experimental observations nobs x dim_x 9 | % theta = epistemic parameters npar x dim_theta 10 | % 11 | % OUTPUTS: 12 | % logL(i) = loglikelihood for the set of parameters theta(i,:) and the 13 | % data D, i = 1, 2, ...npar. logL = npar x 1 14 | 15 | %-------------------------------------------------------------------------- 16 | % who when observations 17 | %-------------------------------------------------------------------------- 18 | % Diego Andres Alvarez Jul-24-2013 First algorithm 19 | % Rocchetta Roberto Gen-12-2016 Modified for crack detection 20 | %-------------------------------------------------------------------------- 21 | % Diego Andres Alvarez - daalvarez@unal.edu.co 22 | 23 | %% 24 | npar = size(theta,1); % number of thetas to evaluate 25 | logL = zeros(npar,1); 26 | for i = 1:npar 27 | logL(i) = sum(log(p_x_theta_pdf(D, theta(i,:)))); 28 | %logL(i) = sum((p_x_theta_pdf(D, theta(i,:),net))); 29 | if isinf(logL(i)) 30 | logL(i) = -1e10; 31 | end 32 | end 33 | 34 | return; 35 | 36 | %% 37 | function p = p_x_theta_pdf(x, theta_i) 38 | 39 | 40 | Mu=theta_i(1); 41 | Sig=theta_i(2); 42 | Vu=theta_i(3); 43 | Omega=theta_i(4); 44 | A=theta_i(5); 45 | B=theta_i(6); 46 | % check the normal distribution parameter,if lbound greater than ubound, fix the likelihoood to infinity and evaluate next theta 47 | if A>B 48 | p=Inf; 49 | return 50 | end 51 | 52 | Ns=2000; %MC samples for the probabilistic model to be tested 53 | X = normrnd(Mu,Sig,[Ns,1]); 54 | Y = betarnd(Vu,Omega,[Ns,1]); 55 | Z = unifrnd(A,B,[Ns,1]); 56 | W_model=X.*Y./Z; 57 | %% Estimate the PDF p_x_theta_pdf(x | theta) 58 | %Type 1) f = ksdensity(x,xi) specifies the vector xi of values, where the density 59 | % estimate of the data in x is to be evaluated 60 | p = ksdensity(W_model, x); % p(i) = p_x_theta_pdf(x(i,:) | theta) 61 | 62 | %% Type 1) compute 2-sided Kolmogorov-Smirnov test values for each of the samples 63 | % binEdges = [-inf ; sort([x;W_model]) ; inf]; 64 | % binCounts1 = histc (x , binEdges, 1); 65 | % binCounts2 = histc (W_model , binEdges, 1); 66 | % sampleCDF1 = cumsum(binCounts1)./sum(binCounts1); 67 | % sampleCDF2 = cumsum(binCounts2)./sum(binCounts2); 68 | % 69 | % ks = abs(sampleCDF1(ismember(binEdges,x))-sampleCDF2(ismember(binEdges,x)))'; 70 | % assuming that the ks value are zero mean normally distributed, evaluate 71 | % the value of the normal pdf for these ks. 72 | % p = normpdf(ks); 73 | 74 | return; 75 | -------------------------------------------------------------------------------- /Ferson_Challenge_Updating.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear variables 3 | close all 4 | %% Ferson Challenge 5 | % Given n samples from 3 ranodm W=(X.*Y)./Z and knowing that 6 | % X~Gaussian(\Mu,\Sig); \Sig>=0 7 | % Y~Beta(\nu,\Omega);\nu>=0 \Omega>=0 8 | % Z~Uniform(\A,\B); 0<\A<=\B 9 | % Estimate the parameters of the probabilistic model [ Mu Sig Vu Omega A B ]. 10 | % what is the uncertianty about the estimator? 11 | 12 | %% 1) extract a random taget for the 'exact' real probabilistic modelparameters 13 | Mu=unifrnd(-50,50); 14 | Sig=unifrnd(1,50); 15 | Vu=unifrnd(1,50); 16 | Omega=unifrnd(1,50); 17 | A=unifrnd(1,30); 18 | B=unifrnd(10,50); 19 | 20 | Targets=[Mu;Sig;Vu;Omega;A;B]; 21 | 22 | Ns=10; 23 | X = normrnd(Mu,Sig,[1 Ns]); 24 | Y = betarnd(Vu,Omega,[1 Ns]); 25 | Z = unifrnd(A,B,[1 Ns]); 26 | W_10=X.*Y./Z; 27 | 28 | Ns=100; 29 | X = normrnd(Mu,Sig,[1 Ns]); 30 | Y = betarnd(Vu,Omega,[1 Ns]); 31 | Z = unifrnd(A,B,[1 Ns]); 32 | W_100=X.*Y./Z; 33 | 34 | Ns=250; 35 | X = normrnd(Mu,Sig,[1 Ns]); 36 | Y = betarnd(Vu,Omega,[1 Ns]); 37 | Z = unifrnd(A,B,[1 Ns]); 38 | W_250=X.*Y./Z; 39 | % Prealocate Memory 40 | Ncases=3; % 3 different n=number of samples available 41 | ThetA=cell(1,Ncases); 42 | [MU_posterior,VAR_Theta,P90Theta,P10Theta]=deal(zeros(Ncases,6)); 43 | Computationa_Time=zeros(1,Ncases); 44 | for k=1:Ncases 45 | if k==1 % update for n=10 46 | Data=W_10'; 47 | elseif k==2 % update for n=100 48 | Data=W_100'; 49 | elseif k==3 % update for n=250 50 | Data=W_250'; 51 | end 52 | %display target 53 | display([' The target parameter vector is [Mu Sig Vu Omega A B] = [' num2str(Targets') ']']) 54 | display(['Number of samples n = ' num2str(length(Data))]) 55 | %% TRANSITIONAL MCMC AND BAYESIAN UPDATING 56 | tic 57 | %% VARIABLE NAME LOWER_BOUND UPPER_BOUND 58 | variables = { ... 59 | '\mu' -50 50 % interval information 60 | '\sigma' 1 50 % interval information 61 | '\nu' 1 50 % interval information 62 | '\omega' 1 50 % interval information 63 | 'a' 1 30 % interval information 64 | 'b' 10 50 % interval information 65 | }; 66 | % Defining the prior PDF p(theta) 67 | lb = cell2mat(variables(:,2))'; 68 | ub = cell2mat(variables(:,3))'; 69 | p_theta = @(x) problemA_p_theta_pdf(x, lb, ub); 70 | p_thetarnd = @(N) problemA_p_theta_rnd(lb, ub, N); 71 | % The loglikelihood of D given theta 72 | log_p_D_theta = @(theta) Likelihood_Ferson_Challenge(Data, theta); 73 | %% Bayesian estimation of theta: bayesian model updating using TMCMC 74 | Nsamples =30; % number of samples from prior; 75 | fprintf('Nsamples TMCMC = %d\n', Nsamples); 76 | [samples_ftheta_D] = problemA_tmcmc(log_p_D_theta, p_theta, p_thetarnd, Nsamples); 77 | Computationa_Time(k)=toc; 78 | display(['CPU Time for the Detection: ' num2str(Computationa_Time(k)) ' seconds']) 79 | %% Save results and some statistic data 80 | ThetA{k}=samples_ftheta_D; 81 | MU_posterior(k,:)=mean(samples_ftheta_D); 82 | VAR_Theta(k,:)=var(samples_ftheta_D); 83 | P90Theta(k,:)=prctile(samples_ftheta_D,90); 84 | P10Theta(k,:)=prctile(samples_ftheta_D,10); 85 | %% Plot 86 | figure(k) 87 | for i=1:6 88 | subplot(3,2,i) 89 | hold on 90 | % hist(samples_ftheta_D(:,i), ceil(sqrt(Nsamples))); 91 | % line([ Target_updating(i) Target_updating(i)],[0 Ymax],'LineWidth',3) 92 | % line([ mean(samples_ftheta_D(:,i)) mean(samples_ftheta_D(:,i))],[0 Ymax],'LineWidth',1) 93 | % KDensity plots 94 | ksdensity(samples_ftheta_D(:,i), 'support', [lb(i) ub(i)]); 95 | Ymax=max(ksdensity(samples_ftheta_D(:,i), 'support', [lb(i) ub(i)])); 96 | line([ Targets(i) Targets(i)],[0 Ymax],'LineWidth',5,'Color',[0.5 0.5 0.5],'LineStyle',':') 97 | line([ mean(samples_ftheta_D(:,i)) mean(samples_ftheta_D(:,i))],[0 Ymax],'LineWidth',3,'Color',[1 0 0]) 98 | line([P90Theta(k,i) P90Theta(k,i)],[0 Ymax],'LineWidth',3,'Color',[0 1 0]) 99 | line([P10Theta(k,i) P10Theta(k,i)],[0 Ymax],'LineWidth',3,'Color',[0 1 0]) 100 | ylabel('PDF') 101 | xlabel(variables{i}) 102 | box on 103 | grid on; 104 | end 105 | title(['Number of samples n = ' num2str(length(Data))]) 106 | pause(0.01) 107 | end -------------------------------------------------------------------------------- /problemA_tmcmc.m: -------------------------------------------------------------------------------- 1 | function [samples_fT_D, log_fD] = problemA_tmcmc(log_fD_T, fT, sample_from_fT, N) 2 | %% Transitional Markov Chain Monte Carlo, modified for problem A 3 | % 4 | % This program implements a method described in: 5 | % Ching, J. and Chen, Y. (2007). "Transitional Markov Chain Monte Carlo 6 | % Method for Bayesian Model Updating, Model Class Selection, and Model 7 | % Averaging." J. Eng. Mech., 133(7), 816-832. 8 | % 9 | % Usage: 10 | % [samples_fT_D, fD] = tmcmc_v1(fD_T, fT, sample_from_fT, N); 11 | % 12 | % where: 13 | % 14 | % inputs: 15 | % log_fD_T = function handle of log(fD_T(t)) 16 | % fT = function handle of fT(t) 17 | % sample_from_fT = handle to a function that samples from of fT(t) 18 | % N = number of samples of fT_D to generate 19 | % 20 | % outputs: 21 | % samples_fT_D = samples of fT_D (N x D) 22 | % log_fD = log(evidence) = log(normalization constant) 23 | 24 | % ------------------------------------------------------------------------ 25 | % who when observations 26 | %-------------------------------------------------------------------------- 27 | % Diego Andres Alvarez Jul-24-2013 First algorithm 28 | %-------------------------------------------------------------------------- 29 | % Diego Andres Alvarez - daalvarez@unal.edu.co 30 | 31 | % We will assume in this algorithm that N0 = N1 = ... = Nm 32 | %% Number of cores 33 | % p = gcp; 34 | % if ~isempty(p) 35 | % Ncores = p.NumWorkers; 36 | % else 37 | % Ncores = 1; 38 | % end 39 | % if Ncores > 1 40 | % fprintf('TMCMC is running on %d cores.\n', Ncores); 41 | % end; 42 | 43 | %% Constants 44 | % beta is a control parameter that is chosen to balance the potential for 45 | % large MCMC moves while mantaining a reasonable rejection rate 46 | beta = 0.2; 47 | S = ones(1,50); 48 | with_replacement = true; % DO NOT CHANGE!!! 49 | plot_graphics = false; 50 | burnin = 50; 51 | lastburnin = 200; % burnin in the last iteration 52 | 53 | %% Obtain N samples from the prior pdf f(T) 54 | j = 0; 55 | thetaj = sample_from_fT(N); % theta0 = N x D 56 | pj = 0; % p0 = 0 (initial tempering parameter) 57 | D = size(thetaj, 2); % size of the vector theta 58 | 59 | %% Initialization of matrices and vectors 60 | thetaj1 = zeros(N, D); 61 | 62 | %% Main loop 63 | while pj < 1 64 | %% Plot the sampled points 65 | if (plot_graphics) 66 | figure 67 | plot(thetaj(:,1), thetaj(:,2), 'b.'); 68 | hold on; 69 | ax = axis; 70 | [xx, yy] = meshgrid(linspace(ax(1),ax(2),100), linspace(ax(3), ax(4), 99)); 71 | if j == 0 72 | zz = reshape(fT([xx(:) yy(:)]), 99, 100); 73 | else 74 | zz = reshape(fj1([xx(:) yy(:)]), 99, 100); 75 | end 76 | contour(xx, yy, zz, 50, 'r'); 77 | grid on; 78 | title(sprintf(... 79 | 'Samples of f_{%d} and contour levels of f_{%d} (red) and f_{%d} (black)', ... 80 | j, j, j+1)); 81 | end; 82 | 83 | j = j+1; 84 | 85 | %% Calculate the tempering parameter p(j+1): 86 | log_fD_T_thetaj = log_fD_T(thetaj); 87 | if any(isinf(log_fD_T_thetaj)) 88 | error('The prior distribution is too far from the true region'); 89 | end 90 | pj1 = calculate_pj1(log_fD_T_thetaj, pj); 91 | fprintf('TMCMC: Iteration j = %2d, pj1 = %f\n', j, pj1); 92 | 93 | %% Compute the plausibility weight for each sample wrt f_{j+1} 94 | fprintf('Computing the weights ...\n'); 95 | % wj = fD_T(thetaj).^(pj1-pj); % N x 1 (eq 12) 96 | wj = exp((pj1-pj)*log_fD_T(thetaj)); 97 | wj_norm = wj./sum(wj); % normalization of the weights 98 | 99 | %% Compute S(j) = E[w{j}] (eq 15) 100 | S(j) = mean(wj); 101 | 102 | %% Do the resampling step to obtain N samples from f_{j+1}(theta) and 103 | % then perform Metropolis-Hastings on each of these samples using as a 104 | % stationary PDF "fj1" 105 | % fj1 = @(t) fT(t).*log_fD_T(t).^pj1; % stationary PDF (eq 11) f_{j+1}(theta) 106 | log_fj1 = @(t) log(fT(t)) + pj1*log_fD_T(t); 107 | 108 | if (plot_graphics) 109 | % In the definition of fj1 we are including the normalization 110 | % constant prod(S(1:j)) 111 | fj1 = @(t) exp(log(fT(t)) + pj1*log_fD_T(t) - sum(log(S(1:j)))); 112 | zz = reshape(fj1([xx(:) yy(:)]), 99, 100); 113 | contour(xx, yy, zz, 50, 'k'); 114 | end 115 | 116 | % and using as proposal PDF a Gaussian centered at thetaj(idx,:) and 117 | % with covariance matrix equal to an scaled version of the covariance 118 | % matrix of fj1: 119 | 120 | % weighted mean 121 | mu = zeros(1, D); 122 | for l = 1:N 123 | mu = mu + wj_norm(l)*thetaj(l,:); % 1 x N 124 | end 125 | 126 | % scaled covariance matrix of fj1 (eq 17) 127 | cov_gauss = zeros(D); 128 | for k = 1:N 129 | % this formula is slightly different to eq 17 (the transpose) 130 | % because of the size of the vectors)m and because Ching and Chen 131 | % forgot to normalize the weight wj: 132 | tk_mu = thetaj(k,:) - mu; 133 | cov_gauss = cov_gauss + wj_norm(k)*(tk_mu'*tk_mu); 134 | end 135 | cov_gauss = beta^2 * cov_gauss; 136 | 137 | % proposal distribution 138 | proppdf = @(x,y) problemA_proppdf(x, y, cov_gauss, fT); %q(x,y) = q(x|y). 139 | proprnd = @(x) problemA_proprnd(x, cov_gauss, fT); %mvnrnd(x, cov_gauss, 1); 140 | 141 | %% During the last iteration we require to do a better burnin in order 142 | % to guarantee the quality of the samples: 143 | if pj1 == 1 144 | burnin = lastburnin; 145 | end; 146 | 147 | %% Start N different Markov chains 148 | fprintf('Markov chains ...\n\n'); 149 | parfor i = 1:N 150 | %% Sample one point with probability wj_norm 151 | idx = randsample(N, 1, with_replacement, wj_norm); 152 | 153 | % smpl = mhsample(start, nsamples, 154 | % 'pdf', pdf, 'proppdf', proppdf, 'proprnd', proprnd); 155 | % start = row vector containing the start value of the Markov Chain, 156 | % nsamples = number of samples to be generated 157 | [thetaj1(i,:), acceptance_rate] = mhsample(thetaj(idx, :), 1, ... 158 | 'logpdf', log_fj1, ... 159 | 'proppdf', proppdf, ... 160 | 'proprnd', proprnd, ... 161 | 'thin', 3, ... 162 | 'burnin', burnin); 163 | % According to Cheung and Beck (2009) - Bayesian model updating ..., 164 | % the initial samples from reweighting and the resample of samples of 165 | % fj, in general, do not exactly follow fj1, so that the Markov 166 | % chains must "burn-in" before samples follow fj1, requiring a large 167 | % amount of samples to be generated for each level. 168 | 169 | %% Adjust the acceptance rate (optimal = 23%) 170 | % See: http://www.dms.umontreal.ca/~bedard/Beyond_234.pdf 171 | %{ 172 | if acceptance_rate < 0.3 173 | % Many rejections means an inefficient chain (wasted computation 174 | %time), decrease the variance 175 | beta = 0.99*beta; 176 | elseif acceptance_rate > 0.5 177 | % High acceptance rate: Proposed jumps are very close to current 178 | % location, increase the variance 179 | beta = 1.01*beta; 180 | end 181 | %} 182 | end 183 | fprintf('\n'); 184 | 185 | %% Prepare for the next iteration 186 | thetaj = thetaj1; 187 | pj = pj1; 188 | end 189 | 190 | % TMCMC provides N samples distributed according to f(T|D) 191 | samples_fT_D = thetaj; 192 | 193 | % estimation of f(D) -- this is the normalization constant in Bayes 194 | log_fD = sum(log(S(1:j))); 195 | 196 | return; % bye, bye 197 | 198 | 199 | %% Calculate the tempering parameter p(j+1) 200 | function pj1 = calculate_pj1(log_fD_T_thetaj, pj) 201 | % find pj1 such that COV <= threshold, that is 202 | % 203 | % std(wj) 204 | % --------- <= threshold 205 | % mean(wj) 206 | % 207 | % here 208 | % size(thetaj) = N x D, 209 | % wj = fD_T(thetaj).^(pj1 - pj) 210 | % e = pj1 - pj 211 | 212 | threshold = 1; % 100% = threshold on the COV 213 | 214 | % wj = @(e) fD_T_thetaj^e; % N x 1 215 | % Note the following trick in order to calculate e: 216 | % Take into account that e>=0 217 | wj = @(e) exp(abs(e)*log_fD_T_thetaj); % N x 1 218 | %fmin = @(e) std(wj(e))/mean(wj(e)) - threshold; 219 | fmin = @(e) std(wj(e)) - threshold*mean(wj(e)) + realmin; 220 | e = abs(fzero(fmin, 0)); % e is >= 0, and fmin is an even function 221 | if isnan(e) 222 | error('There is an error finding e'); 223 | end 224 | 225 | pj1 = min(1, pj + e); 226 | 227 | %{ 228 | figure 229 | p = linspace(0,0.3,10000); 230 | hold on 231 | plot(p, arrayfun(fmin, p)); 232 | plot(e,0,'rx'); 233 | grid minor; 234 | %} 235 | 236 | return; % bye, bye 237 | 238 | function y = problemA_proppdf(x, mu, covmat, box) 239 | % Proposal PDF for the Markov Chain. 240 | % Take into account that for problem A, box is the uniform PDF in the 241 | % feasible region. So if a point is out of bounds, this function will 242 | % return 0. 243 | y = mvnpdf(x, mu, covmat).*box(x); %q(x,y) = q(x|y). 244 | return; 245 | 246 | 247 | function t = problemA_proprnd(mu, covmat, box) 248 | % Sampling from the proposal PDF for the Markov Chain. 249 | while true 250 | t = mvnrnd(mu, covmat, 1); 251 | if box(t) 252 | % For problem A, box is the uniform PDF in the feasible region. So if 253 | % a point is out of bounds, this function will return 0 = false 254 | break; 255 | end 256 | end 257 | 258 | return 259 | --------------------------------------------------------------------------------