\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\begin{document}
\noindent
\subsection*{Some terms}
Square of L2 $||\theta ||^{2}_{2}$\\
Sum of all squares
\\\\
L2 norm $||\theta ||_{2}$\\
Square root of sum of all squares
\\
\\
Collaborative filtering - collaborative filtering is a method of making automatic predictions (filtering) about the interests of a user by collecting preferences or taste information from many users\\
Assumes that one person has same opinion as the other person
\\\\
Inductive learning Assumption - after seeing many training examples result in high accuracy on unobserved examples\\
Hypothesis approximates well\\
\\
average hypothesis only depends on H, does not always lie in H\\
$H_{2} \subset H_{10}$\\
\\
Target function y = f(x)\\
Unbiased learner cannot generalise \rightarrow\) inductive bias
\\\\
h is +ve when h(x) = 1\\
$\geq_{g}$ more general than	 or equal to\\
h_{j} $\geq_{g}$ h_{k} \equiv \forall x \in X (h_{k}(x) = 1) \rightarrow (h_{j}(x) = 1)\)
\\
Not a total order has there may have the same specificity but different hypothesis (maximally specific h)
\\
\\
Consistency: h is consistent iff h(x) = f(x)\\
 h4(\langle0,1\rangle) = 1 \neq f(\langle0,1\rangle) = 0.\\\\
 \textbf{Proposition 1} \\
\)h is consistent with 𝐃 iff every +ve training instance satisfies h and every
-ve training instance does not satisfy h.\\
 \textbf{Proposition 2} \\
\)If h belongs to H, then it is consistent
\subsection*{Naive Bayes}\\
\subsubsection*{Bayes Rule}\\
There can be relatively lesser occurrences of one class
\\
prior odds / likelihood = posterior odds\\
Bayesian thinking\\
How likely is something not going to work?
\\
\includegraphics[height=2cm]{images/s4.png}\\
Hold your beliefs, shift your beliefs as u encounter with the world\\
$P(y | x) = \frac{P(x | y) \times P(y)}{P(x)}$
\\
P(X_{1} = 0, X_{2} = -1) = P(X_{1} = 0 \;|\ C_{1}) \times P(X_{2} = -1 \;|\ C_{1}) \times P(C_{1})
+ P(X_{1} = 0 \;|\ C_{2}) \times P(X_{2} = -1 \;|\ C_{2}) \times P(C_{2})
\)
\\\\
\textbf{a priori} - equally probable\\\\\\
Naive - Assuming that features are conditionally independent of each other
\\\\
Insensitive to the number of training examples
\\\\
Calculate which is more likely\\
#1. $P(Outcome | x)$ = P(X_{1} | Outcome) * P(X_{2}| Outcome) * P(Outcome)\\
#2. \)$P(Not\ Outcome | x)$ = P(X_{1}| Not\ Outcome) * P(X_{2}| Not\ Outcome) * P(Not\ Outcome)
\)
\\\\\\
Calculate probability outcome occurs\\
\#1/(\#1+\#2)
\subsection*{Perceptron classifier}\\
x_{0}\theta_{0} + x_{1} \theta_{1} + x_{2}\theta_{2}\\\) of the previous weight vector
\\
first instance will compute based on initial vector\\
For -1, first point checks == -1, != 1 for the points after\\
Update weight = - (x \times\) learning\_rate) for the misclassified instance\\
\\\)
For +1, first point checks != -1, == 1 for the points after\\
Update weight =  + (x \times\) learning\_rate) for the misclassified instance\\
\\
points are linearly separable if there are no longer any misclassified points
not misclassified: sign(x) function having -1 when x is \leq 0
\\\\
\subsection*{Supervised learning}\\
Identifying objects \\
Output would be a continuous value\\
Given many emails, you want to \textbf{determine} if they are Spam or Non-Spam emails.
\\
\\
Given historical weather records, \textbf{predict} if tomorrow's weather will be sunny or rainy.
\\\\
\subsection*{Unsupervised learning}\\
No labels (no output)
Kmeans
\\
Hard clustering can assign to any one of the cluster\\
\\
Input: Given a set of data points P, number of clusters k
\\1. Randomly pick k points from P as centers $c_{j} = 1.. k$
 \\2.
