\setcounter{chapter}{-1}
\chapter{Getting Started}
%\addcontentsline{toc}{chapter}{Interface}

\objective{An introduction to scientific computing using Python.}

%Latter half of paragraph needs rewording.  Feels rather awkward to read.
This course focuses on the use of computers in solving mathematical problems.  In today's world, many problems that we wish to solve are so large and complex that to solve them by hand would be infeasible.  To be able to effectively use computers to solve these huge problems is becoming a highly demanded skill.  As you work through these labs, you will learn how to use computers to work through complicated problems.  We can say that computation is the muscle of applied mathematics.

These courses will use a powerful yet simple to use programming language called Python as an interface to optimized numerical libraries.  This chapter will walk you through setting up a scientific computing environment based on Python.  All the libraries are freely available on the internet as open source software.

Computation is the muscle of applied mathematics. Regardless of the field of study, inevitably there is a need to utilize computers to answer real world questions.  There exist a variety of tools to tackle these questions.  This course will focus on the use of Python, a powerful, general purpose programming language, to answer these questions.  There are three core libraries for Python that make it viable for scientific computing.  These libraries are SciPy, NumPy, and Matplotlib.  These libraries provide access to many optimized algorithms and to well known libraries such as LAPACK.

\section*{Getting Started}
We will set up our Python environment with the following libraries.
\begin{itemize}
\item Python 2.5 or greater
\item NumPy and SciPy
\item Matplotlib
\item IPython
\item A good editor for editing Python files
\end{itemize}

