-
Notifications
You must be signed in to change notification settings - Fork 42
/
compute_spectrum_error.m
33 lines (25 loc) · 1.44 KB
/
compute_spectrum_error.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function [ devTotal ] = compute_spectrum_error( selectionParams, targetSa, sampleSmall )
% compute the error between a selected set of spectra and the target
% spectrum
% Calculate the deviation from the target
if selectionParams.optType == 0 % computed weighted sum of squared errors of mean and standard deviation
% Compute deviations from target
sampleMean = sum(sampleSmall)/size(sampleSmall,1);
sampleVar = sum((sampleSmall - ones(size(sampleSmall,1),1)*sampleMean).^2) / size(sampleSmall,1);
devMean = sampleMean - targetSa.meanReq;
devSig = sqrt(sampleVar) - targetSa.stdevs;
devTotal = selectionParams.weights(1) * sum(devMean.^2) + selectionParams.weights(2) * sum(devSig.^2);
% Penalize bad spectra (set penalty to zero if this is not required)
if selectionParams.penalty ~= 0
for m=1:size(sampleSmall,2) % for each period
devTotal = devTotal + sum(abs(exp(sampleSmall(:,m))>exp(targetSa.meanReq(m)+3*targetSa.stdevs(m)))) * selectionParams.penalty;
end
end
elseif selectionParams.optType == 1 % compute Kolmogorov-Smirnov error metric
sortedlnSa = [min(sampleSmall); sort(sampleSmall)];
norm_cdf = normcdf(sortedlnSa,repmat(targetSa.meanReq,selectionParams.nGM+1,1),repmat(targetSa.stdevs,selectionParams.nGM+1,1));
emp_cdf = linspace(0,1,selectionParams.nGM+1);
Dn = max(abs(repmat(emp_cdf',1,length(selectionParams.TgtPer)) - norm_cdf));
devTotal = sum(Dn);
end
end