Question 1162599
```python?code_reference&code_event_index=2
from fractions import Fraction

# Setup
total_balls = 8
yellow_balls = 5
green_balls = 3
draw_size = 2

# X is number of yellow balls
# P(X=k) = (comb(5, k) * comb(3, 2-k)) / comb(8, 2)

def combinations(n, k):
    if k < 0 or k > n:
        return 0
    import math
    return math.comb(n, k)

denom = combinations(total_balls, draw_size)

# Probabilities
p_x0 = Fraction(combinations(5, 0) * combinations(3, 2), denom)
p_x1 = Fraction(combinations(5, 1) * combinations(3, 1), denom)
p_x2 = Fraction(combinations(5, 2) * combinations(3, 0), denom)

# E[X]
ex = 0 * p_x0 + 1 * p_x1 + 2 * p_x2

# E[X^2]
ex2 = (0**2) * p_x0 + (1**2) * p_x1 + (2**2) * p_x2

# Var(X)
var_x = ex2 - (ex**2)

# Cov(X, Y) = -Var(X)
cov_xy = -var_x

print(f"{p_x0=}")
print(f"{p_x1=}")
print(f"{p_x2=}")
print(f"{ex=}")
print(f"{ex2=}")
print(f"{var_x=}")
print(f"{cov_xy=}")


```
```text?code_stdout&code_event_index=2
p_x0=Fraction(3, 28)
p_x1=Fraction(15, 28)
p_x2=Fraction(5, 14)
ex=Fraction(5, 4)
ex2=Fraction(55, 28)
var_x=Fraction(45, 112)
cov_xy=Fraction(-45, 112)

```
To find the covariance of $X$ (the number of yellow balls) and $Y$ (the number of green balls), we can use the properties of random variables and the specific relationship between $X$ and $Y$ in this experiment.

### 1. The Relationship between $X$ and $Y$
Since we are drawing a total of $2$ balls and every ball drawn must be either yellow or green, we have the following constraint:
$$X + Y = 2$$
This implies that $Y$ is a linear function of $X$:
$$Y = 2 - X$$

A key property of covariance is that $Cov(X, a + bX) = b \cdot Var(X)$. In this case, $a = 2$ and $b = -1$, so:
$$Cov(X, Y) = Cov(X, 2 - X) = -Var(X)$$

### 2. Find the Variance of $X$
$X$ follows a **hypergeometric distribution** because we are sampling without replacement from a finite population.
* Total balls ($N$) = $8$
* Yellow balls ($K$) = $5$
* Number of balls drawn ($n$) = $2$

The variance of a hypergeometric distribution is given by the formula:
$$Var(X) = n \cdot \frac{K}{N} \cdot \left( \frac{N-K}{N} \right) \cdot \left( \frac{N-n}{N-1} \right)$$

Plugging in the values:
$$Var(X) = 2 \cdot \frac{5}{8} \cdot \frac{3}{8} \cdot \frac{8-2}{8-1}$$
$$Var(X) = 2 \cdot \frac{5}{8} \cdot \frac{3}{8} \cdot \frac{6}{7}$$
$$Var(X) = \frac{180}{448}$$

Simplifying the fraction:
$$Var(X) = \frac{45}{112}$$

### 3. Calculate the Covariance
As established in Step 1, $Cov(X, Y) = -Var(X)$.
$$Cov(X, Y) = -\frac{45}{112}$$

### Summary of Probability Distribution (Optional Verification)
We can also calculate $E[XY] - E[X]E[Y]$ directly:
* **$P(X=0, Y=2)$**: $\frac{\binom{5}{0}\binom{3}{2}}{\binom{8}{2}} = \frac{3}{28}$
* **$P(X=1, Y=1)$**: $\frac{\binom{5}{1}\binom{3}{1}}{\binom{8}{2}} = \frac{15}{28}$
* **$P(X=2, Y=0)$**: $\frac{\binom{5}{2}\binom{3}{0}}{\binom{8}{2}} = \frac{10}{28} = \frac{5}{14}$

**Expectations:**
* $E[X] = (0 \cdot \frac{3}{28}) + (1 \cdot \frac{15}{28}) + (2 \cdot \frac{10}{28}) = \frac{35}{28} = 1.25$
* $E[Y] = (2 \cdot \frac{3}{28}) + (1 \cdot \frac{15}{28}) + (0 \cdot \frac{10}{28}) = \frac{21}{28} = 0.75$
* $E[XY] = (1 \times 1) \cdot P(X=1, Y=1) = \frac{15}{28} \approx 0.5357$

**Covariance Calculation:**
$$Cov(X, Y) = E[XY] - E[X]E[Y] = \frac{15}{28} - (1.25 \times 0.75) = \frac{15}{28} - 0.9375 = \frac{15}{28} - \frac{15}{16}$$
$$Cov(X, Y) = \frac{60 - 105}{112} = -\frac{45}{112}$$

**Final Answer:**
The covariance of $X$ and $Y$ is **$-\frac{45}{112}$** (or approximately **$-0.4018$**).