DPS Theory

January 9, 2009

Math and Nature*…

Filed under: Math, Science — Peter @ 8:02 pm

I am looking for a cool site full of book reviews and recommendation of books that I would love to read, but that it would never occur to me to look for myself. You know what I mean?

Here’s a sight that is clearly not what I’m looking for. Checking them out, I immediately went to their hard science section, and clicked here for this book on General Relativity.  The publisher’s website associates this quote with the book:

” Mathematics is not the language of Nature. It is the language of mathematicians. “

This is so true! Although, honestly, the rest of the book seems like complete bunk. But really, there is the enduring mystery of why nature† is so well described by math. Well, I guess it sort of is well described by math. Simple arithmetic isn’t so good for describing quantum mechanics, but the theory of partial diffential equations does a pretty great job of it, if you interpret it correctly–which is a straightforward, if not intuitive, process.

Some people think it’s quite profound that math is so good at describing nature. It’s really really good for it, almost as if nature was written in math by a cosmic mathematician. Math is really everywhere, not just in physics, but in biology…and I suppose that exhausts all of nature. And the same math comes up so often, in seemingly unrelated problems; for example things like Laplace’s equation, which comes up in heat transfer, diffusion, wave mechanics, quantum mechanics, and lots of other things that don’t have anything to do with that stuff, like probably economics.

Well, here’s why math is so successful at describing nature. It’s because that’s what people have been creating math to do. Math is a tool, created by people, to do things with**. And we have expanded our ideas of what math is until we could use it to describe nature with high precision. Now, maybe it’s remarkable that nature is so predictable that it can be described at all, but given that the universe has been stable enough over the past 4 billion years for us to evolve, it only stands to reason that the universe we see should be predictable.

As fer the strange coincidence of some equations and constants (Euler’s constant comes to mind) popping up everywhere, that has more to do with those equations and constants being very easy to make approximations than that systems really behave that way. I mean, they do behave that way, but only if you aren’t looking closely enough.

*and video games

†I really do mean to get  on with the fascinating project of describing video games mathematically, really!

**it’s been somewhat frustrating to me that math professors seem oblivious to the fact that a lot of the math that they teach was developed with specific problems in mind. I think math education would be much easier on the student if students were introduced to the types of practical problems that math is good for before they are introduced to the proofs and theorems of the theory.

June 20, 2008

Thermodynamics for Climate Change Denialists

Filed under: Math, Politics, Science — Peter @ 7:00 pm

Let’s take a look at the latest ideas in global warming denialism (with a screencap, in case the original source ever gets embarrassed by this). It goes something like this:

Greenhouse gasses can’t increase the temperature of the Earth: they don’t add heat to the climate (causing global warming), they just trap what heat is already there, slowing down any cooling. Those are two different things.

It’s a nonsense argument, so trying to really understand it is impossible, but I think that’s a decent approximation. My first reaction was similar to PZ’s, that it’s true as far as it goes, but omits the effects of any external source of heat.* But really, it’s not even true if you forget about the sun (but remember anything else about the climate). If you slow the cooling of the Earth at evenings and during the transition from summer into winter, you will increase the average temperature of the Earth without increasing the maximum temperature of the Earth. Therefore, the globe will warm.

But let’s see what happens when we don’t forget that big hot yellow Sun that’s heating us up. Generally, as you pour more energy (sunlight, and to a much lesser extent these days geothermal energy) into a system, the system heats up, temperature rising. As the temperature rises, the body (the planet Earth) emits radiation, shedding heat into its surroundings (in the case of the Earth, the radiation is primarily infrared, although atmosphere evaporating into space would count, too). The rate that heat is radiated off is proportional to the temperature of the Earth. The rate that heat is trapped is proportional to the reflectance r (albedo) or absorbance A = 1- r of the object, as well as surface area, and maybe some other things that will be constant here. The temperature T equation looks like this:

CdT/dt = AP0 – kBT

