Installed some new wildlife cameras. Haven’t seen anything new/unusual yet other than this “non-typical” deer:
There’s a cliche probability joke, where the answer to any question, no matter how complicated, is simply: “It’s 50/50 – either it happens or it doesn’t!” and now I’m proud to say I got that joke into a Couch Slouch column in WaPo: https://www.washingtonpost.com/sports/khris-davis-owns-sports-greatest-statistical-oddity-ever/2019/04/21/473ad022-6450-11e9-82ba-fcfeff232e8f_story.html
I also managed to work 52-factorial in as well, with full digit expansion! Enjoy.
I wanted to be able to decode a stream that had multiple, undelineated, JSON string representations in it. For example, a file like this:
[1, 2, 3] [4, 5, 6] [7, 8] [9, 10, 11, 12]
Which has multiple JSON arrays in it, separated only by (optional) whitespace. Perhaps they might be separated by other characters in other applications (e.g., comma separated JSON strings)
First stop, as always, was stackoverflow, which had some relevant answers, like this one:
I decided to write a variant of the several “read it in chunks, try to JSONDecode it, keep reading more if it doesn’t yet parse” solutions. Here is my take at this problem, as a gist:
Posting this in the hopes, as always, that someday someone will be looking for something like this and stumble across it via google (if I’ve somehow worked enough key phrases into this posting)
When I am appointed Supreme Ruler of the Universe, any “creative interpretation” of the Star Spangled Banner will be punishable in kind. The offender will be repeatedly subjected to a medley of Free Bird, “creatively interpreted,” until they see the error of their ways.
Every now and then I make an observation about something I would do to improve everyone’s life, well — almost everyone — if only I were made Supreme Ruler of the Universe. I’m collecting these phrases and posting them periodically on my site.
When I am Supreme Ruler of the Universe, the devs who code the “click every picture that contains a car” robot detector will get run over by cars, ideally driven by robots.
I’ll be adding more; they will all be in the Supreme Ruler of the Universe category so you can find them easily when you are deciding whether you wish to support my humble campaign for this position.
A friend recently asked “what is the difference between an ice sphere and one (large) ice cube?”
Well, let’s do some math. First we have to make some simplifying assumptions, which honestly really would need to be verified. But let’s not let lack of evidence get in the way of doing some fun math!
The idea behind the sphere of ice is that it cools your drink while presenting the minimum surface area, and (presumably) therefore minimizing drink dilution. A sphere has the minimal surface area for a given volume, which is why soap bubbles generally form spheres.
Let’s assume that the total cooling capacity of an ice cube is related to its volume. If we have a cube with side length a, then the volume of that cube will be:
Vc = a3
Similarly, if we have a sphere of radius r, its volume will be:
Vs = (4/3)πr3
Since we want the same cooling capacity in both the cube and the sphere, we need to set the two volumes equal, and then we can determine what the cube side-size a would be for the same volume as a sphere of radius r:
Vc = Vs
a3 = (4/3)πr3
a = [(4/3)πr3](1/3)
 a = [(4/3)π](1/3)•r
