From game RNG to statistics, how to calculate the expected outcome?

590 Views Asked by At

Description

I play a war turn-based game, where attacks only have two variables that are visible before a strike: The attacker's military strength $f_{a}$ and the defender's military strength $f_{d}$. Whether an attack is successful or not is determined by a series of $3$ rolls.

Each roll the attacker and defender will be given a random (uniform) multiplier $r_{a}$ and $r_{d}$ respectively, where $$0.4<r<1.0$$For a roll to be successful this must hold: $$f_{a}r_{a}>f_{d}r_{d}$$ At the end of all $3$ rolls the resulting Victory Type will be determined as follows:

Rolls Won $3$/$3$ $2$/$3$ $1$/$3$ $0$/$3$
Victory Type Immense Triumph (I) Moderate Victory (M) Pyrrhic Victory (P) Utter Failure (U)

Casualties are also calculated through the rolls, in each roll the attacker's and defender's casualties are given respectively by $$c_{a} = 0.01·f_{d}r_{d} \,\,\,\,\,\,\,\,\,\,\,\, c_{d} = 0.018337·f_{a}r_{a}$$ Importantly, casualties are only applied after all rolls, therefor all rolls are independent events. At the end of the 3 rolls, the number of casualties is rounded to the nearest integer.

What I have worked out

My knowledge in statistics isn't great, but through experimentation and some intuition I managed to (after a while) find that for a given $f_{a}$ and $f_{d}$ where $f_{a} < f_{d}$ the probability of a successful roll can be given by: $$P(f_{a}r_{a}>f_{d}r_{d})=\int_{0}^{1}{\frac{1}{0.6}\int_{0.4}^{max(\frac{f_{a}}{f_{d}}x,\,\,0.4)}{\frac{1}{0.6}\,dt}\,dx}$$ Which can be simplified to: $$P(f_{a}r_{a}>f_{d}r_{d})=\frac{25}{9}\left ( \frac{2f_{d}}{25f_{a}} + \frac{f_{a}}{2f_{d}} - \frac{2}{5} \right )$$ And thus, if $p$ is the probabilty of a successful roll, the probabilities for each victory type given $f_{a} < f_{d}$ are as follows: $$I=\begin{pmatrix}3\\3\end{pmatrix}p^{3}(1-p)^{0}$$ $$M=\begin{pmatrix}3\\2\end{pmatrix}p^{2}(1-p)^{1}$$ $$P=\begin{pmatrix}3\\1\end{pmatrix}p^{1}(1-p)^{2}$$ $$U=\begin{pmatrix}3\\0\end{pmatrix}p^{0}(1-p)^{3}$$

What I haven't worked out

So all of this leads me to my question, I have been trying to figure out what the expected number of casualties is for a specified victory type, that is, given $f_{a}$ and $f_{d}$ where $f_{a} < f_{d}$ what are the $E(C_{a}\mid I)$, $E(C_{d}\mid I)$, $E(C_{a}\mid M)$, $E(C_{d}\mid M)$ and so on...

So far I understand that if I were to set $r_{a}=1$ this would be true (or at least I think): $$E(C_{a}\mid I) = 3\int_{0.4}^{\frac{f_{a}}{f_{d}}}{\frac{1}{\frac{f_{a}}{f_{d}}-0.4}\,\frac{f_{d}r_{d}}{100}\,dr_{d}}$$ However given that the bounds of $r_{a}$ are dependent on $r_{d}$ and vise versa, since all the possible rolls that would result in a successful roll must follow these: $$0.4<r_{d}<\frac{f_{a}}{f_{d}}r_{a}$$ $$\frac{f_{d}}{f_{a}r_{d}}0.4<r_{a}<1$$ I have not been able to figure out how to calculates these expected values when both $r_{a}$ and $r_{d}$ are free to take all possible values they can.

Double checking

If anyone knows the solution, I wrote a little Python script that ran through the same scenario $1\times 10^{8}$ times where $f_{a}=3000$ and $f_{d}=4500$ and these were the results:

Victory Type Immense Triumph (I) Moderate Victory (M) Pyrrhic Victory (P) Utter Failure (U)
Total Outcomes $325513$ $5608873$ $32249407$ $61816207$
Total Attackers Casualties $21321676$ $429922319$ $2831691198$ $6117015520$
Average Attackers Casualties $65.5018$ $76.6504$ $87.8060$ $98.9549$
Total Defenders Casualties $46393770$ $739083939$ $3902378593$ $6814545685$
Average Defenders Casualties $142.5251$ $131.7705$ $121.0062$ $110.2388$

So whatever the right formula is, when $f_{a}=3000$ and $f_{d}=4500$ the expected casualties should match the averages computed above.

2

There are 2 best solutions below

2
On BEST ANSWER

First, for convenience we can eliminate the need for considering ${max(...)}$, in any case by simply limiting ourselves to having ${1 < \frac{f_d}{f_a}<2.5}$. This will help make calculations more concrete. ${1 < \frac{f_d}{f_a}}$ comes from our constraint ${f_a<f_d}$ while ${\frac{f_d}{f_a}<2.5}$ comes from our desire to have an actual game where ${b}$ isn't certain to win.

Next we will define ${s=\frac{f_d}{f_a}}$, for visual convenience.

I will use ${E(C_d | M)}$ as an example of the method to calculate the desired dependent probabilities. Let's define ${X_i}$ as the ${i}$-th roll for the defender and ${Y_i}$ as the ${i}$-th roll value for the attacker. Since we are given ${M}$ we know that the the attacker won exactly ${2}$ times, and we are ambivalent to which of the trials were wins due to independence. I will therefore assume that we know the roll sequence is Win, Win, Lose.

