Taking a sample from a discrete probability distribution. Create the Sample class, which has a function Object() { [native code] } that accepts an array p[] of double values as an argument and supports the following two operations: Return an index I with a probability of p[i]/T (where T is the sum of the numbers in p[]) and change(i, v) to change the value of p[i] to v. Use a complete binary tree with an implied weight of p[i] for each node. Keep the total weight of all the nodes in its subtree in each node. Pick a random number between 0 and T to generate a random index and use the cumulative weights to determine which branch of the subtree to explore. Change the weights of all nodes on the path from the root when updating p[i].
Taking a sample from a discrete probability distribution. Create the Sample class, which has a function Object() { [native code] } that accepts an array p[] of double values as an argument and supports the following two operations: Return an index I with a probability of p[i]/T (where T is the sum of the numbers in p[]) and change(i, v) to change the value of p[i] to v. Use a complete binary tree with an implied weight of p[i] for each node. Keep the total weight of all the nodes in its subtree in each node. Pick a random number between 0 and T to generate a random index and use the cumulative weights to determine which branch of the subtree to explore. Change the weights of all nodes on the path from the root when updating p[i].
Step by step
Solved in 2 steps with 1 images