• Emily Zhang
  • Fiddler

Round, Round, Get a Round, I Get a Round

2024 Aug 232024 August 23 Problem Back

This Week's Fiddler

Let's consider the error of a number being the difference between the rounded result and the number itself. So 0.40.40.4 would have an error of −0.4-0.4−0.4, and 0.70.70.7 would have an error of 0.30.30.3. The error is thus uniformly distributed from (−0.5,0.5](-0.5, 0.5](−0.5,0.5], where any x<0.5x\lt0.5x<0.5 incurs an error of xxx, and any x≥0.5x\ge0.5x≥0.5 incurs an error of 1−x1-x1−x.

Now let's consider summing two random numbers. The errors are additive. For example, 0.4+0.30.4+0.30.4+0.3 would have a combined error of −0.4−0.3=−0.7-0.4-0.3=-0.7−0.4−0.3=−0.7. Its sum of rounded numbers would add to 0+0=00+0=00+0=0, but its rounded sum would be rounding 0.4+0.3=0.70.4+0.3=0.70.4+0.3=0.7 to 111. So the combined error must rest between −0.5-0.5−0.5 and 0.50.50.5 for the sum of rounds to agree with the rounded sum.

In other words, the question is asking what the probability is for two independent and identically distributed (i.i.d.) uniform distributions from −0.5-0.5−0.5 to 0.50.50.5 to have a sum between −0.5-0.5−0.5 and 0.50.50.5. As the sum of two uniform distributions leads to a triangular shape, we can find the proportion of the triangle that rests between x=−0.5x=-0.5x=−0.5 and x=0.5x=0.5x=0.5 and divide it by the area of the entire triangle, which ranges from x=−1x=-1x=−1 to x=1x=1x=1 (the bounds of the distribution). Due to symmetry, we can find the area taken up from x=−0.5x=-0.5x=−0.5 to x=0x=0x=0 and divide that by the area from x=−1x=-1x=−1 to x=0x=0x=0.

Mental-math-wise, the left side is a right triangle similar to the larger triangle, with a base scaled by 0.50.50.5. So its area should be 0.250.250.25 that of the larger triangle, making the area we're looking for 1−0.25=0.751-0.25=0.751−0.25=0.75.

This means the probability is 75%.

Answer: 75%


Extra Credit

