The question is stated as follows:
Take two numbers $x$ and $y$ from the uniform distribution $\mathcal{U}(0,1)$. Next, round the quotient $\frac{x}{y}$ to the nearest integer, i.e. $\left\lfloor\frac{x}{y}+\frac{1}{2}\right\rfloor$. What is the probability that this value $\left\lfloor\frac{x}{y}+\frac{1}{2}\right\rfloor$ is odd?
I had no idea how to start with this problem, so I simulated it in Python (code below). The scatter plot and contour plot are also shown below. The result of the last simulation learned that the probability of $\left\lfloor\frac{x}{y}+\frac{1}{2}\right\rfloor$ being odd is about $0.54$.
But how do I get an exact value for this probability? Or in other words, how do I calculate the green area in the scatterplot below? Thank you in advance.
Python code:
import matplotlib.pyplot as plt
from random import random
from numpy import arange, meshgrid, round
# Contourplot.
delta = 0.0001
x_range = arange(0, 1, delta)
y_range = arange(0, 1, delta)
x, y = meshgrid(x_range, y_range)
plt.figure(f"Contour")
plt.contour(x, y, round(x/y) % 2 - 1, 0)
plt.xlabel('x')
plt.ylabel('y')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.grid()
# Scatterplot.
runs = 200000 # Number of points in scatterplot
odd_list, even_list = [], []
for _ in range(runs):
x, y = random(), random()
if round(x/y) % 2: # Odd.
odd_list.append((x, y))
else: # Even.
even_list.append((x, y))
plt.figure(f"Scatterlot")
x_odd_list, y_odd_list = zip(*odd_list)
x_even_list, y_even_list = zip(*even_list)
plt.scatter(x_odd_list, y_odd_list, c='g', s=0.1, label='odd')
plt.scatter(x_even_list, y_even_list, c='r', s=0.1, label='even')
plt.legend(markerscale=20, facecolor='w', loc='upper right')
plt.xlabel('x')
plt.ylabel('y')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.grid()
plt.show()
plt.close()
# Approximation
print(f'Odd: {len(odd_list)/runs}\n'
f'Even: {len(even_list)/runs}')
First, let us ignore the top red and green areas because they hit the top corner. Note that the next green area is a triangle with the base running from $\frac 27$ to $\frac 25$ on the line $x=1$, so its area is $\frac 12(\frac 25-\frac 27)$. The next one has area $\frac 12(\frac 29-\frac 2{11})$ and in general they have area $\frac 12(\frac 2{4k+1}-\frac 2{4k+3})$. The sum of these is $$\sum_{k=1}^\infty \left(\frac 1{4k+1}-\frac 1{4k+3}\right)=\frac \pi 4-\frac 23$$ where I had Alpha do the sum.
The top red triangle has area $\frac 14$ and the area below the top green area is $\frac 13$, so the top green area is $\frac 23-\frac 14$ giving a total green area of $$\frac {\pi -1}4\approx 0.5354$$