Generate pdf distribution with bias

2 views (last 30 days)
Tyler
Tyler on 6 Apr 2017
Commented: Tyler on 2 May 2017
I'm curious if anyone knows how to bias a probability density function. For example, I want to create a pdf from vector A, but I want to bias it according to vector B (without changing the values in A). So if A = [1 2 3 4 5] and B = [1 1 1 1 0.5], the 5th element of A would count half as much as the rest when computing my pdf.
In that case, a simple work-around is to re-create the other elements in A to weight them twice as much, so A' = [1 2 3 4 5 1 2 3 4]. However, this gets more difficult if the biases are not easy integers. If my weighted vector B = [.4265 1 .9361 1 .1532], it would not be easy to simply add more elements. The only solution I can think of is to find the closest common denominator for the biases and add elements accordingly. Seeing as I will be repeating this with much larger vectors and changing biases, a better way would be nice. I haven't been able to find any toolboxes or function options that offer this. If anyone knows a simpler solution I would be most appreciative. Thanks!

Accepted Answer

John D'Errico
John D'Errico on 6 Apr 2017
Edited: John D'Errico on 6 Apr 2017
Simpler than you apparently think. Even trivial.
A = 1:5;
B = [1 1 1 1 0.5];
B = cumsum([0,B])/sum(B);
N = 10000;
[~,~,bin] = histcounts(rand(N,1),B);
X = A(bin);
hist(X,100)
I think you should see that the above code will work regardless of the weights, integer, non-integer. So for your other example...
B = [0.4265 1 .9361 1 0.1532]
B = cumsum([0,B])/sum(B);
N = 1000000;
[~,~,bin] = histcounts(rand(N,1),B);
X = A(bin);
hist(X,100)
With a much larger sample, you can see the sample accurately follows the desired distribution.
  2 Comments
Tyler
Tyler on 11 Apr 2017
Edited: Tyler on 11 Apr 2017
Thanks! Does it matter what N is? Just a high enough number to get an accurate spread?
Tyler
Tyler on 2 May 2017
Thanks John. I notice my results change depending on the value of N. Is there a rule of thumb on that value? Should it be a multiple of the size of my B vector, or just a big enough number to ensure an accurate weighting?

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!