Using R and probability functions

45 Views Asked by At

I am struggling a lot on this one. We are asked to use R a lot in one of my courses, with very little help or explanation so it's tough for those of us who are completely new to programs like R or Matlab.

I have probability function where $p_X(x) = \frac{c}{x}$, where $x = 1, 2, 3, ..., 1000$ and I need to solve for $c$, using R. I have absolutely zero idea on where to start, could someone point me in the right direction, or have any good well explained resources on how to use the R software?

1

There are 1 best solutions below

0
On BEST ANSWER

I guess $p_X(x)$ is a probability mass functions, and you have to compute $c$ from the condition $$1=\sum_{x=1}^{1000} p_X(x)=\sum_{x=1}^{1000} \frac{c}{x}=c\sum_{x=1}^{1000} \frac{1}{x}$$ Using the relation of the digamma function $\psi$ to harmonic numbers, the analytical value of the sum is $$\sum_{x=1}^{1000} \frac{1}{x}=H_{1000}= \psi(1001)+\gamma \approx 7.485471$$ where $\gamma=0.5772156649\dots$ is the Euler–Mascheroni constant and therefore $c\approx 0.1335921$. This is independent of R. In basic R there is a digamma function and you can use $\gamma = - \psi(1)$ to compute

> digamma(1001)-digamma(1)
[1] 7.485471
> 1/.Last.value
[1] 0.1335921

After reading a bit in the R documentation, I found, that you can also directly get the sum without special functions (showing a few more digits)

> sum(1/(1:1000))
[1] 7.485470860550345

and the constant c

> 1/sum(1/(1:1000))
[1] 0.1335921304924401