Sunday, 15 March 2015

I've spent way too much time on this stupid problem: Endor vs Death Star.

Today's main point is a continuation of thoughts on Ewoks.  Earlier this week, I saw this image in my RSS stuff:


And searching online for it again led me to this, which is an overly long discussion of a bunch of nonsense about how Endor would have been incinerated by the debris from the Death Star.  After thinking about it for a few minutes, I came to the conclusion that that didn't make much sense, as pre-explosion, the DS had to have been moving sufficiently to not crash, so after the explosion, only a small fraction should hit the planet.  Right?

So let's throw physics at the problem, and see.  According to the Star Wars wiki page, Endor has a radius of 2450km radius.  If it has the same average density as the Earth, this gives it a mass of 3.39e23 kg.  We're trying to slam stuff into it, however, so let's go with an alternate calculation that the surface gravity is the same as on Earth (the Earth density Endor has about half-Earth gravity).  This gives about twice the mass at 8.82e23 kg.

Now we know what the target looks like, but what about the DS?  It apparently has a radius of 80km.  What does that mean in terms of mass?  No idea.  It's made of "quadanium steel," so we have to make something up.  Is it steel?  That's 7.75 g/cm^3.  Maybe quadanium makes it lighter, so maybe something like aluminum at 2.70 g/cm^3?  Plus, a lot of the DS is empty, because otherwise you couldn't do stuff inside it.  So there's a fill factor to deal with.  Let's say all the rooms look like this one:

This one.
That's an imperial shuttle, and those are 20m long, and roughly square-ish.  Stamp that footprint around, and I come up with something like 120m * 120m * 60m = 864000 m^3.  If the walls are 5m thick, the fill factor for the DS made up of these rooms is 33%.  In the first movie, the walls tend to look thin like a regular wall.  This yields a fill factor of about 1%.  This results in less mass for the DS, which turns out to be super important.  The high end mass is steel with fill factor=1.0, or 1.66e19 kg.  The low end is aluminum with a 1% fill factor, at 5.8832e16 kg.

Why?  The DS blows up.  It's a good "kablooey" kind of explosion.  Assuming this is going to completely disperse all the mass to infinity, this means the explosion is comparable to the gravitational binding energy, U = 3/5 * GM^2 / R.  For our 80km radius, and the high and low mass estimates give binding energies of 1.38e23 and 1.73e18 MJ.  This has to be converted to kinetic energy of all the debris particles, so assuming equal mass, each particle gets, U = 1/2 (M/Np) * v^2, so v = sqrt(Np) * [129 7.67] km/s.

What's left to know?  Where the DS is located in association to Endor.  I remembered this scene:

Zap!
But that turns out to not help.  I had planned to measure the length of the chord on the arc of Endor, and use that to work out how far behind the DS it is.  However, since this is the CGI reconstruction of two models on a stage, this doesn't work.  Using that chord to work out the projected scale of Endor, given the known "real" radii of the two objects means that Endor is in front of the DS, as the model Endor wasn't big enough to create the correct arc.

This isn't a big deal, as I remembered that the DS is orbiting Endor because there's a shield generator that's protecting it.  This means the DS has to be in a geostationary orbit.  That wiki page for Endor claims it has a period of 18 hours.  So the DS orbits at 18433km, and has an orbital velocity of 1.79km/s (which was yesterday's plot).

Put this all together, and run it through the N-body simulator to see what happens, and you get:

It freaking explodes.
How much does it explode?  It explodes all the way.  For the randomly placed particles (they all start on the surface of the sphere at 80km), one triggered the "collision" flag in the simulation.  This makes sense, as at the distance to Endor, it subtends 0.13radians, which translates to 0.435% of the full sky close to the 1/200 simulation particles hitting.  This is still 2.94e14kg being dumped on the planet.  That's about a third the size of the Chicxulub impact, although it'd be spread out somewhat instead of being a single impact.

The other fun thing I tried was reducing the explosion strength by slowing the particle velocity:

Red is 100 times smaller and blue is a 1000.
In these cases, the particles continue to largely follow the original DS orbit.  In the lowest energy case, Endor is spared, and the extra energy goes to kicking particles out.



Monday, 23 February 2015

Trigonometry.

I have five colors of post-it notes, and I want to make a post-it-coaster.  The first time I did this, I did it by eye, and it didn't work so well.  Last time, I realized that I actually know math.
The base post-it.
 Five post-its means I'm making a pentagon.  The sum of interior angles of a polygon is 180 * (n-2) or 540.  There are five such angles, so each one is 108.  The excess from the 90 corner of a post it is 18.  Converting this to radians tells us that q = pi/10, so tan(q) ~ 1/3 (due to small angle formula).  The triangle formed from this has to meet the triangle from the opposite corner halfway across the base post-it (due to symmetry).  This means that the intersection point is 1/6 the height of the post-it.
Make a mark there.
 Now all the remaining post-its can be placed, keeping that angle in common.
And you get a tiny corner that isn't totally correct, but it's close enough.

Saturday, 25 October 2014

Orbital mechanics

