A charity prints a batch of total scratch cards and sells them for price pounds each. A few cards win a prize: prizes is a list of pairs (payout, count), meaning that count cards in the batch pay payout pounds. Every other card pays nothing. You buy one card, picked at random from the whole batch.
Write card_value(prizes, total, price) that returns a tuple of three floats:
- your expected profit (the payout minus the price, on average),
- the standard deviation of your profit,
- the probability that you make a profit, that is, that the payout is strictly larger than the price.
Examples
Input: prizes = [(10, 50), (100, 5), (1000, 1)], total = 1000, price = 2
Output: (0.0, 32.41913015489465, 0.056)
Explanation: the expected payout is (10·50 + 100·5 + 1000·1) / 1000 = 2, exactly the price.
The expected squared payout is (100·50 + 10000·5 + 1000000·1) / 1000 = 1055, so the
variance is 1055 - 2² = 1051 and the standard deviation is about 32.4. 56 of the 1000 cards
pay more than 2.
Input: prizes = [(1, 200), (5, 40)], total = 500, price = 1
Output: (-0.2, 1.3266499161421599, 0.08)
Explanation: a card that pays back exactly the price is not a profit.
Constraints
1 <= total <= 10**6,0 <= price <= 10000 <= len(prizes) <= 100, everypayout >= 0andcount >= 0, and the counts add up to at mosttotal- the same payout may appear in more than one pair
- floats are compared with a tolerance of
1e-6
Goals
- Write down the distribution of a random variable from counts
- Compute an expected value as a probability-weighted average
- Compute the variance and standard deviation of a distribution, not of a data list