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].
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)