In: Statistics and Probability
The Cauchy(µ,σ) distribution has the pdf f(x)= 1 σ π 1 1+((x−µ)/σ)2,x ∈R.
a. Write an R function that generates a random sample of size n from a Cauchy(µ,σ)distribution. Do not use the rcauchy function. Your function should use the inverse transformation from to generate a random sample from a standard Cauchy distribution, then transform the generated sample appropriately according to the location and scale. Hint: The R function for the tangent is tan(x).
b. Use your function from part (a) to generate a random sample
of size n = 10000 from a Cauchy(µ = 10,σ = 3) distribution. Give
the corresponding density histogram and overly the density of a
Cauchy(µ = 10,σ = 3) distribution. You can use the function dcauchy
to produce the curve of the pdf.
R Code for the tanx function will be as follow:
n <- 10000
u <- runif(n)
c.samp <- sapply(u, function(u) tan(u))
hist(c.samp, breaks = 90, col = "blue",
main = "Hist of Cauchy")
The output graph after running the code will be as follow:
For the given question, PDF is not lear for first part hence i am removing that part & creating the PDF as follow:
F(x) = ((x-u)/)^2
In case of any change the F(x) function will change & rest will remain the same. u & are given in the question which can be used.
n <- 10000 x <- runif(n) c.samp <- sapply(x, function(x) ((x-10)/3)^2) hist(c.samp, breaks = 90, col = "blue", main = "Hist of Cauchy")
The output graph will be as follow: