samedi 27 juin 2015

How to Replace Values Exceeding Threshold with Random Number Sampled from a Given Dataset?

I have a 3-dimensional vector called 'simulatedReturnsEVT3'. In that vector, I would like to replace all values that are higher than 'MaxAcceptableVal' or lower than 'MinAcceptableVal'. Such values that are beyond either of these two thresholds should be replaced by a random number that is drawn from the 3-dimensional vector 'data2'. For drawing that random number, I use the matlab function 'datasample'.

I have written the below code, which replaces the values that are beyond either of the thresholds with a random number sampled from 'data2'. However, it seems (when plotting the data in a histogram) that the replacement happens with the same value along dimension 'j'. This is not what I want to do. For every threshold exceedance, I want a new random number to be drawn for replacement from 'data2'.

nIndices = 19
nTrials  = 10000

% data2                has dimensions 782 x 19 x 10000
% simulatedReturnsEVT3 has dimensions 312 x 19 x 10000
% MaxAcceptableVal     has dimensions   1 x 19
% MinAcceptableVal     has dimensions   1 x 19

% Cut off Outliers
for i=1:nIndices
    for j=1:nTrials
        sliceEVT = simulatedReturnsEVT3(:,i,j);
        sliceEVT(sliceEVT < MinAcceptableVal(i))=datasample (data2(:,i,j), 1,1,'Replace',false);
        sliceEVT(sliceEVT > MaxAcceptableVal(i))=datasample (data2(:,i,j), 1,1,'Replace',false);
        simulatedReturnsEVT3(:,i,j) = sliceEVT;
    end
end

The same problem can be illustrated on a smaller scale by creating the following matrices.

% Set Maximum Acceptable Levels for Positive and Negative Returns
MaxAcceptableVal = [0.5   0.3]
MinAcceptableVal = [-0.5 -0.3]

simulatedReturnsEVT3 = [0.6 0.3; 0.3 0.3; 0.3 0.3; 0.3 0.4]
simulatedReturnsEVT3 = repmat(simulatedReturnsEVT3,[1 1 2])
data2                = [0.25 0.15; 0.25 0.15; 0.2 0.1]    
data2                = repmat(data2,[1 1 2])               

% Cut off Outliers
for i=1:2 
    for j=1:2 
        sliceEVT = simulatedReturnsEVT3(:,i,j);
        sliceEVT(sliceEVT < MinAcceptableVal(i))=datasample (data2(:,i,j), 1,1,'Replace',false);
        sliceEVT(sliceEVT > MaxAcceptableVal(i))=datasample (data2(:,i,j), 1,1,'Replace',false);
        simulatedReturnsEVT3(:,i,j) = sliceEVT;
    end
end

Can anybody help?




Aucun commentaire:

Enregistrer un commentaire