Julie mentioned a hack-mo-tron thing about asteroids last week.  I was reasonably sure that the math wasn't super difficult, and so after waking up today, I decided to sit down and see if that was true or not.  First, you need to see the diagram of things from wikipedia:

Then you need to note that the orbit is elliptical, and finally remember Kepler's second law.

And then you do math at it.
It's harder than I thought, mostly because step two doesn't have a analytic solution.  The methodology here is as follows.

  1. First, work in a coordinate system in which the origin is at one focus of the elliptical orbit, and write down the equation for the distance to the orbit from that origin as a function of the true anomaly, listed here as theta.  Precalculate the integral of that because you need it later.
  2. Next, we want to work in fixed (or at least known) time steps.  Therefore we need to know theta as a function of t.  The t here is actually (t_abs - t_perihelion) / period, in order to normalize it sanely.  Next, you do a bunch of calculus, and use the fact that the full area is swept out over one period to calibrate things.  This gives a mess that's effectively a constant for a given t, called kappa here.  This has to be solved analytically, but over the range [-pi:pi], there's only one non-trivial solution, so you can do a quick Newton method solution to find it.
  3. Use the anomaly and orbital radius to write the vector U, which contains the (u,v,w) position in orbit coordinates.
  4. Use R_omega to orient the orbit in real space using omega, the argument of perihelion.  This is just a simple spin.  Next, use R_Omega to handle the inclination, with Omega the ascending node and i the inclination angle.  This yields X, which is the (x,y,z) in a universal coordinate system.
  5. Deproject (x,y,z) to (x',y'), because there is no decent 3d visualization code.

I don't have plots because I couldn't find a good arbitrary plotting library that was easy enough to use to do so.

Sunday, 28 September 2014

The Simpsons cast vs aging.

I came home, watched a Simpson's episode that was sufficiently bland that I sat down and reimplemented the thing I did for the Supreme Court for a generic data file.  Feeding the main voice actor cast for the Simpson's into it (and quantizing on years because working out partial years would have taken more time than I wanted to spend.

Individually.
Summed.
Based on this, The Simpson's is unlikely to lose any of the main cast until 2020 at the earliest.  Assuming they keep going after that, they can continue for about another twenty years before they lose half of the cast.  However, Harry Shearer is that first line, and he voices about a third of all characters on the show.  That would probably be hard to recover from.

Tuesday, 29 July 2014

"Hey, I could probably automate that in perl, and make it a game."

cat kitten sword robot space pink white bumblebee bee helmet

Code under the cut.  Rules for the game:  player one chooses an image, and the other players need to come up with descriptive words for what's on it.  Then, you run the command on the best set of words (maybe you don't want to use all of them because you end up with legos.  Maybe you get points if you only use like 1 word, but if you use 9 or more, but get legos, you lose points).  If the image itself, or the character, or an obviously "these images are related somehow" match happens, then the seekers get the point.  If not, then the player who chose the original image gets a point.  So under these rules, the Bee and Puppycat image gets a point, and I lose two points because I didn't find B&PC, and I ended up with legos when I used 10 words.


Monday, 16 June 2014

Because some questions deserve answers.

I saw this episode of Bob's Burgers last night.  Basically, Bob is nearing his 100000 burger, and some jerk shows up with a cow to protest animal cruelty and asks "how many cows had to die for your burgers?"

First, let's take the National Cattlemen's Beef Association beef research guide, and accept their calculations for a standard 1300-pound steer.  Summing their numbers and then noting after-the-fact that they do it themselves on page 7, this results in a cow containing 638.2 pounds of meat, of which 265.1 pounds are ground beef.  This is where you have to decide if Bob can offset future ground beef with the other cuts contained in the cow or not (is he charged .41 cows or 1 cow per cow).  Assuming he's making standard quarter pound burgers, this results in either 39.173 cows (assuming he's allowed to offset) or 94.304 (if all cows that contribute count).

That's way lower than I would have guessed beforehand.

Saturday, 12 April 2014

I was going to talk about statistics, but instead I'm going to talk about sports.

No, not really.  Mostly it's statistics, just stapled onto a sports frame.

Firstly,

Sports.

I don't generally care about sports.

However, there was that Warren Buffett billion dollar thing, so it became slightly less annoying than it usually would be.  The important thing to remember was to not actually watch any of the games, because they really don't matter.

"Huh?"

Yeah.  Here's the secret: sports teams are just random number generators, and if they come up with a bigger number than their opponent, they win.

"But teams are made up of people..."

More RNGs. (Theorem left unproved due to obviousness: The output of the sum of multiple RNGs is itself a RNG.)

"that have different capabilities that influence how the game ends."

So you're telling me they're biased RNGs.

"And you have to incorporate details about how they're doing, and think about how pumped up they are for a given game, and all sorts of other things that influence the outcome!"

Got it.  Teams are biased RNGs, and if they come up with a bigger number, then they win.  Some RNGs have a built in bias, signifying that they're "a better team."

A Model.

