Assgnment 3

docx

School

University of California, San Diego *

*We aren’t endorsed by this school

Course

204

Subject

Statistics

Date

Jan 9, 2024

Type

docx

Pages

14

Uploaded by DrPrairieDogPerson716

Report
Name: Atishna Samantaray PID: A59016449 Homework 3 4a) The predictive distribution is given by: Where the first term is posterior term is given by the data distribution, and the second term by: *ref class slides We want for which class is it maximized (grass or cheetah). Replacing BDR with the value we obtained from the above calculation with alpha= 1e-4 :
The probability of error vs. alpha graph for D1:– The results suggest the strategy of using the estimate of 1 for the mean of cheetah and 3 for the mean of grass works and is good. When alpha is low, we are more confident in our prior means than when alpha is higher. So, alpha = 10e-4 gives a higher prior mean weightage and hence a lower probability of error. When alpha becomes greater than 10e-0 the probability of error doesn’t increase anymore and becomes constant because of less weightage on prior means. This hurts the overall accuracy. As estimates of 1 and 3 are good estimates, putting less weight on them increases the probability of error.
b) For this using: Which is just the class means for grass and cheetah. We solve the Gaussian Maximum Likelihood problem as last week replacing the Bayesian Decision Rule with obtained value from (a), we get – The probability of error vs. alpha for D1 under strategy 1:–
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
The results suggest ML solution there isn’t any change in the probability of error as alpha changes. This is because in ML equation there is no prior means under consideration so it doesn’t affect the probability of error. c)For MAP It’s a Gaussian Maximum Likelihood. Bayesian Decision Rule with the value we obtained from the above calculation for an alpha value of 1e-4 gives – The probability of error vs. alpha for D1 under strategy 1–
The results for MAP are similar situation to PD. When alpha is small, trust prior means (1 and 3 for cheetah and grass) more than when alpha is large. MAP uses the prior means for calculation, when alpha increases, the probability of error starts off small, then increases and becomes constant. As alpha gets larger, prior means get less weightage. Graph of PD, ML and MAP:–
The results suggest PD is best solution. PD at alpha = 10e-4 gives probability of error =.1451. MAP gives probability of error of .1461 at alpha=10e-4. ML gives probability error of .1472 at alpha=10e-4. This isn’t a huge difference though, it is a change of roughly .0021 from best solution (PD) to worst solution (ML). It is difficult to know which one is the best without knowing the probability of error. PD is better than ML and MAP for all alpha values, for alpha above 10e0, ML and MAP have same probability of error as ML doesn’t consider prior means. For alpha values above 10e0 the prior means don’t contribute anymore and BDR is controlled by data due to less weightage on prior means. d)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
For datasets 2 to 4, PD and MAP solutions are similar till alpha =10e0, at this point all 3 solutions are almost similar. The number of observations of bg for datasets 2-4 are 500, 700 respectively and 900. While the observation numbers of fg data are 125, 175, 225 respectively. This suggests as the number of observations increase, they dont affect the overall solution after a
certain point. There was a big difference in solution quality from dataset 1 to 2, but from 2 to 3 and 3 to 4 there is comparatively very less of a difference in terms of quality of solution. The is a small change in the PoE, about .005, which might be because of difference in the observations among datasets. Using strategy 1 which is a good strategy, gives bg and fg unique prior means. When alpha is low, we are more confident in our prior means across all datasets. When alpha is high, confidence on prior means decreases and data dominates the classification. This suggests our guess for the prior means is good and also, trusting it less results in a higher PoE. e)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
It can be seen that the reverse of previous strategy is happening. The strategy being used for guessing the prior means is bad and is giving same prior means to bg and fg. For this case, when alpha is low confidence in prior means is more but they are poor. So, when alpha is low we see a higher PoE and it decreases as alpha increases. As alpha increases trusting the prior less means increases the quality of the solution. The solutions have similar situation with that of the first dataset, there is some difference in PoE for PD and MAP as to be expected. For datasets 2-4 as the observations increases we see a similar PoE trend past alpha=10e0. Choosing the correct prior means is very important to get better classification as can be seen from strategy 1 and 2 and the values of PoE.
CODE: load( 'TrainingSamplesDCT_subsets_8.mat' ) load( 'Prior_1.mat' ) load( 'Prior_2.mat' ) load( 'Alpha.mat' ) xdim = 255; modxdim = xdim-7; ydim = 270; modydim = ydim-7; errorPD = zeros(1,9); errorML = zeros(1,9); errorMAP = zeros(1,9); img = imread( 'cheetah.bmp' ); img = im2double(img); maskimg = imread( 'cheetah_mask.bmp' ); maskimg = im2double(maskimg); BGMean = mean(D1_BG); FGMean = mean(D1_FG); BGCov = cov(D1_BG); FGCov = cov(D1_FG); priorg = length(D1_BG)/(length(D1_BG)+length(D1_FG)); priorc = length(D1_FG)/(length(D1_BG)+length(D1_FG)); zigZagimg = zeros((modxdim)*(modydim),64); for i = 1:modxdim for j = 1:modydim cb = img(i:i+7,j:j+7); dctmat = dct2(cb); ind = reshape(1:numel(dctmat), size(dctmat)); ind = fliplr( spdiags( fliplr(ind) ) ); ind(:,1:2:end) = flipud( ind(:,1:2:end) ); ind(ind==0) = []; zigZagimg((i-1)*(modydim)+j,:) = dctmat(ind); end end for i=1:length(alpha) Cov0 = diag(alpha(i)*W0); w1bg = (length(D4_BG)* Cov0)/(length(D4_BG) * Cov0 + BGCov); w2bg = (BGCov)/(length(D4_BG) * Cov0 + BGCov); MuNBG = w1bg * BGMean' + w2bg * mu0_BG'; SigmaNBG = (BGCov * Cov0)/(BGCov + length(D4_BG) * Cov0); SigmaNBGCombined = ((BGCov + SigmaNBG) + (BGCov + SigmaNBG)')/2; Cov0 = diag(alpha(i)*W0); w1fg = (length(D4_FG)* Cov0)/(length(D4_FG) * Cov0 + FGCov); w2fg = (FGCov)/(length(D4_FG) * Cov0 + FGCov); MuNFG = w1fg * FGMean' + w2fg * mu0_FG'; SigmaNFG = (FGCov * Cov0)/(FGCov + length(D4_FG) * Cov0);
SigmaNFGCombined = ((SigmaNFG + FGCov) + (SigmaNFG + FGCov)')/2; maskMatrix = zeros((modxdim)*(modydim),1); for x=1:length(zigZagimg) finalBDRGrass = log(mvnpdf(zigZagimg(x,:),MuNBG',SigmaNBGCombined)*priorg); finalBDRCheetah = log(mvnpdf(zigZagimg(x,:),MuNFG',SigmaNFGCombined)*priorc); if (finalBDRGrass < finalBDRCheetah) maskMatrix(x) = 1; else maskMatrix(x) = 0; end end tempMask = zeros(modxdim,modydim); for x=1:modxdim tempMask(x,:) = maskMatrix(((x-1)*(modydim)+1):x*(modydim))'; end maskMatrix = tempMask; figure imshow(maskMatrix,[]) incorrectCount = 0; for x=1:modxdim for y=1:modydim if (maskimg(x,y) ~= maskMatrix(x,y)) incorrectCount = incorrectCount + 1; end end end errorPD(i) = incorrectCount/xdim/ydim; end for i=1:length(alpha) maskMatrix = zeros((modxdim)*(modydim),1); for x=1:length(maskMatrix) finalBDRGrass = log(mvnpdf(zigZagimg(x,:),BGMean,BGCov)*priorg); finalBDRCheetah = log(mvnpdf(zigZagimg(x,:),FGMean,FGCov)*priorc); if (finalBDRGrass < finalBDRCheetah) maskMatrix(x) = 1; else maskMatrix(x) = 0; end end tempMask = zeros(modxdim,modydim); for x=1:modxdim tempMask(x,:) = maskMatrix(((x-1)*(modydim)+1):x*(modydim))'; end maskMatrix = tempMask; figure imshow(maskMatrix,[])
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
incorrectCount = 0; for x=1:modxdim for y=1:modydim if (maskimg(x,y) ~= maskMatrix(x,y)) incorrectCount = incorrectCount + 1; end end end errorML(i) = incorrectCount/xdim/ydim; end for i=1:length(alpha) Cov0 = diag(alpha(i)*W0); w1bg = (length(D4_BG)* Cov0)/(length(D4_BG) * Cov0 + BGCov); w2bg = (BGCov)/(length(D4_BG) * Cov0 + BGCov); MuNBG = w1bg * BGMean' + w2bg * mu0_BG'; Cov0 = diag(alpha(i)*W0); w1fg = (length(D4_FG)* Cov0)/(length(D4_FG) * Cov0 + FGCov); w2fg = (FGCov)/(length(D4_FG) * Cov0 + FGCov); MuNFG = w1fg * FGMean' + w2fg * mu0_FG'; maskMatrix = zeros((modxdim)*(modydim),1); for x=1:length(maskMatrix) finalBDRGrass = log(mvnpdf(zigZagimg(x,:),MuNBG',BGCov)*priorg); finalBDRCheetah = log(mvnpdf(zigZagimg(x,:),MuNFG',FGCov)*priorc); if (finalBDRGrass < finalBDRCheetah) maskMatrix(x) = 1; else maskMatrix(x) = 0; end end tempMask = zeros(modxdim,modydim); for x=1:modxdim tempMask(x,:) = maskMatrix(((x-1)*(modydim)+1):x*(modydim))'; end maskMatrix = tempMask; figure imshow(maskMatrix,[]) incorrectCount = 0; for x=1:modxdim for y=1:modydim if (maskimg(x,y) ~= maskMatrix(x,y)) incorrectCount = incorrectCount + 1; end end end errorMAP(i) = incorrectCount/xdim/ydim; end hold on ; plot(alpha,errorPD) plot(alpha,errorML) plot(alpha,errorMAP)
hold of ; set(gca, 'XScale' , 'log' ) legend( 'PD' , 'ML' , 'MAP' )