\noindent A list of editors for python scripts can be found at \url{http://wiki.python.org/moin/PythonEditors}

\subsection*{Python}
\url{http://www.python.org}

Let's begin by setting up Python.  This text recommends the use Python 2.7, but any Python version greater than 2.5 should work just as well.  Download and install the appropriate installation file for your platform.  If you are running Linux, this step is usually not necessary as most Linux distributions ship with Python pre-installed.  NOTE: On Linux, it is highly recommended you install software from your distribution's package repositories.

\subsection*{Numpy and Scipy}
Numpy: \url{http://numpy.scipy.org/}

\noindent Scipy: \url{http://www.scipy.org/}

Numpy is a fundamental library for scientific computating.  It adds the following features to Python.
\begin{itemize}
\item Efficient N-dimensional arrays
\item Very optimized functions designed for working with large data sets
\item High precision calculations
\item Array language features (like MATLAB)
\end{itemize}

SciPy is another python library that uses NumPy to offer the following additional features.
\begin{itemize}
\item Advanced math (signal processing, optimization, statistics, etc)
\item Very optimized functions for large data sets
\item Scikits (similar to toolboxes for MATLAB)
\end{itemize}

We can obtain both libraries from their respective websites.  Be sure to download the version that matches your version of Python.  For instance, if you installed Python 2.7.1, then you will need the numpy-xxxxx-py27.exe install file.  Installation should be quick and easy as NumPy and Scipy will automatically detect your Python installation and configure themselves accordingly.

\subsection*{Matplotlib}
\url{http://matplotlib.sourceforge.net/}

Matplotlib is a 2D plotting library for Python.  It produces publication quality plots and graphs.
For plotting, we will use the Matplotlib library.  The procedure for installing Matplotlib is very similar to that of NumPy and SciPy.  Again, make sure the version you're downloading matches the version of Python that you have installed.

\subsection*{IPython}
\url{http://ipython.org/}

Finally, we will need to install IPython.  IPython can be downloaded from their website.  Users of Windows will also need to download and install the library PyReadline (a straightfoward process that matches the previous two installations).

For those who wish escape the technical details of configuring your own environment, there are several pre-configured python environments available.  One such environment is Enthought.  It offers a free edition that includes these libraries and more.  Another environment is Python(x,y).  These environments include everything needed for this course and much more.  For Linux and Mac OSX users, SAGE also provides a pre-configured environment that includes these libraries.

One additional tool that we will mention is the Spyder Scientific Computing environment.  This tool provides an interface similar to MATLAB.  Though not required for this course, it is worth looking at.  It requires additional libraries to be installed.

\section*{Introducing Python}
Python syntax is very powerful, but is fun and easy to learn.  One day devoted to studying the syntax to Python will be well worth your time.  You should be able to learn and understand most python syntax after a single day.  At the end of this chapter are several helpful resources for learning python.

\subsection*{Data Types}
Computer programs store and manuipate data.  This data is stored in various forms that makes it easy to work with.  Python has native support for several powerful data types.
%Python is dynamically typed.

\subsubsection*{Numeric and Boolean Types}
Python has native support for integers, floats, and complex numbers.  The standard arithmetic operations are supported for each type.
\begin{lstlisting}[style=python]
 a = 15         #an integer
 b = 15.0       #a float
 c = 15+1j      #complex number
\end{lstlisting}

Boolean types have only two possible values: \li{True} or \li{False}.

\subsubsection*{Container Types}
Powerful container types add to the flexibility of Python.  The container types include:
\begin{itemize}
 \item Tuples
 \item Lists
 \item Dictionaries
 \item Sets
\end{itemize}

\paragraph{Tuples}
Tuples are just like lists except that tuples cannot be have items added or removed.  Once created, a tuple can never change.  Why use tuples over lists?  Tuples have less overhead than lists.

Once a tuple is defined, it cannot be changed (this is referred to as an immutable sequence).  They are represented by parentheses.  Lists are also sequences of objects, but lists are mutable, or changeable.  Lists are represented by square brackets.  Dictionaries store objects in key-value pairs.  Dictionaries are represented by curly braces.  Below are a few examples.
\begin{lstlisting}[style=python]
#Tuples
t = (3,5,6,2, 'this is a tuple') #a tuple with four integers
l = [3,'this is a list', 5 10]
d = {'name':'dictionary', 'value':'Hi There'}
t[0]    #returns 3
l[-1]   #returns 10 (the last item)
d['name']       #returns 'dictionary'
\end{lstlisting}
Note that indexing in Python starts with zero.  That is, the first of item is located at index 0.  Python also allows us to conveniently work backwards from the end of the list using negative indices.

\paragraph{Lists}
Lists are like arrays in that they store ordered sequences of objects.  This datatype is Python's workhorse datatype.  They are incredible useful and efficient.  Mastery of lists will greatly increase your productivity in Python.  The arrays that we will be using later in this course follow the same conventions as lists in Python.  So master lists, and you will be confident working with NumPy arrays!  To create a list we use square brackets around a comma separated list of objects.  They objects can be of any type (they don't all even have to be of the same type).  We initialize a list with 4 objects: an integer, a string, a float and a boolean.
\begin{lstlisting}[style=python]
a_list = [3, 'string', 50.0, True]
\end{lstlisting}
Now, how do we gain access to these elements in the list?  Lists are indexed with integer indices just like arrays. So \li{a_list[0]} will return the first object in the list (the integer \li{3}).  Lists can also be accessed from the end using negative indices (\li{a_list[-1]} returns the boolean \li{True}).

Accessing single elements of the list is great, but what if we what a subset of the list?  This is also very simple.  We can request a subset of any list in Python by slicing the list.  We slice a list using the form \li{a_list[start:end]}.  This will return another list containing the elements of the original list beginning with \emph{start} up to, but not including \emph{end}.  When \emph{start} is 0 or \emph{end} is the -1, we can omit them such as \li{a_list[:3]} and \li{a_list[1:]} because the are implied.  To make a copy of the entire list we can do \li{a_list[:]}.

Now that we can create and slice lists, how do we change the contents of a list?  This is also very simple to do.
\begin{lstlisting}[style=python]
alist = []      #begin with empty list
alist = alist+[1, True] #alist = [1, True]
alist.append(5)           #alist = [1, True, 5]
alist.insert(0,'hi')      #alist = ['hi', 1, True, 5]
alist.extend([0,1,2])     #alist = ['hi', 1, True, 5, 0, 1, 2]
\end{lstlisting}
This listing show how we can add two lists together, append an item at the end, insert an item at a certain index, or extend a list with another list.

Removing items from a list is simple too!  Consider the following listing.
\begin{lstlisting}[style=python]
#alist = ['hi', 1, True, 5, 0, 1, 2]
alist.remove(1)
#alist = ['hi', True, 5, 0, 1, 2]
alist.remove(1)
#alist = ['hi', True, 5, 0, 2]
alist.pop(0)    #returns the item it removed from the list
#alist = [True, 5, 0, 2]
\end{lstlisting}
Those are the basics of Python lists.

\paragraph{Dictionaries}
Dictionaries store data in key-value pairs.  The keys of a dictionary must be unique.  The values can be any data type.

\paragraph{Sets}
Sets are a collection of unordered, unique items.  Very close to the mathematical definition of a set.

\subsubsection*{Functions}
Python functions are defined by the \li{def} keyword.  Notice the colon after the function definition.  This colon tells python to expect a block of code to follow.  Spacing is very important in Python.  Where other languages use brackets to enclose blocks of code, Python uses spacing to define blocks of code.  As such, Python will complain if spacing is not correct or is inconsistent.  This language feature forces correct indentation, making Python code very readable.  Here's an example of a python function.
\begin{lstlisting}[style=python]
def myFunction():
   print "You called?"

myFunction()	#prints: You called? to the console.
\end{lstlisting}

\subsubsection*{Loops and Conditional Statements}
Python also two forms of loops.  The \li{while} loop will execute as long as its test condition evaluates to \li{True}.  The \li{for} loop is used for iterating through lists, tuples, and dictionaries.
\begin{lstlisting}
a = 0
while a is not 10:
   a = a + 1
   print a
print "I'm outside the while loop"
# while loop will continue until a is 10, then print: I'm outside the while loop

for a in [6,5,4,3,2,1,0]:
   print a
   if a == 2:
       print "This is a 2"	#executes if a is 2
   elif a is 5:
       print "I'm a 5 now"	#executes if a is 5
   else:
       print "Still looking..."	#executes if a is anything else
print "I'm outside the for loop"
\end{lstlisting}

\section*{Testing our environment}
We will walk you through some quick tests to demonstrate a working environment.

Start by typing \li{ipython} in your command line.
\begin{lstlisting}
$ ipython
Python 2.7.2 (default, Jun 29 2011, 11:17:09)
Type "copyright", "credits" or "license" for more information.

IPython 0.11 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:
\end{lstlisting}

Our cursor will be waiting for us to type something.  Lets start by loading the SciPy library.  If we have everything set up correctly, SciPy will import correctly and we will be presented with another prompt.
\begin{lstlisting}[style=python]
In [1]: import scipy

In [2]:
\end{lstlisting}

Let's try making an array.  This will also signal that NumPy is working correctly because arrays are defined in NumPy (NumPy is automatically imported by SciPy).
\begin{lstlisting}[style=python]
In [2]: A = scipy.array([[3,3,3],[2,2,2],[1,1,1]]); print A
Out[2]:
array([[3, 3, 3],
       [2, 2, 2],
       [1, 1, 1]])
\end{lstlisting}

IPython allows us to access the help pages for any function in a quick and intuitive way.  Simply pose your command as a question by appending a \li{?} at the end of the command.  To view the source code of the function (if available), use two question marks. Let's read the help page for scipy.eye. (Note: Outside of IPython, the command is \li{help(scipy.eye)}.)
\begin{lstlisting}
In [3]: scipy.eye?
String Form:   <function eye at 0x8762064>
Namespace:        Interactive
File:             /usr/lib/python2.7/site-packages/numpy/lib/twodim_base.py
Definition:       scipy.eye(N, M=None, k=0, dtype=<type 'float'>)
Docstring:
    Return a 2-D array with ones on the diagonal and zeros elsewhere.

    Parameters
    ----------
    N : int
      Number of rows in the output.
    M : int, optional
      Number of columns in the output. If None, defaults to `N`.
    k : int, optional
      Index of the diagonal: 0 (the default) refers to the main diagonal,
      a positive value refers to an upper diagonal, and a negative value
      to a lower diagonal.
    dtype : data-type, optional
    Returns
    -------
    I : ndarray of shape (N,M)
      An array where all elements are equal to zero, except for the `k`-th
      diagonal, whose values are equal to one.

    See Also
    --------
    identity : (almost) equivalent function
    diag : diagonal 2-D array from a 1-D array specified by the user.

    Examples

    Examples
    --------
    >>> np.eye(2, dtype=int)
    array([[1, 0],
           [0, 1]])
    >>> np.eye(3, k=1)
    array([[ 0.,  1.,  0.],
           [ 0.,  0.,  1.],
           [ 0.,  0.,  0.]])
\end{lstlisting}

\section*{Using Python}
Now that we have a working environment, let's learn how to use in the context of this course.

We will be writing and executing many programs written in Python.  How do we accomplish this?  Python programs are contained in files using a *.py extension.  Any number of functions can be contained in these files.  They are executed from the commandline using the following command (where \li{program.py} is the name of the program we want to execute):
\begin{lstlisting}
$ python program.py
\end{lstlisting}

We can treat our programs as libraries (as you will see near the end of this chapter with the benchmarking function).

\section*{Using SciPy}
SciPy is organized into base library with other libraries to handle special features.  We can import these other libraries whenever we need to use them.  Most of the functions we use in this book come from the scipy, linalg, and sparse libraries. The different libraries available in SciPy are:
\begin{lstlisting}
scipy.cluster: Clustering package
scipy.constants: Constants
scipy.fftpack: Fourier transforms
scipy.integrate: Integration and ODEs
scipy.interpolate: Interpolation
scipy.io: Input and output
scipy.linalg: Linear algebra (imported as la)
scipy.maxentropy: Maximum entropy models
scipy.misc: Miscellaneous routines
scipy.ndimage: Multi-dimensional image processing
scipy.odr: Orthogonal distance regression
scipy.optimize: Optimization and root finding
scipy.signal: Signal processing
scipy.sparse: Sparse matrices (imported as spar)
scipy.sparse.linalg: Sparse linear algebra (imported as sparla)
scipy.spatial: Spatial algorithms and data structures
scipy.spatial.distance: Distance computations
scipy.special: Special functions
scipy.stats: Statistical functions
scipy.weave: C/C++ integration
\end{lstlisting}

When we use SciPy in book, it will always be imported as follows. This python statement imports all the main SciPy methods for use and make them available in the \li{sp} object.
\begin{lstlisting}
import scipy as sp
\end{lstlisting}

To use the SciPy's linear algebra library, we use the following import statement.  After this statement, all the linear algebra methods of SciPy will be available under the \li{la} object.
\begin{lstlisting}
import scipy.linalg as la
\end{lstlisting}

\section*{Matplotlib}
SciPy does not provide methods for generating graphics (plots, graphs, etc.).  Matplotlib is a libary written for Python to provide a MATLAB like plotting environment.  If you are familiar with plotting in MATLAB, Matplotlib should be easy to use.  In this course we will be importing Matplotlib as follows.  Again note that after importing \li{matplotlib.pyplot}, the methods will available under the \li{plt} object.
\begin{lstlisting}
import matplotlib.pyplot as plt
\end{lstlisting}

\section*{A Note on Floating Point Arithmetic}
In mathematics we expect that the difference between two equal quantities to be zero.  For example, $10 - 10 = 0$.  floating point arithmetic isn't always so precise - altering the previous expression to $e^{log(10)} - 10 = 0$ changes nothing on paper, but try putting it into IPython:

\begin{lstlisting}
: 10 - 10
0
: scipy.exp(scipy.log(10))-10
1.7763568394002505e-15
\end{lstlisting}

This is due to floating point arithmetic. Floating point numbers do not have arbitrary precision: they only occupy a certain amount of memory.  For numbers with long decimal expressions or irrational numbers, there will inevitably be a loss of precision because of this memory constraint.  Although these errors will generally be small it is important to understand this limitation of numerical computation. The intricacies of floating point arithmetic will be further developed in Volume II of this work.

Another thing to watch out for is integer division.  It is common in programming to return an integer when dividing two integers.  Type \li{a \= 1/2} into IPython.  What do you expect the value of \li{a} to be?  If you're thinking 0.5, that is incorrect.  Type \li{print a}.  Notice that it returns \li{0}.  This is because both \li{1} and \li{2} are integers, so dividing them produces \li{0}.  To get 0.5 (a floating point number), we need to tell Python that it should treat \li{1} or \li{2} as a float.  Type \li{b = 1.0/2; print b}.  Now the result is 0.5.
There are two ways to force Python to always return a float when dividing.  First we can run a script and pass the \li{-Qnew} option to the Python interpreter.  Second, we can import the following at the top of each script where we wish to use only true division.
\begin{lstlisting}
from __future__ import division
\end{lstlisting}

%\section*{Timing Code}
%As a final note, there are several methods for timing code. This will be important, since throughout this book we stress understanding the intrinsic complexity of different methods for solving problems. We have provided a \li{timer} class (which you will have to download), that makes the python timing interface easier to use.

\section*{Benchmarking}
Before we continue, there is one more thing we need to discuss.  Throughout this book, you will be asked to compare execution times of different pieces of code.  IPython allows us to easily do this from the console using the \li{\%timeit} command.
\begin{lstlisting}
: %timeit [a**3 for a in range(30)]
100000 loops, best of 3: 11.5 us per loop
\end{lstlisting}
The \li{\%timeit} command wraps a Python module called \li{timeit}.  This module will execute a statement many times, timing it each time, and report the best execution time.  The reason for multiple timed executions is to account for random system variances which may affect the performance of function.  Also note that \li{timeit} will disable Python's garbage collector during the tests (the garbage collector is how Python automatically manages memory).  We see above that out of 3 million executions (100000 loops repeated 3 times), the fastest time per loop was 11.5 microseconds.  We can even time the execution of functions.

The problem with \li{\%timeit} is that it is only defined inside ipython.  The authors of this book have written a Python module that wraps \li{timeit} into an easy to use Python context.  Please save the following code into an easily accessible file.

\lstinputlisting[style=python, style=fromfile, name=mytimer.py]{mytimer.py}

The reader is not expected to understand the above function (though it would be worthwhile study it).  It uses a feature of Python called contexts.  Consider the following code.
\begin{lstlisting}
from mytimer import timer
with timer() as t:
    t.time(myFunction, arguments)
    t.printTimes()
\end{lstlisting}

Python contexts are used via the \li{with} keyword.  In the above code, we initialize the \li{timer()} context which returns to us a \li{timer()} object bound to the variable \li{t}.  Inside the block, we can execute anything.  If we wish to time a specific function we call the \li{time()} method of \li{t}.  The \li{time()} method accepts a function and its parameters and returns the best execution time (in the form of a string) in the following form:
\begin{lstlisting}
myFunction finished in 0.00283s (1 loops, repeated 3 times): 0.00283s per loop (with gc.disable())
\end{lstlisting}

\section*{Helpful Resources}
\subsubsection*{Python}
\noindent \emph{Dive into Python}:  This python book is a great resource for beginning to understand python.  It is available for for free download. \url{http://diveintopython.org/}

\noindent \emph{Non-Programmer's Tutorial for Python}: \url{http://en.wikibooks.org/wiki/Non-Programmer\%27s_Tutorial_for_Python}

\noindent \emph{The Python Tutorial}: Written by the creator of Python. \url{http://docs.python.org/tutorial/}

\subsubsection*{NumPy and SciPy}
\noindent \emph{NumPy Reference Manual}: \url{http://docs.scipy.org/doc/numpy-1.6.0/reference/}

\noindent \emph{SciPy Reference Manual}: \url{http://docs.scipy.org/doc/scipy-0.9.0/reference/}

\noindent \emph{NumPy for MATLAB Users}: Comparing and contrasting Python and MATLAB. \url{http://www.scipy.org/NumPy_for_Matlab_Users}

\noindent \emph{MATLAB to NumPy translation table}: A table with MATLAB commands and their equivalent NumPy functions.   \url{http://mathesaurus.sourceforge.net/matlab-numpy.html}

\noindent \emph{NumPy and SciPy Cookbook}: \url{http://www.scipy.org/Cookbook}

\noindent \emph{Introduction to Arrays}: Very informative introduction the SciPy arrays. \url{http://pages.physics.cornell.edu/~myers/teaching/ComputationalMethods/python/arrays.html}

\subsubsection*{Matplotlib}
\noindent \emph{Matplotlib User Guide}: \url{http://matplotlib.sourceforge.net/contents.html}

\noindent \emph{Matplotlib Examples}: Useful for seeing the python code that generate certain plots. \url{http://matplotlib.sourceforge.net/examples/index.html}


\section{Introduction}
The standard model of particle physics
(SM) is one of the most precise theories to this date.
Although the SM describes the interaction of the fundamental particles
with high precision, it is known to have limitations.
Neither can it explain the matter-antimatter asymmetry in the universe,
nor does it include a description of interactions at the Planck-scale.
Extensions of the SM including new interactions and particles are called
New Physics (NP).
In the SM the three leptons
$e, \mu, \tau$ only differ by their mass.
In all other properties they are interchangeable,
because they transform the same under the $\symup{SU(3)\times SU(2)\times U(1)}$
gauge group of the SM.
This Lepton Flavor Universality (LFU)
is a key prediction of the SM.

Recent measurements of the LHCb experiment hint at deviations from LFU, which would be a clear sign of NP effects.
There are theoretical models to explain LFU in beyond standard model processes, such as lepto-quarks (LQ) which
directly couple leptons to quarks.

\subsection{\bkll decays}
In the SM some decay rates can be predicted with high accuracy.
The \bkll and similar decays are mediated by $b\to s \ell^+\ell^-$
quark transitions. They contain a hadron in the final state and
are forbidden at tree level in the SM.
The lowest order diagram contributing in the SM is a
penguin diagram as seen in \autoref{fig:SMbkll}.
For that reason the branching fraction of that process
is suppressed on the order of
$\mathcal{O}(10^{-6})$ \cite{Zyla:2020zbs}.
This allows to observe possible NP effects,
where this decay can happen
on tree-level.
An example of such a NP process is the
lepto-quark mediated diagram in \autoref{fig:BSMbkll}.

\begin{figure}
	\centering
	\begin{tikzpicture}
		\begin{feynman}
			\vertex (b1) {$\overline b$};
			\vertex [right=of b1] (b2);
			\vertex [right=of b2] (b3);
			\vertex [right=of b3] (b4) {$\overline s$};

			\vertex [above=.8 of b1] (u1) {$u$};
			\vertex [above=.8 of b4] (u2) {$u$};

			\vertex at ($(b2)!0.5!(b3)!1!-90:(b3)$) (g1);
			\vertex [below=1.5 of b3] (g2);
			\vertex [below=1.2 of b4] (l1) {\(\ell^{+}\)};
			\vertex [below=.6 of l1] (l2) {\(\ell^{-}\)};

			\diagram* {
			(u1) -- [fermion] (u2),
			(b1) -- [anti fermion] (b2) -- [boson, edge label={$W^+$}] (b3) -- [anti fermion] (b4),
			(b2) -- [anti fermion, bend right] (g1) -- [anti fermion, bend right] (b3),
			(g1) -- [photon, bend right, edge label'={$\gamma/Z^0$}] (g2),
			(l1) -- [fermion, bend right] (g2) -- [fermion, bend right] (l2),
			};

		\end{feynman}
		\draw[decorate, decoration={brace, amplitude=5pt},line width=1pt](b1.south west) --node[left] {$B^+ \ $} (u1.north west);
		\draw[decorate, decoration={brace, amplitude=5pt},line width=1pt](u2.north east) --node[right] {$\ K^+ \ $} (b4.south east);
		\node [above=0.15 of g1]  {$\overline u, \overline c, \overline t$};
	\end{tikzpicture}
	\caption{\label{fig:SMbkll} SM lowest order Feynman diagram of the \bkll decay.}%
\end{figure}

\begin{figure}
	\centering
	\begin{tikzpicture}
		\begin{feynman}
			\vertex (b1) {$\overline b$};
			\vertex [right=of b1] (b2);
			\vertex [right=of b2] (b3);
			\vertex [right=of b3] (b4) {$\overline s$};

			\vertex [above=.8 of b1] (u1) {$u$};
			\vertex [above=.8 of b4] (u2) {$u$};

			\vertex [below=1.2 of b4] (l1) {\(\ell^{+}\)};
			\vertex [below=.7 of l1] (l2) {\(\ell^{-}\)};

			\diagram* {
			(u1) -- [fermion] (u2),
			(b1) -- [anti fermion] (b2) -- [scalar, edge label={$LQ$}] (b3) -- [anti fermion] (b4),
			(l1) -- [fermion, bend left] (b3),
			(l2) -- [fermion, bend left] (b2),
			};

		\end{feynman}
		\draw[decorate, decoration={brace, amplitude=5pt},line width=1pt](b1.south west) --node[left] {$B^+ \ $} (u1.north west);
		\draw[decorate, decoration={brace, amplitude=5pt},line width=1pt](u2.north east) --node[right] {$\ K^+ \ $} (b4.south east);
	\end{tikzpicture}
	\caption{\label{fig:BSMbkll} NP example of the \bkll decay with lepto-quark interaction.}%
\end{figure}

\section{Lepton Flavor Universality Tests}
In the last decade multiple tensions with SM predictions were observed in $b\to s \ell^+\ell^-$ transitions.
These deviations in observables from SM predictions, are called \enquote{flavor anomalies}.
This phenomenon recently reached the $\num{3}\sigma$ level, based on proton-proton collision data
at the LHCb detector.
Since calculations in quantum chromodynamics (QCD) rely on lattice-QCD predictions, because
of the non-pertubative nature of QCD,
the resulting uncertainties in QCD effects are high.
Because changing leptons does not affect QCD calculations because of LFU,
it is possible to define observables, which are independent from QCD uncertainties,
as the uncertain values cancel out. One class of theoretically clean observables is \cite{Hiller2004}
\begin{equation*}
	R_{H}:= \frac{\int^{q_{\mathrm{max}}^2}_{q_{\mathrm{max}}^2}\frac{\dif \mathcal{B}\l(B\to H\mu^+\mu^-\r)}{\dif q^2} \dif q^2}{
		 \int^{q_{\mathrm{max}}^2}_{q_{\mathrm{max}}^2}\frac{\dif \mathcal{B}\l(B\to He^+e^-\r)}{\dif q^2} \dif q^2
	} \overset{\mathrm{SM}}{\approx} 1.
\end{equation*}
$\mathcal{B}$ is the branching ratio of the given process, where $H$ can be a hadron ($H=K$ in the discussed analysis)
or even a collection of particles. The differential branching fractions are
integrated over the invariant mass squared of the two leptons
in the \emph{same} region $q^2~\in~(q_{\mathrm{min}}^2,~q_{\mathrm{max}}^2)$.
These observables are free from QCD uncertanties.
For example in the SM without radiative (QED) corrections
$R_{K}=\num{1.00030}_{-\num{0.00007}}^{+\num{0.00010}}$
for $q^2\in(1, 6)\si{\giga\electronvolt}$
has a relative uncertainty in the order of
$\mathcal{O}(10^{-4})$\cite{Hiller}.
Adding QED corrections (which are relevant only because
of the different electron and muon mass)
the full uncertainty of $R_K$ is at the order
of $\mathcal{O}(\SI{1}{\percent})$ \cite{Bordone2016}.
It follows that a deviation from unity in this value
is a strong indication for NP.
Until this year only a part of Run 2
LHCb data was analysed corresponding (together with Run 1) to
an integrated luminosity of
\SI{5}{\femto\barn\tothe{-1}}.
Shown in \autoref{fig:RKearlier} and \autoref{fig:RKstarearlier}
part of the LHCb dataset already showed a small
deviation from unity at the $2\sigma$ level.
The $q^2$ value is always chosen to be smaller than
\SI{6}{\giga\electronvolt} in this analysis, to reduce contributions
from the $J/\psi$ resonance.
\begin{figure}
	\centering
	\includegraphics[width=0.8\linewidth]{media/RK.png}
	\caption{Measurements of $R_K$ \cite{petridis2021test}. The LHCb measurement with Run 1 and 2015/16 data corresponds to \SI{5}{\femto\barn\tothe{-1}}.
		It hints at SM tension at the $2\sigma$ level.
\vspace{-1em}
		}%
	\label{fig:RKearlier}
\end{figure}
\begin{figure}
	\centering
	\includegraphics[width=0.8\linewidth]{media/RKstar.png}
	\caption{Measurements of $R_{K^{*0}}$ \cite{petridis2021test}. The LHCb measurement corresponds to  \SI{3}{\femto\barn\tothe{-1}}.
		It hints at SM tension around the $2\sigma$ level.}%
	\label{fig:RKstarearlier}
\end{figure}

\section{$\bfiftextbf{R_K}$ with the full LHC\lowercase{b} dataset}
This year the remaining Run 2 LHCb data recorded in 2017 and 2018
was added to the earlier \SI{5}{\femto\barn\tothe{-1}}.
This adds \SI{4}{\femto\barn\tothe{-1}} to the dataset,
roughly doubling the amount of observed decays.

%The same analysis strategy as in the smaller dataset is used and the
%analysis is double checked with well known observables.

It was already established in the earlier section that $R_K$ provides
a theoretically clean prediction.
The experimental measurement of $R_K$ is challenging because
of the different behavior of electrons and muons in the detector.
The muons pass the detector nearly unaffected and are stopped at the end
in the muon stations.
Electrons on the other hand radiate bremsstrahlung and for that
reason their energy reconstruction is more difficult
and the mass resolution is poorer.
Most electrons emit a energetic bremsstrahlungs photon before the magnet and
are then curved and emit more photons downstream. The photons are detected by the
electromagnetic calorimeter (ECAL).
This situation is illustrated
in \autoref{fig:bremsstrahlung}.
The lost energy can be added back by choosing
the clusters directed towards the electron direction before the magnet ($E_{T}>\SI{75}{\mega\electronvolt}$).
Even after this recovery the electron mass distribution is still not as sharp as the muon one, which can
be seen in \autoref{fig:masses}.
More differences stem from differences in the trigger, particle identification and tracking efficiency.
Controlling the experimental differences is key to the analysis strategy.\vspace{-2em}
\begin{figure}
	\centering
	\includegraphics[width=0.6\linewidth]{media/electronbrems.png}
	\caption{Reconstruction of the electron energy at LHCb with bremsstrahlung \cite{petridis2021test}.}%
	\label{fig:bremsstrahlung}
\end{figure}
\begin{figure}[h]
	\centering
	\hspace{0.5em}\includegraphics[width=0.83\linewidth]{media/Mmumu.png}
	\includegraphics[width=0.8\linewidth]{media/Mee.png}
	\caption{Invariant mass distribution of the final state particles in the muonic decay (top) and the electronic decay (bottom) \cite{petridis2021test}.}%
	\label{fig:masses}
\end{figure}

\subsection{The double ratio strategy}
To suppress systematic effects coming from electron/muon differences
the branching ratios can be measured relative to well understood control channels.
The channels of choice are $B^+\to K^+J/\psi(\to\ell^+\ell^-)$ due to their high similarity
with the channel of interest. They share identical selections except the cut on $q^2$.
Furthermore the $J/\psi$ mode is known to obey LFU within a precision of  $\mathcal{O}(10^{-4})$ \cite{Moise:2021nje}.
The experimental observable can be defined as
\begin{align}
	\label{eqn:rkexp}
	R_K & = \l .\frac{\mathcal{B}\l(B\to K^+\mu^+\mu^-\r)}{\mathcal{B}\l(B\to K^+J/\psi\l(\mu^+\mu^-\r)\r)} \r/
	\frac{\mathcal{B}\l(B\to K^+e^+e^-\r)}{\mathcal{B}\l(B\to K^+J/\psi\l(e^+e^-\r)\r)} \nonumber               \\
	    & = \l . \frac{N_{\mu}}{\epsilon_\mu}\frac{\epsilon_{J/\psi\l(\to\mu\r)}}{N_{J/\psi\l(\to\mu\r)}} \r/
	\frac{N_{e}}{\epsilon_e}\frac{\epsilon_{J/\psi\l(\to e\r)}}{N_{J/\psi\l(\to e\r)}}.
\end{align}
Where $\epsilon$ are the efficiencies of the indicated decays and $N$ their yields.

The invariant mass distribution of the final state particles is fitted to get the yields
and the efficiencies are calculated using simulated events.
Some aspects are modeled imperfectly by the simulation,
and for that reason the efficiencies need calibration, which
is done with control channels in data.

\subsection{Selection and backgrounds}
To remove background in the dataset
selections have to be applied.
Removing backgrounds decays with the same (detectable)
final state can be done by applying mass vetoes.
For example the decay $B^+\to~\anti {D^0}(\to~K^+e^-\nu)e^+\anti \nu$
has the same final state, since the neutrinos are undetectable.
To remove those decays the invariant mass is constrained to
fulfill the condition $m(K^+ e^-) > m(D^0)$.
Another type of background is misidentified background, where e.g.
pions get identified as electrons.
Such background can be removed by requiring a high value for the identification
probability of the electron (electron PID).
Combinatorial background, where unrelated events are counted towards
a signal event,
can be removed via a multivariate selection which takes multiple
variables at the same time into account.
This is usually done via machine learning techniques like
a boosted decision tree (BDT).

Even more backgrounds can be suppressed by
choosing a $m(K^+\ell^+\ell^-)$ window.
This is shown
in \autoref{fig:backgrounds}
where multiple simulated backgrounds can be seen.
The dominant ones are the $J/\psi$ channel which leaked
into the invariant mass region of interest and the
partially reconstructed decay $B\to K^+ \pi^-e^+e^-$, where the pion
is not reconstructed.

The simulation is calibrated through control data
for trigger efficiency, PID efficiency, $B^+$ kinematics and
the resolutions of $q^2$ and $m(K^+e^+e^-)$.
\begin{figure}
	\centering
	\includegraphics[width=0.8\linewidth]{media/backgrounds.png}
	\caption{Simulated background distributions after applied cuts \cite{petridis2021test}. The dotted line is the lower bound on the selection window.}%
	\label{fig:backgrounds}
\end{figure}

\subsection{Cross-checks}
To verify the experimental procedure
multiple cross-checks on other variables are performed.
The efficiencies are tested with
\begin{equation*}
	r_{J/\psi} = \frac{\mathcal{B}\l(B^+\to K^+J/\psi\l(\mu^+\mu^-\r)\r)}{\mathcal{B}\l(B^+\to K^+J/\psi\l(e^+e^-\r)\r)}
\end{equation*}
which is a factor in \autoref{eqn:rkexp} and the equality to unity is known within \SI{0.4}{\percent}.
Since this requires
the control of muons relative to electrons, which is a complicated procedure for the already mentioned reasons, this test is not trivial at all,
but a very strict one.
The measured value is
\begin{equation*}
	r_{J/\psi} = \num{0.981+-0.020}.
\end{equation*}
This check is performed for the new and old datasets as well as with different trigger configurations.
Another test is to look at $r_{J/\psi}$ in different kinematic regions, since it should always be compatible with 1.
This is done for two kinematic varibales in \autoref{fig:kinematic}. Since the distribution is
flat, we are confident that the efficiencies are understood.

\begin{figure}
	\centering
	\includegraphics[width=\linewidth]{media/kinematic.png}
	\caption{Cross check of $r_{J/\psi} = 1$ throughout the phase space \cite{lhcbcollaboration2021test}.
	On the right the binning in the space of the maximal lepton momentum vs the opening angle between the leptons is shown.
	The bins are chosen to contain approximately the same number of simulated $B^+ \to K^+ J/\psi(e^+ e^-)$ events.
	On the left the flat distribution of $r_{J/\psi}$ is visible.}%
	\label{fig:kinematic}
\end{figure}

To validate the double-ratio procedure, it is performed on the $B^+\to K^+\psi(2S)(\to\ell^+\ell^-)$ decay.
The value
\begin{align*}
	R_{\psi\l(2S\r)}
	&= \l .\frac{\mathcal{B}\l(K^+\psi\l(2S\r)\l(\mu^+\mu^-\r)\r)}{\mathcal{B}\l(K^+J/\psi\l(\mu^+\mu^-\r)\r)} \r/
	\frac{\mathcal{B}\l(K^+\psi\l(2S\r)\l(e^+e^-\r)\r)}{\mathcal{B}\l(K^+J/\psi\l(e^+e^-\r)\r)} \nonumber
	\\ &= \num{0.997+-0.011}
\end{align*}
is consistent with unity in the $1\sigma$ interval. This can additionally be interpreted
as a strong LFU test in $\psi(2S)\to \ell^+\ell^-$.
\vspace{-1em}

\subsection{Systematic uncertainties}
The dominant systematic uncertainties ($\approx \SI{1}{\percent}$)
are due to the choice of the fit model
and the statistics of the calibration samples used to understand the backgrounds.
There are smaller systematic sources of uncertainty ($\approx~{1}$~\textperthousand)
like the dependency of the definition, the precision of $q^2$ and $m(K^+e^+e^-)$ smearing
factors.
The total relative systematic uncertainty in the final $R_K$ measurement is \SI{1.5}{\percent}
and is expected to be dominated by the statistic uncertainties.

\subsection{Measuring $\bfiftextbf{R_K}$}
$R_K$ is determined as a fit parameter connecting both distributions of
\autoref{fig:masses}.
The fit is performed as an extended unbinned maximum likelihood fit.
%Extended means that the yields are a fit parameter.
With this $R_K$ is measured to be
\begin{equation*}
	R_K = \num{0.846}_{-0.039}^{+0.042}(\text{stat})_{-0.012}^{+0.013}(\text{syst}).
\end{equation*}
This gives a $p$-value under SM hypothesis of \num{0.0010},
which corresponds to evidence of LFU violation at $3.1\sigma$.
Using previous measurements of $\mathcal{B}(B^+\to K^+\mu^+\mu^-)$
it is possible to calculate $\mathcal{B}(B^+\to K^+e^+e^-)$:
\begin{equation*}
	\frac{\dif \mathcal{B}}{\dif q^2} = \l(28.6_{-1.4}^{+1.5}\l(\text{stat}\r)\pm 1.4\l(\text{syst}\r)\r)\cdot 10^{-9} \si{c\tothe{4}\per \giga\electronvolt\tothe{2}}.
\end{equation*}
This value implies that electrons seem to behave more SM-like than muons as can be seen in \autoref{fig:lhcbfinal}.
\begin{figure}
	\centering
	\includegraphics[width=0.8\linewidth]{media/lhcbfinal.png}
	\caption{Differential branching fraction $\mathcal{B}(B^+\to K^+\ell^+\ell^-)$ comparing measured results with SM predictions \cite{petridis2021test}.}%
	\label{fig:lhcbfinal}
\end{figure}

\section{Conclusion}
The updated measurement of $R_K$ with the full LHCb dataset
suggests violation of lepton flavor universality at $3.1\sigma$.
This forms a consistent picture of lepton flavor anomalies,
making future investigations of LFU in these decays very important.
To have a better understanding of this phenomenon more data of
LHC Run 3 and more analyses in other channels are needed.
An independent confirmation of this result by other LHC detectors and
Belle 2 is needed.


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{enumitem}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{color}
\usepackage{booktabs}
\usepackage[T1]{fontenc}

% macro to select a scaled-down version of Bera Mono (for instance)
\makeatletter
\newcommand\BeraMonottfamily{%
  \def\fvm@Scale{0.85}% scales the font down
  \fontfamily{fvm}\selectfont% selects the Bera Mono font
}
\makeatother

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\BeraMonottfamily\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=left,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2
}
\lstset{style=mystyle}

\title{CS249 Fall 2020\\
       Problem Set 1: Statistical Inference}
\author{Christopher Munoz Cortes}
\date{October 26, 2020}

\usepackage{natbib}
\usepackage{graphicx}

\begin{document}

\maketitle

\section{Maximum Likelihood Estimation}
\begin{enumerate}[label={(\alph*)}]
    \item Assume we have $n$ positive data points $Y_1,...,Y_n \sim$
    Exponential$(\theta)$. Compute the maximum likelihood estimator for $\theta$.

    We know that the probability density function of an exponential
    distribution is given by
    \begin{align*}
        f(y;\theta) =
        \begin{cases}
            \theta e^{-\theta y} & y \geq 0 \\
            0 & \text{o.w.}
        \end{cases}
    \end{align*}
    The likelihood function of this distribution is
    \[
        \mathcal{L}(\theta) = \prod_{i=1}^{n} \theta e^{-\theta y_i} =
        \left(\theta e^{-\theta y_1}\right) \left(\theta e^{-\theta
        y_2}\right) ...\left(\theta e^{-\theta y_n}\right) =
        \theta^n e^{-\theta\sum_{i=1}^{n}y_i}
    \]
    The MLE of this exponential distribution can be calculated taking the
    derivative of the log-likelihood function (which is easier to compute
    than the likelihood function) and setting it equal to zero.
    \begin{align*}
        \dfrac{d}{d\theta}\left[ln{\mathcal{L}(\theta)}\right]  &= 0 \\
        \dfrac{d}{d\theta} \left[\ln{\left(\theta^n e^{-\theta \sum_{i=1}^{n}
        y_i}\right)}\right]  &= 0 \\
        \dfrac{d}{d\theta}\left[n\ln{\theta} - \theta \sum_{i=1}^{n}
        y_i\right] &= 0 \\
        \dfrac{n}{\theta} - \sum_{i=1}^{n}y_i &= 0
    \end{align*}
    Solving for the $\theta$:
    \begin{equation*}
        \boxed{\theta = \dfrac{n}{\sum_{i=1}^{n} y_i}}
    \end{equation*}
    \item Assume we have $n$ positive data points $Y_1,...,Y_n \sim$
    Uniform$(0, \theta)$, meaning the data is coming from a uniform distribution in the
    interval $[0,\theta]$. Compute the maximum likelihood estimator for $\theta$.

    The pdf of $Y \sim$ Uniform$(0,\theta)$ is
    \begin{equation}
        f(y;\theta) =
        \begin{cases}
            \frac{1}{\theta} & 0 \leq y \leq \theta \\
            0 & \text{o.w.}
        \end{cases}
    \end{equation}
    Now consider a fixed value of $\theta$. If $Y_i > \theta$ for some $i$, then
    $f(Y_i;\theta)$ = 0 and therefore $\mathcal{L}(\theta)$ = 0. This means that if any
    $Y_i > 0$, $\mathcal{L}(\theta) = 0$. This can be written as $\mathcal{L}(\theta) = 0$, if $\theta < Y_n$, where $Y_{(n)} = \max\{Y_1,\ldots,Y_n\}$.
    On the other hand, if $Y_{(n)} \leq \theta$, $f(y;\theta) = 1/\theta$ and consequently $\mathcal{L}(\theta) = 1/\theta^n$
    Hence,
    \begin{equation}
            \mathcal{L}(\theta) =
        \begin{cases}
           \frac{1}{\theta^n} & \theta \geq Y_{(n)} \\
           0 & \theta < Y_{(n)}
        \end{cases}
    \end{equation}
    Finally, since $\mathcal{L}(\theta)$ is strictly decreasing in $[Y_{(n)}, \infty)$, the maximum value occurs at $Y_{(n)}$ and $\hat{\theta} = Y_{(n)}$.

    \item Estimate bias, variance, and RMSE for the estimator in (b) when $\theta = 10$
    and $n=100$ by doing simulations in Python.
    \begin{lstlisting}[language=Python, caption=Code for part (c)]
import numpy as np
np.random.seed(0)

# Estimate bias, variance, and RMSE for a estimator from part (b)
# for theta=10, n=100
low = 0
theta = 10
n=100
theta_hats = []

def question_one(low, theta, n):
  theta_hats = []
  for i in range(1000):
    y = np.random.uniform(low=0, high=theta, size=n)
    theta_hat = np.max(y)
    theta_hats.append(theta_hat)
  theta_hats = np.array(theta_hats)

  # Calculate bias, variance, and RMSE of the estimator theta_hat
  # To calculate the bias, take the mean of the theta_hats as the expectation
  bias = np.mean(theta_hats) - theta
  variance = np.var(theta_hats)
  mse = bias**2 + variance
  rmse = np.sqrt(mse)
  return bias, variance, rmse

bias, variance, rmse = question_one(low, theta, n)
\end{lstlisting}

Output:
    \begin{itemize}
        \item bias$(\hat{\theta})$: -0.100
        \item Var$(\hat{\theta})$: 0.00829
        \item RMSE$(\hat{\theta})$: 0.135
    \end{itemize}

    \item What are the estimated values in (c) if $n$ increases to 500? Describe your observations.
    If $n$ increases from 100 to 500, $\hat{\theta}$ starts to converge towards $\theta$ and the ``spread'' of the distribution begins to shrink, as evidenced by the decreasing variance and RSME. In other words, $\hat{\theta}$ becomes a better estimator of $\theta$.
    \begin{itemize}
        \item bias$(\hat{\theta})$: -0.0193
        \item Var$(\hat{\theta})$: 0.000360
        \item RMSE$(\hat{\theta})$: 0.0271
    \end{itemize}
\end{enumerate}
\pagebreak

\section{Bootstrap}
\begin{enumerate}[label={(\alph*)}]
    \item Assume we have a sample of 20 IDD data points with the following values:

    3.0 1.9 6.4 5.9 4.2 6.2 1.4 2.9 2.3 4.8 7.8 4.5
    0.7 4.4 4.4 6.5 7.6 6.1 2.7 1.6

    Assume we define $T$ as the median among 20 data points. Use bootstrap
    to estimate the standard error and the confidence interval for $T$.
    \begin{itemize}
        \item Bootstrap standard error: 0.733
        \item Bootstrap 95\% confidence interval: (2.964, 5.836)
    \end{itemize}
    \begin{lstlisting}[language=Python, caption=Standard Error and Confidence Interval for $T$]
# Create array with data
data = np.array([3.0, 1.9, 6.4, 5.9, 4.2, 6.2, 1.4, 2.9, 2.3, 4.8, 7.8, 4.5,
                 0.7, 4.4, 4.4, 6.5, 7.6, 6.1, 2.7, 1.6])

# Create the bootstrap datasets and calculate the median for each
t_boot_list = [np.median(np.random.choice(data, len(data), replace=True))
               for _ in range(1000)]

# Calculate the standard error and the confidence interval
t_boot_se = np.std(t_boot_list)
ci_lower = np.median(data) - 1.96*t_boot_se
ci_upper = np.median(data) + 1.96*t_boot_se
    \end{lstlisting}

    \item Use \lstinline{y = np.random.normal(0, 5, 100)} to generate 100 data points from
    the normal distribution $N(0,5)$. Consider the generated data points as your observed data.
        \begin{itemize}
            \item Apply the bootstrap method to estimate the standard error for
            $T_1$ and $T_2$, where $T_1$ is the sample median and $T_2$ is the maximum
            value in the sample.
            \begin{itemize}
                \item $T_1$ Bootstrap standard error: 0.504
                \item $T_2$ Bootstrap standard error: 1.680
            \end{itemize}

            \item Next, compute the actual standard error for $T_1$ and $T_2$ by simulating
            many times from the data source (i.e., $N(0, 5)$).
            \begin{itemize}
                \item Actual standard error by sim from source for $T_1$: 0.623
                \item Actual standard error by sim from source for $T_2$: 2.196
            \end{itemize}
        \end{itemize}
\begin{lstlisting}[language=Python, caption=Bootstrap and Actual Standard Error]
# Generate new data points. This is the observed data.
y = np.random.normal(0,5,100)

# Apply the bootstrap method to estimate
# T1: sample median
# T2: max value
t1_boot_list = [np.median(np.random.choice(y, len(y), replace=True))
               for _ in range(1000)]
t1_boot_se = np.std(t1_boot_list)

t2_boot_list = [np.max(np.random.choice(y, len(y), replace=True))
                for _ in range(1000)]
t2_boot_se = np.std(t2_boot_list)

# Compute the actual standard error for T1 and T2 by simulating many times
# from the data source
sim_t1_list = [np.median(np.random.normal(0,5,100)) for _ in range(1000)]
sim_t1_se = np.std(sim_t1_list)

sim_t2_list = [np.max(np.random.normal(0,5,100)) for _ in range(1000)]
sim_t2_se = np.std(sim_t2_list)
\end{lstlisting}

\end{enumerate}
\pagebreak

\section{Parametric Bootstrap}
\begin{enumerate}[label={(\alph*)}]
    \item First, we need a parametric distribution model for the data. Let’s
    assume the data points in part (a) are generated from a normal distribution
    with the mean of $\theta$ and the standard deviation of 2. Using
    the observed data, compute $\hat{\theta}$, the estimated value of the $\theta$ in this
    distribution model.

    We can compute $\hat{\theta}$ using MLE:
    \begin{align*}
        \mathcal{L}(\theta,\sigma) &= \prod_i
        \dfrac{1}{\sqrt{2\pi\sigma^2}}\exp{\left\{-\dfrac{1}{2\sigma^2}
        \left(X_i - \theta \right)^2\right\}} \\
        \mathcal{L}(\theta,\sigma) &=
        \dfrac{1}{\sqrt{(2\pi\sigma^2)^n}}\exp{\left\{-\dfrac{1}{2\sigma^2}
        \sum_i{\left(X_i - \theta \right)^2} \right\}}
    \end{align*}
    Taking the log on both sides,
    \begin{align*}
        \ln\mathcal{L}(\theta,\sigma) &= -\dfrac{n}{2}\ln 2\pi\sigma^2 -
        \dfrac{1}{2\sigma^2} \sum_i{(X_i - \theta)^2}
    \end{align*}
    Taking the partial derivative with respect to $\theta$,
    \begin{equation*}
        \dfrac{\partial}{\partial \theta} \ln \mathcal{L}
        (\theta, \sigma) =
        \dfrac{1}{\sigma^2} \sum_i (X_i - \theta) =
        \dfrac{1}{\sigma^2} n(\bar{x} - \theta) \\
    \end{equation*}
    Setting equal to 0 and solving for $\theta$,
    \begin{equation*}
        \boxed{\hat{\theta} = \bar{x}}
    \end{equation*}
    Consequently, $\boxed{\hat{\theta} = \bar{x} = 4.265}$.

    \item Generate bootstrap samples (20 data points each) from the distribution
    with the estimated parameter (i.e., $N(\hat{\theta},2)$).

    \texttt{See listing below for code}

    \item In each bootstrap sample, estimate the value of $\theta$ again based on the
    simulated data.

    \texttt{See listing below for code}

    \item Compute the standard deviation for the estimated parameter among
    the bootstrap samples. This standard deviation is the estimated standard
    error for $\theta$.

    The estimated standard error for $\theta$ was 0.453.

\begin{lstlisting}[language=Python, caption=Parametric Bootstrap code]
# Compute theta_hat for this distribution model (using MLE)
theta_hat = np.mean(data)
print(f"Estimator for the mean of theta (theta_hat): {theta_hat}")

# Generate bootstrap samples, i.e. simulate the data from source
sim_data = [np.random.normal(theta_hat,2,100) for _ in range(1000)]

# Estimate the value of theta_hat in each bootstrap sample
theta_hats = [np.mean(sim_sample) for sim_sample in sim_data]

# Compute the standard error fot theta_hat
theta_hat_se = np.std(theta_hats)
print(f"Standard error for theta_hat: {theta_hat_se}")
\end{lstlisting}
\end{enumerate}
\pagebreak

\section{Bayesian Inference}
\begin{enumerate}[label={(\alph*)}]
    \item Assume we have observed IID data points $X_1,\ldots,X_{10}$ from the distribution
    $N(\theta,1)$, where the sample average, $\bar{X}$, is 1.68. If our prior
    belief about $\theta$ can be described by $N(0,3)$, compute the posterior distribution
    of $\theta$ after observing this data.
%    \begin{table}[h]
%        \centering
%        \begin{tabular}{@{}cccc@{}}
%        \toprule
%        Parameter & \texttt{mean} & \texttt{se\_mean} & \texttt{sd} \\
%        \midrule
%        $\theta$ & 1.33 & 0.01 & 0.39 \\
%        $\sigma$ & 1.24 & 0.01 & 0.35 \\ \bottomrule
%        \end{tabular}
%    \end{table}
    We know that the posterior distribution is given by
    \begin{align*}
        f(\theta|X_1,\ldots,X_{10}) \propto \mathcal{L}(\theta)f(\theta)
    \end{align*}
    \begin{align*}
        \mathcal{L}(\theta)f(\theta) &= \prod_{i=1}^{n} \dfrac{1}{\sqrt{2\pi\sigma^2}}
        \exp{\left\{-\dfrac{(X_i - \theta)^2}{2\sigma^2}\right\}}
        \dfrac{1}{\sqrt{2\pi\sigma_0^2}} \exp{\left\{ - \dfrac{(\theta -
        \theta_0)^2}{2\sigma_0^2}\right\}} \\
        &= \dfrac{1}{\sqrt{(2\pi\sigma^2)^n}}\exp{\left\{-\dfrac{1}{2\sigma^2}
        \sum_i{\left(X_i - \theta \right)^2} \right\}}
        \dfrac{1}{\sqrt{2\pi\sigma_0^2}} \exp{\left\{ - \dfrac{(\theta -
        \theta_0)^2}{2\sigma_0^2}\right\}} \\
        &= \dfrac{1}{(2\pi)^\frac{n+1}{2} \sqrt{\sigma^{2n} \sigma_0^2}}
        \exp{\left\{ \dfrac{\theta^2 + 2\theta \theta_0 - \theta_0^2}{2\sigma_0^2}
        -\sum_{i=1}^n \dfrac{X_i^2 2\theta X_i + \theta^2}{2\sigma^2}\right\}} \\
        &\propto \exp{\left\{ \dfrac{-\theta^2\sigma^2 + 2\theta\theta_0\sigma^2
        - \theta_0^2\sigma^2 - (\sum_i X_i^2\sigma_0^2 - -2\sum_i X_i\theta\sigma_0^2 +
        n\theta^2\sigma_0^2)}{2\sigma_0^2\sigma^2}\right\}} \\
        &\propto \exp{\left\{ \dfrac{-\theta^2(\sigma^2 + n\sigma_0^2) +2\theta(\theta_0 \sigma^2
        + \sum_i X_i\sigma_0^2) - (\theta_0^2\sigma^2 + \sum_i X_i^2 \sigma_0^2)} {
        2\sigma_0^2 \sigma^2} \right\}} \\
        \begin{split}
            &\propto \exp{\left\{ \dfrac{-\theta^2 + 2\theta \left(\dfrac{\theta_0\sigma^2 +
            \sum_i X_i\sigma_0^2}{\sigma^2 + n\sigma_0^2}\right) -
            \left(\dfrac{\theta_0^2\sigma^2 + \sum_i X_i^2 \sigma_0^2}{\sigma^2 +
            n\sigma_0^2}\right)^2}{\dfrac{2\sigma_0^2\sigma^2}{\sigma^2 +
            n\sigma_0^2}}\right\}} \\
            &\qquad\qquad \times
            \exp{\left\{-\dfrac{\theta_0^2\sigma^2 + \sum_i X_i^2 \sigma_0^2}
            {2\sigma_0^2\sigma^ r}\right\}}
        \end{split}\\
        &\propto \exp{\left\{-\dfrac{\left( \theta -
        \dfrac{\theta_0^2\sigma^2 + \sum_i X_i^2 \sigma_0^2}{\sigma^2
        + n\sigma_0^2}\right)^2}{2\dfrac{\sigma_0^2 \sigma^2}{\sigma^2 +
        n\sigma_0^2}}\right\}}
    \end{align*}
    Here we can define:
    \[
        \sigma_1^2 = \dfrac{\sigma_o^2 \sigma^2}{\sigma^2 + n\sigma_0^2} =
        \dfrac{1}{\frac{\sigma^2 + n\sigma_0^2}{\sigma_0^2\sigma^2}} =
        \dfrac{1}{\frac{1}{\sigma_0^2} + \frac{1}{\frac{\sigma^2}{n}}} =
        \left( \dfrac{1}{\sigma_0^2} + \dfrac{1}{\frac{\sigma^2}{n}} \right)^{-1}
    \]
    and
    \[
        \theta_1 = \dfrac{\theta_0\sigma^2 + \sum_i X_i \sigma_0^2}{\sigma^2 + n\sigma_0^2} =
        \dfrac{\theta_0\sigma_0^{-2} + \sum_i X_i \sigma^{-2}}{\sigma_0^{-2} + n\sigma^{-2}} =
        \sigma_1^2 \left(\theta_0 \sigma_0^{-2} + \sum_i X_i \sigma^{-2}\right) =
        \sigma_1^2 \left(\dfrac{\theta_0}{\sigma_0^2} + \dfrac{\bar{X}}{\frac{\sigma^2}{n}}\right)
    \]
    which shows that the resulting expression for the posterior distribution corresponds to a
    normal distribution with mean $\theta_1$ and standard deviation $\sigma_1$, i.e.
    $N(\theta_1,\sigma_1)$.

    Plugging in our values, we have:
    \[
        \sigma_1^2 = \dfrac{\sigma_0^2\sigma^2}{\sigma^2 + n\sigma_0^2} =
        \dfrac{(3)^2(1)^2}{(1)^2 + 10(3)^2} = 0.0989
        \rightarrow \boxed{\sigma_1 = 0.314}
    \]
    and
    \[
        \theta_1 = \sigma_1^2 \left(\dfrac{\theta_0}{\sigma_0^2} +
        \dfrac{\bar{X}}{\frac{\sigma^2}{n}}\right) =
        0.0989 \left(\dfrac{0}{3^2} + \dfrac{1.68}{1/10}\right)
        \rightarrow \boxed{\theta_1 = 1.66}
    \]
    \item Assume we have gathered more evidence about $\theta$ in part (a) by
    experimenting with another signal related to the same source and gathering new data
    points $Z_1, \ldots, Z_{20} \overset{\text{\emph{iid}}}{\sim} N(\theta,4)$. If
    $\bar{Z} = 0.8$,
    compute the new posterior distribution of $\theta$ after seeing both
    samples. Assume samples in part (a) and (b) are independent from each
    other.

    Considering the prior distribution as $N(\theta_1, \sigma_1) = N(1.66,0.314)$, the
    posterior distribution given the new data $Z_1, \ldots, Z_{20} \sim N(\theta,4)$ is:
    \[
        \sigma_2^2 = \dfrac{\sigma_1^2\sigma^2}{\sigma^2 + n\sigma_1^2} =
        \dfrac{(0.0989)(4)^2}{(4)^2 + 20(0.0989)} = 0.088
        \rightarrow \boxed{\sigma_2 = 0.297}
    \]
    and
    \[
        \theta_2 = \sigma_2^2 \left(\dfrac{\theta_1}{\sigma_1^2} + \dfrac{\bar{X}}{\frac{\sigma^2}{n}}\right) =
        0.088 \left(\dfrac{1.66}{0.0989} + \dfrac{0.8}{4^2/20}\right)
        \rightarrow \boxed{\theta_2= 1.565}
    \]
    \item How does your inference result change if your sample in part (b) had
    more data points?

    As the number of data points $n$ increases, the posterior distribution becomes more influenced by the likelihood function of the observed data. Eventually, for very large values of $n$, the prior distribution vanishes and the Bayesian inference method yield a similar result to a frequentist approach.
    \item How does your inference result change if you had more certainty about
    your prior belief (i.e. the prior distribution had lower variance)?

    The posterior distribution would be closer to the prior distribution, provided that
    the observed data didn't indicate a significantly different distribution.

%\begin{lstlisting}[language=Python, caption=Bayesian Inference Code]
%import pystan
%
%n = 10
%x_bar = 1.68
%y = np.random.normal(x_bar, 1, n)
%
%model_code = """
%data {
%  int<lower=0> n;
%  real y[n];
%}
%parameters {
%  real<lower=0,upper=100> theta;
%  real<lower=0,upper=10> sigma;
%}
%model {
%  y ~ normal(theta,sigma);
%}
%"""
%norm_dat = {
%    'n': 10,
%    'y': y
%}
%
%# Fit the model with the original data
%model = pystan.StanModel(model_code=model_code)
%fit = model.sampling(data=norm_dat, iter=1000, chains=4, n_jobs=2)
%print(fit)
%
%# Fit the model with the new data
%new_dat = {
%    'n': 20,
%    'y': np.random.normal(0.8, 4, 20),
%}
%
%fit2 = model.sampling(data=new_dat)
%print(fit2)
%\end{lstlisting}

\end{enumerate}

%\bibliographystyle{plain}
%\bibliography{references}
\end{document}


%
%Free under Creative Commons Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
%

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\usepackage{color}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage{graphicx}
\usepackage{sectsty}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage{array}
\usepackage{alltt}
\newcommand{\sectionbreak}{\clearpage}
\usepackage[first=-10, last=30]{lcg}

\usepackage{tikz}
	\usetikzlibrary{arrows,shapes,trees}

\usepackage{tkz-euclide}
\usetkzobj{all}

\allsectionsfont{\centering}

\usepackage{draftwatermark}
	\SetWatermarkText{wolf-math.com}
	\SetWatermarkScale{5}
	\SetWatermarkAngle{90}
	\SetWatermarkLightness{1}

\usepackage[margin=1in, headsep=0pt]{geometry}
\setlength{\parindent}{0cm}
%\pagestyle{empty}

\let\stdsection\section
\renewcommand\section{\newpage\stdsection}

\begin{document}
\newcommand{\random}{\rand\arabic{rand}}

\tableofcontents

\section{Function Notation \& Inverse Functions}

Mr. Wolf \\ wolf-math.com

\subsection{Goals}

\textbf{SWBAT} use function notation to write functions\\

\textbf{SWBAT} perform operations on functions using function notation\\

\textbf{SWBAT} perform compositions of functions\\

\textbf{SWBAT} identify inverse functions by composing one into another\\

\textbf{SWBAT} create inverse functions

\subsection{Standards}

\textbf{Interpreting Functions \hfill F-IF}\\

1. Understand that a function from one set (called the domain) to
another set (called the range) assigns to each element of the domain
exactly one element of the range. If f is a function and x is an element
of its domain, then f(x) denotes the output of f corresponding to the
input x. The graph of f is the graph of the equation y = f(x).\\

2. Use function notation, evaluate functions for inputs in their domains,
and interpret statements that use function notation in terms of a
context.\\

\textbf{Building Functions \hfill F-BF}

Build new functions from existing functions\\

3. Identify the effect on the graph of replacing $f(x)$ by $f(x) + k$, $k f(x)$,
$f(kx)$, and $f(x + k)$ for specific values of $k$ (both positive and negative);
find the value of $k$ given the graphs. Experiment with cases and
illustrate an explanation of the effects on the graph using technology.
Include recognizing even and odd functions from their graphs and
algebraic expressions for them.\\

4. Find inverse functions.\\

a. Solve an equation of the form $f(x) = c$ for a simple function $f$
that has an inverse and write an expression for the inverse. For
example, $f(x) =2 x^3$ for $x > 0$ or $f(x) = (x+1)/(x–1)$ for $x ≠ 1$.\\

b. (+) Verify by composition that one function is the inverse of
another.\\

c. (+) Read values of an inverse function from a graph or a table,
given that the function has an inverse.\\

d. (+) Produce an invertible function from a non-invertible function
by restricting the domain.\\

\subsection{Before \& After}

\textbf{Before} we learned about exponential functions and their applications.\\

\textbf{Now} we are learning about function notation so that we can learn about inverse functions, specifically the inverses of exponential functions (i.e. logarithms).\\

\textbf{After} we will learn about exponential functions, and logarithms, and how logarithms are the inverse functions of exponential functions.

\section{What is a Function?}

A function is a machine that turns one thing into another. \\

A toaster is a ``function'' that turns bread into toast.\\

A mathematical function is a rule that changes some number or expression into a different number or expression.\\

The input of a function is called the \textbf{DOMAIN}, and the output is called the \textbf{RANGE}.\\

Functions are written with ordered pairs.

\section{Using Function Notation}

Another perk of using function notation is that you can plug things into the function really easily. If we want to find out when $x=3$ we will write $f(3)$\\

$$\mathbf{f(x)=3x-1}$$

$f(1)=3(1)-1$\\

$f(2)=3(2)-1$\\

$f(3)=3(3)-1$\\

\textbf{You can also plug in other variables. To plug these}\\

\begin{enumerate}
\item Put a set of parenthesis wherever $x$ was\\

\item Then plug your variable/expression into the parenthesis\\

\item Simplify\\
\end{enumerate}

$f(c)=$\\

$f(5r)=$\\

Plug in expressions\\

$f(x+1)=$\\

$f(y^2)=$\\

\hrulefill

\textbf{You Try:}

$$\mathbf{f(x)=x^2-2x-1}$$

\begin{enumerate}

	\item $f(1)=$

	\item $f(-1)=$

	\item $f(7)=$

	\item $f(0)=$

\end{enumerate}

\section{Function Notation \& Operations}

\textbf{Function notation} is a common way to write equations. Function notation is written $f(x)$ and read "f of x." When we are dealing with functions it is enough to say $y=f(x)$. $y$ and $f(x)$ are pretty much interchangeable, but there are few things that function notation allows us to do that a regular $y$ doesn't.\\

$$f(x)=2x  \text{ and } g(x)=x+5$$

\vspace{12pt}

\begin{center}
\def\arraystretch{2}%  1 is the default, change whatever you need
\begin{tabular}{l|c|c}

\textbf{Operation} 		& \textbf{Definition} 				& \textbf{Example} \\ \hline
Addition 		& $[f+g](x) = f(x)+g(x)$	& $2x + (x+5)$ \\ \hline
Subtraction 	& $[f-g](x) = f(x)-g(x)$	& $2x - (x+5)$ \\ \hline
Multiplication 	& $[f \cdot g](x) = f(x) \cdot g(x)$ 	& $2x \cdot (x+5)$ \\ \hline
Division 		& $[f \div g](x) = \frac{f(x)}{g(x)}$ 	& $\frac{2x}{x+5}$ \\

\end{tabular}
\end{center}

\hspace{12pt}

\hrulefill

\textbf{Example:}\\ $f(x)=3x+4$ \hspace{.5in} and \hspace{.5in} $g(x)=5x-8$

\begin{enumerate}
	\item $[f+g](x)=$\\

	\item $[f-g](x)=$\\

	\item $[g-f](x)=$\\

	\item $[f \cdot g](x)=$\\

	\item $[f\div g](x)=$\\
\end{enumerate}

\pagebreak

\textbf{You Try} Perform the indicated operations on the two functions.\\

$$h(x)=x-8$$

$$k(x)=5x$$\\

\begin{enumerate}
	\item $[h+k](x)=$\\

	\item $[h-k](x)=$\\

	\item $[k-h](x)=$\\

	\item $[h \cdot k](x)=$\\

	\item $\left[\frac{h}{k}\right](x)=$\\
\end{enumerate}

\hrulefill

\textbf{You Try:} Perform the operations on functions.\\

$$p(x)=x+10$$

$$q(x)=2x-10$$

\begin{enumerate}
	\item $[p+q](x)=$\\

	\item $[p \cdot q](x)=$\\

	\item $\left[\frac{p}{q}\right] (x)=$\\

	\item $[q-p](x)=$\\

	\item $[p-q](x)=$\\
\end{enumerate}

\section{Composition of Functions}

Composition of functions means that we can put a function inside a function. There are two equivalent ways to write composition of functions.\\

$$f(x)=2x \hspace{.25in} g(x)=x^3$$

$$[f \circ g](x)= f[g(x)]=f(x^3)$$\\

\textbf{3-step process}  \\

Step 1) Write out $f(x)$ with parenthesis around where the $x$ would be $$2(\hspace{.5cm})$$\\

Step 2) Fill in the blank spots with $g(x)$ $$2(x^3)$$\\

Step 3) Combine like-terms and simplify $$2x^3$$\\

\hrulefill

These are not commutative. Therefore it's going to be different if we write $$[g \circ f](x)$$

Step 1) Write $g(x)$ with parenthesis around where the $x$ would be $$(\hspace{.5cm})^3$$

Step 2) Fill in the blank spots with $f(x)$ $$(2x)^3$$

Step 3) Combine like-terms and simplify $$2^3x^3=8x^3$$

\textbf{Example 2} Compose the functions. $h(x)=x+2$ and $k(x)=2x^2$\\

$$[h \circ k](x)=$$

\vspace{1in}

$$[k \circ h](x)=$$

\vspace{1in}

\hrulefill

\textbf{You Try:} Compose the Functions. $p(x)=5x+5$ and $q(x)=x-10$\\

$$[p \circ q](x)=$$

\vspace{1in}

$$[q \circ p](x)=$$

\pagebreak

\textbf{Directions:} Compose the functions as indicated.

\begin{large}
$$ f(x)=x+3$$

$$g(x)=x^2$$

$$h(x)=5x$$\\
\end{large}

\begin{enumerate}
\begin{multicols}{2}

\item $[f \circ g](x)=$\\

	\vspace{1in}

\item $[g \circ h](x)=$\\

	\vspace{1in}

\item $[f \circ h](x)=$\\

	\vspace{1in}

\item $[g \circ f](x)=$\\

	\vspace{1in}

\item $[h \circ g](x)=$\\

	\vspace{1in}

\item $[h \circ f](x)=$\\

	\vspace{1in}

\end{multicols}
\end{enumerate}

\section{Function Notation Review }

\hfill NAME:\underline{\hspace{3in}}

Review day for function notation. This worksheet is due at the end of class.\\

\subsection{Function Notation \& Operations}

\textbf{Directions:} Complete the following operations on the above listed functions.\\ Use $f(x)=3x-10$ \hspace{.3in} and \hspace{.3in} $h(x)=3x$\\

\begin{enumerate}
\begin{multicols}{2}

	\item $[f+h](x)=$\\

	\item $[f-h](x)=$\\

	\item $[h-f](x)=$\\

	\item $[f \cdot h](x)=$\\

\end{multicols}
\end{enumerate}

\hrulefill

\subsection{Using Function Notation}

\textbf{Directions:} Plug the numbers or expressions into function notation\\ Use $f(x)=3x-10$ \hspace{.3in} and \hspace{.3in} $h(x)=3x$

\begin{enumerate}[resume]

	\item $f(3)=$\\

	\item $f(16)=$\\

	\item $h(-12)=$\\

	\item $h(0)=$\\

	\item $f(2y)=$\\

	\item $h(2y)=$\\

	\item $f(p+14)=$\\

	\item $h(2b^2)=$\\
\end{enumerate}

\subsection{Composition of Functions}

\textbf{Directions:} Use the following functions and compose them as each question indicates.

 $$p(x)=5x-10$$ $$r(x)=9x$$  $$t(x)=x^2$$

\begin{enumerate}[resume]

	\item $[p \circ r](x)=$\\

		\vspace{1cm}

	\item $[p \circ t](x)=$\\
				\vspace{1cm}

	\item $[r \circ p](x)=$\\

		\vspace{1cm}

	\item $[r \circ t](x)=$\\

		\vspace{1cm}

	\item $[t \circ p](x)=$\\

		\vspace{1cm}

	\item $ [p \circ p](x)=$\\

\end{enumerate}

Accelerated Objective: A new car dealer is discounting all new cars by 12\%. At the same time, the manufacturer is offering a \$1500 rebate on all new cars. Mr. Slaughter is buying a car that is priced \$24,500. Will the final price be lower if the discount is applied before the rebate or if the rebate is applied before the discount?\\

Let x represent the original price of a new car, $d(x)$ represent the price of a car after the discount, and $r(x)$ represent the price of the car after the rebate.

\section*{Making Connections}

\newcommand{\negspace}{\vspace{-1.5\baselineskip}}

\begin{center}
\bgroup
\def\arraystretch{1.5}
\begin{tabular}{l | p{2.0in}}
Math & Python \\ \hline

$ f(x) = x + 10 $
&
\negspace
\begin{verbatim}
>>> def f(x):
       return x + 10
\end{verbatim}\\

$ g(x) = x^{2} $ &
\negspace
\begin{verbatim}
>>> def g(x):
      return x**2
\end{verbatim}\\ \hline

$ f(7) = 7 + 10 = 17 $ &
\negspace
\begin{verbatim}
>>> f(7)
17
\end{verbatim}\\

$ g(7) = 7^{2} = 49 $ &
\negspace
\begin{verbatim}
>>> g(7)
49
\end{verbatim}\\ \hline

$ f \circ g(7) = f(g(7))$ &
\negspace
\begin{verbatim}
>>> f(g(7))
\end{verbatim}\\

$= f(7^{2}) = f(49) = 59 $ &
\negspace
\begin{verbatim}
>>> f(g(7))
59
\end{verbatim}

\end{tabular}
\egroup
\end{center}

\section{Domain \& Range}

\textbf{Domain:} $x$ values.\\

\textbf{Range:} $y$ values.\\

\hfill simple!\\

\hrulefill

\subsection{Domain and Range from a Graph}

\textbf{Example 1:} Use the graph to determine the \textit{domain} and \textit{range}.

\begin{center}
\includegraphics[scale=.5]{domain.png}
\end{center}

\bgroup
\def\arraystretch{1.5}

\begin{center}

\begin{tabular}{ l | c | c | c  }

 & Which values? & Where does the graph go & Answer\\ \hline

\textbf{Domain} & $x$ --values & from -4 to +6 & Domain: [-4,6] \\ \hline

\textbf{Range:}  & $y$--values & from -2 to 1 & Range: [-2,1] \\ \hline

\end{tabular}
\end{center}

\vspace{1cm}

\textbf{Example 2:} Find the domain and range of the graph. $f(x)=x^{2}-2$\\

The hard part of this one is thinking about the \textit{end behavior} of the graph.

\begin{center}
\includegraphics[scale=.5]{domain2.png}\\
\end{center}

\textbf{Domain:}

\vspace{1cm}

\textbf{Range:}\\

\textbf{You Try:} Find the domain and range of the graph.\\

\begin{center}
\includegraphics[scale=.5]{domain4.png}\\
\end{center}

\textbf{Domain:}

\vspace{1cm}

\textbf{Range:}\\

\hrulefill

\textbf{You Try:} Find the domain and range of the graph.\\

\begin{center}
\includegraphics[scale=.5]{domain3.png}\\
\end{center}

\textbf{Domain:}

\vspace{1cm}

\textbf{Range:}\\

\hrulefill

More Practice\\

\url{https://www.khanacademy.org/math/algebra2/functions_and_graphs/domain_range/e/range_of_a_function}

\clearpage

\subsection{Domain and Range from Number Sets}

If we just have a set of numbers we can still determine the \textit{domain} and \textit{range}. \\

Example 1) Determine the domain and range of the data set.\\

$$(-3,18),(7,3),(8,10),(17,-1)$$\\

\begin{multicols}{2}
\begin{center}
	\begin{tabular}{c | c}

		smallest $x$ & -3 \\

		biggest $x$ & 17\\

	\end{tabular}
\end{center}

\begin{center}
	\begin{tabular}{c | c}

		smallest $y$ & -1 \\

		biggest $y$ & 18\\

	\end{tabular}
\end{center}

\end{multicols}

\vspace{1cm}

So the \textbf{domain} is $[-3,17]$\\

and the \textbf{range} is $[-1,18]$\\

\hrulefill

\textbf{You Try:} Find the domain and range of the data set.\\

$$(-2,9),(4,2),(3,3),(10,-3),(-4,11)$$\\

\begin{multicols}{2}
\begin{center}
	\begin{tabular}{c | c}

		smallest $x$ &  \\

		biggest $x$ & \\

	\end{tabular}
\end{center}

\begin{center}
	\begin{tabular}{c | c}

		smallest $y$ &  \\

		biggest $y$ & \\

	\end{tabular}
\end{center}
\end{multicols}

\vspace{1.5cm}

\textbf{Domain =}\\

\textbf{Range =}

\section{Inverse Functions}

Inverse functions are just functions that are \textit{opposites}. If you compose two functions and the result is just $x$ then they are called \textit{inverse functions}. It need to go both ways though.\\

Inverse functions are another way of just saying that they undo one another\\

%source: https://cheezburger.com/7068514304/inverse-function
\begin{center}
\includegraphics[scale=.4]{stapler.jpg}
\end{center}

The stapler puts things together with a staple. The staple remover separates things by removing a staple. So they are inverse functions.\\

For one function to be the opposite of the other it must have the opposite \textit{operations}\\

\begin{center}
\begin{tabular}{ c | c }

Function & Inverse \\ \hline

$+$ & $-$\\

$\times$ & $\div$ \\

$x^2$ & $\sqrt{x}$ \\

$x^3$ & $\sqrt[3]{x}$\\

\end{tabular}
\end{center}

If $f(x)$ and $g(x)$ are inverses, then
$$[f \circ g](x)=x$$ $$[g \circ f](x)=x$$\\

English: If two functions are inverses, then when composed the answer is $x$. But to be sure we must compose both ways.\\

\clearpage

\textbf{Example 1:} $d(x)=x+5$ and $b(x)=x-5$. Are they inverses?\\

$[d \circ b](x)=$\\

\vspace{.65in}

$[b \circ d](x)=$\\

\vspace{.65in}

\textbf{Example 2:} $j(x)=2x-1$ and $k(x)=2x+1$. Are they inverses?\\

$[j \circ k](x)=$

\vspace{.65in}

$[k \circ j](x)=$\\

\vspace{.65in}

\textbf{Example 3:} $p(x)=x^2+2$ and $q(x)=\sqrt{x+2}$. Are they inverses?\\

$[p \circ q](x)=$\\

\vspace{.65in}

$[q \circ p](x)=$\\

\vspace{.65in}

\textbf{Example 4:} $f(x)=\frac{x}{2}-4$ and $g(x)=2x+8$\\

$[f \circ g](x)=$\\

\vspace{.65in}

$[g \circ f](x)=$\\

\subsection{Creating Inverse Functions}

We know that when a function is composed into its inverse the result is just $x$. How do we create inverse functions though?\\

\textbf{Step 1:} Change $f(x)$ into a $y$. \\

\textbf{Step 2:} Switch $x$ and $y$.\\

\textbf{Step 3:} Solve for $y$. You should get $y=.....$\\

\textbf{Step 4:} replace $y$ with $f^{-1}(x)$\\

\hrulefill

\textbf{Example 1:} Find the inverse of $f(x)=7x-3$

\begin{enumerate}
\item $y=7x-3$\\

\item $x=7y-3$\\

\item $x+3=7y \longrightarrow y=\frac{x+3}{7}$\\

\item $f^{-1}(x)=\frac{x+3}{7}$
\end{enumerate}

\textbf{Example 2:} Find the  inverse of $p(x)=x^2-9$\\

\vspace{1 in}

\textbf{You Try:} Find the inverse of $h(x)=2x-10$\\

\clearpage

\subsection*{Calculating Inverse Functions}

\hfill NAME:\underline{\hspace{3in}}

\hfill DATE: \underline{\hspace{2in}}

\textbf{Directions:} Determine if the functions are inverses.\\

\begin{enumerate}
\setlength\itemsep{2cm}

\item $a(x)=2x+1$\\

		$b(x)=\frac{x}{2}-1$\\

\item $c(x)=\sqrt{x+10}$\\

		$d(x)=x^2 -10$\\

\item $t(x)=8x^2 + 13$\\

		$u(x)=\sqrt{\frac{x-13}{8}}$

\end{enumerate}

\vspace{2cm}

\textbf{Directions:} Create the inverse function of each function given.\\
\begin{enumerate}[resume]
\setlength\itemsep{1.2cm}

\item $f(x)=\random x$\\

\item $g(x)=\random x + \random$\\

\item $h(x)=\frac{x}{\random}$\\

\item $j(x)=\frac{\random}{x}$\\

\item $k(x)=\frac{2x}{\random}+\random$\\

\item $m(x)=\sqrt{\random x}$\\

\item $n(x)=\sqrt{\random x + \random}$\\

\item $p(x)=\sqrt{\random x + \random} +\random$\\

\item $q(x)=x^2+ \random$\\

\item $r(x)=(2x)^2 + \random$\\

\end{enumerate}

\textbf{Challenge Problem:} $f(x)=\frac{3x^2+1}{2}-\random$

\clearpage

\subsection{Graphs of Inverse Functions}

Graphs of inverse functions are always \textit{reflections} over the line $y=x$ (yes, the same $x$ that's a solution when we find they're inverses).\\

Another way of saying this is that if $(a,b)$ is a point

$$(a,b) \in f(x) \Leftrightarrow (b,a) \in f^{-1}(x)$$

That's another way of saying: switch the $x$ and $y$.\\

\textbf{Example 1)}

\color{red}$$f(x)=5x-4$$
\color{blue}$$f^{-1}(x)=\frac{x+4}{5}$$ \color{black}

\begin{center} \includegraphics[scale=.5]{inverse1.png} \end{center}

\begin{multicols}{2}
	\begin{center}
	\color{red}
	\begin{tabular}{c | c}

		\hspace{.5cm}$x$\hspace{.5cm} & $y$ (or $f(x)$) \\ \hline

		0 & -4 \\
		1 & 1 \\
		2 & 6

	\end{tabular}

	\color{blue}
	\begin{tabular}{ c | c }
		\hspace{.5cm} $x$\hspace{.5cm} & $y$ (or $f^{-1}(x)$) \\ \hline
		-4 & 0\\
		1 & 1\\
		6 & 2 \\
	\color{black}
	\end{tabular}
\end{center}
\end{multicols}

The values of the points are reversed. There is also a reflection over the line $y=x$ \\

Geogebra demo. Download Geogebra!

\textbf{Example 2)} No graph necessary

\color{red} $$g(x)=2x-1$$
\color{blue} $$g^{-1}(x)=\frac{x+1}{2}$$ \color{black}

\begin{multicols}{2}
\begin{center}
	\begin{tabular}{c | c}

		\hspace{.5cm}$x$ \hspace{.5cm}& $y$ (or $g(x)$) \\ \hline

		-2 & -5\\

		-1 & -3\\

		0 & -1 \\

		1 & 1 \\

		2 & 3 \\

	\end{tabular}

		\begin{tabular}{c | c}

		\hspace{.5cm}$x$\hspace{.5cm} & $y$ (or $g^{-1}(x)$) \\ \hline

		-5 & \\

		-3 & \\

		-1 &  \\

		1 & \\

		3 & \\

	\end{tabular}

\end{center}
\end{multicols}

\hrulefill

\textbf{You Try:} Fill in the t-chart with the values of the inverse function based on the information of the regular function.\\

 $$p(x)=3x+4$$
 $$p^{-1}(x)=\frac{x-4}{3}$$\\

\begin{multicols}{2}
\begin{center}
	\begin{tabular}{c | c}

		\hspace{.5cm}$x$ \hspace{.5cm} & $y$ (or $g(x)$) \\ \hline

		-2 & -2\\

		-1 & 1\\

		0 & 4 \\

		1 & 5 \\

		2 & 8 \\

	\end{tabular}

		\begin{tabular}{c | c}

		\hspace{.5cm}$x$\hspace{.5cm} & $y$ (or $g^{-1}(x)$) \\ \hline
		 & \\
		 & \\
		 & \\
		 & \\
		 & \\

	\end{tabular}

\end{center}
\end{multicols}

\section{Review}

Mr. Wolf \hfill NAME:\underline{\hspace{3in}}\\
Pre-Calc \\
$MC^2$ High School \hfill DATE:\underline{\hspace{2in}}\\
2014-2015\\

\textbf{Operations with Function Notation}\\

$$a(x)=4x-13 \hspace{.3in} b(x)=9x \hspace{.3in} c(x)=x^2$$\\

Complete the following operations on the functions (2 points each)\\

\begin{enumerate}
\begin{multicols}{2}
	\setlength\itemsep{1cm}

	\item $[a+b](x)=$\\

	\item $[a+c](x)=$\\

	\item $[b-a](x)=$\\

	\item $[b \cdot c](x)=$\\

	\item $c(5)=$\\

	\item $c(-5)=$\\

	\item $a(3h-2)=$\\

\end{multicols}
\end{enumerate}

\hrulefill

$$k(x)=7x+7 \hspace{1in} h(x)=\frac{x}{7}-1$$

\begin{enumerate}[resume]
	\setlength\itemsep{1cm}

	\item $[k\circ h](x)=$\\

	\item $[h \circ k](x)=$\\

	\item are $h$ and $k$ inverses?\\

	\item Write the inverse of $p(x)=2x-1$\\

		\vspace{1in}

	\item Fill out the t-chart for $p(x)$

	\begin{center}
	\begin{tabular}{c | c}

		\hspace{.5cm}$x$ \hspace{.5cm} & $y$ (or $p(x)$) \\ \hline

		-2 & \\

		-1 & \\

		0 &  \\

		1 &  \\

		2 &  \\

	\end{tabular}
	\end{center}

	\item List the domain and range of the t-chart\\

	\item Fill the t-chart for $p^{-1}(x)$\\
		\begin{center}
		\begin{tabular}{c | c}

		\hspace{.5cm}$x$ \hspace{.5cm} & $y$ (or $p(x)$) \\ \hline

		& \\

		& \\

		&  \\

		&  \\

		&  \\

	\end{tabular}
	\end{center}
\end{enumerate}

\section*{Quiz Review 2}

Mr. Wolf \hfill NAME:\underline{\hspace{3in}}\\

\textbf{Operations with Function Notation}\\

$$a(x)=3x-10 \hspace{.3in} b(x)=2x \hspace{.3in} c(x)=x^2$$\\

Complete the following operations on the functions (2 points each)\\

\begin{enumerate}
\begin{multicols}{2}
	\setlength\itemsep{1cm}

	\item $[a+b](x)=$\\

	\item $[a+c](x)=$\\

	\item $[b-a](x)=$\\

	\item $[b \cdot c](x)=$\\

	\item $c(10)=$\\

	\item $c(-7)=$\\

	\item $a(2k+15)=$\\

\end{multicols}
\end{enumerate}

\hrulefill

$$k(x)=\frac{x}{2}-3 \hspace{1in} h(x)=2x+6$$

\begin{enumerate}[resume]
	\setlength\itemsep{1cm}

	\item $[k\circ h](x)=$\\

	\item $[h \circ k](x)=$\\

	\item are $h$ and $k$ inverses?\\

	\item Write the inverse of $p(x)=5x-10$\\

		\vspace{1in}

	\item Fill out the t-chart for $p(x)$

	\begin{center}
	\begin{tabular}{c | c}

		\hspace{.5cm}$x$ \hspace{.5cm} & $y$ (or $p(x)$) \\ \hline

		-2 & \\

		-1 & \\

		0 &  \\

		1 &  \\

		2 &  \\

	\end{tabular}
	\end{center}

	\item List the domain and range of the t-chart\\

	\item Fill the t-chart for $p^{-1}(x)$ (from answer to 11)\\
		\begin{center}
		\begin{tabular}{c | c}

		\hspace{.5cm}$x$ \hspace{.5cm} & $y$ (or $p^{-1}(x)$) \\ \hline

		& \\

		& \\

		&  \\

		&  \\

		&  \\

	\end{tabular}
	\end{center}
\end{enumerate}

\section*{Quiz}

Mr. Wolf \hfill NAME:\underline{\hspace{3in}}\\
Pre-Calc \\
CMSD-JFK \hfill DATE:\underline{\hspace{2in}}\\
2014-2015\\

\textbf{Operations with Function Notation}\\

$$a(x)=3x-10 \hspace{.3in} b(x)=5x \hspace{.3in} c(x)=x^2$$\\

Complete the following operations on the functions (2 points each)\\

\begin{enumerate}
\begin{multicols}{2}
	\setlength\itemsep{1cm}

	\item $[a+b](x)=$\\

	\item $[a+c](x)=$\\

	\item $[b-a](x)=$\\

	\item $[b \cdot c](x)=$\\

	\item $c(2)=$\\

	\item $b(-3)=$\\

	\item $a(7r)=$\\

\end{multicols}
\end{enumerate}

\hrulefill

$$k(x)=2x+6 \hspace{1in} h(x)=2x-6$$

\begin{enumerate}[resume]
	\setlength\itemsep{1cm}

	\item $[k\circ h](x)=$\\

	\item $[h \circ k](x)=$\\

	\item are $h$ and $k$ inverses?\\

	\item Calculate the inverse of $p(x)=3x+2$\\

		\vspace{1in}

	\item Fill out the t-chart for $p(x)$

	\begin{center}
	\begin{tabular}{c | c}

		\hspace{.5cm}$x$ \hspace{.5cm} & $y$ (or $p(x)$) \\ \hline

		-2 & \\

		-1 & \\

		0 &  \\

		1 &  \\

		2 &  \\

	\end{tabular}
	\end{center}

	\item List the domain and range of the t-chart\\

	\item Fill the t-chart for $p^{-1}(x)$\\
		\begin{center}
		\begin{tabular}{c | c}

		\hspace{.5cm}$x$ \hspace{.5cm} & $y$ (or $p(x)$) \\ \hline

		& \\

		& \\

		&  \\

		&  \\

		&  \\

	\end{tabular}
	\end{center}
\end{enumerate}

\vspace{.5in}

\textbf{Extra Credit:} What is the answer to the extra credit question?

\pagebreak

\begin{enumerate}
\item $(x^2+5x-3)+(4x^2-9x+12)$\\

\item $(xy^3+2x-7y+3)-(x^2+2xy^3+2x-y+10)$\\

\item $(2x-1)(3y+3)$\\

\item $(3x-2)^2$\\
\end{enumerate}

\end{document}