Iduno wrote:How would I calculate the variance/standard deviation/some percent confidence of a die roll?
Worse, I'm trying to calculate the variance (or standard deviation, or something that gets me similar information) on exploding dice.
I calculated the average of a non-exploding die with x sides as (1+x)/2, and exploding as (1+x)/2+(1/x)((1+x)/2)+(1/x^2)((1+x)/2) because neither excel nor I know how to do proper calculus anymore, and the difference is negligible for the inputs.
Short answer: with an exploding die with
S sides, you have:
Mean:
S * (
S + 1) / (2 * (
S - 1))
Variance:
S * (
S + 1) * (
S^2 + 7 *
S - 2) / (12 * (
S - 1)^2)
So with a D6, you get Mean 6 * 7 / 10 = 4.2 and Variance 6 * 7 * (36 + 42 - 2) / (12 * 25) = 10.64
Standard deviation is the square root of the variance.
Happily the result is infinite in the case of one sided dice, which is as it should be.
Long answer: in spoilers, because I'm not that evil. It's fun, though!
The method described by Pixels is the right way to compute the mean, as it's quick, elegant, and works. Sadly, for the purpose of computing the variance, we need to introduce the slow and ugly method (which also works).
An exploding die with S sides has possible values of 1... S -1, S+1... 2S-1, 2S+1... and so on, with probabilities of 1/S for the first group, 1/S^2 for the second, etc.
You can compute the mean by taking the (infinite) sum of values times their probabilities, and a quick bit of algebra shows it can be expressed as:
Sum(for i=0 to infinite){ ( (i * S + 1) + (i * S + 2) + ... + (i * S + S - 1) ) / S^(i+1) }
Which can be simplified into two parts:
Sum { (i * S * (S - 1)) / S^(i+1) + (1 + 2 + ... + S - 1) / S^(i+1) }
Where the first part is a constant in each step and happens S - 1 times (there are 5 values between 1 and 6 on an exploding D6, 5 values between 7 and 12, etc.). The second part is a constant across all steps, and the sum can be computed.
Using recursion, one can find and confirm that the sum of numbers from 1 to N is N * (N + 1) / 2, so we know the value of the second term in the sum (except for the S^(i+1) part).
Again, we can simplify, moving constants (things that do not depend on i) outside the sum and breaking it into two sums:
S * (S - 1) * Sum { i / S^(i+1) } + S * (S - 1) / 2 * Sum { 1 / S^(i+1) }
Now if only we knew how to compute infinite sums of terms with a power law, we'd be set!
First, the i+1 term is ugly, so let's simplify it to S^i * S. That lets us cut down on the sums a bit more:
(S - 1) * Sum { i / S^i } + (S - 1) / 2 * Sum { 1 / S^i }
Next, note the identity 1 + r + r^2 + ... + r^n = (1 - r^(n+1)) / (1 - r). You can confirm it by multiplying the left side by (1 - r).
So if r < 1 (hint: a die with more than one side), as n goes to infinity, the left hand sum converges to 1 / (1 - r), since r^(n+1) goes to zero.
We have r = 1 / S < 1, so we can use that in the second sum. That gives (S - 1) / 2 * 1 / (1 - 1 / S), so S / 2 (multiply numerator and denominator by S).
The first sum is a bit trickier, because we have 0 / S + 1 / S + 2 / S^2 + ...
Enter the calculus. The derivative of 1 + r + r^2 + ... r^n is 1 + 2*r + 3*r^2 + ... + n * r^(n-1). So if we multiply it by r, we get 1*r + 2*r^2 + ... + n*r^n, which is what we want.
And the derivative of 1 / (1 - r) is simply 1 / (1 - r)^2, so we can use that rather than deal with infinite sums: r + 2r^2 + 3r^3 +... = r / (1 - r)^2
So the first sum of (S - 1) * Sum { i / S^i } is equal to:
(S - 1) * (1 / S) / (1 - 1 / S)^2, which simplifies to:
S / (S - 1)
Adding the two together, we get S * (1 / (S - 1) + 1 / 2), which is equal to S * (S + 1) / (2 * (S - 1)), as stated above.
Why did we bother doing this when easier ways to compute it exist? Because we'll use something very like it for the variance.
Add some probability to the mix: given that E() is the expected value (mean), then for a random distribution X, the variance of X is E(X^2) - E(X)^2. Mind the parentheses.
So to get the variance, we just need to compute the mean of the square of the exploding die, and do a bit more algebra after. So that's a roll on a D6 that is equal to 1, 4, 9, 16, 25, 49, 64...
Fortunately (hah!) the methodology we used before can easily be converted to this. First, the infinite sums:
Sum(for i=0 to infinite){ ( (i * S + 1)^2 + (i * S + 2)^2 + ... + (i * S + S - 1)^2 ) / S^(i+1) }
Which simplifies to a three part sum:
(S - 1) * i^2 * S^2 / S^(i+1)
i * S * 2 * (1 + 2 + ... + S - 1) / S^(i+1)
(1 + 4 + ... + (S - 1)^2) / S^(i+1)
Recursion tells us that the sum of squares from 1 to N^2 is N (N + 1) (2 N + 1) / 6, so we can easily compute the third equation. I'd go into how you can derive the equation for sum of different powers, but the important parts are recursion and the notion that the sum of terms to the power x is a polynomial of degree x+1.
The second equation is made of terms we've already computed (hi, calculus and infinite sums!), so it's easy.
The first equation means we have to dip into calculus again and take the second derivative of the infinite sum 1 + r + r^2 + ..., getting the sum of terms of the form (n^2 - n) * r^(n - 2), and we already know the sum of the -n r^(n-2) part, so we can compute the rest. The derivative of 1/(1-r)^2 is, of course 2/(1-r)^3 (calculus!) and the rest follows.
So with a pile of algebra after that, we can simplify things out to get the mean of the square of an exploding die. Subtract the square of the mean of an exploding die, simplify more, and the solution is as stated.
You're welcome.
Excel answer: the spoiler above reveals that the variance of a variable X is E(X^2) - E(X)^2, so if you just do a SUMPRODUCT on enough terms you can get an approximation. There are ways to speed things up, but if you wanted to do it quickly you'd use the final equations anyway.
For a D6 with the basic version, you'd have
1 1 1/6
2 4 1/6
3 9 1/6
4 16 1/6
5 25 1/6
6 36 0
7 49 1/36
8 64 1/36
9 81 1/36
10 100 1/36
11 121 1/36
12 144 0
13 169 1/216
...
Where you compute the SUMPRODUCT of the first and last column to get the mean, and get the variance by taking the SUMPRODUCT of the second and third columns, then subtracting the square of the mean.
As you add more terms, the approximation comes closer.
Edit: If you just want an approximation, the following should do:
Normal die: Mean is (
S + 1) / 2, Variance is
S^2 / 12
Exploding die: Mean is normal die mean times
S/(
S-1), Variance is (
S + 5)^2 / 12, so with an exploding D6 you get 20% more mean (6/5) and roughly the variance from a normal D11.
These values are not exact, but a lot easier to deal with if you want to get a feel for the results.