The nice thing is that this is kind of already a solved problem.  Latent ability models.  Given a dataset representing the outcomes of many games, you can assign a score that represents how good a team is.  This "ability" is "latent" because you don't know what value it has, or even what determines the value in terms of real-world qualities.  But you can look at the data you have, and use it to decide if in a future match up, team A or team B would win.  Here's a plot:

Once you've assigned abilities, then you expect the probability of a victory to be related to the difference of the abilities for the two teams.  You use this logistic function to model that probability to ensure that you don't get absolute probabilities for most situations.

"But what about teams that don't play each other?"

Since the ability of team A is based on the differences in ability with all of the teams they did play.  This means you don't need to know the full matrix of team A vs everyone else, you can use the teams A did play, and compare the scores.  In other words, if A is better than B, and B is better than C, odds are good that A is better than C, even if you've never seen A and C compete.

There is some concern about the connectivity of the dataset that can cause problems.  If the dataset is poorly connected, you can get scores where one subset is poorly scaled to the other subsets, as you don't have enough overlap to determine a good solution.  So, what's the connectivity of my dataset look like?
The logistic shape is largely coincidental.  Mathematically motivated, but not important now.  This is also a realization for a single team (team 790), because I didn't care to trace out the full connectivity statistics.
The teams team A plays are a tiny fraction (about 1-2% of the full set of teams).  However, the teams those teams play connects about 25% of the set.  Three steps yield 95%.  Four doesn't quite get to 100%, but I stopped caring at that point.

So since the data is reasonably well connected, you'll probably get a decent solution to the latent ability scores.  The final check (that I totally did when I was developing things, thus proving validity, and not at all just ten minutes ago when I realized I should include this) was to compare the ability scores that I came up with the official "seeds," which is a dumb name for "ranking priors."
Noisy but fairly linear.


My Results.

That's all nice and all, but how does it work?  First, the caveats:  I wrote the LAM code and pushed some tests through in January, and then completely forgot about this whole thing until the day before the tournament started.  This means I basically had to accept the simple LAM solutions, and go with it.  I did a full year solution, and also broke the games down by month, using those solutions to look for trends (if the abilities were improving or decreasing).  Finally, I used the scientific method of "which of these are bigger?" to choose who would win.

A side note unrelated to the statistics:  dancing around team name strings was by far the most annoying part of this project.  Everyone should use clear and concise integer team ids, like I did.  It's the superior solution than inconsistent abbreviations.

Here's who I chose.
Not bad, as I even picked up a few of the upsets.  Things fell apart a bit with the later games, but 38/63 isn't bad, right?

Donking Sports.

At least I beat Julie, which was really what my goal was, if I couldn't win a billion dollars.
One of the things that I probably should have researched is the fact that people who do sports all the time are dumb.  Therefore, later games magically get more points than early games, so you aren't just trying to have more correct picks than the other people.  I suspect this is due to someone getting pissed off that they chose the winner correct, but donked up everything else, and decided that they should win instead of someone who got most of the first round correct.  I can't come up with any other logic for it (if you get round 1 picks wrong, you automatically lose out on any subsequent round outcomes that are based on that incorrect pick.  Why add extra penalties?).

In any case, you can see that I really wasn't too far back in terms of number correct.  You'll also note that I am actually #12, not #13, unless there's some other dumb tie-breaker rule that says that even though 12 and I have the same number of points, and I have more correct picks, I don't win because sports.

I also checked earlier in the week, and I didn't beat Barack Obama, regardless of whether you do number correct or this stupid point system.  I don't remember the values I got.  Like 40 correct and 68 points, maybe?  Whatever, I'm losing interest in this post anyway.

How Can This Be Improved?

As I mentioned above, I kind of rushed at the last minute, and spent more time dancing strings around than I wanted.  This means the theory time and development was more rushed than I would have wanted.  In any case, here are the obvious ways to improve things.

  1. Wichita State.  After determining everything, I later heard on NPR that WS was undefeated in the regular season.  "Great?"  Nope.  Because they never lost, the LAM had no reason not to rank them higher than everyone else.  Basically, no matter how high a score some other team got, WS would always rank higher.
  2. Time evolution.  My month-by-month breakout is way too chunky, and I'm pretty sure it didn't equally distribute the data.  Equal game count bins would be better, and that can be tuned to yield the minimum size that still provides a connected data set.
  3. Game quality.  The dataset I have contains scores for each game.  It's easy to imagine a LAM extension in which the distribution of score differences by ability differences tells you something about the distribution of that teams ability.  The other option would be to use that score difference to amplify a given win.  If the victory was by 1000 points, you can probably assume that the winner is "more better" than the loser than if they'd only won by 1 point.
  4. Field advantage.  It also contains home/away status.  This might have a similar effect, where you don't weight home victories as much as away or something.
  5. Inter-year convolution.  I have lots of years.  Teams are comprised of players, and players are not new each year.  Therefore, you could convolve previous and subsequent year abilities with this year's, and see what that gives you.
  6. Data.  I have lots of years.  I should really have been applying the algorithm to all the years, and checking the results against what really happened.  Beyond just this one tournament, women's sports are identical, so that's another set of years with data and results that can be used to train the algorithm.