Aalskdfsdlhfgdsgh, this is **super** cool.

Today in probability we were talking about the binary expansion of decimal fractions—in particular, those in the interval [0,1).

**Q:** WTF is the binary expansion of a decimal fraction?

**A:** If you know about binary in general, you know that to convert a decimal number like 350 to binary, you have to convert it to 0’s and 1’s based on a base-2 system (2^{0}, 2^{1}, 2^{2}, 2^{3}, 2^{4}, …). So 350 = 2^{8} + (0*2^{7}) + 2^{6} + (0*2^{5}) + 2^{4} + 2^{3} + 2^{2} + 2^{1} + (0*2^{0}) = 101011110 (1’s for all the “used” 2^{k}’s and 0’s for all the “unused” 2^{k}’s).

It’s the same thing for fractions, except now the 2^{k}’s are 2^{-k}‘s: 1/2^{1}, 1/2^{2}, 1/2^{3}, …).

So let’s take 5/8 as an example fraction we wish to convert to binary. 5/8 = 1/2^{1} + (0*1/2^{2}) + 1/2^{3}, so in binary, 5/8 = 101. Easy!*

So we used this idea of binary expansion to talk about the Strong Law of Large Numbers for the continuous case (rather than the discrete case, which we’d talked about last week), but then we did the following:

where x = 0.x_{1} x_{2} x_{3} x_{4}… is the unique binary expansion of x containing an infinite number of zeros.

Dr. Chen asked us to, as an exercise, plot γ_{1}(x), γ_{2}(x), and γ_{3}(x) for all x in [0,1). That is, plot what the first, second, and third binary values look like (using the indicator function above) for all decimal fractions in [0,1).

We could do this by hand (it wasn’t something we had to turn in), but I’m obsessive and weird, so I decided to write some R code to do it for me (and to confirm what I got by hand)!

Code:

x = seq(0, 1, by = .0001) x = sort(x) n = length(x) y = matrix(NaN, n) z = matrix(NaN, n) bi = matrix(NaN, n, nrow=n, ncol=3) for (i in 1:n) { pos1 = trunc(x[i]*2) if (pos1 == 0) { bi[i,1] = 1 y[i] = x[i]*2 } else if (pos1 == 1) { bi[i,1] = -1 y[i] = -1*(1-(x[i]*2)) } pos2 = trunc(y[i]*2) if (pos2 == 0) { bi[i,2] = 1 z[i] = y[i]*2 } else if (pos2 == 1) { bi[i,2] = -1 z[i] = -1*(1-(y[i]*2)) } pos3 = trunc(z[i]*2) if (pos3 == 0) { bi[i,3] = 1 } else if (pos3 == 1) { bi[i,3] = -1 } } bin = cbind(x,bi) bin = as.matrix(bin) plot(bin[,1],bin[,2], type = 'p', col = 'black', lwd = 7, ylim = c(-1, 1), xlim = c(0, 1), xlab = "Value", ylab = "Indicator Value", main = "Indicator Value vs. Value on [0,1) Interval") lines(bin[,1],bin[,3], type='p', lwd = 4, col='green') lines(bin[,1],bin[,4], type='p', lwd = .25, col='red')

Results:

(black is 1/2, green is 1/4, red is 1/8)

I tried to make the plot readable so that you could see the overlaps. Not sure if it works.

Makes sense, though. Until you get to ½, you can’t have a 1 in that first expansion place, since 1 represents the value 1/2^{1}. Once you get past ½, you need that 1 in the first expansion place. Same things with 1/2^{2} and 1/2^{3}.

**SUPER COOL!**

(I love R.)

*Note that this binary expression is not unique. All the work we did in class was done under the assumption that we were using the expressions that had an infinite number of zeroes rather than an infinite number of ones.