An attacker win means that ${s*X_i <Y_i<1}$ and ${0.4 < X_i < s^{-1}}$.Thus for roll ${i}$ we can calculate the probability of the attacker winning as ${\int_{0.4}^{s^{-1}} \int_{sx}^1 \frac{25}{9}dydx=\frac{25}{9}\int_{0.4}^{s^{-1}}(1-sx)dx = \frac{25}{9}*\frac{1}{2}*(s^{-1}-0.4)*(1-s*0.4)}$.

The probability of the attacker losing is ${1-\frac{25}{9}*\frac{1}{2}*(s^{-1}-0.4)*(1-s*0.4)}$.

Now we are interested in the density function of ${X_i}$ and ${Y_i}$, given a win. By Bayes Law this will be ${\frac{f_{X_i,Y_i}(x,y)}{P(win)}}$. Recall ${f_{X_i,Y_i}(x,y) = \frac{1}{0.6}\frac{1}{0.6}=\frac{25}{9}}$ by independence and uniformity. It's important to realize that ${\frac{f_{X_i,Y_i}(x,y)}{P(win)}}$ is a density function for the possible roll values.

Since we're given the ${1_{st}}$ roll is a win for the attacker. We have:

${\frac{f_{X_1,Y_1}(x,y)}{P(win)} = \frac{\frac{25}{9}}{\frac{25}{9}*\frac{1}{2}*(s^{-1}-0.4)*(1-s*0.4)}=\frac{1}{\frac{1}{2}*(s^{-1}-0.4)*(1-s*0.4)}=z}$. Notice ${z}$ is just a constant that depends on ${s}$ because we have chosen a uniform distribution.

Therefore: ${E(Y_1|win) = \int_{0.4}^{s^{-1}}\int_{1.5x}^1 z*y\,dy\,dx}$. An integral that will be easy for you to compute. Likewise ${Y_2}$ is a win for the attacker, ${E(Y_2|win)=E(Y_1|win)}$.

Now we are also given that roll 3 is a loss for the attacker. This means our bounds will be different. They are ${0.4<X_3<s^{-1} \cap 0.4<Y_3<s*X_3}$ and ${s^{-1}<X_3<1 \cap 0.4<Y_3<1}$. The density function will be similar to the winning case ${\frac{f_{X_3,Y_3(x,y)}}{1-P(win)}=\frac{\frac{25}{9}}{1-\frac{25}{9}*\frac{1}{2}*(s^{-1}-0.4)*(1-s*0.4)}=l}$. (Again we just plop in a constant because the uniform distribution makes it easy).

Using the above dependent density we can calculate ${E(Y_3|loss)=\int_{0.4}^{s^{-1}}\int_{0.4}^{sx}l*y\,dy\,dx + \int_{s^{-1}}^{1}\int_{0.4}^{1}l*y\,dy\,dx }$.

Finally, we put it all together and calculate ${E(C_d | M)=0.018337*f_a*[E(Y_1|win) + E(Y_2|win)+E(Y_3|loss)]}$.

If you follow this operation precisely with ${s=\frac{4500}{3000}=1.5}$ and ${f_a=3000}$, you will get an answer of about ${132.3}$, which largely agrees with your simulated value of ${131.8}$.

1
On

I haven't worked out the dependent expectations yet, but I did some validation on your simulation table as a quick gut-check to see if it checks out and it does.

Gut check calculation

Let ${C_d}$ be the total number of casualties. Let random variables ${r_a^1,r_a^2,r_a^3}$ be the attacker's roll factors from rolls ${1, 2,}$ and ${3}$ respectively, with each ${r_a^i}$ uniform on ${[0.4,1]}$ and mutually independent. Then the defender's total casualties ${C_d = 0.018337*f_a*(r_a^1+r_a^2}+r_a^3)$. Where ${f_a}$ is a constant. Hence the expected number of the defender's total casualties will be: $$E(C_d) = E[0.018337*f_a*(r_a^1+r_a^2+r_a^3)]$$$$\Rightarrow 0.018337*f_a*[E(r_a^1) + E(r_a^2) + E(r_a^3)] = 0.018337*f_a*[0.7 + 0.7+0.7] $$$$\Rightarrow 0.018337*0.21*f_a = 0.0385077*f_a$$ $$\therefore E(C_d) = 0.0385077*f_a$$

For the case you describe with ${f_a = 3,000}$, we expect there to be ${0.0385077*3000=115.5231}$ casualties.

We also have that ${p=\frac{25}{9}*(\frac{4500}{3000}\frac{2}{25}+\frac{3000}{4500}\frac{1}{2}-\frac{4}{10})\approx0.148148148}$.

Using the Law of Total Probability, it should be the case that:

$${E(C_d) = E(C_d|i)*P(i) + E(C_d|m)*P(m) + E(C_d|p)*P(p)+E(C_d|u)*P(u)}$$ We can calculate this from your table using the final average values, so ${E(C_d|i)=142.5251, E(C_d|m)=131.7705}$, and so forth. Likewise the probabilities ${P(i), P(m), P(p)}$, and ${P(u)}$ can be calculated using the binomial formula you suggest with ${p=0.148148148}$. Multiplying and summing based on the Law of Total Probability shows your simulation giving ${E(C_d)\approx115.0240856}$ which agrees with the true expected value of ${115.5231}$.

My gut tells me that the Law of Total Probability will also help us figure out ${E(C_d|i)}$ and the like.