Iterate (until max-iterations or $c_{j}$ no longer changes up to some threshold) \\
- Assign each point to nearest center: $y_{i}$ = arg min {\parallel p_{i} - c_{j} \parallel}^{2}\)
\\
$c_{j}$ =  Re-estimate each center as mean of points assigned to it
\\\\
Given a set of news articles from many different news websites, find out what are the main topics covered.
\\\\
From the user usage patterns on a website, figure out what different groups of users exist.
\\\\
\subsection*{Motivation}
Importance of models, learning a probability distribution\\
Can learn an arbitrary distribution, not all distribution can be described by the same model\\
Restricted so small number of parameters to be learned in a reasonable amount of time
\\
Can tell water drops and rain\\
Shaking the toy causes the rattling noise\\
Get a good approximation on the cost and structure using limited amount of resources
\\
Data is generated by a physical process\\
\\\\\\
Only wants to capture the signal
\\
Intrinsicity - How many independent dimension are there in each layer\\
\\
\textbf{PCA}\\
z is the reconstruction and v is the principal component\\
Chooses orthonormal bases\\
Ellipse is defined by the eigenvectors and values
\\\\
\textbf{SVD}\\
V - principal components\\\\
$\sum$ - eigenvalues\\
Top left corner will have the largest eigenvalue\\
Bottom right corner have the least significant eigenvalue\\
0 on off-diagonal \\
\\
U - projection of data\\
\\
\textbf{Clustering}\\
k means++ default initialization by sklearn\\
Generalized EM when the approximation is intractable\\
Incremental EM estimates on small number of points, like difference between batch gd and sgd
\\
\subsection*{RL}\\
Key usage: Planning problems, app placements product recommendations\\\\
\textbf{Goal}\\
Low risk when performing actions\\
Goal of markov property: operate in ease\\
choices that maximise the outcome\\
\\
Precision vs control\\
Decisions we make the optimize\\
\\
Prediction allows us to evaluate how good a policy is for a state space\\
Control\\
Reward at any step is optimal, highest reward
\\
continuous MDP - common in robots\\
No supervisor, only reward signal\\
Reward is a scalar feedback\\
whether one state vs the other is preferred\\
\\
RL Challenge: Does not have a good idea if it is right to do, which one is feasible, figuring out rules, which gives the best reward
\\
Given action will give observation and reward\\
Does not know the rules\\
\\
Planning Challenge:\\
A* search - Going through the search space to figure the appropriate action\\
what if i did this, what kind of consequences
\\
Rules are known\\
Uses a search space which requires tree search
\\
\\
Action affects the subsequent states\\
Action $\rightarrow$ Observation (what state it is in) $\rightarrow$  Reward\\
time t will not take the final reward but t - 1\\
Sometimes actions affect the observations\\
May contain irrelevant information of the state at time t\\
Although we have large field of vision, our neural cortex is trying to observe t changes in the environment\\
Function of history is not visible to the environment and is specific to agent\\
State - what information that we have in the brain is what we are going to need to predict the future\\
\\
\textbf{Fully observable}\\
The state can be represented different based on how we want the agent to see the sequence\\
congruent between the agent and the environment\\
\\
\textbf{Partially observable}\\
Align/ localize itself based on the map e.g. game
\\
Represented state can influence the agent
\\
Depending on the agent, we can end up in a different state\\
E.g. if we want to turn the rotor around the corner, it may turn out not to behave that way
\\
Recurrent neural networks\\
Using ReLu to construct each state in deep learning, easy to compute as no exponential operations
\subsubsection*{Policy}
Look through the mapping function\\
Deterministic $s_{1}$ to $a_{1}$ \\
Stochastic Take a particular action with a probability\\
\\
Value function - future reward\\
Sum of expected reward, reward farther in the future will have gamma to discount it\\
Model - probability that it will go to the next state, sum over all possible actions has to be 1
\\\\
\textbf{Agent taxonomy}\\
Value Based\\
Policy - in this state we take a particular action\\
Actor Critic - model based
\\
\subsection*{Exploration vs Exploitation}
Going to the restaurant that i like \\
Exploration - discover more information about the environment
\\
Exploitation - Exploits known information to maximise reward
\\\\
\subsection*{State transition probability matrix}\\
Represented with the symbol P\\
some of the values are 0 in the transition matrix if it cannot move to a particular state\\
episodes are finite\\
there are always probability attach to the state, eventually it will reach the terminal state
\subsection{Markov Reward process}
Attach the reward and discount\\
Differentiating reward,create two different states with different reward\\
$E(r_{t+1} | ..)$ immediately receive the reward in next time step
\\\\
Discount factor$\gamma $
\\ Near sighted - given where i am now where should I go
\\ Far sighted - sum all the rewards add all of them, favor longer samples (should have more reward)
\\
\textbf{Role of Discount}\\
Infinitely sample the cycle, a lot of uncertainties on the future
\subsection{Bellman equation}
Reward based on executing the state
\subsection*{Value iteration}\\
Idea of value iteration\\
Instead of looking what policy to take, look back and determine what is the best action brought us to get the best value function \\\\
Emits the observation and reward\\
Recover information or t and r\\
\\
Value iteration is a simple form of policy iteration\\
Use optimal V*  to create back the policy\\
\subsection*{Q learning}
Use q value to engineer the policy (monodically increasing)\\
r(s, a) reward at comes at next state\\
Higher value of q means we see it a lot of times, smooth it over time
\newpage\noindent
\subsection*{Neural Networks}
$\Theta$ dimension = ($d_{l-1}$ + 1) \times $d_{l}$ where l is the layer before the activation function (denote by x or a)\\
g function will be relu for deep learning and tanh for NN\\
For the hard threshold, when it is close to 0 it becomes a linear line\\
a = g(s) = $g\big(\sum\limits_{j=0}^{d[l-1]} \Theta_{i,j}^{[l]}\ x_{j}^{[l-1]}\big)$
\\
1 represent correct classification
\\
-1 to 1 step function \\
Going from -1 to 1
h = \begin{matrix}
[1;-1;-1]
\end{matrix}
where 1 represents the output unit to be positive (for eg first from the top $a_{1}^{[l]}$)
\\\\
Number of kernels N = 2 for gray, 6 for RGB\\
\includegraphics[height=4cm]{images/s6}
\\
\textbf{Motivation}\\
In traditional ml, it is difficult to handcraft features, neural networks are good in feature learning\\\\
NN generalise badly\\
As the number of parameters (weights and layers) increase in a neural network, it can easily overfit the data.\\
Backpropogation is a complicated and tedious process. Moreover, the surface plotted by the error of a neural network in non-convex and high-dimensional and can consist of many hills and valleys - making it hard to reach the optimal set of weights.\\
\\
Do all the neurons in a layer always share the same bias value?
\\
Yes, that is correct.  The bias value is the same value ($x_0=1$) for all units at all layers, because we are going to learn the particular weight for the bias for each individual unit, $\theta_{0}$, separately.  It's just pictorially cleaner to think that each layer has a separate bias unit, but it doesn't matter, because the value of the bias unit is always (by convention) set to unity, such that the individual weights $\theta_{0}$ (or in the NN matrix notation  $\theta^{(l)}_0$ are the controls for the biases. \\\\
\\
Random initialisation of weights for sign function\\
the sign() function takes different constant values in different regions and therefore has gradient 0.\\
the sign() function is discontinuous at x=0.\\
\\
Stack in together in a linear unit\\
Generalise things that are out of sample\\
\\
Regularisation for NN\\
Weight decay square of the weights are small so it is more toward linear regression (L2 regularisation) \\
\\
\\
\\
Is the Soft Weight Elimination function a Cost Function?\\
No, but it closely related.  It is a regulariser, a separate, artificial part of the augmented loss function, as what we learned in Week 05.  We denote regulariser functions as $\Omega(\theta)$.
\\\\
Would it have been a good idea to stop the first time the validation error goes up?\\
The validation error might fluctuate and therefore stopping it the first time it goes up will not give the optimal result. One can consider using a moving average or some other metric.\\
\subsection*{Deep Learning}
Capability to take in multiple input and give multiple outputs\\\\
Dropout - Generalise well rather than approximate well and overfit
ReLU - Vanishing gradients
\\
Bias- Variance tradeoff - Not generalizing due to too many parameters, model representation fit the particularities. Not fitting the additional noise.
\\
\\
Higher budget will have a wide set of solution and encompasses $\theta_{lin}$ (the ideal solution) such that no regularisation is used
\\
$\theta_{reg}$ is proportional to the size of the circle, larger C causes smaller regularisation, smaller circle leads to larger gradient
\\\\
GAN create adversary to help u train better\\
E.g. mock test paper to do better in exam
\\
RNN time series inputs are correlated to each other\\
E.g. determiner followed by noun
\\
Weights of output from one node to another model layer of another time step is the same \\
\\
Model constraint input uses the same weights so that we can fewer parameters than 2
\\
\\
1 to Many: Image captioning\\
Many to 1: Sentiment Classification\\
Many to Many: NMachine translation
Many to Many (time sensitive): Stock market prediction\\
\\
\subsection*{RNN}
Tries to get the softmax chance higher\\\\
\textbf{Truncated backprop}\\
Instead of going to the start, backprop only to the start of the partition block
\subsection*{CNN}
Finding context where locality is important or somehow related to the things nearby\\
Key idea: Make it invariant to position\\
E.g. Non-linear transformation to find a horizontal lines\\
AND and OR gate to form a high level representation\\
\\
Image that maximises the strength of the corresponding units activation\\
\\
Tied samples over all the subsamples of the image\\
Sliding windows to build new feature maps\\
Sub-sampling (Pooling layers)\\
Stride - ignore intermediate squares
Max pooling gives the maximum of the units based on the filter size\\
\\
Layers at the beginning of the network has smaller activations as it is the problem of vanishing gradients (gradient is very small at end layers) stacking all of the layers together, when run on training (after pre-training)
\\
Layers get more positive or negative further down the network as compounding signals get more extreme, causing vanishing problem, doesn't give the same magnitude of gradient at different parts of the network
\\
- Learning rate has to compensate\\
- Weight decay regularisation
\\\\
Or when values are close to 1, then we have issues with learning
\\
ReLu loses representation \\
\\
softmax cannot be used in nlp to predict next word as it can be computationally expensive
\\
hierarchical softmax can be computed in parallel
For unsupervised pre-training, weights by supervised learning and fine tuning it
\\
Final softmax layer needs supervision so we can only pre-train the middle layers
\\
GPU allows operation to happen simultaneously\\
each part of the GPU computing one feature map
\\
\\
performance will only get better for resnet with more layers as the residual will be learned by the next layer only if in the previous layer the residual is not captured.
\subsection*{Sparse Autoencoders}
Have to learn a smaller set of weights like a lossy compression algorithm \\
L1 to encourage sparsity
\subsection*{Dropout}
Idea: Random forest feature projection\\
Keep input fixed and only the output at other layers change\\
Rotate over multiple batches\\
Only certain neuron get the weight which can help in regularisation
\subsection*{Transfer learning}
Unsupervised feature extraction layers, or bottom and middle layers to use for new tasks\\
Hyperparameter to decide where to cut off softmax, FC, pooling
\\
Not useful for\\
VGG net has learned color or natural images that are not part of the domain
\subsection*{Representation Learning}
Uses embedding in the compressed layers
\subsection*{SVM}
Another linear unit
\newpage\noindent
\subsection*{Find S}
1.Initialise h to most specific h\\
2. If the positive instance differs, then replace with ?
\subsection*{Version space}
List all hypothesis, remove any hypothesis that is inconsistent with any training example h(x) $\neq$ f(x)
\subsection*{Candidate Elimination}
prefers positive examples over negatives as there can only be one maximally specific hypothesis in each iteration
? don't care\\
\empty no value\\
Set $S_{0}$ = \langle \emptyset \emptyset \emptyset \emptyset \emptyset \emptyset \rangle\\
\\
$G_{0}, G_{1}$ = \langle ? ? ? ? ? ? \rangle\\
\\
\)
For the negative example,\\
Create h from the specific hypothesis when it differs based on previous instance\\
Remove those that are inconsistent from the previous general hypotheses\\
Keep S to the previous instance S
\\\\
For the positive example,\\
replace with ? when it differs
\\
\subsection*{Cost functions}
Logistic regression\\
Is binary classification\\
Need meta strategies for multi class and it chooses the model that has the highest confidence in predictions
\\
Only gives values 0 and 1 so it is not a good result
\\\\
J(\theta) = \frac{1}{m} \sum\limits_{j = 1}^{m} ln(1 + exp^{-y^{{j}\theta^{T}x^{j}}})\\
\\\)\)
Linear regression\\
J(\theta) = \frac{1}{m} \sum\limits_{j = 1}^{m} (\theta^{T}x^{j} - y^{j})^2

\subsection*{Linear algebra}
trace(AB) = trace(BA) = trace(I_{n+1}) = n + 1\\
\\
Idempotency\\
H^2 = H\\
(I - H) ^2 = I - 2H + H = I - H\\
\\
\subsection*{Pseudoinverse}
(X^{T}X)^{-1} X^{T}\\
\theta = X^{+} y\\
X = \begin{pmatrix}
	1 &.. &..\\
	1 &.. &..\\
	1 &.. &..
	\end{pmatrix}
\\
\subsection*{Maximizing likelihood}
Minimize cross entropy = - $\frac{1}{m} \sum\limits_{j=1}^{m} ln\;g(y^{j} \theta^{T} x^{j})$
\subsection*{Ridge Regression}
$\frac{1}{m}\sum\limits_{i=1}^{m} (y_{i} - w^{T}x_{i})^2 + \lambda \parallel w \parallel ^2$
\\
$ -2 X^\top (Y - X * \theta) + 2 * \alpha * \theta $
\\
\subsection*{Noise}
Not deterministic only deterministic for f(x) = 0 \\
y - f(x)\\
$P(y | x)$\\
Noisy target = $E[y|x] + (y - f(x))$
\\
\subsubsection*{Stochastic noise vs Deterministic}

To reduce stochastic noise, the only way is to re-measure y
\\
To reduce deterministic noise, change H
\subsection*{Underfitting and Overfitting}
Why do we regularize?
We fit the data too much, we are fitting to the noise, balance fitting to the observed data. If we fit the data too well, the performance can be poor called overfitting. Overfitting can happen even if the data is not noisy.\\\\
\textbf{Overfit} - high level of noise, high complexity of f(x)\\
\textbf{Underfit} - high bias and low variance\\\\
Reduce bias by increasing complexity of hypotheses\\
simple representation of more complex reality
\subsection*{Non-linear transformation}
Transform this non-linear equation to a linear one: $y = ax^b$?
\\\\
 Under the condition $y>0$ and $x>0$
 \\
$ln(y)=ln(ax^b)$
\\
$ln(y)=ln(a)+ln(x^b)$
\\
$ln(y)=ln(a)+bln(x)$
\\
let $y'=ln(y)$, $x'=ln(x)$, $a'=ln(a)$, the equation can be written as $y'=a'+bx'$
\subsection*{Decision trees}
Diversifying dataset similar to validation set\\
Piecewise linear but ends up with a complex function\\
Decision tree uses the entire hypothesis space\\
Can overfit so we need pruning
\\
Divide and conquer log n to find $\theta$ for decision stump (one node of decision tree)
\\
If it is in the wrong class, then the weight of the misclassified is increased, shrink those that are correct\\\\
Random forest (Feature subspaces) \\
Does not use all the data\\
\\
To classify new data, \\
Same as any other ensembling method\\
Give the input to all the trees\\
And take the vote\\
\\\\
Bagging - Sample with replacement\\
Diversity in features\\
Some subset of features\\
\\
Ensemble usage\\
Continuously verify the presence of the user
\\\\
Adaboost\\
1. Find a weak estimator
\\2. Update the weights, to classify incorrect inputs to be correct
\\3. Find another estimator
\\
If we take the best hypothesis, error rate is larger than 0.5, then no estimator will give a better result
\\
Based on probabilities, not all positives/ negatives\\
To lower variance: \\
DTL - pruning\\
Bootstrap - Average to lower variance\\
\\
To have more variation: \\
Feature projection for Random forest
<<<<<<< HEAD
\subsection{Ethics of ML}
Sensitive attributes: race, ethnicity, sex\\
Fine line between personalization and in building our bias in the system\\\\
Can look at correlations on search data\\
\\
Sometimes created by people themselves on social media, what people like, or malicious users when the user is not at home\\\\
Quantifying the risk\\
Techniques that can preserve those properties, privacy-preserving, fair and explanable\\
Defense mechanisms - don't want to share the data, share compressed data so everyone can benefit from the data without leaking the information
\\
Trustworthy ML - large capacity if u expect the model to do very well, vulnerable to data leakage or manipulation attacks.
\\
When is trained to be more fair, it can craft more data in the final model compared to the model that is not fair, data poisoning
\\
Robust to data poisoning, can i trust in a typical setting\\
Critic-ability, can it convince me? Is it trustworthy?\\
Fairness, privacy and robustness\\\\
Engineering bias in the machine learning algorithm\\
Hiding corrupt practices behind mathematics\\
Going through the data to double/ triple check\\
Triplet ratings, build a machine and scale it in a massive international scale
\\
ML vs Mathematics - Predict people instead of markets\\
\\
Algorithms are nothing more than opinions embedded in code\\
1. Data u train the algorithm on\\
2. Objective / Penalty at stake\\
\\
Make more meals that are success in the past\\
There is an agenda that is imposed, person building the algorithm defines success, but may not be success for the stakeholders
\\
\\
Widespread -
Mysterious - formula is not available, not aware they are being spotted. Constituency right to know what is right \\
Destructive - Ruin people's lives unknowingly, opportunities taken from them, destructive feedback loops
\\
If u label a teacher as bad if the students' proficiency scores were bad
\\
Value added teacher model - It is better if they score more than expected, dinged for the points lower than expected
\\
Students should score higher in their finals, punished for previous teachers' teaching (elevated scores)\\
\\
Teachers should be able to understand how the scoring work\\
Misusing personality tests, algorithms that sort through resumes\\
\\
Looks at who are successful in the past, picking out past patterns and repeating them\\
\\
Non violent crime are more prevalent and predictable
\\
Pre-emptive punishing people for things they haven't done\\
Embedded in a risk score\\
Go back to prison due to high risk scores\\
Same algorithm, different use case can be very different\\
Can be anonymous but related to the person in a precise way\\
Build a model by construction is fair, alter the algorithm for fairness\\
Big data is not a silver bullet, is just a tool\\
How many miles for a self driving car for the teacher evaluation model
\\
Fairness to auditing\\
Restrict people from using decision trees, if u have to explain why\\
When to disclose? when it involves human preference and taste, subject to vetting
\\
Automatic bias, easy to use machine learning. But used only when there is no infrastructure.
\\
Group attribution, generalised to the distribution\\
\\
Implicit bias, not taking into fact of missing information
\subsection*{EM - Expectation Maximization}
Investigate one of the things (latent variables, membership) and hold the others constant (model)\\

\includegraphics[height=7cm]{images/s5.png}
\subsection*{Dimensionality reduction}
May not work well for images\\
Easier means better performance

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\usepackage{pgfplots}
\usepackage{natbib}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{natbib}
\usepackage{graphicx}
\usepackage{minted}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}
\allowdisplaybreaks

\title{CSE546 Machine Learning HW1 - B}
\author{Bobby Deng | 1663039 | dengy7 }
\date{15 April 2020}

\begin{document}
\maketitle

\section*{B.1}
\subsection*{a.}

Intuitively, when the function curve is really smooth and flat, meaning overly simple model, it ideally has low variance and high bias. And when the function curve is overly zigzag, meaning too complex model, it ideally has high variance, and low bias.
In this problem, when step size, m, is small, the curve tends to be smoother. And when step size, m, is big, each interval is big, so the curve tends to be more zigzag.

\subsection*{b.}

Since:
\[\hat{f}_m(x) = \sum_{j=1}^{n/m}  \frac{1}{m}\sum_{i=(j-1)m+1}^{jm}(f(x_i)+\epsilon_i)*1\{x \in (\frac{(j-1)m}{n},\frac{jm}{n}]\}
\]
Let:
\[
A = \frac{1}{n}\sum_{i=1}^{n}(E[\hat{f}_m(x_i)]-f(x_i))^2
\]
So,
\begin{subequations}
\begin{align*}
A & = \frac{1}{n}\sum_{i=1}^{n} (E[\sum_{j=1}^{n/m}\frac{1}{m}\sum_{i=(j-1)m+1}^{jm}(f(x_i)+\epsilon_i)*1\{x \in (\frac{(j-1)m}{n},\frac{jm}{n}]\}]-f(x_i))^2 \\
& = \frac{1}{n}\sum_{i=1}^{n} (\sum_{j=1}^{n/m}(\frac{1}{m}\sum_{i=(j-1)m+1}^{jm}E[(f(x_i)+\epsilon_i)]*1\{x \in (\frac{(j-1)m}{n},\frac{jm}{n}]\}-f(x_i))^2 \\
& \text{ Recall that $\epsilon_i$ is centered around 0 and $f(x_i)$ is fixed.} \\
& = \frac{1}{n}\sum_{i=1}^{n} (\sum_{j=1}^{n/m}\frac{1}{m}\sum_{i=(j-1)m+1}^{jm} f(x_i) * 1\{x \in \frac{(j-1)m}{n},\frac{jm}{n}]\}-f(x_i))^2  \\
& = \frac{1}{n}\sum_{i=1}^{n} (\sum_{j=1}^{n/m}\bar{f}^{(j)} * 1\{x \in \frac{(j-1)m}{n},\frac{jm}{n}]\}-f(x_i))^2  \\
& \shortintertext{Now, for all $x_i$, we only need x in certain j interval, which from (j-1)m+1 to jm.}  \\
& =  \frac{1}{n} \sum_{j=1}^{n/m} \sum_{i=(j-1)m+1}^{jm} (\bar{f}^{(j)} - f(x_i))^2
\end{align*}
\end{subequations}

\subsection*{c.}

1. \newline
From previous question, we know that:
\[
E[\hat{f}_m(x)] = \sum_{j=1}^{n/m} \bar{f}^{(j)} *1\{x \in (\frac{(j-1)m}{n},\frac{jm}{n}]\}
\]
Let
\[A = E[\frac{1}{n} \sum_{I=1}^{N}(\hat{f}_m(x_i) - E[\hat{f}_m(x_i)])^2]\]
Then let's compute something:
\begin{subequations}
\begin{align*}
E[c_j] & = E[\frac{1}{m} \sum_{i=(j-1)m+1}^{jm}y_i] \\
& = E[\frac{1}{m} \sum_{i=(j-1)m+1}^{jm} f(x_i) + \epsilon_i] \\
& = \frac{1}{m} \sum_{i=(j-1)m+1}^{jm} E[f(x_i) + \epsilon_i] \\
& \text{ Recall that $\epsilon_i$ is centered around 0 and $f(x_i)$ is fixed.} \\
& = \frac{1}{m} \sum_{i=(j-1)m+1}^{jm} f(x_i) \\
& = \bar{f}^{(j)}
\end{align*}
\end{subequations}
Now, with $E[c_j] = \bar{f}^{(j)}$, we have:
\begin{subequations}
\begin{align*}
A & = \frac{1}{n} \sum_{I=1}^{N} E[ (\hat{f}_m(x_i) - E[\hat{f}_m(x_i)])^2] \\
& = \frac{1}{n} \sum_{I=1}^{N} E[(\sum_{j=1}^{n/m}  c_j*1\{x \in (\frac{(j-1)m}{n},\frac{jm}{n}]\} - E[\sum_{j=1}^{n/m} c_j *1\{x \in (\frac{(j-1)m}{n},\frac{jm}{n}]\}])^2] \\
& = \frac{1}{n} E[\sum_{j=1}^{n/m} \sum_{i=(j-1)m+1}^{jm} (c_j - E[c_j])^2  ] \\
& = \frac{1}{n} E[\sum_{j=1}^{n/m} \sum_{i=(j-1)m+1}^{jm} (c_j -  \bar{f}^{(j)})^2  ] \\
& = \frac{1}{n} \sum_{j=1}^{n/m} mE[(c_j -  \bar{f}^{(j)})^2]
\end{align*}
\end{subequations}

2. \newline

\begin{subequations}
\begin{align*}
A & = \frac{1}{n} \sum_{I=1}^{N} E[ (\hat{f}_m(x_i) - E[\hat{f}_m(x_i)])^2] \\
& = \frac{1}{n} \sum_{I=1}^{N} E[(\sum_{j=1}^{n/m}  c_j*1\{x \in (\frac{(j-1)m}{n},\frac{jm}{n}]\} - \sum_{j=1}^{n/m} E[c_j] *1\{x \in (\frac{(j-1)m}{n},\frac{jm}{n}]\})^2] \\
& = \frac{1}{n} E[\sum_{j=1}^{n/m} \sum_{i=(j-1)m+1}^{jm} (c_j - E[c_j])^2  ] \\
& = \frac{1}{n} \sum_{j=1}^{n/m} m E[(c_j - E[c_j])^2] \\
& = \frac{1}{n} \sum_{j=1}^{n/m} m  var(c_j) \\
& = \frac{1}{n} \sum_{j=1}^{n/m} m  var(\frac{1}{m} \sum_{i=(j-1)m+1}^{jm}(f(x_i) + \epsilon_i)) \\
& =  \frac{1}{n} \sum_{j=1}^{n/m} m * \frac{1}{m^2} \sum_{i=(j-1)m+1}^{jm}var(f(x_i) + \epsilon_i) \\
& = \frac{1}{n} \sum_{j=1}^{n/m} \frac{1}{m} \sum_{i=(j-1)m+1}^{jm} \sigma^2 \\
& =  \frac{1}{n} \sum_{i=1}^{n} \frac{\sigma^2}{m} \\
& = \frac{\sigma^2}{m}
\end{align*}
\end{subequations}

\subsection*{d.}

\begin{minted}[mathescape,linenos,obeytabs=true,tabsize=2]{python}

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import random

np.random.seed(10)
n = 256
sigma2 = 1
mean = 0
m_list = [1,2,4,8,16,32]
xList = np.array(range(1,n+1))/n

def f(x):
	return 4 * np.sin(np.pi * x) * np.cos(6 * np.pi * x ** 2)

def y_i(x):
	return f(x) +  np.random.normal(0,1)

def cj(m,j):
	sum = 0.0
	for i in range((j-1) * m + 1, j * m + 1):
		sum += y_i(i/n)
	return sum/m

def f_hat(x, m):
	sum = 0.0
	for j in range(1, int(n/m) + 1):
		if ((x > (j -1) *1.0* m /n) and (x <= j*m*1.0/n)):
			sum += cj(m,j)
	return sum

def f_bar(j,m):
	sum = 0.0
	for i in range((j-1)*m+1, j*m + 1):
		sum += f(i/n)
	return sum/m

def bias(m,n):
	output = 0.0
	for j in range(1, int(n/m)+1):
		for i in range((j-1)*m, j*m):
			output += (f_bar(j, m) - f(i/n))**2
	return output/n

def variance(sigma2, m):
	return sigma2 / m

#Initialize
empirical_error = []
bias_list = []
bias_sum = 0.0
variance_list = []
total_error = []

#Start iteration
for m in m_list:
	empirical_error_sum = 0.0
	# empirical_error
	for i in range(1,n+1):
		empirical_error_sum += (f_hat(i/n, m) - f(i/n))**2
	empirical_error.append(empirical_error_sum/n)
	# variance
	bias_list.append(bias(m,n))
	# bias
	variance_list.append(variance(sigma2, m))
	#total error
	total_error = np.array(bias_list) + np.array(variance_list)

plt.plot(m_list,variance_list, label="Average Variance")
plt.plot(m_list,bias_list, label="Average Bias")
plt.plot(m_list,total_error, label="Total Error")
plt.plot(m_list,empirical_error, label="Average Empirical Error")
plt.xlabel("Step Size")
plt.ylabel("Average Error")
plt.legend()
\end{minted}

\includegraphics[width=10cm, height=7cm]{pic_1}

\subsection*{e.}

In the average bias squared expression $\frac{1}{n} \sum_{j=1}^{n/m} \sum_{i=(j-1)m+1}^{jm} (\bar{f}^{(j)} - f(x_i))^2$, we can notice that $\frac{1}{n} \sum_{j=1}^{n/m} \sum_{i=(j-1)m+1}^{jm}$ does not make the total expression much bigger or smaller. It is only doing the summation and then do the average. I will just ignore it for a moment.

From the question, we know that:
\[ min_{i=(j-1)m+1, \dots ,jm} f(x_i) \le \bar{f}_{(j)} \le max_{i=(j-1)m+1, \dots ,jm} f(x_i) \]
Based on the L-Lipschitz rule, we got:

\[|\bar{f}_{(j)} - f(x_i)|  \le  | max_{i=(j-1)m+1, \dots ,jm} f(x_i) - min_{i=(j-1)m+1, \dots ,jm} f(x_i) |\]
\[ |\bar{f}_{(j)} - f(x_i)|  \le \frac{L}{n}|  max_{i=(j-1)m+1} - min_{i=(j-1)m+1} |\]
\[ |\bar{f}_{(j)} - f(x_i)|  \le  \frac{L}{n} |m| \]
Recall that each interval has m elements
\begin{subequations}
\begin{align*}
(\bar{f}_{(j)} - f(x_i))^2 & \le (\frac{L}{n} |m|)^2 \\
& \le \frac{L^2m^2}{n^2}
\end{align*}
\end{subequations}
As for the total error, and recall $\sigma^2 = 1$, \newline
\[ (\bar{f}_{(j)} - f(x_i))^2 + \frac{\sigma^2}{m} \le \frac{L^2(m)^2}{n^2} + \frac{1}{m} \]

Minimizing the total error with respect to m.
\[\frac{dO}{dm} = \frac{2L^2m}{n^2} - m^{-2}\]
\[0 = \frac{2L^2m}{n^2} - m^{-2}\]
\[m = (\frac{n^2}{2L^2})^{\frac{1}{3}}\]

From above, we know that m can not be 0, can not be negative. And it is really intuitive that when m increases, the bias term decrease, and variance increase. It make sense that it need to find a balance point according to the data size. Also we can see that there is a positive relationship between m and n, and there is a negative relationship between n and L.

For this particular scenario, n is 256, and we can see the lowest valley is when m is 8.
\[8 = (\frac{256^2}{2L^2})^{\frac{1}{3}}\]
\[L = 8\]

\section*{B.2}
\subsection*{a.}

\begin{minted}[mathescape,linenos,obeytabs=true,tabsize=2]{python}

import numpy as np
import matplotlib.pyplot as plt
import mnist

