options(repos = c(CRAN = "https://cran.rstudio.com/"))
knitr::opts_chunk$set(echo = TRUE)
options(scipen=999)
##Number 1##
alpha1 = 4
alpha2 = 7
theta1 = 8
theta2 = 20
y = seq(0, 500, by = 0.5)
fx1 = dgamma(y, alpha1, theta1)
fx2 = dgamma(y, alpha2, theta2)
a = 0.3 
b = 1 - a
fy = a*fx1 + b*fx2
plot(y, fy, main = "Mixture Density plot", xlab = "y", ylab = "f(y)")
lines(y, a*fx1, col = "green")
lines(y, b*fx2, col = "pink")

##Multimodal distributions , contrary to unimodal distributions, have more than one local maximums. They could be studied in econometrics, and famous examples include 2 beta distributions or 2 normal distributions. The two gammas here in this case from a bimodal distribution with 2 maximums, the higher one being the first one##
####Question 2####
install.packages("actuar")
## Installing package into 'C:/Users/DELL/AppData/Local/R/win-library/4.2'
## (as 'lib' is unspecified)
## package 'actuar' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\DELL\AppData\Local\Temp\RtmpmKxAB5\downloaded_packages
library("actuar")
## Warning: package 'actuar' was built under R version 4.2.3
## 
## Attaching package: 'actuar'
## The following objects are masked from 'package:stats':
## 
##     sd, var
## The following object is masked from 'package:grDevices':
## 
##     cm
alpha = 3
theta = 10
n = 10000
set.seed(900192239)
lambda = rinvgamma(n, shape = alpha, scale = theta)
y1 = rexp(n , rate = 1/lambda)
y2 = rpareto(n, shape = alpha, scale = theta)
ecdf1 = ecdf(y1)
ecdf2 = ecdf(y2)
plot(ecdf1, col = "green", lwd = 6)
lines(ecdf2, col = "red" , lwd = 3)

###We can see that both CDFs have the exact, or at least very closely similar, graphs. This shows what we wanted to prove, that simulating a pareto distribution is the same as simulating a continous mixture(Monte Carlo)###

```