Clear Filters
Clear Filters

Sampling according to difference in function value

2 views (last 30 days)
Hello
I have 20 values x1,...x20. Each value is between 0 and 1, for example 0.22,0.23,0.25,...
x = rand(20,1);
x = sort(x);
Now I would like to choose one data point but not uniform at random. The data point with the lowest value should have the highest probability and the other values should have a probability proportional to the difference in function value to the lowest value.
For example, if the lowest function value is 0.22, a data point with a function value of 0.23 has a difference to the best value of 0.23 - 0.22 = 0.01 and should therefore have a probability similar to the 0.22 value. But a value of 0.3 has a difference of 0.3 - 0.22 = 0.08 and should therefore have a much smaller probability.
How can this be done?

Accepted Answer

per isakson
per isakson on 18 Jun 2016
Edited: per isakson on 18 Jun 2016
A homemade approach
%%Sample data that better fits the tooltip display
x = randi([1,100],[1,20]); % 100 is magic number
x = sort(x);
dx = x - x(1) + 4; % 4 is magic number
f = @(d) 1./(d.*d); % "much smaller probability"
csf = [ 0, cumsum( f(dx) ) / sum(f(dx)) ];
is = histc( rand, csf ) == 1;
disp( x(is) )
"other values should have a probability proportional to the difference in function value to the lowest value." &nbsp I don't understand the descripton on how to calculate the weight. However, replacing &nbsp f = @(d) 1./(d.*d); &nbsp by &nbsp f = @(d) 1./d; &nbsp may be closer.

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!