mndata = mnist.MNIST('./python-mnist/data/')
X_train, labels_train = map(np.array, mndata.load_training())
X_test, labels_test = map(np.array, mndata.load_testing())
X_train = X_train/255.0
X_test = X_test/255.0
# This function trains the model the return the weights
def train(X, Y):
	lambda_ = 0.0001
	n, d = np.shape(X)
	W = np.linalg.solve(X.T @ X + lambda_ * np.identity(d), X.T @ Y)
	return W

# This function do the prediction
def predict(W,X):
	return (X @ W).argmax(axis = 1)

# This function apply the transformation to data
def h1(X_train, X_test, p):
	n, d = X_train.shape
	sigma = np.sqrt(0.1)
	G = np.random.normal(0, sigma, p * d).reshape(p, d)
	b = np.random.uniform(0, 2 * np.pi, p).reshape(p, 1)
	h_train = np.cos(np.dot(X_train, G.T) + b.T)
	h_test = np.cos(np.dot(X_test, G.T) + b.T)

	return h_train, h_test, G, b

n, d = X_train.shape
training_error_all = []
validing_error_all = []
W_list = []
Gb_list = []
train_index = np.random.choice(np.arange(n), int(X_train.shape[0] * 0.8), replace=False)
valid_index = np.setdiff1d(np.arange(n), train_index)
# loop from p=500 to p=6000, step=500
for p in list(range(500, 6001, 500)):
	h_train, h_test, G, b = h1(X_train[train_index, :], X_train[valid_index, :], p)
	# h = h1(X_train, p)
	# Train test split with 80%-20%
	Gb_list.append((G,b))
	train_data = h_train
	valid_data = h_test
	y_train = np.eye(10)[labels_train[train_index]]

	# Compute weights
	W_hat = train(train_data, y_train)
	W_list.append(W_hat)
	# Compute train predicted
	predict_train = predict(W_hat, train_data)
	predict_train = labels_train[train_index] - predict_train
	train_error_single = np.count_nonzero(predict_train) / len(predict_train) #train_size
	training_error_all.append(train_error_single)
	# Compute test predicted
	predicted_test = predict(W_hat, valid_data)
	predicted_test = labels_train[valid_index] - predicted_test
	valid_error_single = np.count_nonzero(predicted_test) / len(predicted_test)
	validing_error_all.append(valid_error_single)

	print("p: ", p,", train_err: ", train_error_single, ", test_err: ", valid_error_single)

x_index = list(range(500, 6001, 500))
plt.plot(x_index, training_error_all, label="Training Error")
plt.scatter(x_index, training_error_all)
plt.plot(x_index, validing_error_all, label="Testing Error")
plt.scatter(x_index, validing_error_all)
plt.xlabel("P")
plt.ylabel("Prediction Error Rate")
plt.legend()
\end{minted}

\includegraphics[width=10cm, height=7cm]{pic_2}

\subsection*{b.}

From the question, we could have:
\[ \frac{1}{m} \sum_{i=1}^{m} x_i -  \sqrt{\frac{\log(\frac{2}{\delta})}{2m}} \le  \mu \le \frac{1}{m} \sum_{i=1}^{m} x_i +  \sqrt{\frac{\log(\frac{2}{\delta})}{2m}}  \]

And $ \frac{1}{m} \sum_{i=1}^{m} x_i$ is basically the mean test error.

\begin{minted}[mathescape,linenos,obeytabs=true,tabsize=2]{python}
n, d = X_train.shape
p = 6000
dt = 0.05
sigma = np.sqrt(0.1)

h_train, h_test, G, b = h1(X_train, X_test, p)
y_train = np.eye(10)[labels_train]

# Compute weights
W_hat = train(h_train, y_train)
# Compute test predicted
predicted_test = predict(W_hat, h_test)
valid_error_single = 1 - (sum(labels_test == predicted_test) / len(labels_test))

H = np.sqrt(np.log(2/dt)/(2*len(labels_test)))
print(f'The test_error is {valid_error_single}')
print(f'Confidence Interval:[{valid_error_single - H} : {valid_error_single + H}]')
# The test_error is 0.04600000000000004
# Confidence Interval:[0.03241898484259385 : 0.059581015157406235]
\end{minted}

\end{document}


\documentclass[12pt]{article}

\usepackage{amsmath}    % need for subequations
\usepackage{graphicx}   % need for figures
\usepackage{verbatim}   % useful for program listings
\usepackage{color}      % use if color is used in text
\usepackage{subfigure}  % use for side-by-side figures
\usepackage{hyperref}   % use for hypertext links, including those to external documents and URLs

% don't need the following. simply use defaults
\setlength{\baselineskip}{16.0pt}    % 16 pt usual spacing between lines

\setlength{\parskip}{3pt plus 2pt}
\setlength{\parindent}{20pt}
\setlength{\oddsidemargin}{0.5cm}
\setlength{\evensidemargin}{0.5cm}
\setlength{\marginparsep}{0.75cm}
\setlength{\marginparwidth}{2.5cm}
\setlength{\marginparpush}{1.0cm}
\setlength{\textwidth}{150mm}

\begin{comment}
\pagestyle{empty} % use if page numbers not wanted
\end{comment}

% above is the preamble

\begin{document}

\begin{center}
{\large Introduction to \LaTeX} \\ % \\ = new line
\copyright 2006 by Harvey Gould \\
December 5, 2006
\end{center}

\section{Introduction}
\TeX\ looks more difficult than it is. It is
almost as easy as $\pi$. See how easy it is to make special
symbols such as $\alpha$,
$\beta$, $\gamma$,
$\delta$, $\sin x$, $\hbar$, $\lambda$, $\ldots$ We also can make
subscripts
$A_{x}$, $A_{xy}$ and superscripts, $e^x$, $e^{x^2}$, and
$e^{a^b}$. We will use \LaTeX, which is based on \TeX\ and has
many higher-level commands (macros) for formatting, making
tables, etc. More information can be found in Ref.~\cite{latex}.

We just made a new paragraph. Extra lines and spaces make no
difference. Note that all formulas are enclosed by
\$ and occur in \textit{math mode}.

The default font is Computer Modern. It includes \textit{italics},
\textbf{boldface},
\textsl{slanted}, and \texttt{monospaced} fonts.

\section{Equations}
Let us see how easy it is to write equations.
\begin{equation}
\Delta =\sum_{i=1}^N w_i (x_i - \bar{x})^2 .
\end{equation}
It is a good idea to number equations, but we can have a
equation without a number by writing
\begin{equation}
P(x) = \frac{x - a}{b - a} , \nonumber
\end{equation}
and
\begin{equation}
g = \frac{1}{2} \sqrt{2\pi} . \nonumber
\end{equation}

We can give an equation a label so that we can refer to it later.
\begin{equation}
\label{eq:ising}
E = -J \sum_{i=1}^N s_i s_{i+1} ,
\end{equation}
Equation~\eqref{eq:ising} expresses the energy of a configuration
of spins in the Ising model.\footnote{It is necessary to process (typeset) a
file twice to get the counters correct.}

We can define our own macros to save typing. For example, suppose
that we introduce the macros:
\begin{verbatim}
 \newcommand{\lb}{{\langle}}
 \newcommand{\rb}{{\rangle}}
\end{verbatim}
\newcommand{\lb}{{\langle}}
\newcommand{\rb}{{\rangle}}
Then we can write the average value of $x$ as
\begin{verbatim}
\begin{equation}
\lb x \rb = 3
\end{equation}
\end{verbatim}
The result is
\begin{equation}
\lb x \rb = 3 .
\end{equation}

Examples of more complicated equations:
\begin{equation}
I = \! \int_{-\infty}^\infty f(x)\,dx \label{eq:fine}.
\end{equation}
We can do some fine tuning by adding small amounts of horizontal
spacing:
\begin{verbatim}
 \, small space       \! negative space
\end{verbatim}
as is done in Eq.~\eqref{eq:fine}.

We also can align several equations:
\begin{align}
a & = b \\
c &= d ,
\end{align}
or number them as subequations:
\begin{subequations}
\begin{align}
a & = b \\
c &= d .
\end{align}
\end{subequations}

We can also have different cases:
\begin{equation}
\label{eq:mdiv}
m(T) =
\begin{cases}
0 & \text{$T > T_c$} \\
\bigl(1 - [\sinh 2 \beta J]^{-4} \bigr)^{\! 1/8} & \text{$T < T_c$}
\end{cases}
\end{equation}
write matrices
\begin{align}
\textbf{T} &=
\begin{pmatrix}
T_{++} \hfill & T_{+-} \\
T_{-+} & T_{--} \hfill
\end{pmatrix} , \nonumber \\
& =
\begin{pmatrix}
e^{\beta (J + B)} \hfill & e^{-\beta J} \hfill \\
e^{-\beta J} \hfill & e^{\beta (J - B)} \hfill
\end{pmatrix}.
\end{align}
and
\newcommand{\rv}{\textbf{r}}
\begin{equation}
\sum_i \vec A \cdot \vec B = -P\!\int\! \rv \cdot
\hat{\mathbf{n}}\, dA = P\!\int \! {\vec \nabla} \cdot \rv\, dV.
\end{equation}

