# Plotting a cdf in r

I am trying to work out and plot the CDF from a PDF. The pdf is $$f(x) = \begin{cases} 0&\text{if}\quad -1\leq x \leq 1\\ |1/x^3|&\text{ otherwise} \end{cases}.$$

I know that the CDF is the integral of the PDF, and I tried integrating the PDF but all I got was 1, and I'm not sure how to plot this.

• In general $F(x)=\int_{-\infty}^xf(u)du$ where $f$ denotes PDF and $F$ denotes CDF. Maybe you only computed $\int_{-\infty}^{\infty}f(u)du$ ("...all i got was $1$..."). Commented Oct 28, 2022 at 8:44
• Are you able to find $F(x)$ by doing the integration proposed in my comment? Commented Oct 28, 2022 at 10:17

Note that yours is not a valid probability density function.

Indeed, you have $$f(x)< 0$$, for all $$x< -1$$. But, by definition, a PDF is always non-negative, ie. $$f(x)\geq 0, \text{ for all } x.$$

$$\int_{-\infty}^{-1} 1/x^3\,dx + \int_{1}^{\infty} 1/x^3\, dx= -\frac{1}{2}+\frac{1}{2}=0.$$ Thus the CDF is also not valid either.

Update

Note: The above post refers to the original question. Below is the answer to the updated question.

For this pdf the CDF is

$$F(x) = \begin{cases} \frac{1}{2x^2}& \text{ if } x\leq -1\\ \frac{1}{2} & \text{ if } x\in[0,1]\\ 1-\frac{1}{2x^2}& \text{ if } x>1 \end{cases}.$$

Below you can see the PDF, the CDF and the relative R code.

ff_pdf <- function(x) abs(1/x^3)
ff_cdf_l <- function(x) (1/(2*x^2))
ff_cdf_r <- function(x) 1/2 + 1/2 - 1/(2*x^2)

x_l <- seq(-7,-1,len=50)
x_r <- seq(1,7,len=50)
pdfv_l <- sapply(x_l, ff_pdf)
pdfv_r <- sapply(x_r, ff_pdf)

plot(x_l, pdfv_l, type = "l", xlim=c(-7,7),
lwd=2, ylim=c(0,1.3), ylab = "Density", xlab="x")
points(x_r, pdfv_r, type = "l", xlim=c(-7,7), lwd=2)
segments(x0=-1, y0=0, x1=1, y1=0, lwd=2)

cdfv_l <- sapply(x_l, ff_cdf_l)
cdfv_r <- sapply(x_r, ff_cdf_r)

plot(x_l, cdfv_l, type = "l", xlim=c(-7,7),
lwd=2, ylim=c(0,1.1), ylab = "Distribution function", xlab="x")
segments(x0=-1, y0=1/2, x1=1, y1=1/2, lwd=2)
points(x_r, cdfv_r, type = "l",lwd=2)
abline(h = c(0,1), lwd=2, lty=2, col="gray")

• This is swift. You ought to check the definition before even proceeding. +1. Commented Oct 28, 2022 at 9:31
• @henry This answers your original question. Later your question was edited and function $f$ is now a PDF. Commented Oct 28, 2022 at 10:07
• @henry I've updated my answer. Commented Oct 28, 2022 at 13:26
• @henry, if you find this answer helpful, please upvote and accept it by clicking on the tick symbol alongside the post. Commented Oct 29, 2022 at 6:36