Equation  tells us what size cube we need (side length a) in terms of a sphere of radius r, so that the volume of the cube and the volume of the sphere are the same.
Now let’s look at surface area. Surface area of a sphere of radius r:
Ss = 4πr2
Surface area of a cube of side length a:
Sc = 6a2
which, using equation  to get that in terms of r becomes:
Sc = 6[(4/3)π](2/3)r2
and therefore the ratio of the surface area of the cube, Sc, to the sphere, Ss, becomes:
Sc/Ss =6[(4/3)π](2/3)r2 / 4πr2
=6[(4/3)π](2/3) / 4π
=3[(4/3)π](2/3) / 2π
= 1.24 (approx)
So the cube with the same total volume as a sphere will have about 24% more surface area. Still way better than having multiple small cubes of ice in your drink. Enjoy!
The coin flip experiment I described before is still running… so far for 62 days. It is running on a dedicated linux box on my network:
nw@randomlinux$ uname -a Linux randomlinux 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1 (2018-04-29) x86_64 GNU/Linux nw@randomlinux:~$ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 Thread(s) per core: 1 Core(s) per socket: 4 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 76 Model name: Intel(R) Atom(TM) x5-Z8350 CPU @ 1.44GHz Stepping: 4 CPU MHz: 482.695 CPU max MHz: 1920.0000 CPU min MHz: 480.0000 BogoMIPS: 2880.00 Virtualization: VT-x L1d cache: 24K L1i cache: 32K L2 cache: 1024K NUMA node0 CPU(s): 0-3
and the updated results are:
- Trials Completed: 5,008,009
- Total Coin Flips: 8,604,595,583,786
- Longest Trial: 2,785,974,537,774
- Trials Ending in 2 flips: 2,504,265 (50.005%)
Updated breakdown of the percentages of trials completed in N flips, and the corresponding prediction:
|length (flips)||actual %||theoretical %|
I’m letting this run until the next power failure terminates the experiment. Unfortunately the little box this is running on is not set up on a UPS. My power typically glitches (short outages) a few times each year (I’ve written about this before) so I don’t know how long to expect this run to get, but as long as it is still running I will keep collecting the data. Let’s hope for no thunderstorms in the hilltop area any time soon.
They are (finally) sort of working. There seems to be a wiring problem with some of the strings; consequently they are “out of sync” (e.g., red when the rest are green, etc). I am hopeful that will be fixed Friday… in the meantime, I just choose to interpret the cacophony of color as “festive” 🙂
In a previous post I wrote about this question:
On average, how many times do you have to flip a (fair) coin to get an equal number of heads and tails?
and suggested that the answer was probably infinite (on average), although half the time you get there in just two flips: HT or TH, which are half the possible cases for two flips: HH, HT, TH, TT.
To review: there are 4 possible combinations of two flips: HH, HT, TH, TT. Each is equally likely. Two of them – HT and TH – reach equal heads/tails equality. Let’s call this “reaching delta zero” where delta refers to the difference in heads vs tails count. Since two out of four equally-likely sequences reach delta zero, there is a 50% chance of getting there in just two flips.
The next stage has four flips (three flips will never have delta zero), and there are sixteen possibilities for four flips. However, half of them were eliminated at two flips, leaving only the sequences that start with HH or TT:
HHHH, HHHT, HHTH, HHTT TTHH, TTHT, TTTH, TTTT
Two of these sequences reach delta zero: HHTT and TTHH. So there is a 2 out of 8 ( i.e., 1 out of 4) chance of reaching delta zero at four flips, except we have to account for the 50% chance of never even getting to four flips, so there is a 1 out 8 = 12.5% chance of finishing at four flips.
In the prior post I discussed the catalan numbers and how they relate to the number of these sequences. From that discussion we can compute the probabilities of a given trial run ending (reaching delta zero) at any particular stage. This works out as follows:
# FLIPS % reaching equality here 2 50% 4 12.5% 6 6.25% 8 3.91% 10 2.73% 12 2.05% 14 1.61%
Unlike the chart shown in the previous posting, these numbers are the chance of the trial ending at the specific level (e.g., a 3.91% chance any given trial run will take exactly 8 flips, no more / no fewer).
I wrote a simulation in python to test this, and I have been running it for over three weeks. Highlights of key results:
- Trials Completed: 3,414,318. This is how many runs were made where each run consisted of python code along these lines (except the actual code used some unrolling and optimization techniques I’ve written about before). This code is essentially simulating flipping a coin and counting the “delta” between the number of heads and tails, so each flip either adds one or subtracts one from that running delta and equality of heads and tails is reached when the delta becomes zero.
delta = random.choice((-1, 1)) flips = 1 while delta != 0: delta += random.choice((-1, 1)) flips += 1
- Total number of coin flips: 3,197,243,199,696. Yes, you are reading that correctly, over 3 trillion simulated flips. I will point out that python, like many (most? all?) runtimes these days uses the Mersenne Twister random number generator with period 2**19937-1; no older style random number generators that have much much shorter periods would be suitable for this sort of experiment.
- Longest run: 1,204,531,561,152. Yes, that’s another “you read that right”. The longest string of coin flips it took to reach equality was a little over 1.2 trillion. That is, after the first flip generated a delta of +1 or -1, in that sequence it wasn’t until 1.2 trillion flips later that the delta was back down to zero.
- Runs ending at 2 flips: 1,707,887 (50.02%). This is as we predicted. experimental results error is 728 out of over 3.4M trials, which comes to a difference of about 213 ppm (or however you want to think of that difference from the perfect 50%).
Here’s a breakdown of the percentages of trials completed in N flips, and the corresponding prediction:
|length (flips)||actual %||theoretical %|
I found this write-up nicely describing the math of this and in particular analyzing that the average will indeed diverge to infinity.
The results of seemingly-simple probability questions are often counter-intuitive. “How long will it take me to get an equal number of heads and tails while flipping a coin?” and the answer is “fairly often it could take a very very long time”