\section{Tables}
Tables are a little more difficult. TeX
automatically calculates the width of the columns.

\begin{table}[h]
\begin{center}
\begin{tabular}{|l|l|r|l|}
\hline
lattice & $d$ & $q$ & $T_{\rm mf}/T_c$ \\
\hline
square & 2 & 4 & 1.763 \\
\hline
triangular & 2 & 6 & 1.648 \\
\hline
diamond & 3 & 4 & 1.479 \\
\hline
simple cubic & 3 & 6 & 1.330 \\
\hline
bcc & 3 & 8 & 1.260 \\
\hline
fcc & 3 & 12 & 1.225 \\
\hline
\end{tabular}
\caption{\label{tab:5/tc}Comparison of the mean-field predictions
for the critical temperature of the Ising model with exact results
and the best known estimates for different spatial dimensions $d$
and lattice symmetries.}
\end{center}
\end{table}

\section{Lists}

Some example of formatted lists include the
following:

\begin{enumerate}

\item bread

\item cheese

\end{enumerate}

\begin{itemize}

\item Tom

\item Dick

\end{itemize}

\section{Figures}

We can make figures bigger or smaller by scaling them. Figure~\ref{fig:lj}
has been scaled by 60\%.

\begin{figure}[h]
\begin{center}
\includegraphics{figures/sine}
\caption{\label{fig:typical}Show me a sine.}
\end{center}
\end{figure}

\begin{figure}[h]
\begin{center}
\scalebox{0.6}{\includegraphics{figures/lj}}
\caption{\label{fig:lj}Plot of the
Lennard-Jones potential
$u(r)$. The potential is characterized by a length
$\sigma$ and an energy
$\epsilon$.}
\end{center}
\end{figure}

\section{Literal text}
It is desirable to print program code exactly as it is typed in a
monospaced font. Use \verb \begin{verbatim} and
\verb \end{verbatim} as in the following example:
\begin{verbatim}
double y0 = 10; // example of declaration and assignment statement
double v0 = 0;  // initial velocity
double t = 0;   // time
double dt = 0.01; // time step
double y = y0;
\end{verbatim}
The command \verb \verbatiminput{programs/Square.java}\ allows
you to list the file \texttt{Square.java} in the directory
programs.

\section{Special Symbols}

\subsection{Common Greek letters}

These commands may be used only in math mode. Only the most common
letters are included here.

$\alpha,
\beta, \gamma, \Gamma,
\delta,\Delta,
\epsilon, \zeta, \eta, \theta, \Theta, \kappa,
\lambda, \Lambda, \mu, \nu,
\xi, \Xi,
\pi, \Pi,
\rho,
\sigma,
\tau,
\phi, \Phi,
\chi,
\psi, \Psi,
\omega, \Omega$

\subsection{Special symbols}

The derivative is defined as
\begin{equation}
\frac{dy}{dx} = \lim_{\Delta x \to 0} \frac{\Delta y}
{\Delta x}
\end{equation}
\begin{equation}
f(x) \to y \quad \mbox{as} \quad x \to
x_{0}
\end{equation}
\begin{equation}
f(x) \mathop {\longrightarrow}
\limits_{x \to x_0} y
\end{equation}

\noindent Order of magnitude:
\begin{equation}
\log_{10}f \simeq n
\end{equation}
\begin{equation}
f(x)\sim 10^{n}
\end{equation}
Approximate equality:
\begin{equation}
f(x)\simeq g(x)
\end{equation}
\LaTeX\ is simple if we keep everything in proportion:
\begin{equation}
f(x) \propto x^3 .
\end{equation}

Finally we can skip some space by using commands such as
\begin{verbatim}
\bigskip    \medskip    \smallskip    \vspace{1pc}
\end{verbatim}
The space can be negative.

\section{\color{red}Use of Color}

{\color{blue}{We can change colors for emphasis}},
{\color{green}{but}} {\color{cyan}{who is going pay for the ink?}}

\section{\label{morefig}Subfigures}

As soon as many students start becoming comfortable using \LaTeX, they want
to use some of its advanced features. So we now show how to place two
figures side by side.

\begin{figure}[h!]
\begin{center}
\subfigure[Real and imaginary.]{
\includegraphics[scale=0.5]{figures/reim}}
\subfigure[Amplitude and phase.]{
\includegraphics[scale=0.5]{figures/phase}}
\caption{\label{fig:qm/complexfunctions} Two representations of complex
wave functions.}
\end{center}
\end{figure}

We first have to include the necessary package,
\verb+\usepackage{subfigure}+, which has to go in the preamble (before
\verb+\begin{document}+). It sometimes can be difficult to place a figure in
the desired place.

Your LaTeX document can be easily modified to make a poster or a screen
presentation similar to (and better than) PowerPoint. Conversion to HTML is
straightforward. Comments on this tutorial are appreciated.

\begin{thebibliography}{5}

\bibitem{latex}Helmut Kopka and Patrick W. Daly, \textsl{A Guide to
\LaTeX: Document Preparation for Beginners and Advanced Users},
fourth edition, Addison-Wesley (2004).

\bibitem{website}Some useful links are
given at \url{}.

\end{thebibliography}

{\small \noindent Updated 5 December 2006.}
\end{document}


