Suppose that $x \sim U(0, 255)$ and and $y \sim U(0, 255)$, and that $x$ and $y$ are independent.

What is the distribution of $x - y$?

If I were to draw 1,000 samples from this distribution, how many times should I expect to see each possible value?

NB. These are all discrete random variables, in case that wasn't clear...


I've written a computer program to work out the expected count for each difference value. However, when I ask the computer to sum them all, it doesn't add up to 1,000. Clearly I've done something wrong somewhere, but I'm not sure precisely what. I'm probably just being dumb; if somebody could walk me through this slowly, it would be helpful.

The reason I'm doing this is that I want to do a chi-squared test to see if the data I'm getting actually does follow the expected distribution. Clearly this doesn't work if you calculate your expectations wrong. (!)

In addition, I think I may have over-simplified the program description. What my program actually does is obtain $x_1 \ldots x_{1000}$, and then compute $d_n = x_{n+1} - x_n$. Would that mean that the $d$ values are not statistically independent? If so, presumably a chi-squared test won't work at all. (I suppose I could throw away all the even-numbered $d_n$ so I'm left with non-overlapping pairs...)

  • 2
    $\begingroup$ One way to think of it is as $x+(-y).$ Then you can view it as the sum of two uniform random variables, which is triangular. $\endgroup$ – soakley Jun 29 '13 at 13:24
  • $\begingroup$ @soakley So, a triangular distribution with a peak at $0$, minimum of $-255$ and maximum of $+255$. (?) How would I compute the actual probability for, say, $x-y = 17$? $\endgroup$ – MathematicalOrchid Jun 29 '13 at 13:43
  • $\begingroup$ How many ways can $x=y+17$ ? What is the probability of each outcome? $\endgroup$ – soakley Jun 29 '13 at 14:27
  • 5
    $\begingroup$ Assuming that $X$ and $Y$ are independent discrete random variable equally likely to take on any of the $256$ numerical values $\{0, 1, 2, \ldots, 255\}$ (which is not the same as your $\sim U(0,255)$ but matches your comment), then $$P\{X-Y = 17\} = \sum_{i=17}^{255}P\{X=i,Y=i-17\}=\sum_{i=17}^{255}P\{X=i\}\{Y=i-17\}\\=\sum_{i=17}^{255}\left(\frac{1}{256}\right)\times \left(\frac{1}{256}\right)$$ $\endgroup$ – Dilip Sarwate Jun 29 '13 at 14:31

For a uniform, distribution, the distribution of the difference is a triangle centered on zero and falling to zero at +/- 2*255, the height given by requiring normalizing to 1.

You're problem is using integers so this isn't exact. The exact value is given by the convolution of the two distributions. The distribution is just the number of possible values from both distribution whose difference is that value (and normalized to 1).


Following the comments from Dilip Sarwate, I was able to get the expected outcomes to sum to 1,000.

My mistake was in assuming that I only need to count how many ways you can make (for example) 17, not the probability of each of those ways.

What I eventually came up with is

\begin{equation} \Pr(d = v) = \frac{256 - v}{256\times256} \end{equation}

and therefore the expected count for $d=v$ is the above probability multiplied by the number of items in the sample.

This just leaves open the question of whether it's appropriate to use a chi-squared test on the differences of overlapping pairs in the first place...

  • 1
    $\begingroup$ You need to rethink this. What about, for example, $v=-255$? $\endgroup$ – soakley Jun 29 '13 at 16:20
  • $\begingroup$ What's the probl... oh, wait, it's $256 - |v|$. Sorry, my bad. I missed that out when I converted source code into mathematics. $\endgroup$ – MathematicalOrchid Jun 29 '13 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.