with C the heat capacity, heat per unit temperature, of the Earth, P0 = the (constant) rate of energy flow into the system, from the sun (Isun•σ), geothermal sources, cosmic rays, whatever, and kB the constant describing the rate of heat loss per unit temperature due to radiation (and other sources, such as evaporating atmosphere or maybe chemical reactions, which are mostly constant-”conduction” and “convection” wouldn’t really apply to the planet Earth as a whole). Roughly speaking, to determine the temperature of the Earth, you solve this equation for dT/dt = 0, the thermal equilibrium condition where heat flow in is equal to heat flow out. Increasing the absorbance of the Earth by increasing greenhouse gas concentrations increases the equilibrium temperature, which we’ll interpret as an increase in extreme temperatures of the climate, as well as increased average temperature.

Of course, the fine details of weather and climate are MUCH MUCH MUCH more complicated than this–but they’re much more complicated that you’d expect to see on a children’s climate website, too, and they’re more complicated than Kate and Hans Schreuder seem to realize, too.

*It’s amazing to me that they have made fundamentally the same mistake that some people make when they claim that evolution violates the 2nd law of thermodynamics, i. e. they forget that there is a Sun.

Alright, here goes: the atmosphere looks like a thin film, with light shining down, with no transmission through the surface of the earth, some absorption, some reflection; some of the reflected sunlight is reflected by greenhouse gases back to the Earth’s surface, and either absorbed or re-reflected back to the atmosphere to be re-re-reflected, and so on. With each reflection back to the surface, a bit more gets absorbed; each reflection back to the atmosphere a little more gets back out to space; and also, there is a bit perpetually trapped reflecting back and forth between surface and atmosphere (which helps keep us from dropping to near 0 kelvins at nights). Turn the Sun off, and that trapped bit will decay exponentially, but the Sun isn’t going anywhere anytime soon.

Chemical reactions and some other things are technically additional degrees of freedom, and change the heat capacity C of the system. More chemical reactions, for example, would slow down temperature rise, but not necessarily the rate of heat gain or loss.

May 31, 2008

Obscure details in d20

Filed under: D&D, Games, Math, d20 — Peter @ 6:09 pm

I was wondering, what’s the probability that you’ll land AT LEAST some number of hits in d20. Turns out, it’s a lot easier to ask the question than to answer it. Start with at least 1 hit…easy enough:

P(at least 1 hit) = 1- P(all misses) = 1 – ∏i(1 – Pi(hit))

That was so easy! Plus, it makes it super duper easy to calculate P(at least 2 hits):

P(at least 2 hits) = P(1 hit) × (1 – P(all the rest misses)) = ∑iP(hit)×[1 - ∏j≠i(1-Pj(hit))]

And so on, until you exhaust all of your BAB-(5 per swing), at which point you get

P(all hit) = ∏iPi(hit)

Which is again easy. So, computing that stuff is very time consuming, but straightforward. It’s not as bad as it could be, though, because your max hit chance is P(hit) = .95, and min is P(hit) = .05, and at -0.25 per swing, you’ll only have at most 6 distinct Pi(hit), including several easy swings at P(hit) = .95 and hard swings at P(hit) = .05, at one each of the other 4. So if you get many many attacks per round, many of the terms in those sums will be identical. Maybe I’ll write a program to compute these things, maybe I’ll work out a rough approximation that’ll be easier to compute. /shrug

May 26, 2008

DPS in d20

Filed under: DPS, Games, Math, d20 — Peter @ 3:39 pm

Damage per combat round in d20 almost looks simple, until you know the rules. Which I don’t, thankfully. However, I know some of the rules of the Neverwinter Nights games, which while simpler than the pen and paper version of D&D, are still pretty arcane. But in a nutshell, what we need to know to calculate DPR(ound) is that you get one attack per round, plus one for every 5 points of BAB above 1. So you get 2 attacks for BAB on [6,10], 3 if BABε[11,15], and so on. Each additional attack has an additional -5 penalty to the to-hit roll, i.e. -5 on the second hit, -10 on third hit, -15 on 4th hit and so on. So if you like, you get to keep attacking at -5 more per attack until your remaining BAB is 0, at which point you can’t attack any more. You may also get off-hand weapon attacks, which are penalized in a complicated way depending on various feats you’ve earned and the size of the weapon you’re wielding in the off-hand. It can go from -6 main hand, -10 off hand to -2 each hand in; getting from the worst case to the best takes one more feat in NWN than in NWN2. Additional off-hand attacks will cost more feats.

