You can use R to evaluate sums and integrals if you can not do it analytically.
Say the random variable X has density \(f(x)=c/x^k\), \(x=1,2,3,...;k=2, 3\text{ or }4\). Find the mean and the variance of X.
First note that
\[ \begin{aligned} &\sum_{x=1}^\infty \frac{1}{x^2} = \frac{\pi^2}{6} \\ &\sum_{x=1}^\infty \frac{1}{x^4} = \frac{\pi^4}{90} \end{aligned} \] We also need \(\sum_{x=1}^\infty \frac{1}{x^3}\), but there is no nice formula for this, so
n=1:1e5
sum(1/n^3)
## [1] 1.202057
k=2 \[ \begin{aligned} &1 =\sum_{x=1}^\infty \frac{c}{x^2} = c\pi^2/6 \\ &E[X] = \sum_{x=1}^\infty x\frac{6/\pi^2}{x^2} =\frac{6}{\pi^2}\sum_{x=1}^\infty \frac{1}{x}=\infty\\ \end{aligned} \] so the mean (and therefore also the variance) do not exist.
k=3 \[ \begin{aligned} &1 =\sum_{x=1}^\infty \frac{c}{x^3} = 1.202c \\ &E[X] = \sum_{x=1}^\infty x\frac{0.832}{x^3} =0.832\sum_{x=1}^\infty \frac{1}{x^2}=0.832\pi^2/6=1.368\\ &E[X^2] = \sum_{x=1}^\infty x^2\frac{0.832}{x^3} =0.832\sum_{x=1}^\infty \frac{1}{x}=\infty\\ \end{aligned} \] so there is no variance
k=4 \[ \begin{aligned} &1 =\sum_{x=1}^\infty \frac{c}{x^4} = c\frac{\pi^4}{90} \\ &E[X] = \sum_{x=1}^\infty x\frac{90/\pi^4}{x^4} =90/\pi^4\sum_{x=1}^\infty \frac{1}{x^3}=90/\pi^4 1.202=1.111\\ &E[X^2] = \sum_{x=1}^\infty x^2\frac{90/\pi^4}{x^4} =90/\pi^4\sum_{x=1}^\infty \frac{1}{x^2}=(90/\pi^4)(\pi^2/6)=1.52\\ &var(X)=E[X^2]-E[X]=1.52-1.111^2=0.286 \end{aligned} \]
x=sample(1:100, size=1e4, replace=TRUE, prob = 1/(1:100)^3)
table(x)
## x
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 8353 977 318 130 70 49 30 18 10 7 7 4 2 3 2 2
## 17 18 19 21 22 23 24 27 28 29 49 73 97
## 2 1 3 1 1 2 2 1 1 1 1 1 1
mean(x)
## [1] 1.3743
x=sample(1:100, size=1e4, replace=TRUE, prob = 1/(1:100)^4)
table(x)
## x
## 1 2 3 4 5 6 7 8 9 20
## 9227 589 116 44 13 5 2 2 1 1
c(mean(x), var(x))
## [1] 1.1083000 0.2259937
Let X be a random variable with density \(f(x)=c/x^a;x>1\). Find the mean and the variance of X.
\[ \begin{aligned} &E[X^k] = \int_{-\infty}^{\infty} x^k f(x) dx = \\ &\int_1^\infty x^k c/x^a dx = c\int_1^\infty x^{k-a} dx =\\ &c\left\{ \begin{array} .\frac{x^{k-a+1}}{k-a+1}|_1^\infty&\text{ if }&k-a+1<0\\ \infty&\text{ if }&k-a+1\ge 0 \end{array}\right. = \\ &c\left\{ \begin{array} .\frac{-1}{k-a+1}&\text{ if }&k-a+1<0\\ \infty&\text{ if }&k-a+1\ge 0 \end{array}\right. \\ \end{aligned} \]
\[ \begin{aligned} &1=E[X^0] = &c\left\{ \begin{array} .\frac{-1}{-a+1}&\text{ if }&-a+1<0\\ \infty&\text{ if }&-a+1\ge 0 \end{array}\right. \end{aligned} \]
so \(c=1-a\) if \(a>1\) and does not exist otherwise.
\[ \begin{aligned} &E[X] = (1-a)\left\{ \begin{array} .\frac{-1}{1-a+1}&\text{ if }&1-a+1<0\\ \infty&\text{ if }&1-a+1\ge 0 \end{array}\right. = \\ &\left\{ \begin{array} .\frac{1-a}{2-a}&\text{ if }&a>2\\ \infty&\text{ if }&a\le 2 \end{array}\right. \end{aligned} \]
\[ \begin{aligned} &E[X^2] = (1-a)\left\{ \begin{array} .\frac{-1}{2-a+1}&\text{ if }&2-a+1<0\\ \infty&\text{ if }&2-a+1\ge 0 \end{array}\right. = \\ &\left\{ \begin{array} .\frac{1-a}{3-a}&\text{ if }&a>3\\ \infty&\text{ if }&a\le 3 \end{array}\right. \\ &var(X) = E[X^2]-E[X]^2 =\frac{1-a}{3-a}-(\frac{1-a}{2-a})^2 \end{aligned} \]
rf=function(n,a) {runif(n, 0, 1)^(1/(1-a))}
a=3
x=rf(1e4, a)
c(mean(x), (1-a)/(2-a))
## [1] 1.959929 2.000000
a=4
x=rf(1e4, a)
c(mean(x), (1-a)/(2-a))
## [1] 1.497755 1.500000
c(var(x), (1-a)/(3-a) - ((1-a)/(2-a))^2)
## [1] 0.6135606 0.7500000
Let X be a random variable with density \(f(x)=6x(1-x);0<x<1\). Find the skewness and the kurtosis of X.
\[ \begin{aligned} &E[X^k] =\int_0^1 x^k 6x(1-x)dx= \\ &6\int_0^1 x^{k+1}-x^{k+2} dx = \\ &6\left(\frac{x^{k+2}}{k+2}-\frac{x^{k+3}}{k+3}|_0^1\right) = \\ &\frac{6}{k+2}-\frac{6}{k+3} = \frac{6}{(k+2)(k+3)} \end{aligned} \]
so
\[ \begin{aligned} &E[X] =\frac12 \\ &E[X^2] =\frac{3}{10} \\ &E[X^3] =\frac{1}{5} \\ &E[X^4] =\frac{1}{7} \end{aligned} \]
\[ \begin{aligned} &\mu=E[X] = \frac12\\ &\kappa_2 =E[(X-\mu)^2]=E[X^2]-2\mu E[X]+\mu^2 =\\ &\frac3{10}-2\frac12\frac12+(\frac12)^2=\frac{1}{20}\\ &\kappa_3 =E[(X-\mu)^3]=E[X^3]-3\mu E[X^2]+3\mu^2 E[X] - \mu^3 =\\ &\frac15-3\frac12\frac3{10}+3(\frac12)^2\frac1{2}-(\frac12)^3 = 0 \\ &\gamma_1=\frac{\kappa_3}{\kappa_2^{3/2}}=0 \end{aligned} \]
\[ \begin{aligned} &\kappa_4 =E[(X-\mu)^4]=\\ &E[X^4]-4\mu E[X^3]+6\mu^2 E[X^2] - 4\mu^3E[X]+\mu^4 =\\ &\frac17-4\frac12\frac15+6(\frac12)^2\frac3{10}-4(\frac12)^3\frac12+(\frac12)^4=\\ &\frac17-\frac25+\frac{9}{20}-\frac14+\frac1{16}=\frac{3}{560}\\ &\gamma_2=\frac{\kappa_4}{\kappa_2^2}-3=\frac{3/560}{(1/20)^2}-3=-\frac67 \end{aligned} \]
n=1e5
x=rbeta(n, 2, 2)
c(mean(x), 1/2)
## [1] 0.5013541 0.5000000
c(mean((x-1/2)^2), 1/20)
## [1] 0.04963053 0.05000000
c(mean((x-1/2)^3), 0)
## [1] 0.0001274215 0.0000000000
c(mean((x-1/2)^4), 3/560)
## [1] 0.005280003 0.005357143
c(mean((x-1/2)^4)/mean((x-1/2)^2)^2-3, -6/7)
## [1] -0.8564366 -0.8571429
Say \((X,Y)\) is a random vector with joint density proportional to \(g(x,y)=(x+1)y^x;0<y<1;x=1,2,3\). Find the correlation of X and Y.
\[ \begin{aligned} &E[X^kY^j] =\sum_{x=1}^3 \int_0^1 cx^ky^j(x+1)y^x dx =\\ &c\sum_{x=1}^3 x^k \frac{x+1}{x+j+1}y^{x+j+1}|_0^1 = \\ &c\sum_{x=1}^3 \frac{(x+1)x^k}{x+j+1} \\ &1=E[X^0Y^0] = c\sum_{x=1}^3 \frac{(x+1)x^0}{x+0+1}=3c\\ &E[X]=E[X^1Y^0] = \frac13\sum_{x=1}^3 \frac{(x+1)x^1}{x+0+1}=\frac13\sum_{x=1}^3 x=2\\ &E[Y]=E[X^0Y^1] = \frac13\sum_{x=1}^3 \frac{(x+1)x^0}{x+1+1}=\\ &\frac13\sum_{x=1}^3 \frac{x+1}{x+2}=0.739\\ &E[XY]=E[X^1Y^1] = \frac13\sum_{x=1}^3 \frac{(x+1)x^1}{x+1+1}=\\ &\frac13\sum_{x=1}^3 \frac{(x+1)x}{x+2}=1.522\\ &cov(X,Y)=E[XY]-E[X]E[Y]=1.522-2\times 0.739=0.044 \end{aligned} \]
\[ \begin{aligned} &E[X^2]=E[X^2Y^0] = \frac13\sum_{x=1}^3 \frac{(x+1)x^2}{x+0+1}=\\ &\frac13\sum_{x=1}^3 x^2=4.667\\ &var(X) = E[X^2]-E[X]^2 = 4.677-2^2=0.667\\ &E[Y^2]=E[X^0Y^2] = \frac13\sum_{x=1}^3 \frac{(x+1)x^0}{x+2+1}=\\ &\frac13\sum_{x=1}^3 \frac{x+1}{x+3}=0.589\\ &var(Y) = E[Y^2]-E[Y]^2 = 0.589-0.739^2= 0.043\\ &cor(X,Y) =\frac{cov(X,Y)}{\sqrt{var(X)var(Y)}} =\\ &\frac{0.044}{\sqrt{0.667\times 0.043}}=0.26 \end{aligned} \]