```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
# py_install("matplotlib")
```



# Introduction

* Made available UCL Week 16
* Due UCL Week 24; 9 February 2023, 13:00 London time.
* You are to work in groups of 2-4; there should be no overlap with your ICA2 groups.
* This ICA consists of  questions, worth 55 points;
* another 5 points will be given based on presentation, so that the total available points is 60  
* This ICA is worth 30 percent of your grade for this module.
* Many questions will require you to write and run R/Python code.
* The ICA must be completed in R Markdown/Juptyer Notebook and typeset using  Markdown with Latex code, just like the way our module content is generated.  You can choose to use either R or Python.
* Please hand in the html file and the Rmd/ipynb source file.
* As usual, part of the grading will depend on the clarity and presentation of your solutions.
*  You are to do this assignment by yourselves, without any help from others.
*  You are allowed to use any materials and code that was presented so far.
*  Do not search the internet for answers to the ICA.
*  Do not use any fancy code or packages imported from elsewhere.



##  Plagiarism and collusion

Please familarize yourself with [the following excerpt on plagiarism and collusion from the student handbook ](https://tsoo-math.github.io/ucl/Plagiarism-Collusion.pdf)


By ticking the submission declaration box in Moodle you are agreeing to the following declaration:

<br>

**Declaration:** I am aware of the UCL Statistical Science Department's regulations on plagiarism for assessed coursework. I have read the guidelines in the student handbook and understand what constitutes plagiarism. I hereby affirm that the work I am submitting for this in-course assessment is entirely my own.



## Anonymous Marking 

Please do **not** write your name anywhere on the submission. Please include only your **student number** as the proxy identifier.





# Questions



## 1.) An ellipse and a circle [5 points]

By running simulations of uniform random variables, estimate the area in the intersection of an ellipse and circle, with equations given by:
$$\frac{x^2}{25} + y^2=1$$
and
$$(x-4)^2 +(y-2)^2 =4.$$



## 2.) Bivariate normals [5 points]

Let $Z_1, Z_2, \ldots$ be independent standard bivariate normal random taking values in $\mathbb{R}^2$.   A lecturer claims that by simply knowing whether $Z_i$ is in the unit circle $x^2 + y^2 =1$, for a large number for $i=1,2,3, \ldots$, they can estimate the value of $\exp(1) \approx 2.71828..$.  Is this claim true or false?  Explain. 


## 3.)  Markov chains  [10 points]

Let $$X = (X_0,X_1 \ldots, X_N)$$ be $(N+1)$-steps of an aperiodic irreducible finite state Markov chain with a transition matrix $P$ and state space $S$.   Consider the sequence of random variables given by 
$$Y := (Y_0, Y_1, \ldots, Y_N)= (X_N, X_{N-1} \ldots, X_0).$$

- [3 points] Show that $Y$ is $(N+1)$-steps of a  Markov chain that is *not* necessarily time-homogeneous. 
  - [3 points] Show that if $X_0$ has the stationary distribution for $P$, then $Y$ is a time-homogeneous Markov chain; 
    - [4 points] in this case, identify the $\lim_{N \to \infty}\mathbb{P}(X_0 = j | X_N = i)$ for states $i,j \in S$.


## 4.) Poisson processes [5 points]

Given $\lambda_N > \lambda_Q >0$, show that there exists Poisson processes $N$ and $Q$, with rates $\lambda_N$ and $\lambda_Q$, respectively, such that $N(t) \geq Q(t)$ for all $t \geq 0$, and such that the first arrival for $N$  always comes *strictly* before the first arrival for $Q$.





## 5.)  Continuous-time Markov chains [10 points]

Consider a  continuous-time Markov chain $X$ on three states $\{1,2,3\}$ with transition rate matrix
$$ Q=
\begin{pmatrix}
-5 & 2 & 3\\
1 & -4 & 3 \\
1 & 2 & -3
\end{pmatrix}.
$$

Suppose that $X$ is started at $1$, so that $X(0)=1$.  Let $Y= \inf \{t \geq 0: X(t) \not = 1\}$.      Let $T = \inf \{t> Y:  X(t) =1 \}$.  

- [5 points] Compute (analytically, exactly), $\mathbb{E}(T)$. 
- [5 points] Compute, by simulating the Markov chain,  $\mathbb{E}(T)$.




## 6.) Infinite servers [20 points]

Consider the following system where items arrive as a Poisson process with intensity $\lambda >0$, and there are an infinite number of servers, each of which gives service time corresponding to a continuous random variable with cdf $F$, independently, to the customers.  Assume that a random variable with cdf $F$ has finite mean.    

- [10 points] 
    * [6 points] Code this queue in the special case that $\lambda =1$ and  $F$ corresponds to the uniform distribution on interval $[1,2]$.
    * [2 points] In particular, for $t=100$, plot a probability histogram of $Q(t)$, the number of items in queue at time $t$.
    * [1 point] What is the mean and variance? 
    * [1 point] Identify the distribution.
- [3 points] Apply Little's law to analytically obtain the (long term) average number of items in the queue. 
- [7 points] In the following question, we will analytically compute the distribution for $Q(t)$; do not assume that $\lambda=1$ or that $F$ corresponds to the uniform distribution.  Let $N(t)$ be the number of arrivals by time $t\geq 0$.
  * [2 points] Suppose there is only one arrival by time $t>0$; that is, one item has arrived in the time interval $[0,t]$. Show that $$p:=\mathbb{P}(Q(t) =1 | N(t)=1 ) = \frac{1}{t}\int_0^t[1- F(z)]dz.$$  Hint:  recall that if there is only one Poisson point in $[0,t]$, then it is uniformly distributed in that interval.
  * [2 points] Compute and identify the pmf given by $\mathbb{P}(Q(t) =k | N(t)=n)$ for $0 \leq k \leq n$ and fixed $n \geq 1$. 
  * [2 points] Compute and identify the pmf for $Q(t)$.  
  Hint: if you use all your knowledge about Poisson processes, you may be able to avoid a minor calculation.
  *  [1 point] What is the limiting pmf for $Q(t)$ as $t \to \infty$?
   



# Endnotes

* Q2: the word *standard* is used to mean that the two components are independent are mean-zero with unit-variance.
* Q4: the word *strictly* is added to clarify.

* Version: `r format(Sys.time(), '%d %B %Y')`
* [Rmd Source](https://tsoo-math.github.io/ucl3/2022-ica3-stat9-release.Rmd)