Once we know all that, we can describe the chance to hit a target as:

P(hit|on the sth swing) = min[19/20, max(1/20, 20 + (∑ to hit bonuses and penalties) - AC - 5s)]

for main hand attacks. Then add those up over the S = int[BAB-1/5]+1 swings, and you’re set! Putting that together with the expected damage calculation from last post, it looks like:

DPR = d ∑BAB-5s>0 {min[19/20, max(1/20, 20 + (bonuses and penalties) - AC - 5s)]×[1 + (n-1)×min(w/20, P(hit|sth swing)]}

not accounting for feats like power critical (+4 to confirm critical) and so on. Adding in additional damage from extra attacks is straightforward.

Of course, we probably don’t care in particular how we’ll do against opponents with a specific value of armor class, and would like a measure of our effectiveness overall. Well, if you want, you can average that beast above over all armor classes, 1-∞…or up to some reasonable number, say 1-your character level, or character level +20, or something, depending on your environment. Of course, that’s overkill…there is a built in cut-off point, any AC > 19 more than your BAB+bonuses you’ll only hit on on a roll of 20, so you don’t need to give extra consideration to AC > 20 more than BAB+bonuses. Similarly, but more complicatedly, any AC < BAB – 5S will be hit on all but a roll of 20 (on every attack in a round), although you might allow some slack for those awful occasions when you’re debuffed by an enemy spell. Ideally, if you can assign a reasonable estimate, you could weight each AC by the likelihood of encountering that AC, like so:

<DPR> = ∑AC P(AC) DPRAC

and even better, would be to weight it not just by probability of encountering that particular AC but also by the threat that an opponent with that AC represents. That is, an opponent with an AC of 8 is likely a level 1 pauper who can’t afford any armor, and probably can’t afford anything deadlier than a stout cudgel (i. e. broken table leg), and will barely be able to touch your level 18 weapon master in enchanted plate armor let, so do you really need to worry about taking him out as fast as mathematically possible? So the best calculation we can do is probably something like:

<ΘDPR> = ∑AC <Θ>ACP(AC) DPRAC

where Θ is the measure of the threat that the target represents, which I will talk about in a future post.

Anyway, short of being able to put all that information together, I recommend plotting your expected damage out in a spreadsheet. Maybe I just suck with spreadsheets, but the use of the min and max functions seems to me to make it a pretty cumbersome thing to enter into the spreadsheet…the contents of the cells won’t vary in an orderly way.

May 21, 2008

Critical Hits in d20

Filed under: DPS, Games, Math, d20 — Peter @ 8:36 pm

Since of course there’s nothing more important to write about, such as states full of people who’d refuse to nominate a black HARVARD LAW GRADUATE for President of the United States just because he’s black (and therefore inferior to all the white people who’ve GRADUATED FROM HARVARD LAW SCHOOL magna cum laude–oh, well there ya go, a white person would’a graduated suma…)

Anyway, since there’s nothing like that to blog about this week, I’ll move on to how to weigh critical hit properties of weapons in d20. Is a broader threat range better or a higher multiplier? I’ll denote critical hit properties in d20 like so:

{w|×n}

where w is the width of the threat range, such as 18-20, 19-20, or just 20, and n is the threat multiplier. The threat chance is the width of the threat range divide by 20, or the chance to hit–whichever is smaller. Once you roll a critical threat, you roll d20 again to “confirm” the critical, with the goal to simply roll a hit this time. If you fail this roll, you hit the target, but don’t get the critical multiplier to your damage. Therefore, the expected damage on an attack looks like:

<D> = ∑ P(D) × D = d×P(regular hit) + nd×P(crit) = d[P(hit) - P(threat) + P(threat)P(miss) + nP(threat)P(hit)]

= d×P(hit)[ 1 + (n - 1) P(threat) ]

where d is the expected value of the damage roll, d = (min damage + max damage)/2, and P(threat) = w/20 (or hit probability if it’s lower) is the probability of rolling a critical threat. Note that for typical threat ranges (19-20 for sword, 20 for axe) and multipliers (2 for sword, 3 for axe), you get about 10% of your damage from critical hits; while with some feats, keen weapons, weapon master perks, it might get up to 20% or more (unless you fight a lot of undead, etc. with critical hit immunity).

So, we have

(1/d×P(hit))<D+> = w(n-1)/20

(1/d×P(hit))Δ<D> = [w/20] × Δn

(1/d×P(hit))Δ<D> = [(n-1)/20] × Δw

with <D+> the extra damage that comes from critical hits, which depends on 1 less than n because you were going to do some damage anyway. When weighing critical hit properties, you are mostly concerned about the product (width of threat range)×(crit multiplier – 1). A sword with {19-20|×2} is worth 2×1=2, an axe with {20|3} is worth 1×2=2, and a falchion with {18-20|×2} is the same as a scythe with {20|×4}, both equal 1.5. Keen weapon or improved critical both double the threat range, which doubles the value of critical hits. Adding one the the critical damage multiplier doubles the value of a sword, but only increases the value of a scythe by 1/3.

The exception is if you expect to be fighting enemies with ACs so high you’ll only hit them on rolls of 20, or if you are acquiring feats or magic weapons that give extra damage on critical hits. Extra critical threat range is just wasted in that situation. Also, since you’ll rarely hit such a beast, you’ll want to get the most out of every hit. Therefore, the scythe, if available, is the way to go…except that really, you’ll only be hitting one in every 20 swings, and critical hitting one in every 400 swings, so maybe you should just run in that situation. Except that, of course, advancing the plot probably depends on winning the fight, and the designers have probably locked you in a room with him. /sigh

Anyway, there are some annoying discrepancies in my notation, which I’ll straighten out in a future edit.

May 17, 2008

Hitting a Target in d20

Filed under: DPS, Games, Math, d20 — Peter @ 6:46 pm

In the current (version 3.5, anyway, I think ver. 4 is out now) edition of Dungeons & Dragons, hitting (or missing) a target is pretty easy. It is, after all, meant to be done by people playing on a tabletop with dice, a pencil, and paper…not necessarily on a 3GHz microprocessor pushing a few billion floating point operations per second. So all you do is roll a 20 sided die, you always miss on a 1, always hit on a 20*, and otherwise, if your hit roll plus “base attack bonus(BAB)” ± other modifiers is greater than your target’s Armor Class (AC), you hit your target! The BAB depends on your character’s class and level, you’ll look it up in a table. Other modifiers are any applicable strength or dexterity bonuses, weapon enchantment bonuses, buffs or debuffs, and about a hundred other details in the D&D rules. Armor class depends on the type of armor, be it plate armor, chain mail, leather, whatever, and dexterity (for dodging), and some other things. Thus, the basic probability of a hit is

P(scoring a hit) = {MIN[19/20, MAX(1/20, (20 + BAB + to-hit modifiers - AC)/20)]}

All very linear and easy, no squaring, no roots, no exponentials. Yawn.

However, just to spice things up, Wizards of the Coast have also allowed you to roll a “critical hit (or crit, for short).” On a crit, your damage is multiplied by two, three, or more depending on the type of weapon you’re weilding and your proficiency with that weapon, as described by “feats” you have acquired. To roll a crit under the v. 3.5 rules, you must roll high enough to hit (including modifiers), and the number on the face of the d20 must be within the “critical threat range” of the weapon you’re swinging: 20 for a battle ax, 19-20 for most swords, and 18-20 for weapons such as scimitars. However, after rolling (a hit) within the critical threat range, you must then roll the d20 again, with the same modifiers (you may have a feat that gives an extra bonus to this roll), and score a hit. Failing this second roll still results in a hit, but not a critical hit. So, the probability of a critical hit given that you’ve rolled a critical threat is:

P(crit hit|crit threat) = P(scoring a hit)

or, with the aforementioned feat (Power Critical, gives +4 bonus to hit on this roll with a specific weapon)

P(crit hit|crit threat with power critical feat) = P(scoring a hit) + 4/20 (for 2/20 < P(scoring a hit) < 16/20)

The probability at the ends is still capped at 1/20 to miss and 1/20 to hit.

Thus,

P(crit) = .05×MIN(threat range, hit chance) × P(scoring a hit)

without Power Critical and

P(crit with power critical) = .05×MIN[threat range, hit chance] × [P(scoring a hit) + .2] .2 < P(scoring a hit) < .8

Now, what’s the chance of scoring a regular hit, but not a crit?

P(regular hit) = P(scoring a hit) – P(crit) = 1 – P(crit) – P(miss)

That’s straightforward enough.

Now that we can hit a target, which is worth more? A more damaging crit (higher crit multiplier) or a more accurate crit (broader threat range/power critical)? I’ll look at that next time.

*If for some reason you can’t see your target, say cuz your character is blinded or the target is invisible, there will be another d100 roll against a concealment percentage.

April 19, 2008

Real War

Filed under: Iraq, Math, Politics — Peter @ 5:28 pm

I get annoyed every time I hear about how tragic it is that 4000+ US soldiers have been killed* in Iraq. Of course, every death is tragic to someone, but people die all the time. In particular, about 90 people aged 15-24 of every 100k die each year in the US, without even going to war. Since there are about 170kUS soldiers in Iraq, a soldier is about 5x more likely to die in Iraq than if s/he had stayed home. Still, the odds of dying at home would have been pretty darn small, so a year in Iraq actually only reduces your life expectancy by about 90 days** (out of 76 years baseline life expectancy). Smoking, for example, cuts about 11 minutes per cigarette: 90 days of life expectancy would be 1 per day for 35 years–or 10 per day for 3.5 years. 60,000 dead and 300k wounded in 10 years in Vietnam. It’s been a fairly safe war for US soldiers.

Each year, about 43,000 people in the US die in car accidents. In the past 8 years, some 4000 people in the US have died by acts of international terrorism. So far, the Iraq war is costing us about $2 trillion.

Estimates of Iraqi deaths due to the war vary wildly, from a little over 100k to over 1M, out of a total population of 28M.

*According to Wikipedia, there are about 30,000 more wounded in combat and about another 30,000 wounded, diseased, or other impairments outside of combat.

**Very rough estimate. Life expectancy = <L> = P(surviving a year in Iraq) x (76 years) + P(dying in a given year in Iraq) x (current age of about 20 years) = (.9953)(76) + (.0047)(20). Soldiers will be healthier on average than the general population, so would probably have been more likely to survive/longer life expectancy if they’d stayed home. Also, it doesn’t account for reduction in life expectancy due to non-lethal injuries, the chance of which might reduce <L> by an even rougher 3 years more. And not all soldiers are there to fight, so the actual reduction in <L> will depend on mission.

Basics of DPS

Filed under: DPS, Games, Math — Peter @ 3:50 pm

Roughly, a video game warrior’s threat can be described by it’s ability to output damage (dps) and it’s ability take damage (total hitpoints, adjusted for armor and resistances, depending on the game and situation). Let’s parse dps:

The simplest measure of dps is average damage per attack times number of attacks opportunities per unit time. In a first person shooter, that might be

dps = (rate of fire) x (damage per shot)

In a Dungeons & Dragons game, like Neverwinter Nights 2, a fighter could expect something like

average damage per attack = d = (max damage) + (min damage)2 + (str bonus) + (other modifiers)

damage per round = d x (number of attacks per round)

= d x {1 + int([BAB-1]/5)}

= d x {1 + int([level-1]/5)}

In MMOs I’ve played, including WoW, Star Wars Galaxies*, Pirates of the Burning Sea, and maybe EVE Online,

dps = (average attack damage)/(attack speed)

because for some odd reason, attack “speed” in those games is actually the time between attacks, or the attack period, instead of the rate of attack, like “speed” would mean in the rest of the world.

These numbers can be useful for comparing small tweaks among similar characters, but they aren’t realistic approximations of the actual dps you’d see while you’re playing a game. First, of course, you have to actually hit your target, and these formulas don’t account for accuracy**. Then, some games award extra dps for critical hits or head shots. There are also area effect attacks, situational bonuses (like sneak attacks and backstabs), debuffing attacks that do less damage but tilt the battle in other ways, etc. Which is good, because if video games could be distilled to such simple equations, they wouldn’t be any fun.

*mathematically, this was the most muddled and egregiously unbalanced game I’ve ever seen. SOE has changed the rules drastically since I played, hopefully for the better.

**formulas accounting for accuracy will get pretty monstrous, of course

Blog at WordPress.com.