Formula for average damage

I was designing some ships last night at around 4 AM (damn you Stardock!) and was trying to work out in my head how they would stack up against some of the ships my opponents were fielding. Unfortunately, figuring out damage probabilities for differing amounts of attack and defense were too much for me to do in my sleep deprived state, so I found myself making a lot of rough estimates. This morning the question kept nagging at me, so I decided to grab a pencil and paper and find a formula for the average damage that can be expected for any pair of attack and defense values. After a good amount of head scratching, I found not one but two formulas, for slightly different situations:

If the attack value of one ship ship is less than the defense value of the other then use this formula (where "a" is attack value and "d" is defense value):

              (a + 1) (a + 2)
Avg. damage = ---------------
                 6 (d + 1)

If the attack value is greater than the defense value, then use this longer verson:

              a (a + 1) (a + 2) - (a - d - 1) (a - d) (a - d + 1)
Avg. damage = ---------------------------------------------------
                                  6 a (d + 1)

The simpler formula can actually be used whenever a <= d + 1, not just when a < d. It works out that the formulas both give the same answer when a is equal to d or (d + 1).

There may be a way to simplify the second formula, but I was not able to find one. When I multiplied the numerator out, I got:

              d^3 + 3 a^2 + 3 a^2 d - 3 a d^2 + 3 a - d
Avg. damage = -----------------------------------------
                             6 a (d + 1)

That is pretty ugly, and harder to remember than the version above. If anyone sees any factorization that can be done to simplify things, I'd love to hear it.

I'll type up my derivations of the two formulas in a bit and post them later. The only thing that might be a bit controversial about them is that I have assumed that the attack roll is always at least one, whereas the defense roll may be zero. This does not agree with what the developers have told us, but it more closely matches what I have seen in my games. (I have tried to do some tests in the game, but kept getting really odd results, like a 1 attack/1 defense fighter doing 5 damange in a single shot.) The change to the formulas is not very big if you want to calculate using a 0 based attack though: muliply the equations above by "a / (a + 1)".


Edit:

The Derivations

First, we need to understand what it is we are taking the average of. The damage delt by a single shot can be thought of as a point in a probability space, where one axis represents the attack roll and one axis representing the defense roll, like so:

           ||     Attack Roll
           || 1 | 2 | 3 | 4 | 5 |...
===========++===+===+===+===+===+~~~
         0 || 1 | 2 | 3 | 4 | 5 |...
        ---++---+---+---+---+---+~~~
         1 || 0 | 1 | 2 | 3 | 4 |...
        ---++---+---+---+---+---+~~~
         2 || 0 | 0 | 1 | 2 | 3 |...
        ---++---+---+---+---+---+~~~
Defense  3 || 0 | 0 | 0 | 1 | 2 |...
 Roll   ---++---+---+---+---+---+~~~
         4 || 0 | 0 | 0 | 0 | 1 |...
        ---++---+---+---+---+---+~~~
         5 || 0 | 0 | 0 | 0 | 0 |...
        ---++---+---+---+---+---+~~~
        ...~~...~...~...~...~...~...

The size of the space depends on the probability space depends on the attack and defense values of the ships we are considering. The space I drew above would work for a ship with 5 weapon strength firing on a ship with 5 defense. The important thing about the probability space is that each of the points in the space is equally likely to be the result. So, to find the average value, we simply need to add up all the values and devide by the area of the space.

While it is possible to add up by hand the values in a small space like the one above, you would not want to try it for a space that was has dimensions that are much bigger.

Fortunately, there is a pretty obvious pattern to the values in the space. They form a triangle in the upper right corner of the space. We can use that pattern to find a formula for the sum.

First lets make a formula for the total value of each column as we go from left to right. The first column has just 1 (and a lot of zeros). The second column has a 2 and a 1 (and zeros). The nth column has the first n natural numbers (and zeros). Since we are adding everything up, we can ignore the zeros and just add up the numbers. There is an easy formula for the sum of the first n natural numbers:

                        n (n + 1)
Sum(for i=1 to n, i) =  ---------
                            2