% Created 2018-08-23 jue 15:19
\documentclass[a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usepackage{khpreamble}
\author{Kjartan Halvorsen}
\date{Due 2018-09-07}
\title{Computerized control - Homework 3}
\hypersetup{
  pdfkeywords={},
  pdfsubject={},
  pdfcreator={Emacs 24.5.1 (Org mode 8.2.10)}}
\begin{document}

\maketitle

\section{Polynomial controller design (RST)}
\label{sec-1}

\subsection{Two-degree of freedom controller}
\label{sec-1-1}
\begin{figure}[h]
  \begin{center}
  \begin{tikzpicture}
  \tikzset{node distance=2cm,
      block/.style={rectangle, draw, minimum height=15mm, minimum width=20mm},
      sumnode/.style={circle, draw, inner sep=2pt}
  }

    \node[coordinate] (input) {};
    \node[block, right of=input] (TR) {$F_f(z)=\frac{T(z)}{R(z)}$};
    \node[sumnode, right of=TR, node distance=30mm] (sum) {$\sum$};
    \node[block,right of=sum, node distance=30mm] (plant) {$H(z)$};
    \node[sumnode, right of=plant, node distance=30mm] (sumdist) {$\sum$};
    \node[coordinate, above of=sumdist, node distance=15mm] (dist) {};
    \node[coordinate, right of=sumdist, node distance=15mm] (measure) {};
    \node[coordinate, right of=measure, node distance=10mm] (output) {};
    \node[sumnode,below of=measure, node distance=25mm] (sumnoise) {$\sum$};
    \node[coordinate, right of=sumnoise, node distance=15mm] (noise) {};
    \node[block,left of=sumnoise, node distance=30mm] (SR) {$F_b(z) = \frac{S(z)}{R(z)}$};

    \draw[->] (input) -- node[above, pos=0.2] {$y_{ref}$} (TR);
    \draw[->] (TR) -- node[above] {$u_1$} (sum);
    \draw[->] (sum) -- node[above] {$u$} (plant);
    \draw[->] (plant) -- (sumdist);
    \draw[->] (dist) -- node[at start, right] {$v$} (sumdist);
    \draw[->] (sumdist) -- node[at end, above] {$y$} (output);
    \draw[->] (measure) -- (sumnoise);
    \draw[->] (noise) -- node[at start, above] {$n$} (sumnoise);
    \draw[->] (sumnoise) -- (SR);
    \draw[->] (SR) -| (sum) node[right, pos=0.8] {$u_2$} node[left, pos=0.96] {$-$};
  \end{tikzpicture}
  \caption{Two-degree-of-freedom controller}
  \label{fig:2dof}
  \end{center}
\end{figure}

\subsection{The plant model}
\label{sec-1-2}

A plant to be controlled is described by the pulse-transfer function
\[ H(z) = \frac{\big(1-\frac{\sqrt{3}}{2}\big)z - \big(\frac{\sqrt{3}}{2}-1\big)}{z^2 - \sqrt{3}z + 1}.\]

\subsubsection{Plot the poles and zeros of the system}
\label{sec-1-2-1}

You can use matlab for convenience
\begin{verbatim}
num = []; % numerator coefficients
den = []; % denominator coefficients
h = 1; % Normalized time scale
H = tf(num, den, h)
figure(1); clf;
pzmap(H)
zgrid
\end{verbatim}

\textbf{Include the pole-zero map in your report, and describe briefly some interesting properties of the plant.}

\subsection{Determine the desired closed loop poles}
\label{sec-1-3}
Assume that we want to achieve a closed-loop system from the reference signal $y_{ref}(k)$ to the output $y(k)$ that has two poles that are \textbf{equally fast as the plant}, but has a \textbf{damping ratio of 1} (critically damped poles).

\textbf{Determine where the poles should be in the z-plane and calculate the desired characteristic polynomial \(A_c(z)\).}

\subsection{Design a 2-DoF controller}
\label{sec-1-4}
Design a discrete-time controller with the structure given in figure \ref{fig:2dof}. The controller is given by
\[ R(q)u = -S(q)y + T(q)u_c \]
and the the plant-model is
\[ A(q)y = B(q)u.\]
This gives the following difference equation for the closed-loop system
\[ \big( A(q)R(q) + B(q)S(q) \big) y = B(q)T(q) u_c. \]
Assume a suitable order (as low as possible) of the controller polynomials $R(q)$ and $S(q)$ and solve the diophantine equation
\[ A(q)R(q) + B(q)S(q)  = A_c(q)A_o(q) \]
for $R$ and $S$. Place all observer poles in the origin (deadbeat observer).

\subsection{Implement the controller}
\label{sec-1-5}
Implement your controller and closed-loop system in matlab (or simulink), and test step plots both from the reference signal $y_{ref}(k)$ and from the disturbance $v(k)$.
\textbf{Discuss the performance of the closed-loop system in 3-4 sentences. Is it what you expected?}.

Some code to help you
\begin{verbatim}
% The coefficients of the controller
R_coeffs =
S_coeffs =
T_coeffs =

% The forward part of the controller
TR = tf(T_coeffs, R_coeffs, h);
% The feedback part of the controller
SR = tf(S_coeffs, R_coeffs, h);

% The closed-loop system from reference to output
Hc =

% The closed-loop system from disturbance to output
Hv =

figure(1)
clf
pzmap(Hc) % Verify that the closed-loop poles are as desired

figure(2)
clf
step(Hc, Hv) % Expected performance?
\end{verbatim}
% Emacs 24.5.1 (Org mode 8.2.10)
\end{document}


\documentclass{article}

\renewcommand{\baselinestretch}{1.5}

\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{graphicx}

\author{D. Zack Garza}
\title{A Slightly More Advanced \LaTeX Document}
\date{\today}

\begin{document}

\maketitle
\newpage
\tableofcontents
\newpage

\begin{abstract}
    Most research papers have this section; it's also useful for reports.
\end{abstract}

\section{Modes}
    There are two modes when typing in \LaTeX - text mode (which is what this sentence is in), and math mode.

    Text mode takes most characters literally, so things like x^2 don't quite work.

    Entering math mode can be done by enclosing text in \$ or \$\$, as seen earlier, or by using a special "environment", which we'll see shortly. Then things like $x^2$ work a bit better.

    But don't mix your modes! $Typing text in math mode doesn't turn out well.$ (Not by default anyways)

\section{Symbols}
    Certain characters are reserved for the underlying Tex language. To produce them, you just need to escape them with a backslash.

    \textbackslash (backslash) is used to escape characters

    \textbackslash\textbackslash (double backslash) is used to manually entire a line break

    \textasciitilde (tilde) is used to insert spacing between characters

    \# (number sign) is used for parameters to macros

    \$ (USD) is used to begin and end math mode

    \% (percent) is used to denote a comment

    \& (ampersand) is used to align things

    \_ (underscore) is used for subscripts

    \{ and \} are generally used for grouping things

\section{Text Formatting}
    You can do the standard sort of formatting you'd expect from any word editor:

    \textbackslash textbf{} is used to make words \textbf{bold}.

    \textbackslash textit{} is used to make words \textit{italic}.

    \textbackslash underline{} is used to \underline{underline} words.

\section{Environments}
    Before seeing all of the math syntax we'll need, we have to take a brief interlude to discuss environments. Environments are commands that come in pairs, with a "begin" command that needs to be match to an "end" command.

    We've already seen one example - the document environment, which is where all of our content goes. You can use other environments in the same way; here are a few commonly used examples:

    \subsection{center}
        \begin{center}
            The \textbf{center} environment centers whatever's in it.
        \end{center}

        \subsection{tabular}
        The \textbf{tabular} environment lets you align things in a table:
        \begin{tabular}{ c c c }
          cell1 & cell2 & cell3 \\
          cell4 & cell5 & cell6 \\
          cell7 & cell8 & cell9 \\
        \end{tabular}

        Notes: here, \textbackslash\textbackslash is used to tell the environment where each row ends.

        The tabular environment takes mandatory options to specify how many columns there are, and how they should be aligned.

        Here, the three 'c's denote 3 \textbf{center-aligned} columns.

        You could also choose 'l' for \textbf{left-aligned} or 'r' for \textbf{right-aligned} columns.

        Note that the number of letters in the argument must match the number of columns!

        The \& symbol then functions as the "anchor points" for how the alignment is performed. The point where the 1st \& occurs on each row will all be aligned in a vertical column, and the 2nd and 3rd are all grouped similarly.

    \subsection{center + tabular}
        You can also combine these two environments:

        \begin{center}
            \begin{tabular}{| l | l | l | p{5cm} |}
            \hline
            Day & Min Temp & Max Temp & Summary \\ \hline
            Monday & 11C & 22C & A clear day with lots of sunshine.
            However, the strong breeze will bring down the temperatures. \\ \hline
            Tuesday & 9C & 19C & Cloudy with rain, across many northern regions. Clear spells
            across most of Scotland and Northern Ireland,
            but rain reaching the far northwest. \\ \hline
            Wednesday & 10C & 21C & Rain will still linger for the morning.
            Conditions will improve by early afternoon and continue
            throughout the evening. \\
            \hline
            \end{tabular}
        \end{center}

        Here we just added a few more things: the \textbar~ symbol places vertical bars between each column.

        The "p\{5cm\}" in the last column is another option that styles the contents like a paragraph of text, and allows setting a manual width so the table doesn't overflow the side of the page.

        The \textbackslash hline command creates horizontal lines. \\\\

        \textbf{A Note on Tables:} The table functionality built into \LaTeX is very powerful, but also very tricky to get right. It's often easier to design your table layout in an external editor first, then copy-paste the code into your document.

        One such editor that I recommend is \url{http://www.tablesgenerator.com/}

    \subsection{enumerate}
        Enumerate is used to make \textbf{ordered} lists, and will automatically number the elements.

        \begin{enumerate}
          \item First Item
          \item Another Item
          \item Even more items

          You can also just add text, it'll align to the last "item" command.
        \end{enumerate}

    \subsection{itemize}
        Itemize is used for \textbf{unordered} lists, and will supply bulletsfor each item.

        \begin{itemize}
          \item An Item
          \item An Equally Good item

            You can still add aligned text to any item.

          \item Also a Good Item

        \end{itemize}

    \subsection{equation}
        We saw block-level equations in the last document, but there are several environments that are particularly suited to these - especially if they span multiple lines.
        \begin{equation}
            % Everything in here is automatically in math mode.
            a^2 + b^2 = c^2
            \label{eq:pythagoras} % Here we make up a "shortcut" that we'll refer back to lter
        \end{equation}

        Why is this better than using \$\$?

        Notice the (1) that was added on the right hand side automatically - the equation environment will automatically number your equations, and even let you refer back to individual equations using label names.

        These work automatically using the \textbackslash ref command, i.e. "See Equation (\ref{eq:pythagoras})"

        \newpage

        You can also suppress the automatic numbering using \* in the environment name:

        \begin{equation*}
            % Everything in here is automatically in math mode.
            a^2 + b^2 = c^2
            \label{eq:pythagoras2}
        \end{equation*}

        This is also useful for easily grouping together a result or calculation and, for example, boxing it:

        \begin{equation}
         \boxed{x = \pm \sqrt{z^2 - y^2}}
        \end{equation}

        However, this environment isn't the best for multi-line equations. For those, you'll want to import the AMSMath package, which provides:

    \subsection{align}
        This functions almost exactly like \textbf{equation} by default, but supports multiple lines.
        \begin{align}
         f(x) &= (x+a)(x+b) \\
              &= x^2 + (a+b)x + ab
        \end{align}

        You can still suppress numbering altogether with \*:

        \begin{align*}
         f(x) &= (x+a)(x+b) \\
              &= x^2 + (a+b)x + ab
        \end{align*}

        Or prevent numbering on individual lines:

        \begin{align}
         f(x) &= (x+a)(x+b) \nonumber \\
              &= x^2 + (a+b)x + ab
        \end{align}

    \subsection{figure}
        The figure environment is what you'll use just about any time you want to include an external image. It allows you to supply captions, and automatically handles numbering. Note that if your images are larger than the page, they will overflow! So it's a good habit to always supply the desired width.

        \begin{figure}
          \caption{Noncontractible paths on a torus.}
            \includegraphics[width=\textwidth]{ending-3} % This does the magic of including the image
        \end{figure}

        Note that the image doesn't always go exactly where you wrote it in the document - behind the scenes, Latex makes some default decisions about where images should be placed to keep the text flowing smoothly.

        You can control the placement and sizing a bit:
        \begin{figure}[h] % h denotes "here"
        \centering
          \caption{Noncontractible paths on a torus.}
            \includegraphics[width=0.5\textwidth,keepaspectratio]{ending-3}
        \end{figure}

\section{Etc}

    \subsection{Footnotes}
        You can create a footnote at any time using the \textbackslash footnote command, and it will be placed roughly on the same page as the other text surrounding it. The command will insert a hyperlink wherever you place it, though, with the number automatically assigned.\footnote{\label{myFootnoteName}I'm all the way down here!}

        You can also give it a label name (much like for equations), and refer to it later: "See footnote~\ref{myFootnoteName}"

    \newpage

    \subsection{Bibliography}
    Once you have the bibliography defined, you can reference it with the cite command. For example, see \cite{sourceName2} for more information.

\begin{thebibliography}{1}
  \bibitem{sourceName1} Amazing Source #1: www.source1.com
  \bibitem{sourceName2} Amazing Source #2: www.source2.com
\end{thebibliography}

\end{document}


%
% Appendix B
%

\chapter{Misidentification rate method}
\label{fakefactor}

The misidentified lepton background is dominant in hadronic channels, and it is estimated using the misidentification rate method discussed in Chapter~\ref{bkg_est}. The systematic uncertainties associated with estimating the misidentified lepton background impact the median expected limits significantly. To recap, we are using a normalization uncertainty of 30\% for the misidentified lepton background along with a 10\% normalization uncertainty uncorrelated across the categories of each channel. Similarly, the shape uncertainties are estimated in a \PW boson enriched control region, and these uncertainties have a high impact and are constrained by the data. To improve the analysis's sensitivity, we need a better handle on the misidentified lepton background and its associated uncertainties. An improvement to the misidentification rate method is presented here.

Since the jet flavor composition of different background processes is different, leading to different probabilities of these jets getting misidentified as \tauh, the misidentification rate is measured separately for each dominant process. The dominant background processes are \wjets, QCD multijet, and \ttbar. First, determination regions are defined, each enriched in one of the above backgrounds. From each determination region, one extracts a dedicated misidentification rate, which is parametrized by its main dependencies to reduce statistical fluctuation. Further, so-called closure corrections in important variables not used in the misidentification rates parametrization are calculated. Contaminations in the different determination regions other than the process of interest are subtracted from data by using MC simulation and embedding samples. The misidentification rate is applied on an event-by-event basis as a weighted sum in the so-called application region. The application region is equivalent to the ``background-like'' region and signal region is equivalent to the ``signal-like'' region defined in Chapter~\ref{bkg_est}. The weights applied to each misidentification rate contribution are called fractions and represent the probability of the event being of a particular background process. Fractions are determined from MC simulation and embedding samples. Figure~\ref{fig:ff} shows a graphical summary of the misidentification rate method.

\begin{figure}[htbp]
  \centering
  \includegraphics[width=0.9\textwidth]{plots/appendix/FF.png}
  \caption{Illustration of the misidentification rate method. For each of the processes (\wjets, QCD, and \ttbar), a dedicated misidentification rate is measured in a process enriched determination region. The measured misidentification rate is corrected and then applied on an event by event basis inside the application region. The application of the misidentification rate inside the application region also involves the fractions $f_i$, representing the probabilities of an event in the application region originating from process $i$.}
  \label{fig:ff}
\end{figure}

The basic assumption of the misidentification rate method is the so-called universality. The misidentification rate measured in a determination region is the same as if measured in the signal region or any other phase space region. Universality is not strictly given because by measuring the misidentification rate in a determination region, one introduces biases—for example, the jet-flavor composition changes in the determination region with respect to the signal region. Therefore, the misidentification rate method also implements process-dependent bias corrections to restore the misidentification rate universality. In summary, the misidentification rate is measured in dedicated determination regions and parametrized in variables reflecting its most dominant dependencies. Afterward, corrections are derived for improving the modeling of the jet-\tauh misidentified contribution in variables not used for the parametrization. A last step bias correction is calculated, accounting for possible differences between the determination region where the misidentification rate is measured and the application region where the misidentification rate is applied.

A future Higgs boson LFV decays search can benefit from using this method for calculating the misidentified lepton background in hadronic channels. The corrections used for improving the modeling and the bias corrections can give a better description of the misidentified lepton background in hadronic channels. This method has been successfully incorporated in the SM \Htt analyses~\cite{CMS:2019pyn}.


\subsection{Signal Extraction}
Once the excess noise has been diminished, the gravitational wave signal needs to be identified and pulled. Excess power and Template matching are some of the more frequently used methods to identify and extract the signals.

\subsubsection*{Excess power method}

The “excess power” method is much enhanced when several detectors are employed. With it, the data streams from each observatory are searched for signals that are not easily accounted for by the noise characteristics of that particular instrument. When such interesting signals are found, corresponding signals, using an appropriate time window, are searched for in the data from the other observatories. Essentially, the signals from the different detectors are cross-correlated with each other. Since the noise in each experiment is uncorrelated with that of the others, a real signal should give a large spike in the correlation statistic. Complicating the analysis is the sensitivity of the detectors to the direction to the source, which can weaken the signal in one observatory relative to the others. But this effect can be taken into account and poses no serious problem.

\subsubsection*{Template matching}

Another method to find gravitational waves is to look for signals that coincide with events that are visible using other means, and that should also emit gravitational waves. The first step in computing a template signal is computing the gravitational wave signature of different astrophysical sources. While template matching is a powerful way to extract a gravitational wave signal from the noise, it only works for sources that can be easily modelled. \\

The models compared to the LIGO data are called phenomenological models, and they are fit to numerical simulations of systems created by solving the Einstein equation on supercomputers. A smaller number of simulations are computed, and then an analytic model is created that links one simulation to another. A set of freely adjustable parameters is used with these models that allow them to match all of the available numerical simulations and to interpolate between them. There are several models that are used for this. Each uses a slightly different method, and so they produce slightly different wave forms. The implied properties of the modelled systems also differ as a result, but only in small ways. However, template matching alone would result in a lot of spurious matches and random data fluctuations.

\begin{figure}[h]
    \centering
    \includegraphics[scale=0.92]{images.tex/template_matching.jpg}
    \caption{Matching the received signal using Gravitational waveform as a template.\\ Source :- \href{https://kiss.caltech.edu/workshops/LISA/presentations/Babak.pdf}{Introduction to Data Analysis of Gravitational Wave Signals by Stanislav Babak}}
\end{figure}

Therefore, we need to apply a “SNR” filter13, taking only the samples above some threshold value of the signal-to-noise ratio. In the LIGO data, it turns out that if one sample has a high SNR, then it is usually surrounded by neighbors that also have high SNR. Most of these are false positives and LIGO performs yet another cut, keeping only the single highest SNR candidate in each such cluster. This further reduces the size of the data set. After that, the software looks for coincidence between the two LIGO antennas. From the reduced data set, a statistic called the log likelihood ratio, or LLR is computed . The larger the LLR for a candidate, the more likely it is to be caused by a signal rather than noise, and vice versa. When a real signal is present in the data, it is generally surrounded by a large number of candidates which are the result of matches by similar-shaped templates to the matching one. This has been determined by many runs in which simulated waveform data have been injected into the data stream to test the software. \\

This allows candidates to be clustered, similar to the way the SNR threshold peaks were clustered; in this case, if a candidate falls within 4 seconds of another candidate that has a higher LLR, the weaker candidate is discarded. It turns out that there is a high probability of finding a low-ranked candidate next to a high ranked one, and so this method successfully trims many low ranked candidates from the sample. However, for candidates with LLR larger than about 6, this clustering method is not effective because the probability of two such candidates being within 4 seconds of one another so low: the recent data run produced one candidate like this about every 5 minutes, or 10, 000 of them for the entire run. The total sample in the data set has now been reduced from 500 million per second to only 10, 000 total, a small enough sample that it is possible to perform detailed statistical testing on each one of them.\\

Once the GW signal is extracted, then it's frequency will be changed by multiplying it with a conversion ratio, such that the resulting frequency lies in the audible range. On September 14 2015, the first characteristic `chirp' sound of the GW150914 was decoded using LIGO.

\pagebreak


\chapter{Robust estimation of location and scatter}
\label{chap:mv}

\begin{itemize}
\item
    Minimum covariance determinant (MCD) and minimum volume ellipsoid (MVE) estimators of location and scatter
\item
    Possibly: Other multivariate location and scatter estimators such as the
    Stahel-Donoho estimate
\item
    S-estimators and MM-estimators of location and scatter
\end{itemize}

\endinput