\documentclass[11pt,twoside,titlepage]{article} % Document font size and equations flushed left

\usepackage[english]{babel} % Specify a different language here - english by default
\renewcommand{\familydefault}{\sfdefault}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage[a4paper,left=1.9cm,top=2.0cm,right=1.9cm,bottom=2.0cm]{geometry}
\renewcommand{\baselinestretch}{1.5}
\usepackage{natbib}
\usepackage{hyperref}
\usepackage{fancyhdr}
\usepackage{mdframed}
\usepackage{pdfpages}
\usepackage{siunitx}
\usepackage{amsmath}
\usepackage{unicode-math}
\usepackage{pdfpages}

\begin{document}

\section {Question 1}

The two Probability Distribution Functions are as given in Equations \ref{pdfs}.

\begin{subequations}
    \label{pdfs}
    \begin{equation}
        f_1(t) = \frac{1}{ \sigma_1 \sqrt{2\pi}} \exp{-\frac{1}{2} \left(\frac{     t - \mu_1}{\sigma_1}\right)^2}
	\end{equation}

	\begin{equation}
	    f_2(t) = \frac{1}{t\sigma_2 \sqrt{2\pi}} \exp{-\frac{1}{2} \left(\frac{\ln(t) - \mu_2}{\sigma_2}\right)^2}
	\end{equation}
\end{subequations}

Using Equation \ref{comboGeneric} gives the resulting PDF. Because the Normal distribution has no analytical solution, the analysis must be performed numerically.

\begin{equation}
    \label{comboGeneric}
    f(t) = f_1(t) R_2(t) + f_2(t) R_1(t)
\end{equation}


%% todo something about censored data

Because of confidence in Python, it was chosen to do this analysis. The Jupyter notebook is available at the end of this file.

A helper class was written to aid in use and avoid the potential for confusion later in the analysis.
The PDF was implemented, as was the CDF. From this, it was trivial to implement the Reliability function as well.

Some plots were made to explore the state space, using $\mu_1$ and $\mu_2$ on one plot, and $\sigma_1$ and $\sigma_2$ on the other plot.
The full range of possible values was used on the plots that have them changing, and the initial values of fixed values were the midpoints of possible values. Estimating the maximum value of the varying values from the plot, these were then set to the fixed values of the other plot.
This was a crude way to check for values that could be expected from the optimizer. This gave expected values of $\mu_1 \approx 30, \sigma_1 \approx 9, \mu_2 \approx 2.5, \sigma_2 \approx 0.6$. This was not to be relied on heavily, as the loss landscape may be quite erratic.

Scipy's \texttt{shgo} was chosen on a whim out of the global optimizers. After testing, other optimizers give the same results.

The manual optimisation was useful, as there were some errors in the implementation of MLE. Using the values that I expected, as well as plotting the PDF over a histogram of the data showed that this was wrong. Once the implementation was fixed, the outputted values were as follows in the Table \ref{1aresults}.


\subsection{1 a}
\begin{table}[htb!]
\centering
    \begin{tabular}{|l|l|l|}
        \hline
        \textbf{distribution} & \(\symbf{\mu}\) & \(\symbf{\sigma}\) \\
        \hline
        normal & 38.376 & 1.6501\\
        \hline
        lognorm & 2.710 & 1.2011\\
        \hline
    \end{tabular}
    \caption{yippee!! These are my answers for section 1a}
    \label{1aresults}
\end{table}

As seen in Figure \ref{histogramwpdf}, the PDF fits the histogram of the data quite well, suggesting that even if I'm wrong, I'm not too wrong.

\begin{figure}[h]
    \centering
    \includegraphics[width=0.4\textwidth]{images/1a.png}
    \caption{yep. looks good}
    \label{histogramwpdf}
\end{figure}

Figure \ref{losslandscapes} shows that at these given values, the function is quite smooth, and that these values correspond to eachother. This is not very meaningful, but it looks nice.

\begin{figure}[h]
    \centering
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=0.9\textwidth]{images/losslandscape_mu.png}
        \caption{Log-likelihood at most likely values of \(\symbf{\mu}\) of \(\symbf{\sigma}\)}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=0.9\textwidth]{images/losslandscape_sigma.png}
        \caption{Log-likelihood at most likely values of \(\symbf{\mu}\) of \(\symbf{\sigma}\)}
    \end{subfigure}
    \caption{}
    \label{losslandscapes}
\end{figure}


\subsection{1 b}

As the CDF is already defined to make use of the censored data, this result was simply obtained by passing in 20.

\begin{equation*}
    F(35) = 0.7641
\end{equation*}

\newpage

\section{Question 2}
\subsection {2 a}
There are \textbf{7} routes. The workings for this are an excersise left to the reader.
\begin{table}[htb!]
\centering
    \begin{tabular}{|l|l|}
    \hline
    route 1 & G1; S1; S4; G2\\
    \hline
    route 2 & G1; S1; S5; G2\\
    \hline
    route 3 & G1; S2; S4; G2\\
    \hline
    route 4 & G1; S2; S5; G2\\
    \hline
    route 5 & G1; S2; S6; G2\\
    \hline
    route 6 & G1; S3; S5; G2\\
    \hline
    route 7 & G1; S3; S6; G2\\
    \hline
    \end{tabular}
    \caption{The routes as required}
    \label{2aresults}
\end{table}
\subsection{2 b}
The Weibull reliability function was implemented, as was \(m\) of \(n\) reliability. Using the right combination of \(m\), \(n\), \(\beta\) and \(\eta\) for each subsystem, and multiplying the subsystems gives the reliability of the whole system. It is then easy to enter a time into the Reliability for the whole system.
\[R(20) = 0.6902 \]

\subsection{2 c}
Figure \ref{reliabilities} shows the thruster to be the least reliable system over the lifespan of the satellite. This is the first area to attempt to upgrade, followed by the power distribuition.

\begin{figure}[h]
    \centering
    \includegraphics[width=0.4\textwidth]{images/2b.png}
    \caption{yep. looks good}
    \label{reliabilities}
\end{figure}


\subsection{2 d}

A function is written to determine the available number of routes given the status of each satellite, and is named as expected.
A random number is generated with a uniform distribution between 0 and 1, and if the number is larger than the reliability function, the satellite has failed.
The statuses are passed into the function, and if the number of routes is greater than 0, the system has not failed.

The monte carlo simulation typically provides around a probability of 0.92 that it is still possible to transmit the signal.

To ensure that this number is accurate and not an anomaly, the simulation was run 10,000 times. Figure \ref{mc_hist} shows the resulting distribution, confirming that this is the correct number.

\begin{figure}[h]
    \centering
    \includegraphics[width=0.4\textwidth]{images/mc-dist.png}
    \caption{Histogram of Monte Carlo outputs}
    \label{mc_hist}
\end{figure}

\section{jupyter notebook}
\includepdf[pages=-]{Untitled-2.pdf}
\end{document}
