Understanding Likelihood Ratio Tests: R Code and Analysis

School
Universidad UTEL**We aren't endorsed by this school
Course
CS 34
Subject
Electrical Engineering
Date
Dec 12, 2024
Pages
2
Uploaded by AmbassadorDovePerson1265
---title: "A2q2"output: pdf_document---```{r setup, include=FALSE}knitr::opts_chunk$set(echo = TRUE)```## Question 2## (a)(i)```{r}LRT_func1 = function(){A = rnorm(5, mean = 2, sd = sqrt(2))L_theta0 = prod(dnorm(A, mean = 2, sd = sqrt(2)))avg_A = sum(A)/length(A)L_theta1 = prod(dnorm(A, mean = avg_A, sd = sqrt(2)))return(-2*log(L_theta0/L_theta1))}```(ii)```{r}LRT_vec = replicate(10000, LRT_func1())head(LRT_vec)```(iii)```{r}hist(LRT_vec, freq = FALSE, breaks = 50, col = "green")```(iv)```{r}samples = rchisq(100000, df = 1)density = density(samples)hist(LRT_vec, freq = FALSE, breaks = 50, col = "green")lines(density, col = "red", lwd = 3)```## (b)(i)```{r}LRT_func2 = function(){A = rpois(5, lambda = 2)L_theta0 = prod(dpois(A, lambda = 2))avg_A = sum(A)/length(A)L_theta1 = prod(dpois(A, lambda = avg_A))return(-2*log(L_theta0/L_theta1))}```(ii)
Background image
```{r}LRT_vec = replicate(10000, LRT_func2())head(LRT_vec)```(iii)```{r}hist(LRT_vec, freq = FALSE, breaks = 50, col = "green")```(iv)```{r}samples = rchisq(100000, df = 1)density = density(samples)hist(LRT_vec, freq = FALSE, breaks = 50, col = "green")lines(density, col = "red", lwd = 3)```## (c)The sample size n would affect the closeness of the histograms and the chi-square(df=1) density. For this case(n=5), which is relatively small, there is some biases between two density curve. By CLT, if n approach to infinity, the LRT will converge to chi-square density with df=1, then the histogram and the chi-square density will match more perfectly.
Background image