Continuing from before, the sum of errors for NNN rounded random variables would actually be the sum of NNN i.i.d. uniformly distributed variables from 0.50.50.5 to 0.50.50.5. The Irwin–Hall distribution describes this sum of uniformly distributed variables, albeit for U(0,1)U(0,1)U(0,1) (we'll adjust accordingly).

fX(x;N)=1(N−1)!∑k=0⌊x⌋(−1)k(Nk)(x−k)N−1f_X(x;N)=\frac{1}{(N-1)!}\sum_{k=0}^{\lfloor x\rfloor}(-1)^k{N\choose k}(x-k)^{N-1}fX​(x;N)=(N−1)!1​k=0∑⌊x⌋​(−1)k(kN​)(x−k)N−1

This distribution goes from 000 to NNN, unlike our distribution from the previous part that would go from −N2\frac{-N}{2}2−N​ to N2\frac{N}{2}2N​. Because we are trying to find the probability that the sum of rounds matches the rounded sum, we want to find the integral of this value in the middle of its distribution, namely the middle from N−12\frac{N-1}{2}2N−1​ to N+12\frac{N+1}{2}2N+1​. Due to this distributions symmetry, we can simply take the first half of this (integrating from N−12\frac{N-1}{2}2N−1​ to N2\frac{N}{2}2N​) and double it. We're left with the integral:

2∫N−12N2fX(x;N) dx=2∫N−12N21(N−1)!∑k=0⌊x⌋(−1)k(Nk)(x−k)N−1 dx=2(N−1)!∑k=0⌊N−12⌋(−1)k(Nk)∫N−12N2(x−k)N−1 dx=2(N−1)!∑k=0⌊N−12⌋(−1)k(Nk)[1N(x−k)N]N−12N2=2N!∑k=0⌊N−12⌋(−1)k(Nk)((N2−k)N−(N−12−k)N)\begin{align*} 2\int_{\frac{N-1}{2}}^{\frac{N}{2}}f_X(x;N)\ \textrm{d}x&=2\int_{\frac{N-1}{2}}^{\frac{N}{2}}\frac{1}{(N-1)!}\sum_{k=0}^{\lfloor x\rfloor}(-1)^k{N\choose k}(x-k)^{N-1}\ \textrm{d}x \\ &=\frac{2}{(N-1)!}\sum_{k=0}^{\lfloor\frac{N-1}{2}\rfloor}(-1)^k{N\choose k}\int_{\frac{N-1}{2}}^{\frac{N}{2}}(x-k)^{N-1}\ \textrm{d}x \\ &=\frac{2}{(N-1)!}\sum_{k=0}^{\lfloor\frac{N-1}{2}\rfloor}(-1)^k{N\choose k}\biggl[\frac{1}{N}(x-k)^N\biggr]_{\frac{N-1}{2}}^{\frac{N}{2}} \\ &=\frac{2}{N!}\sum_{k=0}^{\lfloor\frac{N-1}{2}\rfloor}(-1)^k{N\choose k}\biggl(\biggl(\frac{N}{2}-k\biggr)^N-\biggl(\frac{N-1}{2}-k\biggr)^N\biggr) \\ \end{align*}2∫2N−1​2N​​fX​(x;N) dx​=2∫2N−1​2N​​(N−1)!1​k=0∑⌊x⌋​(−1)k(kN​)(x−k)N−1 dx=(N−1)!2​k=0∑⌊2N−1​⌋​(−1)k(kN​)∫2N−1​2N​​(x−k)N−1 dx=(N−1)!2​k=0∑⌊2N−1​⌋​(−1)k(kN​)[N1​(x−k)N]2N−1​2N​​=N!2​k=0∑⌊2N−1​⌋​(−1)k(kN​)((2N​−k)N−(2N−1​−k)N)​

This was the closest I could get to a closed form. At the very least, it's easy to compute! The following computes and simulates the probabilities for NNN from 111 to 101010.

Code
import math
import random
from fractions import Fraction

random.seed(12)
T = 10**6

for n in range(1, 11):
    print(f'{n=}')

    expected_prob = Fraction(2, math.factorial(n)) * sum(
        (-1) ** k
        * math.comb(n, k)
        * ((Fraction(n, 2) - k) ** n - (Fraction(n - 1, 2) - k) ** n)
        for k in range(0, (n + 1) // 2)
    )
    print(f'{expected_prob=!s} ({float(expected_prob)})')

    successes = 0
    for _ in range(T):
        nums = [random.random() for _ in range(n)]
        successes += round(sum(nums)) == sum(round(num) for num in nums)
    simulated_prob = successes / T
    print(f'{simulated_prob=}')
    print()
Output
n=1
expected_prob=1 (1.0)
simulated_prob=1.0

n=2
expected_prob=3/4 (0.75)
simulated_prob=0.749818

n=3
expected_prob=2/3 (0.6666666666666666)
simulated_prob=0.666279

n=4
expected_prob=115/192 (0.5989583333333334)
simulated_prob=0.597873

n=5
expected_prob=11/20 (0.55)
simulated_prob=0.550337

n=6
expected_prob=5887/11520 (0.5110243055555556)
simulated_prob=0.510976

n=7
expected_prob=151/315 (0.4793650793650794)
simulated_prob=0.47955

n=8
expected_prob=259723/573440 (0.4529209681919643)
simulated_prob=0.452742

n=9
expected_prob=15619/36288 (0.4304177689594356)
simulated_prob=0.430889

n=10
expected_prob=381773117/928972800 (0.4109626428244185)
simulated_prob=0.410581

NNN Probability of success Approx.
1111100%
234\frac{3}{4}43​75%
323\frac{2}{3}32​66.67%
4115192\frac{115}{192}192115​59.79%
51120\frac{11}{20}2011​55.03%
6588711520\frac{5887}{11520}115205887​51.10%
7151315\frac{151}{315}315151​47.96%
8259723573440\frac{259723}{573440}573440259723​45.27%
91561936288\frac{15619}{36288}3628815619​43.09%
10381773117928972800\frac{381773117}{928972800}928972800381773117​41.06%

Answer: 2/N! * sum from k=0 to floor((N - 1) / 2) for (-1)^k * (N choose k) * ((N / 2 - k)^N - ((N - 1) / 2 - k)^N)

Back to Fiddler list
© 2024 Emily Bradon Zhang·Support Gaza Recovery