Now we make a sum of the column totals (a of them, since columns represent the ship's attack values). We'll try to break it up in to pieces, then deal with each individually:

   [              n (n + 1) ]      [              n^2 + n ]
Sum[for n=1 to a, --------- ] = Sum[for n=1 to a, ------- ]
   [                  2     ]      [                 2    ]


                                Sum(for n=1 to a, n^2) + Sum(for n=1 to a, n)
                              = ---------------------------------------------
                                                      2

We already know how do to the second of those sums, but for the first we need the formula for the sum of the first n squares:

                         n (n + 1) (2 n + 1)
Sum(for i=1 to n, i^2) = -------------------
                                  6

Plugging that in (along with the previous one) gives:

                                [a (a + 1) (2 a + 1)   a (a + 1)]
                                [------------------- + ---------]
                              = [         6                2    ]
                                ---------------------------------
                                                2


                                [2 a^3 + 3 a^2 + a   a^2 + a]
                                [----------------- + -------]
                              = [        6              2   ]
                                -----------------------------
                                              2


                                [2 a^3 + 6 a^2 + 4 a]
                                [-------------------]
                              = [         6         ]
                                ---------------------
                                          2


                                a^3 + 3 a^2 + 2 a
                              = -----------------
                                        6


                                a (a + 1) (a + 2)
                              = -----------------
                                        6

So we have have a formula for the sum of the columns. At least, we have for any tall and skinny probability space, where there are lots of zeros at the bottom of each column. What if the space is short and wide? Then some of the columns won't have all the values we counted. This will happen any time the attack value is greater than the defense value by more than 1. Here's an example, 5 attack vs 2 defense:

   || 1 | 2 | 3 | 4 | 5 |
===++===+===+===+===+===+
 0 || 1 | 2 | 3 | 4 | 5 |
---++---+---+---+---+---+
 1 || 0 | 1 | 2 | 3 | 4 |
---++---+---+---+---+---+
 2 || 0 | 0 | 1 | 2 | 3 |
---++---+---+---+---+---+

So we need to correct for the space we counted, but which is not actually present in the real probability space. Fortunately, the missing piece has the same pattern as the whole space does! Ignoring the columns that are all zeros, it starts with a column with a 1, then has a column with 1 and 2, etc. So we can use the same formula we calculated above for the correction factor. How many columns are in the space we are correcting for? By looking at the space, you can see it is a - d - 1 (where a is the attack value and d is the defense value). In the example above, that is 2 columns. So, the sum for short and wide spaces is:

a (a + 1) (a + 2) - (a - d - 1) (a - d) (a - d + 1)
---------------------------------------------------
                         6

Now we have two formulae for the sum, and we can construct our average. This is where I stray in to controversial (and possibly simply wrong) territory, as I belive that attack rolls are always at least 1, despite what the developers have said to the contrary. In any case, what we need is the area of the probability space. In my view, that is a (d + 1). If you belive that attack rolls may be zero, add a column of zeros to the beginning each of the spaces I have drawn. The area will then be (a + 1) (d + 1). The average is the sum of the values, devided by the area:

                   [a (a + 1) (a + 2)]  |                     [a (a + 1) (a + 2) - (a - d - 1) (a - d) (a - d + 1)]
           Sum     [-----------------]  |             Sum     [---------------------------------------------------]
Average = ------ = [        6        ]  |  Average = ------ = [                         6                         ]
           Area    -------------------  |             Area    -----------------------------------------------------
                        a (d + 1)       |                                           a (d + 1)
                                        |
                                        | 
                   (a + 1) (a + 2)      |                     a (a + 1) (a + 2) - (a - d - 1) (a - d) (a - d + 1)
                 = ---------------      |                   = -----------------------------------------------------
                      6 (d + 1)         |                                          6 a (d + 1)

So there you have it! Formulae to use to calculate the average damage for each shot. I hope this will be useful to you in designing your ships. Let me know what you think.

6,158 views 9 replies
Reply #1 Top
Hmm, no responses to the OP. Perhaps a bump is needed?
Reply #2 Top
Hi!
Formula for average damage

You seem to have too much time.
What I'm using to calculate the average damage is much more simple:
attack value halved - defense value halved,
where defense value is a sum of all particular defenses, already sqrt-ed where needed.

BR, Iztok
Reply #3 Top
Forum post error. Please disregard.
Reply #4 Top
What I'm using to calculate the average damage is much more simple:
attack value halved - defense value halved,

Much more simple, yes, but wrong in most cases.
E.g. the average damage of attack x against defense x is 0 for your calculation, which is clearly not the case.
Reply #5 Top
Hi!
the average damage of attack x against defense x is 0 for your calculation

Clearly the sign the war doesn't pay out, isn't it? When this happens I make peace or change weapons. Usually the later.
BR, Iztok
Reply #6 Top
That's a clear (and ridiculously well formatted! ) description of how to calculate average damage... nice. It inspired me to throw together a few tables showing the formula for attack and defense values up to 30.

Mean damage
Increase in mean damage for an additional unit of Attack
Decrease in mean damage for an additional unit of Defense

Always nice to see the math under the surface explained so clearly.

Reply #7 Top
Yeah, all this is great in theory, but somebody needs to test it.

One question I have is how the random numbers are generated. Lets say that you have a defense rating of 5. Supposedly they determine your defense in any given turn by generating a number between 0 and 5. Its safe to assume that they only do integers because they say that the defense is always rounded down to an integer when they take the square root. Common sense dictates that they would generate an integer and just go with that. But, I have seen games where it appears that they generate a number between 0 and 1, multiply by 5, and then round to the nearest integer (this is done because a lot of pseudorandom number generators actually only get you numbers between 0 and 1). This is actually different than just generating a random integer because the probability of getting a 0 or a 5 is half that of getting 1,2,3, or 4. Thus, your forumula could be wrong.

Also, the assumtion that attack is between 1 and Max but defense is between 0 and Max obviously needs to be tested.
Reply #8 Top
Actually, since they always round down, they probably generate a number between 0 and 1 then multiply it by 6 and then round down. You'll never get 6 because 5.99999999999 still rounds down to 5.
Reply #9 Top
They round down for square root calculations, but that doesnt mean that they do for everything.

What I suggested was more of an example of something that could go wrong. I assume that the coding isnt sloppy enough so that random number generation is an issue. I have seen things like games with dice where this was a problem (you got too few 1s and 6s), so it was just an immediate example of a reason why testing is needed for the formulae.