---
title: 'STA 141A: Homework 3'
subtitle: 'Instructor: Akira Horiguchi'
author: "Student name: ABCDE FGHIJ; Student ID: 123456789"
date: "2025-10-22 23:59:00 PDT"
date-format: "MMM D, YYYY (dddd), HH:mm z"
format: 
    pdf: 
        include-in-header:
          text: |
            \usepackage{fancyhdr}
            \usepackage{lastpage}
            \pagestyle{fancy}
            \fancyhf{} % sets both header and footer to nothing
            \renewcommand{\headrulewidth}{0pt}
            \fancyhead[RO,RE]{}
            \fancyhead[LO,LE]{}
            \fancyfoot[LE,LO]{}
            \fancyfoot[LE,CO]{Page \thepage\ of \pageref{LastPage}}
editor: source
geometry:
  - top=5mm
  - bottom=20mm
  - left=5mm
  - right=5mm
urlcolor: blue
---

The assignment must be done in an [R Markdown](https://rmarkdown.rstudio.com) or [Quarto](https://quarto.org/) document. The assignment must be submitted by the due date above by uploading:

-   a .pdf file in GRADESCOPE (if you can knit/compile your .rmd to a .html file only, please save the created .html file as a .pdf file (by opening the .html file -\> print -\> save to .pdf)).

Email submissions will not be accepted.

Each answer has to be based on `R` code that shows how the result was obtained. The code has to answer the question or solve the task. For example, if you are asked to find the largest entry of a vector, the code has to return the largest element of the vector. If the code just prints all values of the vector, and you determine the largest element by hand, this will not be accepted as an answer. No points will be given for answers that are not based on `R`. This homework already contains chunks for your solution (you can also create additional chunks for each solution if needed, but it must be clear to which tasks your chunks belong).

There are many possible ways to write `R` code that is needed to answer the questions or do the tasks, but for some of the questions or tasks you might have to use something that has not been discussed during the lectures or the discussion sessions. You will have to come up with a solution on your own. Try to understand what you need to do to complete the task or to answer the question, feel free to search the Internet for possible solutions, and discuss possible solutions with other students. It is perfectly fine to ask what kind of an approach or a function other students use. However, you are not allowed to share your code or your answers with other students. Everyone has to write the code, do the tasks and answer the questions on their own.

During the discussion sessions, you may be asked to present and share your solutions.

![If you cannot type math in TeX, you can write with a tablet or replace this image file (turkey.jpg) with a screenshot of your handwritten solution.](turkey.jpg){width="10cm"}

\newpage

# 1. PMF


### Let $X$ be a discrete random variable with PMF $f_X(x) = (1/2)^x$ for $x \in \mathbb{N}$ (that is, for all natural numbers $x$).

### a) Draw or plot the PMF of the random variable $X$.
$\vspace{2in}$

<!-- ### d) Let $C$ be the event that $X \in \{1,3\}$, and let $D$ be the event that $X \geq 4$. Compute $P(C) + P(D)$.  -->
<!-- - *Hint*: the solution can be written in just one line of math. -->
### b) Compute (by hand) $P(X \in \{1,3\}) + P(X \geq 4)$. *Hint*: the solution can be written in just one line of math.
$\vspace{0.5in}$

### c) Let $A$ be the event that $X$ is even, and let $B$ be the event that $X$ is odd. Are $A$ and $B$ independent? Are $A$ and $B$ disjoint? What is $P(A) + P(B)$? Justify your answer.
$\vspace{3in}$

### d) Compute the expected value $\mathbb{E} (X)$. You can use R for this.
<!-- $\vspace{0.5in}$ -->

```{r}

```


\newpage

# 2. Expected value and variance

### a) If $X$ is a continuous random variable and its PDF is symmetric about $0$, show that the expected value $\mathbb{E} (X)$ is $0$. *Hint*: A PDF $f_X$ is symmetric about $0$ if and only if $f_X(c) = f_X(-c)$ for any $c \in \mathbb{R}$.
$\vspace{1in}$

### For the remaining parts of this problem, let $X$ be a random variable with PDF $f_X(x) = |x|$ for $x \in (-1,1)$. (Here $|x|$ denotes the absolute value of $x$.)

### b) Calculate (by hand) the expected value $\mathbb{E} (X)$. *Hint*: use part a).
$\vspace{0.1in}$

### c) Calculate (by hand) the expected value $\mathbb{E} (3X + 5)$. *Hint*: use your answer from part b).
$\vspace{0.1in}$

### d) Calculate (by hand) the variance $Var(X)$.
$\vspace{2.5in}$

### e) Calculate (by hand) the variance $Var(3X + 5)$. *Hint*: use your answer from part d).
$\vspace{1in}$


\newpage

# 3. Variance and covariance

Let $Z_1, Z_2, Z_3, Z_4$ be random variables (not necessarily from the same distribution) each with the same variance $\sigma^2$. Assume that the covariance between any pair of these random variables equals $a \sigma^2$ for some $a \in [-1, 1]$. That is, assume $Cov(Z_i, Z_j) = a \sigma^2$ whenever $i \neq j$.

### a) Compute the variance of $Z_1 + Z_2 + Z_3 + Z_4$. (*Hint:* It can be written as $Cov(Z_1 + Z_2 + Z_3 + Z_4, Z_1 + Z_2 + Z_3 + Z_4)$.) How does it compare to the variance of just one of these random variables $Z_i$ if $a=0$? If $a=0.9$? If $a=-0.9$? 
$\vspace{5in}$

### b) Compute the variance of $(Z_1 + Z_2 + Z_3 + Z_4)/4$. (*Hint:* Use your answer from part (a).) How does it compare to the variance of just one of these random variables $Z_i$ if $a=0$? If $a=0.9$? If $a=-0.9$? 


\newpage

# 4. Estimate expected value using Monte Carlo integration

Suppose we want to compute the expected value $$\mathbb{E} [g(X)] = \int g(x) f_X(x) \, dx$$ for a continuous random variable $X$ with pdf $f_X(x)$, where $g(x)$ is a function of $x$. The exact value of this expected value can be difficult to compute if the function $g$ or the pdf $f_X$ is complicated. However, by noting that $$\lim_{N \rightarrow \infty} \frac{1}{N} \sum_{i=1}^N g(x_i) = \int g(x) f_X(x) \, dx$$ for samples $x_1, x_2, \ldots, x_N$ independently drawn according to pdf $f_X$, we can approximate the expected value $\mathbb{E} g(X)$.

**For this problem, suppose the random variable $X$ follows a standard uniform distribution, and $g(x) = x^2$.**

### a) Compute (by hand) the expected value $\mathbb{E} [g(X)]$.

$\vspace{2in}$

### b) Now we will estimate the expected value $\mathbb{E} [g(X)]$ using R and this Monte Carlo method described above.

i. Draw $N=10^7$ samples $x_1, x_2, \ldots, x_N$ according to the distribution of the random variable $X$. 
ii. Then compute the $N$ values of $g(x_1), g(x_2), \ldots, g(x_N)$, and compute the cumulative mean of these $N$ values. That is, for all positive integers $n \leq N$, compute $\frac{1}{n} \sum_{i=1}^n g(x_i)$. (You can use the R function `cummean()`.)
iii. Plot the points $(n, y_n)$ for $n=1,\ldots,N$, where $y_n$ is the mean of the $n$ values $g(x_1), g(x_2), \ldots, g(x_n)$. To your plot, also add a dashed horizontal line whose height is the value you computed in part (a). (If you are using `ggplot2`, you can use `geom_hline()`.) 
iv. As $n$ increases, what happens to the cumulative mean?

```{r}
# Your solution
```






