C ++ discrete sample distribution with frequently changing probabilities

Problem: I need to sample from a discrete distribution built from specific weights, for example. {w1, w2, w3, ...} and, therefore, the probability distribution {p1, p2, p3, ...}, where pi = wi / (w1 + w2 + ...).

some of the wi change very often, but only a very low proportion of all wi. But the distribution itself, therefore, must be renormalized every time this happens, and therefore I believe that the Alias ​​method does not work efficiently, because every time you need to build the entire distribution from scratch.

The method that I am considering now is a binary tree (heap method), where all wi are stored at the lowest level, and then the sum of each of the two at a higher level, etc. The sum of all of them will be at the highest level, which is also a normalization constant. Thus, to update the tree after changing wi, you need to make changes to log (n), as well as the same amount, to get a sample from the distribution.

Question:

Q1. Do you have a better idea on how to achieve this faster? Q2. The most important part: I'm looking for a library that has already done this.

Explanation: I did this myself a few years ago, creating a heap structure in a vector, but since then I have learned a lot of things, including detecting libraries (:)) and containers like a map ... Now I need to rewrite this code with a higher functionality, and I want to do it right this time:

so Q2.1 is a good way to make a C ++ map ordered and search not by index, but by the total amount of its elements (so we read, right? ..). (this is my current theory, how I would like to do it, but it should not be that way ...)

Q2.2 Maybe there is an even more pleasant way to do the same? I would believe that this problem is so frequent that I am very surprised that I could not find any library that would do this for me ...

, , - , , , ...

-z

: , , , , , .

Edit2: , , ...

+4
2

- ( ++, , ). wi "w1_1", "w1_2",... "w1_ [w1]" (.. w1, "w1_" ).

, , . w5_ *, , 5. - , , .

, wi A B, B-A ( B > A) A-B wi ( A > B).

.

, , . , , - . 3 4 , , : O (1) O (| A-B |) .

, , , - . . : 1 w1 , 2 - w2 .., - .

+1

. .

w_sum = w_sum_old - w_i_old + w_i_new;

p_i p_i = w_i/w_sum, p_i p_i , . ,

expected_something = (something_1 * w_1 + something_2 * w_2 + ...) / w_sum;

expect_something , .

, , . , , ? , , .

- . N/2 . N/4 .. , , , O (1) . , .

0

All Articles