Reputation: 1235
Say I have a matrix of ones and twos. I want to randomly sample (without replacement) 2 rows with specified weights. This is what I have done here (I think?):
x = [1 1 1 2 1; 1 2 1 1 1; 1 1 1 1 1; 1 2 2 1 2; 1 2 2 1 1];
w1 = 1; % weight of ones
w2 = 4; % weight of twos
sum_wts = sum(w1.*(x==1),2) + sum(w2.*(x==2),2); %normalise weights
norm_weights = sum_wts./sum(sum_wts);
row_ind = randsample([1:size(x,1)], 2, true, norm_weights); %take random sample
new_x = x(row_ind,:);
I want the weight given to the twos to be additive. For example, a row containing 3 twos is more likely to be selected compared with a row containing 2 twos. Is this what I have done here? I'm unsure how to check if I have done this correctly...
Upvotes: 0
Views: 83
Reputation: 8459
Your code appears to be working correctly. I tested it by creating a matrix like this:
[2 1 1 1 1
2 2 1 1 1
2 2 2 1 1
2 2 2 2 1
2 2 2 2 2]
and making your code produce a large number of samples, and making a histogram for how often row n was picked. I expect that if your code is working, the bars should get taller as n increases, that is, row 5 is more likely than row 4, etc. The red line is based on the weights, and is the expected number of samples being row n.
Here's the code:
x=ones(5)+tril(ones(5))
w1 = 1; % weight of ones
w2 = 4; % weight of twos
sum_wts = sum(w1*(x==1),2) + sum(w2*(x==2),2); %normalise weights
norm_weights = sum_wts./sum(sum_wts);
row_ind = randsample([1:size(x,1)], 2000000, true, norm_weights); %take random sample
new_x = x(row_ind,:);
hist(row_ind,1:5)
hold on
plot(1:5,2000000*norm_weights,'r','LineWidth',4)
hold off
And the result
Upvotes: 1