from symbulate import *
%matplotlib inline

"""
Explanation: Symbulate Lab 7 - Stochastic Processes
This Jupyter notebook provides a template for you to fill in.  Read the notebook from start to finish, completing the parts as indicated.  To run a cell, make sure the cell is highlighted by clicking on it, then press SHIFT + ENTER on your keyboard.  (Alternatively, you can click the "play" button in the toolbar above.)
In this lab you will use the Symbulate package.  Many of the new commands are discussed in the Random processes section of the Symbulate documentation. You should use Symbulate commands whenever possible.  If you find yourself writing long blocks of Python code, you are probably doing something wrong.  For example, you should not need to write any long for loops (though you will need to write a simple for loop in Problem 1 part a).
There are 2 parts, and at the end of each part there are some reflection questions.  There is no need to type a response to the reflection questions, but you should think about them and discuss them with your partner to try to make sense of your simulation results.
Warning: You may notice that many of the cells in this notebook are not editable. This is intentional and for your own safety. We have made these cells read-only so that you don't accidentally modify or delete them. However, you should still be able to execute the code in these cells.
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: Problem 1.
Here is one example of a discrete time, continuous state process.  Suppose that $X_0 = 0$ and for $n = 0, 1, 2, \ldots$,
$$
X_{n+1} = 0.5 X_n + Z_{n}
$$
where $Z_1, Z_2, \ldots$ are i.i.d. $N(0,1)$.  Such a process is called an autoregressive process (of order 1).
a)
Define in Symbulate the $X$ process, for time steps $n= 0, 1,2, \ldots, 20$.  Hint: this example should be very helpful.

Define a probability space P corresponding to an infinite sequence of i.i.d. $N(0, 1)$ values.  (Hint: bottom of this page.)
Define an RV Z on the probability space P; each component of Z can be indexed with brackets [] , e.g. Z[0], Z[1], etc.
Define a RandomProcess X on P.
Write a simple for loop to define the value of X[n+1] based X[n] and Z[n].
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: b)
Simulate and plot a single sample path for n= 0, ..., 20.  (Hint: see the plots here.  You might need to change alpha = if the plot is too light.)
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: c)
Simulate and plot 100 sample paths.
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: d)
Simulate and plot the distribution of $X_5$, and approximate its mean and variance.  (Hint.)
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: e)
Simulate and plot the distribution of $X_{10}$, and approximate its mean and variance.
Reflection question: How does the distribution of $X_{10}$ compare to that of $X_5$?
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: f)
Simulate and make a histogram or density plot of the joint distribution of $X_5$ and $X_{10}$, and approximate its correlation and covariance.
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: g)
Simulate and make a histogram or density plot of the joint distribution of $X_5$ and $X_6$, and approximate its correlation and covariance.
Reflection question: How does the joint distribution of $X_5$ and $X_6$ compare to that of $X_5$ and $X_{10}$?
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: h)
Simulate and make a histogram of density plot of the joint distribution of $X_{10}$ and $X_{15}$, and approximate its correlation and covariance.
Reflection question: How does the joint distribution of $X_{10}$ and $X_{15}$ compare to that of $X_{5}$ and $X_{10}$?
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: i)
Simulate and make a histogram or density plot of the joint distribution of $X_{10}$ and $X_{11}$, and approximate its correlation and covariance.
Reflection question: How does the joint distribution of $X_{10}$ and $X_{11}$ compare to that of $X_{5}$ and $X_{6}$?
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: j) More reflection questions
Does the $X$ process appear to be stationary?  Note: The process starts with $X_0=0$ so it is technically not stationary.  But aside from the first few times, does it appear that the process is stationary?
Problem 2)
Consider a random signal with both a random amplitude and a random "phase shift".
$$
X(t) = A\cos(2\pi t + \Theta)
$$
where $A$ and $\Theta$ are independent, $A$ is equally likely to be 0.5, 1, or 2, and $\Theta$ has a Uniform(0,$2\pi$) distribution.
Note: to define $\cos(2\pi t)$ in Python, use cos(2 * pi * t)
a)
Define in Symbulate the $X$ process.

Hint: use ContinuousTimeFunction to define the deterministic process $f(t)=t$.
However, to do some of the parts below, you'll need to explicitly define RV for $A$ and $\Theta$ like here.
Define the probability space P for $A$ and $\Theta$ and then define RV on that space and also RandomProcess on that space.
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: b)
Simulate and plot a single sample path for $0\le t \le 3$.  (You might need to change alpha = if the plot is too light.)
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: c)
Simulate and plot 100 sample paths.
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: d)
Simulate and plot the conditional distribution of $X(1)$ given $A = 1$, and approximate its mean and variance.  (Remember this.)
Reflection question: You might suspect that since $\Theta$ has a uniform distribution then $X(1)$ has a uniform distribution when $A=1$.  But is that true?
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: e)
Simulate and plot the distribution of $X(1)$, and approximate its mean and variance.
Reflection question: Can you explain the shape?
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: f)
Simulate and plot the joint distribution of $X(1)$ and $X(2)$ and approximate the covariance and correlation.
Reflection question: Can you explain why the scatterplot looks the way it does?)
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: g)
Simulate and plot the joint distribution of $X(1)$ and $X(1.5)$ and approximate the covariance and correlation.
Reflection question: Can you explain why the scatterplot looks the way it does?
End of explanation
"""


# Type all of your code for this problem in this cell.
# Feel free to add additional cells for scratch work, but they will not be graded.

"""
Explanation: h)
Simulate and plot the joint distribution of $X(1)$ and $X(1.25)$ and approximate the covariance and correlation.
Reflection question: Can you explain why the scatterplot looks the way it does?
End of explanation
"""


