The inverse tangent function arctan is usually calculated by \[
\arctan(x) = \int_0^x \frac{1}{1 + y^2} dy \,.
\] Write a function that uses Monte Carlo simulation to approximate arctan. You can use the same sample for all values of \(x\) or different samples; the latter is much slower.
Use the function to plot the function for values between \(1\) and \(2\) (calculate it for \(1.01, 1.02, 1.03\) and so on till \(2\)).
In particular, we are interested in \(x = \sqrt{3}\) (we know that this is \(\pi/ 3\)).
atan_approx <-function(x, u =NULL, seed =123, N =10^5){if(is.null(u)){set.seed(seed) u <-runif(N) } z <- x/(1+u^2*x^2)return (mean(z))}set.seed(123)N <-10^5u <-runif(N)x <-sqrt(3)cat("MC approx. of arctan(x^(0.5)): ", atan_approx(x, u), "\nApproximation error: ", pi /3-atan_approx(x, u))
MC approx. of arctan(x^(0.5)): 1.048289
Approximation error: -0.001091812
Code
xs <-seq(from =1, to =2, by =0.01)atans <-sapply(xs, atan_approx, u = u) plot(xs, atans, col ="red") lines(xs, atan(xs), col ="black") legend("right", legend=c("MC arctan", "R arctan"),col=c("red", "black"), lty=c(NA, 1), pch=c(1, NA))
Question 2
Calculate the area defined by the points \(\{(x,y):|x|^{1.6}+|y|^{1.6}<1\}\).
Focus on \(S = \{(x,y): x^{1.6}+y^{1.6}<1, \, x,y \in [0,1]\}\)
N <-10^5set.seed(123) X <-runif(N) Y <-runif(N) Z <- X^1.6+ Y^1.6<1pr <-mean(Z) area <-4* pr cat("Area: ", area)
Area: 2.83872
Question 3
Use the function hist, boxplot, qqnorm, qqline to plot a random variable \(z\) from standard normal distribution and from a normal distribution with mean \(20\) and standard deviation \(5\) of sample size \(10^3\). More specifically, plot hist(z), plot(1:1000,z), qqnorm(z).
Code
par(mfrow =c(2, 2), asp =1)z <-rnorm(10^3, mean =0, sd =1)hist(z, col ="steelblue3")qqnorm(z, pch =1, cex =1, col ="mediumpurple1")qqline(z)boxplot(z, main ="Box Plot", col ="tomato")plot(1:10^3, z, xlab ="N", ylab ="z", type ="l", main ="A white noise process")
Question 4
Create a random sample of size \(1000\) from a normal distribution with mean \(10\) and standard deviation \(20\). Compute the mean, the standard deviation, the median, and the quantiles at \(5\%\) and \(95\%\) probability level.
Write a function that returns the moving averages: \[
\frac{x_1 + x_2 + x_3}{3}, \, \frac{x_2 + x_3 + x_4}{3}, \, \ldots, \, \frac{x_{n-2} + x_{n-1} + x_{n}}{3} \,.
\] Use it for the sample in (a) to get a histogram of values.
Remark: Moving Average processes are just averages of white noise.
MA3_f <-function(xVector){ n <-length(xVector) x <- (xVector[-c(n-1, n)] + xVector[-c(1, n)] + xVector[-c(1, 2)]) /3return (x)}MA <-MA3_f(X)hist(MA,col ="steelblue3", main ="Histogram of Moving Average")
Question 5
Let \(Y_t\) be the yield of a fund at \(t\) year and \(Y_t = \exp(X_t)\), where \(X_1, X_2, \ldots , X_t\) is an independent identically distributed sequence of normal random variables with mean \(0.09\) and variance \(0.1\).
An investor invests an amount \(1\) at times \(t = 0, 1, 2\) and wants to know what is the probability that the accumulation at \(t = 3\) will be higher than \(4\) for a vector of \(10^6\) possible returns.