# $\nu$-electron scattering

Testing the neutrino-electron scattering rate calculation for Xenon. We use the free electron approximation but also include a stepping process to approximate the impact of the atomic ionisation energy levels which suppress the rate at very low recoil energies.

First we look at the energy levels in Xenon. The approximation works by applying a heaviside function to the free electron neutrino-electron cross section de-weight those electrons that can't be ionised by the incoming neutrino with a defined energy. i.e.

\begin{equation}
\frac{\textrm{d}\sigma^i}{\textrm{d} E_R} = \sum\limits_{k=1}^{Z} \Theta(E_R-E^k_B) \frac{\textrm{d}\sigma^i_0}{\textrm{d}E_R}
\end{equation}

where $i$ refers to neutrino flavour, $\sigma_0$ is the cross section for a free electron, and $E^k_B$ is the ionisation energy of the $k$th level.

### First we take a look at the xenon energy levels:

```python
import sys
sys.path.append('../erec')
from numpy import *
import matplotlib.pyplot as plt
from Params import *
from LabFuncs import *
from DMFuncs import *
from PlotFuncs import *
from NeutrinoFuncs import *
from AtomicFuncs import *

# Function for outputting the sum of the heaviside functions
def XeStep(E_r): # E_r is always in keV
    Z = 54
    # Xenon energy levels (in eV) from arXiv:1610.04177
    E_B_vals = array([34759.3]*2\
                     +[5509.8]*2+[5161.5]*2+[4835.6]*4\
                     +[1170.5]*2+[1024.8]*2+[961.2]*4+[708.1]*2+[694.9]*8\
                     +[229.4]*2+[175.6]*2+[162.8]*4+[73.8]*2+[71.7]*8\
                     +[27.5]*2+[13.4]*4+[12.0]*2)
    TH = zeros(shape=shape(E_r))
    for i in range(0,Z):
        TH += 1.0*(E_r>E_B_vals[i]/1000.0)
    # Neutrino rate later multiplies by Z so we divide by it here to not overcount
    TH = TH/(1.0*Z)
    return TH,E_B_vals

# Set up plot
fig,ax = MySquarePlot(r'$E_R$ [keV]',r'$\frac{1}{Z}\sum\limits_{k=1}^{Z} k \Theta(E_R-E^k_B)$',lfs=40,tfs=30)

# Plot step functions
ne = 2000
E_r = logspace(-1,3,ne)
st,E_B_vals = XeStep(E_r)
plt.step(E_r,st,'-',lw=5,color='firebrick')

E_prev = 0.0
for i in range(0,54):
    if (E_B_vals[i]!=E_prev)&(E_B_vals[i]/1000.0>E_r[0]):
        plt.plot(E_B_vals[i]*array([1,1])/1000.0,[0.0,1.1],'k-',lw=3,alpha=0.1)
    E_prev = 1.0*E_B_vals[i]

# Add shell labels
plt.text(34759.3/1000.0*0.9,1.11,'K',fontsize=30)
plt.text(5509.8/1000.0*0.75,1.11,'L$_{1-3}$',fontsize=30)
plt.text(1170.5/1000.0*0.6,1.11,'M$_{1-5}$',fontsize=30)
plt.text(229.4/1000.0*0.65,1.11,'N$_{1-3}$',fontsize=30)

# Plot tweaking
#plt.yscale('log')
plt.xlim([E_r[0],E_r[-1]])
plt.ylim([0.0,1.1])
plt.xscale('log')
plt.gcf().text(0.64,0.17,r'{\bf Xenon} $Z = 54$',fontsize=35)

# Show and save
plt.show()
pltname = "XenonStep"
fig.savefig('../plots/'+pltname+'.pdf',bbox_inches='tight')
fig.savefig('../plots/plots_png/'+pltname+'.png',bbox_inches='tight')
```

# Neutrino-electron recoil rates for the two approximations

```python
import cmocean

# Load neutrino fluxes
NuBG = GetNuFluxes(0.0,Xe131)
E_nu_all = NuBG.Energy
Flux_all = NuBG.Flux
n_nu = NuBG.NumberOfNeutrinos
Errs = NuBG.Uncertainties
solar = NuBG.SolarLabel

fig,ax = MySquarePlot(r'$E_R$ [keV]',r'$\textrm{d}R/\textrm{d}E_R$ [ton$^{-1}$ year$^{-1}$ keV$^{-1}$]',lfs=40,tfs=30)

ne = 5000
E_r = logspace(-4.0,3.2,ne)
TH,E_B_vals = XeStep(E_r)
ymax = 5.0
ymin = 3.0e-3

col = cmocean.cm.phase(linspace(0.1,0.8,3))

# pp
dR1 = dRdEe_nu(E_r,0.0,solar[0],E_nu_all[:,0],Flux_all[:,0],Xe131,flav_Solar)
plt.plot(E_r,dR1,'--',lw=3,color=col[0,:])
plt.plot(E_r,dR1*TH,'-',lw=3,color=col[0,:])

# 7Be
dR2 = dRdEe_nu(E_r,0.0,solar[3],E_nu_all[:,3],Flux_all[:,3],Xe131,flav_Solar)\
        +dRdEe_nu(E_r,0.0,solar[4],E_nu_all[:,4],Flux_all[:,4],Xe131,flav_Solar)
plt.plot(E_r,dR2,'--',lw=3,color=col[1,:])
plt.plot(E_r,dR2*TH,'-',lw=3,color=col[1,:])

# CNO
dR3 = dRdEe_nu(E_r,0.0,solar[6],E_nu_all[:,6],Flux_all[:,6],Xe131,flav_Solar)\
        +dRdEe_nu(E_r,0.0,solar[7],E_nu_all[:,7],Flux_all[:,7],Xe131,flav_Solar)\
        +dRdEe_nu(E_r,0.0,solar[8],E_nu_all[:,8],Flux_all[:,8],Xe131,flav_Solar)
plt.plot(E_r,dR3,'--',lw=3,color=col[2,:])
plt.plot(E_r,dR3*TH,'-',lw=3,color=col[2,:])

# pep
# dR4 = dRdEe_nu(E_r,0.0,solar[1],E_nu_all[:,1],Flux_all[:,1],Xe131,flav_Solar)
# plt.plot(E_r,dR4,'--',lw=3,color=col[3,:])
# plt.plot(E_r,dR4*TH,'-',lw=3,color=col[3,:])

plt.plot(-E_r,dR1+dR2+dR3,'k--',lw=3,label='Free electron approx.')
plt.plot(-E_r,(dR1+dR2+dR3),'k-',lw=3,label='Free electron approx. with energy levels')
plt.legend(frameon=False,fontsize=30,borderpad=0.1,handlelength=1)

plt.gcf().text(0.15,0.43,'CNO',color=col[2,:])
plt.gcf().text(0.15,0.6,'$^7$Be',color=col[1,:])
plt.gcf().text(0.15,0.81,'$pp$',color=col[0,:])
plt.gcf().text(0.75,0.82,r'{\bf Xenon}',fontsize=35)

plt.xscale('log')
plt.yscale('log')
plt.ylim([5.0e-4,10.0])
plt.xlim([0.1,2000.0])
plt.show()
pltname = "NuRate_Xe_electrons"
fig.savefig('../plots/'+pltname+'.pdf',bbox_inches='tight')
fig.savefig('../plots/plots_png/'+pltname+'.png',bbox_inches='tight')
```

# Does a finite energy resolution remove these features?

Energy resolution in xenon detectors looks something like,
\begin{equation}
\frac{\sigma}{E_R} = c_1\sqrt{\frac{1 {\rm keV}}{E_R}} + c_2
\end{equation}
From arXiv:1610.02076 the fit to known peaks in LUX's ER background has $c_1 = 0.33\pm0.01$ and $c_2\simeq0.0$. For results to come we should be using the 2d (S1,S2) pdf for ERs but for now we just want to see how it looks.

```python
fig,ax = MySquarePlot(r'$E_R$ [keV]',r'$\textrm{d}R/\textrm{d}E_R$ [ton$^{-1}$ year$^{-1}$ keV$^{-1}$]',lfs=40,tfs=30)

n_c1_vals = 10
c1_vals = linspace(0.05,0.5,n_c1_vals)
dR0 = TH*(dR1+dR2+dR3)

col = cmocean.cm.balance(linspace(0.1,1.0,n_c1_vals))

for i in flipud(range(0,n_c1_vals)):
    c1 = c1_vals[i]
    c2 = 0.00
    sig_E = c1*sqrt(E_r)+c2*E_r
    dR = SmearE(E_r,dR0,sig_E)
    plt.fill_between(E_r,dR,y2=dR0,color=col[i,:],alpha=0.5)
    plt.plot(E_r,dR,'-',color=col[i,:])

plt.plot(E_r,dR0,'k-',lw=3)
plt.plot(E_r,(dR1+dR2+dR3),'k--',lw=3)

plt.gcf().text(0.15,0.15,r'$\frac{\sigma}{E_R} = c_1\sqrt{\frac{1 {\rm keV}}{E_R}} + c_2$',fontsize=50)
plt.gcf().text(0.15,0.82,r'$pp$+$^7$Be+CNO',fontsize=35)
plt.gcf().text(0.75,0.82,r'{\bf Xenon}',fontsize=35)
plt.gcf().text(0.15,0.65,r'Free electron',fontsize=35)

plt.xscale('log')
plt.yscale('log')
plt.xlim([0.1,40.0])
plt.ylim([5.0e-1,8.0])

# Custom colorbar
import matplotlib as mpl
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
cbaxes = inset_axes(ax, width="15%", height="20%", bbox_to_anchor=[560, -650, 200, 1000])
norm = mpl.colors.Normalize(vmin=c1_vals[0],vmax=c1_vals[-1])
sm = plt.cm.ScalarMappable(cmap=cmocean.cm.balance, norm=norm)
sm.set_array([])
plt.colorbar(sm,cax=cbaxes,ticks=arange(c1_vals[0],c1_vals[-1],0.1),boundaries=c1_vals,orientation='vertical')
f = plt.gcf().get_children()
cbar = f[2]
cbar.tick_params(labelsize=25)
cbar.tick_params(which='major',direction='out',width=2,length=10,right=True,top=False,pad=7)
cbar.tick_params(which='minor',direction='out',width=2,length=7,right=True,top=False)
plt.gcf().text(0.73,0.26,r'$c_1$',fontsize=40,color='k')

plt.show()
pltname = "NuRate_Xe_electrons_vs_resolution"
fig.savefig('../plots/'+pltname+'.pdf',bbox_inches='tight')
fig.savefig('../plots/plots_png/'+pltname+'.png',bbox_inches='tight')
```

```python

```


```python
from sympy import cos, sin, pprint
import numpy as np
import sympy as sp
from math import pi
import matplotlib.pyplot as plt

#forward kinematics
def trans_matrix(a,r,d1,d2):
    t = [sp.Matrix([[cos(a[0]), 0, -sin(a[0]), 0], [sin(a[0]), 0, cos(a[0]), 0], [0, -1, 0, d1], [0, 0, 0, 1]]),
          sp.Matrix([[cos(a[1]), -sin(a[1]), 0,r*cos(a[1])], [sin(a[1]),cos(a[1]), 0,r*sin(a[1])], [0, 0, 1, 0], [0, 0, 0, 1]]),
          sp.Matrix([[cos(a[2]), -sin(a[2]), 0,0], [sin(a[2]),cos(a[2]), 0,0], [0, 0, 1, d2], [0, 0, 0, 1]])]

  #consider identity matrix to initialize
    tran = [sp.Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])]

    # Multiplying transformation matrices
    for i in range(0, len(t)):
        tran.append(tran[i] * t[i])
    print('final transformation matrix')
    pprint(tran[-1])
    return tran

#inverse kinematics
def jacobian(tran):
    j_temp = []
    o_2 = tran[-1].extract([0, 1, 2], [3])              # Extracting Fourth column from final transformation matrix

    for i in range(0, len(tran) - 1):

        z_i = tran[i].extract([0, 1, 2], [2])          # Extracting zi from Ti matrix
        o_i = tran[i].extract([0, 1, 2], [3])           # Extracting oi from Ti matrix
        row_1 = z_i.cross(o_2 - o_i)
        j0n = row_1.col_join(z_i)
        j_temp.append(j0n)
    j_1 = j_temp[0]
    for k in range(1, len(j_temp)):
        j_1 = j_1.row_join(j_temp[k])
    return j_1

def circle(x_off, y_off, z_off, r, s):
    t1 = np.linspace(2 * 3.14, 0, s)
    x_val = []
    z_val = []
    for i in t1:
        x_val.append(r * sin(i) + x_off)
        z_val.append(r * cos(i) + z_off)
    y_val = np.ones(s) * y_off
    return x_val, y_val, z_val

def plot_line(f1, f2):
    ax.plot3D([f1[0, 3], f2[0, 3]], [f1[1, 3], f2[1, 3]], [f1[2, 3], f2[2, 3]], 'black')

def plot_frame(f):
    rx = f[:, 0]
    ry = f[:, 1]
    rz = f[:, 2]
    tx = f[0, 3]
    ty = f[1, 3]
    tz = f[2, 3]
    ax.plot3D([tx, 3 * rx[0, 0] + tx], [ty, 3 * rx[1, 0] + ty], [tz, 3 * rx[2, 0] + tz], 'red')
    ax.plot3D([tx, 3 * ry[0, 0] + tx], [ty, 3 * ry[1, 0] + ty], [tz, 3 * ry[2, 0] + tz], 'pink')
    ax.plot3D([tx, 3 * rz[0, 0] + tx], [ty, 3 * rz[1, 0] + ty], [tz, 3 * rz[2, 0] + tz], 'blue')

def plot_arm(tran):
    plot_line(tran[0], tran[1])
    plot_line(tran[1], tran[2])

    plot_frame(tran[0])
    plot_frame(tran[1])
    plot_frame(tran[2])

if __name__ == '__main__':

    # Joint-angles in radians
    q = [1.57,3.14,0]

    # calculating transformation matrices

    tm0n = trans_matrix(q,-788.40,130.9,215)

    # calculating the jacobian matrix

    j = jacobian(tm0n)
    print('jacobian')
    pprint(j)

    # plotting the arm
    plt.figure()
    ax = plt.axes(projection='3d')
    plot_arm(tm0n)

    # Drawing the circle

    x, y, z = circle(100, 260,200, 100, 100)
    ax.plot3D(x, y, z, 'violet')

    time = 5 / len(x)         # Total time by number of points to cover

    # For the pen to follow the circle

 #inverse kinematics validation
    for k in range(0, len(x)):
        end_eff = tm0n[-1].extract([0, 1, 2], [-1])
        path_to_follow = sp.Matrix([[x[k]], [y[k]], [z[k]]])

        # rate of change of position and angles

        rate_pos = (path_to_follow - end_eff) / time
        rate_angle = j.pinv() * rate_pos.col_join(sp.Matrix([[0], [0], [0]]))

        for l in range(0, 2):
            q[l] = ((q[l] + (rate_angle[l] * time)) % (2 * 3.14))

        print('\n',q)

        plt.pause(0.1)

    plt.show()

```

```python

```


### Mie First
Mie theory provides a route to the exact solution to Maxwell's equations for light interacting with a spherical object; this is done by expanding the incident and scattered
light waves in a basis of multi-polar functions; hence one usually speaks about the dipolar contribution to scattering, the quadrupolar contribution, the octupolar, and so on.  For very large particles, many orders of this expansion may be needed, but for small particles the scattering and absorption is dominated by the dipolar contribution.  Hence, for spherical nanoparticles that are less than or equal to about 10 nm in diameter, the Mie theory expansion is truncated after only the first term.  As a concrete example, the Mie theory expression for the absorption of a small spherical nanoparticle is as follows:

\begin{equation}
\sigma_{abs}(\lambda) = \frac{2 \pi}{\lambda} {\rm Im}\left( \alpha(\lambda) \right),
\end{equation}
where $\alpha(\lambda)$ is the dipole polarizability given by
\begin{equation}
\alpha(\lambda) = 4 \pi r^3 \left( \frac{\epsilon_p(\lambda) - \epsilon_s}{\epsilon_p(\lambda) + 2\epsilon_s} \right)
\end{equation}
where $r$ is the radius of the particle, $\epsilon_p(\lambda)$ denotes the wavelength-dependent dielectric function of the material the particle is made from, $\epsilon_s$ denotes the dielectric function of the surroundings (or solvent) that the particle is immersed in, and ${\rm Im}$ means we will only take the imaginary part of the quotient (the dielectric function of the particle will typically be complex; if it is not, there will be no absorption).

We can use WPTherml to get the refractive index of materials as a function of wavelength, which is equivalent to the dielectric function as a function of wavelength by
\begin{equation}
\epsilon_p(\lambda) = n_p(\lambda)^2,
\end{equation}
where $n_p(\lambda)$ is the refractive index of the particle.

```python
pip install wptherml
```

    Collecting wptherml
      Downloading https://files.pythonhosted.org/packages/20/2e/484f8af5ba3516407d226cdd2aec810aab905b947d2a4d49c933ae4932e9/wptherml-1.0.0-py3-none-any.whl (1.1MB)
         |████████████████████████████████| 1.1MB 2.7MB/s
    Installing collected packages: wptherml
    Successfully installed wptherml-1.0.0

```python
from wptherml.wpml import multilayer
from matplotlib import pyplot as plt
from wptherml.datalib import datalib
import numpy as np
from numpy import linalg as LA
import math

structure = {

        'Material_List': ['Air', 'Ag', 'Air'],
        'Thickness_List': [0,  200e-9, 0],
        'Lambda_List': [200e-9, 700e-9, 2000],

        }

### create the silver object - call it sphere for no particular reason
sphere = multilayer(structure)
### get the refractive index of Ag (since it is layer 1 based on the structure above)
n_Ag = sphere.layer_ri(1)
### get the dielectric function of Ag
eps_Ag = n_Ag * n_Ag

plt.plot(sphere.lambda_array*1e9, sphere.emissivity_array)
plt.show()

### render color of the structure
sphere.ambient_color()

'''Lets compute the Mie theory absorption of a 3 nm Ag particle in water, which
   has refractive index = 1.33, here!'''
#r = ### fill in radius here, use meters!
#eps_s =  ### fill in dielectric function of water here!
#quotient = ### compute the quotient whose Imaginary part you need from Mie theory expression here
#alpha = ### compute alpha here
#pre = ### compute pre-factor that depends on wavelength and radius here!
#sigma_abs = ### compute full absorption spectrum here!

#plt.plot(1240/(sphere.lambda_array*1e9), sigma_abs, 'red')###
#plt.plot(1240/(sphere.lambda_array*1e9), np.real(alpha), 'red')
#plt.plot(1240/(sphere.lambda_array*1e9), np.imag(alpha), 'blue')
#plt.xlim(1.5, 4)
#plt.show()

```


# 선형연립방정식 사례: 간단한 트러스
Example of Systems of Linear Equations : Simple Truss

```python
# 그래프, 수학 기능 추가
# Add graph and math features
import pylab as py
import numpy as np
import numpy.linalg as nl
# 기호 연산 기능 추가
# Add symbolic operation capability
import sympy as sy

```

화살표를 그리는 함수
Function to draw an arrow

```python
def draw_2dvec(x, y, x0=0, y0=0, color='k', name=None):
    py.quiver(x0, y0, x, y, color=color, angles='xy', scale_units='xy', scale=1)
    py.plot((x0, x), (y0, y), alpha=0)
    if name is not None:
        if not name.startswith('$'):
            vec_str = '$\\vec{%s}$' % name
        else:
            vec_str = name
        py.text(0.5 * x + x0, 0.5 * y + y0, vec_str)

```

정삼각형을 그리는 함수
Function to draw an equilateral triangle

```python
def triangle_support(x, y, length):
    # https://matplotlib.org/gallery/lines_bars_and_markers/fill.html
    height = py.cos(py.radians(30)) * length
    py.fill((x, x + length*0.5, x + length*-0.5), (y, y - height, y - height))

```

## 4 절점 트러스
A Four Node Truss

다음과 같은 트러스를 생각해 보자.

Let's think about a truss as follows.

(ref: "[Application of system of linear equations](https://www.chegg.com/homework-help/questions-and-answers/application-system-linear-equations-sure-work-matlab-problem-figure-1-shows-mechanical-str-q22676917)", Chegg Study)

```python
# 마디점 좌표 nodal point coordinates
xy_list = [(0, 0), (1, 1), (1, 0), (2, 0)]

# 각 부재의 양 끝 점 end points of each member
connectivity_list = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)]

for k, i_j in enumerate(connectivity_list):
    i, j = i_j

    py.plot(
        (xy_list[i][0], xy_list[j][0]),
        (xy_list[i][1], xy_list[j][1]),
        '.-'
    )

    # 부재 번호 표시 Indicate member id
    py.text(0.5 * (xy_list[i][0] + xy_list[j][0]),
            0.5 * (xy_list[i][1] + xy_list[j][1]), k + 1)

# 마디점 번호 표시 Indicate node ids
for k, xy in enumerate(xy_list):
    py.text(xy[0], xy[1], '(%d)' % (k+1))

draw_2dvec(0, -0.5, xy_list[2][0], xy_list[2][1], name='$F_1$')

triangle_support(xy_list[0][0], xy_list[0][1], 0.25)
triangle_support(xy_list[3][0], xy_list[3][1], 0.25)

py.axis('equal')
# https://stackoverflow.com/questions/9295026/matplotlib-plots-removing-axis-legends-and-white-spaces
py.axis('off')

py.savefig('triangular_truss.svg')

```

모든 각은 45도 이다.

All angles are 45 degrees.

$$
\alpha = sin\left(\frac{\pi}{4}\right) = cos\left(\frac{\pi}{4}\right)
$$

각 마디에서의 힘의 평형은 다음과 같다. $f_i$ 는 $i$번째 부재의 장력이다.

Force equilibrium equations at respective nodes are as follows. $f_i$ is the tensile force of $i$th member.

$$
\begin{align}
    R_{1x} + \alpha \cdot f_{1}+f_{2} &= 0 \\
    R_{1y} + \alpha \cdot f_{1} &= 0 \\
    -\alpha \cdot f_{1}+\alpha \cdot f_{4} &=0 \\
    -\alpha \cdot f_{1}-f_{3}-\alpha \cdot f_{4} &=0 \\
    -f_{2}+f_{5}&=0 \\
    f_{3}&=F_{1} \\
    -\alpha \cdot f_4 - f_5 &=0 \\
    \alpha \cdot f_4 + R_{4y} &=0 \\
\end{align}
$$

행렬형태로는:

In matrix form:

$$
\begin{bmatrix}
1 & 0 &  \alpha &  1 &  0 &       0 &  0 & 0 \\
0 & 1 &  \alpha &  0 &  0 &       0 &  0 & 0 \\
0 & 0 & -\alpha &  0 &  0 &  \alpha &  0 & 0 \\
0 & 0 & -\alpha &  0 & -1 & -\alpha &  0 & 0 \\
0 & 0 &       0 & -1 &  0 &       0 &  1 & 0 \\
0 & 0 &       0 &  0 &  1 &       0 &  0 & 0 \\
0 & 0 &       0 &  0 &  0 & -\alpha & -1 & 0 \\
0 & 0 &       0 &  0 &  0 &  \alpha &  0 & 1 \\
\end{bmatrix}
\begin{pmatrix}
R_{1x} \\ R_{1y} \\ f_1 \\ f_2 \\ f_3 \\ f_4 \\ f_5 \\ R_{4y}
\end{pmatrix}
=
\begin{pmatrix}
0 \\ 0 \\ 0 \\ 0 \\ 0 \\ F_1 \\ 0 \\ 0
\end{pmatrix}
$$

```python
alpha = py.sin(py.radians(45))

```

```python
matrix = np.array([
    [1, 0,  alpha,  1,  0,      0,  0, 0],
    [0, 1,  alpha,  0,  0,      0,  0, 0],
    [0, 0, -alpha,  0,  0,  alpha,  0, 0],
    [0, 0, -alpha,  0, -1, -alpha,  0, 0],
    [0, 0,      0, -1,  0,      0,  1, 0],
    [0, 0,      0,  0,  1,      0,  0, 0],
    [0, 0,      0,  0,  0, -alpha, -1, 0],
    [0, 0,      0,  0,  0,  alpha,  0, 1],
])

```

행렬의 계수를 계산해 보자.
Let's check the rank of the matrix.

```python
nl.matrix_rank(matrix)

```

미지수의 갯수와 정방행렬의 계수가 같다는 것은 이 선형 연립 방정식의 해를 구할 수 있다는 뜻이다.

The number of unknowns and the rank of the matrix are the same; we can find a root of this system of linear equations.

우변을 준비해 보자.

Let's prepare for the right side.

```python
vector = np.array([[0, 0, 0, 0, 0, 100, 0, 0]]).T

```

파이썬의 확장 기능 가운데 하나인 NumPy 의 선형 대수 기능 `solve()` 를 사용하여 해를 구해 보자.

Using `solve()` of linear algebra subpackage of `NumPy`, a Python package, let's find a solution.

```python
sol = nl.solve(matrix, vector)

```

```python
sol

```

이렇게 찾은 `sol`이 원래의 연립방정식을 만족하는지 확인해보자.

Let's check whether `sol` found this way satisfies the original system of equations.

```python
import numpy.testing as nt

nt.assert_array_almost_equal(matrix @ sol, vector)

```

## Final Bell
마지막 종

```python
# stackoverfow.com/a/24634221
import os
os.system("printf '\a'");

```

```python

```


# A Gentle Introduction to HARK

This notebook provides a simple, hands-on tutorial for first time HARK users -- and potentially first time Python users.  It does not go "into the weeds" - we have hidden some code cells that do boring things that you don't need to digest on your first experience with HARK.  Our aim is to convey a feel for how the toolkit works.

For readers for whom this is your very first experience with Python, we have put important Python concepts in **boldface**. For those for whom this is the first time they have used a Jupyter notebook, we have put Jupyter instructions in _italics_. Only cursory definitions (if any) are provided here.  If you want to learn more, there are many online Python and Jupyter tutorials.

```python
# This cell has a bit of initial setup. You can click the triangle to the left to expand it.
# Click the "Run" button immediately above the notebook in order to execute the contents of any cell
# WARNING: Each cell in the notebook relies upon results generated by previous cells
#   The most common problem beginners have is to execute a cell before all its predecessors
#   If you do this, you can restart the kernel (see the "Kernel" menu above) and start over

import matplotlib.pyplot as plt
import numpy as np
import HARK
from copy import deepcopy
mystr = lambda number : "{:.4f}".format(number)
from HARK.utilities import plot_funcs
```

## Your First HARK Model: Perfect Foresight

We start with almost the simplest possible consumption model: A consumer with CRRA utility

\begin{equation}
U(C) = \frac{C^{1-\rho}}{1-\rho}
\end{equation}

has perfect foresight about everything except the (stochastic) date of death, which occurs with constant probability implying a "survival probability" $\newcommand{\LivPrb}{\aleph}\LivPrb < 1$.  Permanent labor income $P_t$ grows from period to period by a factor $\Gamma_t$.  At the beginning of each period $t$, the consumer has some amount of market resources $M_t$ (which includes both market wealth and currrent income) and must choose how much of those resources to consume $C_t$ and how much to retain in a riskless asset $A_t$ which will earn return factor $R$. The agent's flow of utility $U(C_t)$ from consumption is geometrically discounted by factor $\beta$. Between periods, the agent dies with probability $\mathsf{D}_t$, ending his problem.

The agent's problem can be written in Bellman form as:

\begin{eqnarray*}
V_t(M_t,P_t) &=& \max_{C_t}~U(C_t) + \beta \aleph V_{t+1}(M_{t+1},P_{t+1}), \\
& s.t. & \\
%A_t &=& M_t - C_t, \\
M_{t+1} &=& R (M_{t}-C_{t}) + Y_{t+1}, \\
P_{t+1} &=& \Gamma_{t+1} P_t, \\
\end{eqnarray*}

A particular perfect foresight agent's problem can be characterized by values of risk aversion $\rho$, discount factor $\beta$, and return factor $R$, along with sequences of income growth factors $\{ \Gamma_t \}$ and survival probabilities $\{\mathsf{\aleph}_t\}$.  To keep things simple, let's forget about "sequences" of income growth and mortality, and just think about an $\textit{infinite horizon}$ consumer with constant income growth and survival probability.

## Representing Agents in HARK

HARK represents agents solving this type of problem as $\textbf{instances}$ of the $\textbf{class}$ $\texttt{PerfForesightConsumerType}$, a $\textbf{subclass}$ of $\texttt{AgentType}$.  To make agents of this class, we must import the class itself into our workspace.  (Run the cell below in order to do this).

```python
from HARK.ConsumptionSaving.ConsIndShockModel import PerfForesightConsumerType
```

    /home/sb/projects/econ-ark/HARK/HARK/Calibration/Income/IncomeTools.py:449: UserWarning: Sabelhaus and Song (2010) provide variance profiles for ages 27 to 54. Extrapolating variances using the extreme points.
      warn(
    /home/sb/projects/econ-ark/HARK/HARK/datasets/SCF/WealthIncomeDist/SCFDistTools.py:79: UserWarning: Returning SCF summary statistics for ages (20,25].
      warn("Returning SCF summary statistics for ages " + age_bracket + ".")

The $\texttt{PerfForesightConsumerType}$ class contains within itself the python code that constructs the solution for the perfect foresight model we are studying here, as specifically articulated in [these lecture notes](http://www.econ2.jhu.edu/people/ccarroll/public/lecturenotes/consumption/PerfForesightCRRA/).

To create an instance of $\texttt{PerfForesightConsumerType}$, we simply call the class as if it were a function, passing as arguments the specific parameter values we want it to have.  In the hidden cell below, we define a $\textbf{dictionary}$ named $\texttt{PF_dictionary}$ with these parameter values:

| Param | Description | Code | Value |
| :---: | ---  | --- | :---: |
| $\rho$ | Relative risk aversion | $\texttt{CRRA}$ | 2.5 |
| $\beta$ | Discount factor | $\texttt{DiscFac}$ | 0.96 |
| $R$ | Risk free interest factor | $\texttt{Rfree}$ | 1.03 |
| $\aleph$ | Survival probability | $\texttt{LivPrb}$ | 0.98 |
| $\Gamma$ | Income growth factor | $\texttt{PermGroFac}$ | 1.01 |

For now, don't worry about the specifics of dictionaries.  All you need to know is that a dictionary lets us pass many arguments wrapped up in one simple data structure.

```python
# This cell defines a parameter dictionary. You can expand it if you want to see what that looks like.
PF_dictionary = {
    'CRRA' : 2.5,
    'DiscFac' : 0.96,
    'Rfree' : 1.03,
    'LivPrb' : [0.98],
    'PermGroFac' : [1.01],
    'T_cycle' : 1,
    'cycles' : 0,
    'AgentCount' : 10000
}

# To those curious enough to open this hidden cell, you might notice that we defined
# a few extra parameters in that dictionary: T_cycle, cycles, and AgentCount. Don't
# worry about these for now.
```

Let's make an **object** named $\texttt{PFexample}$ which is an **instance** of the $\texttt{PerfForesightConsumerType}$ class. The object $\texttt{PFexample}$ will bundle together the abstract mathematical description of the solution embodied in $\texttt{PerfForesightConsumerType}$, and the specific set of parameter values defined in $\texttt{PF_dictionary}$.  Such a bundle is created passing $\texttt{PF_dictionary}$ to the class $\texttt{PerfForesightConsumerType}$:

```python
PFexample = PerfForesightConsumerType(**PF_dictionary)
# the asterisks ** basically say "here come some arguments" to PerfForesightConsumerType
```

In $\texttt{PFexample}$, we now have _defined_ the problem of a particular infinite horizon perfect foresight consumer who knows how to solve this problem.

## Solving an Agent's Problem

To tell the agent actually to solve the problem, we call the agent's $\texttt{solve}$ **method**. (A method is essentially a function that an object runs that affects the object's own internal characteristics -- in this case, the method adds the consumption function to the contents of $\texttt{PFexample}$.)

The cell below calls the $\texttt{solve}$ method for $\texttt{PFexample}$

```python
PFexample.solve()
```

Running the $\texttt{solve}$ method creates the **attribute** of $\texttt{PFexample}$ named $\texttt{solution}$.  In fact, every subclass of $\texttt{AgentType}$ works the same way: The class definition contains the abstract algorithm that knows how to solve the model, but to obtain the particular solution for a specific instance (paramterization/configuration), that instance must be instructed to $\texttt{solve()}$ its problem.

The $\texttt{solution}$ attribute is always a $\textit{list}$ of solutions to a single period of the problem. In the case of an infinite horizon model like the one here, there is just one element in that list -- the solution to all periods of the infinite horizon problem.  The consumption function stored as the first element (element 0) of the solution list can be retrieved by:

```python
PFexample.solution[0].cFunc
```

    <HARK.interpolation.LinearInterp at 0x7f38dd5b7f70>

One of the results proven in the associated [the lecture notes](http://www.econ2.jhu.edu/people/ccarroll/public/lecturenotes/consumption/PerfForesightCRRA/) is that, for the specific problem defined above, there is a solution in which the _ratio_ $c = C/P$ is a linear function of the _ratio_ of market resources to permanent income, $m = M/P$.

This is why $\texttt{cFunc}$ can be represented by a linear interpolation.  It can be plotted between an $m$ ratio of 0 and 10 using the command below.

```python
mPlotTop=10
plot_funcs(PFexample.solution[0].cFunc,0.,mPlotTop)
```

The figure illustrates one of the surprising features of the perfect foresight model: A person with zero money should be spending at a rate more than double their income (that is, $\texttt{cFunc}(0.) \approx 2.08$ - the intersection on the vertical axis).  How can this be?

The answer is that we have not incorporated any constraint that would prevent the agent from borrowing against the entire PDV of future earnings-- human wealth.  How much is that?  What's the minimum value of $m_t$ where the consumption function is defined?  We can check by retrieving the $\texttt{hNrm}$ **attribute** of the solution, which calculates the value of human wealth normalized by permanent income:

```python
humanWealth = PFexample.solution[0].hNrm
mMinimum = PFexample.solution[0].mNrmMin
print("This agent's human wealth is " + str(humanWealth) + ' times his current income level.')
print("This agent's consumption function is defined (consumption is positive) down to m_t = " + str(mMinimum))
```

    This agent's human wealth is 50.49994992551661 times his current income level.
    This agent's consumption function is defined (consumption is positive) down to m_t = -50.49994992551661

Yikes! Let's take a look at the bottom of the consumption function.  In the cell below, the bounds of the `plot_funcs` function are set to display down to the lowest defined value of the consumption function.

```python
plot_funcs(PFexample.solution[0].cFunc,
          mMinimum,
          mPlotTop)
```

## Changing Agent Parameters

Suppose you wanted to change one (or more) of the parameters of the agent's problem and see what that does.  We want to compare consumption functions before and after we change parameters, so let's make a new instance of $\texttt{PerfForesightConsumerType}$ by copying $\texttt{PFexample}$.

```python
NewExample = deepcopy(PFexample)
```

You can assign new parameters to an `AgentType` with the `assign_parameter` method. For example, we could make the new agent less patient:

```python
NewExample.assign_parameters(DiscFac = 0.90)
NewExample.solve()
mPlotBottom = mMinimum
plot_funcs([PFexample.solution[0].cFunc,
           NewExample.solution[0].cFunc],
          mPlotBottom,
          mPlotTop)
```

(Note that you can pass a **list** of functions to `plot_funcs` as the first argument rather than just a single function. Lists are written inside of [square brackets].)

Let's try to deal with the "problem" of massive human wealth by making another consumer who has essentially no future income.  We can virtually eliminate human wealth by making the permanent income growth factor $\textit{very}$ small.

In $\texttt{PFexample}$, the agent's income grew by 1 percent per period -- his $\texttt{PermGroFac}$ took the value 1.01. What if our new agent had a growth factor of 0.01 -- his income __shrinks__ by 99 percent each period?  In the cell below, set $\texttt{NewExample}$'s discount factor back to its original value, then set its $\texttt{PermGroFac}$ attribute so that the growth factor is 0.01 each period.

Important: Recall that the model at the top of this document said that an agent's problem is characterized by a sequence of income growth factors, but we tabled that concept.  Because $\texttt{PerfForesightConsumerType}$ treats $\texttt{PermGroFac}$ as a __time-varying__ attribute, it must be specified as a **list** (with a single element in this case).

```python
# Revert NewExample's discount factor and make his future income minuscule
# print("your lines here")

# Compare the old and new consumption functions
plot_funcs([PFexample.solution[0].cFunc,NewExample.solution[0].cFunc],0.,10.)
```

Now $\texttt{NewExample}$'s consumption function has the same slope (MPC) as $\texttt{PFexample}$, but it emanates from (almost) zero-- he has basically no future income to borrow against!

If you'd like, use the cell above to alter $\texttt{NewExample}$'s other attributes (relative risk aversion, etc) and see how the consumption function changes.  However, keep in mind that \textit{no solution exists} for some combinations of parameters.  HARK should let you know if this is the case if you try to solve such a model.

## Your Second HARK Model: Adding Income Shocks

Linear consumption functions are pretty boring, and you'd be justified in feeling unimpressed if all HARK could do was plot some lines.  Let's look at another model that adds two important layers of complexity: income shocks and (artificial) borrowing constraints.

Specifically, our new type of consumer receives two income shocks at the beginning of each period: a completely transitory shock $\theta_t$ and a completely permanent shock $\psi_t$.  Moreover, lenders will not let the agent borrow money such that his ratio of end-of-period assets $A_t$ to permanent income $P_t$ is less than $\underline{a}$.  As with the perfect foresight problem, this model can be framed in terms of __normalized__ variables, e.g. $m_t \equiv M_t/P_t$.  (See [here](http://www.econ2.jhu.edu/people/ccarroll/papers/BufferStockTheory/) for all the theory).

\begin{eqnarray*}
v_t(m_t) &=& \max_{c_t} ~ U(c_t) ~ +  \phantom{\LivFac} \beta \mathbb{E} [(\Gamma_{t+1}\psi_{t+1})^{1-\rho} v_{t+1}(m_{t+1}) ], \\
a_t &=& m_t - c_t, \\
a_t &\geq& \underset{\bar{}}{a}, \\
m_{t+1} &=& R/(\Gamma_{t+1} \psi_{t+1}) a_t + \theta_{t+1}, \\
\mathbb{E}[\psi]=\mathbb{E}[\theta] &=& 1, \\
u(c) &=& \frac{c^{1-\rho}}{1-\rho}.
\end{eqnarray*}

HARK represents agents with this kind of problem as instances of the class $\texttt{IndShockConsumerType}$.  To create an $\texttt{IndShockConsumerType}$, we must specify the same set of parameters as for a $\texttt{PerfForesightConsumerType}$, as well as an artificial borrowing constraint $\underline{a}$ and a sequence of income shocks. It's easy enough to pick a borrowing constraint -- say, zero -- but how would we specify the distributions of the shocks?  Can't the joint distribution of permanent and transitory shocks be just about anything?

_Yes_, and HARK can handle whatever correlation structure a user might care to specify.  However, the default behavior of $\texttt{IndShockConsumerType}$ is that the distribution of permanent income shocks is mean one lognormal, and the distribution of transitory shocks is mean one lognormal augmented with a point mass representing unemployment.  The distributions are independent of each other by default, and by default are approximated with $N$ point equiprobable distributions.

Let's make an infinite horizon instance of $\texttt{IndShockConsumerType}$ with the same parameters as our original perfect foresight agent, plus the extra parameters to specify the income shock distribution and the artificial borrowing constraint. As before, we'll make a dictionary:

| Param | Description | Code | Value |
| :---: | --- | --- |  :---: |
| \underline{a} | Artificial borrowing constraint | $\texttt{BoroCnstArt}$ | 0.0 |
| $\sigma_\psi$ | Underlying stdev of permanent income shocks | $\texttt{PermShkStd}$ | 0.1 |
| $\sigma_\theta$ | Underlying stdev of transitory income shocks | $\texttt{TranShkStd}$ | 0.1 |
| $N_\psi$ | Number of discrete permanent income shocks | $\texttt{PermShkCount}$ | 7 |
| $N_\theta$ | Number of discrete transitory income shocks | $\texttt{TranShkCount}$ | 7 |
| $\mho$ | Unemployment probability | $\texttt{UnempPrb}$ | 0.05 |
| $\underset{\bar{}}{\theta}$ | Transitory shock when unemployed | $\texttt{IncUnemp}$ | 0.3 |

```python
# This cell defines a parameter dictionary for making an instance of IndShockConsumerType.

IndShockDictionary = {
    'CRRA': 2.5,          # The dictionary includes our original parameters...
    'Rfree': 1.03,
    'DiscFac': 0.96,
    'LivPrb': [0.98],
    'PermGroFac': [1.01],
    'PermShkStd': [0.1],  # ... and the new parameters for constructing the income process.
    'PermShkCount': 7,
    'TranShkStd': [0.1],
    'TranShkCount': 7,
    'UnempPrb': 0.05,
    'IncUnemp': 0.3,
    'BoroCnstArt': 0.0,
    'aXtraMin': 0.001,    # aXtra parameters specify how to construct the grid of assets.
    'aXtraMax': 50.,      # Don't worry about these for now
    'aXtraNestFac': 3,
    'aXtraCount': 48,
    'aXtraExtra': [None],
    'vFuncBool': False,   # These booleans indicate whether the value function should be calculated
    'CubicBool': False,   # and whether to use cubic spline interpolation. You can ignore them.
    'aNrmInitMean' : -10.,
    'aNrmInitStd' : 0.0,  # These parameters specify the (log) distribution of normalized assets
    'pLvlInitMean' : 0.0, # and permanent income for agents at "birth". They are only relevant in
    'pLvlInitStd' : 0.0,  # simulation and you don't need to worry about them.
    'PermGroFacAgg' : 1.0,
    'T_retire': 0,        # What's this about retirement? ConsIndShock is set up to be able to
    'UnempPrbRet': 0.0,   # handle lifecycle models as well as infinite horizon problems. Swapping
    'IncUnempRet': 0.0,   # out the structure of the income process is easy, but ignore for now.
    'T_age' : None,
    'T_cycle' : 1,
    'cycles' : 0,
    'AgentCount': 10000,
    'tax_rate':0.0,
}

# Hey, there's a lot of parameters we didn't tell you about!  Yes, but you don't need to
# think about them for now.
```

As before, we need to import the relevant subclass of $\texttt{AgentType}$ into our workspace, then create an instance by passing the dictionary to the class as if the class were a function.

```python
from HARK.ConsumptionSaving.ConsIndShockModel import IndShockConsumerType
IndShockExample = IndShockConsumerType(**IndShockDictionary)
```

Now we can solve our new agent's problem just like before, using the $\texttt{solve}$ method.

```python
IndShockExample.solve()
plot_funcs(IndShockExample.solution[0].cFunc,0.,10.)
```

## Changing Constructed Attributes

In the parameter dictionary above, we chose values for HARK to use when constructing its numeric representation of $F_t$, the joint distribution of permanent and transitory income shocks. When $\texttt{IndShockExample}$ was created, those parameters ($\texttt{TranShkStd}$, etc) were used by the **constructor** or **initialization** method of $\texttt{IndShockConsumerType}$ to construct an attribute called $\texttt{IncomeDstn}$.

Suppose you were interested in changing (say) the amount of permanent income risk.  From the section above, you might think that you could simply change the attribute $\texttt{TranShkStd}$, solve the model again, and it would work.

That's _almost_ true-- there's one extra step. $\texttt{TranShkStd}$ is a primitive input, but it's not the thing you _actually_ want to change. Changing $\texttt{TranShkStd}$ doesn't actually update the income distribution... unless you tell it to (just like changing an agent's preferences does not change the consumption function that was stored for the old set of parameters -- until you invoke the $\texttt{solve}$ method again).  In the cell below, we invoke the method $\texttt{update_income_process}$ so HARK knows to reconstruct the attribute $\texttt{IncomeDstn}$.

```python
OtherExample = deepcopy(IndShockExample)  # Make a copy so we can compare consumption functions
OtherExample.assign_parameters(PermShkStd = [0.2])           # Double permanent income risk (note that it's a one element list)
OtherExample.update_income_process()        # Call the method to reconstruct the representation of F_t
OtherExample.solve()
```

    GPFPF            = 0.985648
    GPFInd           = 1.023116
    GPFAgg           = 0.965935
    Thorn = APF      = 0.995505
    PermGroFacAdj    = 0.973012
    uInvEpShkuInv    = 0.954556
    FVAF             = 0.898046
    WRPF             = 0.289257
    DiscFacGPFIndMax = 0.906690
    DiscFacGPFAggMax = 1.015641

In the cell below, use your blossoming HARK skills to plot the consumption function for $\texttt{IndShockExample}$ and $\texttt{OtherExample}$ on the same figure.

```python
# Use the line(s) below to plot the consumptions functions against each other
```


# Stochastic processes, Stochastic Calculus and Itô's Lemma

$Def\DeclareMathOperator*{\argmin}{argmin}
\DeclareMathOperator*{\argmax}{argmax}
\newcommand{\using}[1]{\stackrel{\mathrm{#1}}{=}}
\newcommand{\ffrac}{\displaystyle \frac}
\newcommand{\space}{\text{ }}
\newcommand{\bspace}{\;\;\;\;}
\newcommand{\QQQ}{\boxed{?\:}}
\newcommand{\void}{\left.\right.}
\newcommand{\CB}[1]{\left\{ #1 \right\}}
\newcommand{\SB}[1]{\left[ #1 \right]}
\newcommand{\P}[1]{\left( #1 \right)}
\newcommand{\dd}{\mathrm{d}}
\newcommand{\Exp}{\mathrm{E}}
\newcommand{\Tran}[1]{{#1}^{\mathrm{T}}}
\newcommand{\d}[1]{\displaystyle{#1}}
\newcommand{\EE}[2][\,\!]{\mathbb{E}_{#1}\left[#2\right]}
\renewcommand{\FcF}{\mathcal{F}}
\renewcommand{\AsA}{\mathscr{A}}
\renewcommand{\FsF}{\mathscr{F}}
\newcommand{\Var}[2][\,\!]{\mathrm{Var}_{#1}\left[#2\right]}
\newcommand{\Cov}[2][\,\!]{\mathrm{Cov}_{#1}\left(#2\right)}
\newcommand{\Corr}[2][\,\!]{\mathrm{Corr}_{#1}\left(#2\right)}
\newcommand{\I}[1]{\mathrm{I}\left( #1 \right)}
\newcommand{\N}[1]{\mathrm{N} \left( #1 \right)}
\newcommand{\ow}{\text{otherwise}}$

Given the $r.v.$, we define a probability triple, $\P{\Omega, \FcF, \mathbb{P}}$, here $\Omega$ is the ***sample space***, $\FcF$ is a *collection* of subset of $\Omega$, the ***events***, $\Omega \in \FcF$. And $\mathbb{P}$ is the probability of each event, say $A \in \FcF$.

$Def$ *Measurable*

A real-valued $r.v.$ $X$ is a real-valued function on $\Omega$ that is $\FcF$***-measureable***, meaning that $\forall x \in \mathbb{R}, \CB{\omega \in \Omega: X\P{\omega} \leq x }\in \FcF$. The notation is $X \in \FcF$. The distribution can be defined $F\P{x} = \mathbb{P}\SB{X \leq x}$

## Stochastic Processes

$Def$ *Stochastic Processes*

A collection of $r.v.$ that are all defined on a common sample space $\Omega$ and indexed by $t \in I$:

$$\CB{X_t}_{t\in I}, \forall t \in I, X_t : \Omega \to \mathbb{R}$$

- Discrete time process: $I$ discrete
- Continuous time process: $I$ continuous
- Continuous variable process: the underlying variable $X$ can take any value within a certain range.
- Discrete variable process: certain discrete values are possible.

### Discrete Time Stochastic Processes

**e.g.** *Random Walk*

$\bspace$Let $X_1,X_2,\dots$ be $i.i.d.$ distributed $r.v.$ and let $S_n = \sum\limits_{i=1}^n X_i$. Here $S_n$ is the ***random walk***, and the special case when $\mathbb{P}\SB{X_i = 1} = \mathbb{P}\SB{X_i = -1} = 0.5$ is called ***simple random walk***.

$Def$ *filtration*

To specify a discrete time stochastic process, we require not just a single $\sigma$-filde, but an increasing sequence of them, $\FcF_n \subset \FcF_{n+1} \subset \cdots \subset \FcF$. Then the collection $\CB{\FcF_n}_{n\geq 0}$ is the ***filtration***.

$Def$ *adapted*

We call the **discrete time stochastic process** $\CB{X_n}_{n\geq 0}$ ***adapted*** to the **filtration** $\CB{\FcF_n}_{n\geq 0}$ if $X_n$ is $\FcF_n$-measurable for each $n$.

$Remark$

> $\FcF_n$ can be regarded as encoding all the information about the evolution of the stochastic process up until time $n$. That is, if we know whether each event in $\FcF_n$ happens or not then we can infer the path followed by the stochastic process up until time $n$.

$Def$ *conditional expectation*

The ***conditional expectation*** of $X$ is $\Exp\SB{X\mid \FcF_n}$, given the information available at time $n$.

- Linearity: $\Exp\SB{c_1 X + c_2 Y \mid \FcF_n} = c_1 \Exp\SB{X \mid \FcF_n} + c_2 \Exp\SB{Y \mid \FcF_n}$
- We can taking out what we've known: if $Y$ is $\FcF_n$-measurable, then $\Exp\SB{YX \mid \FcF_n} = Y\Exp\SB{X \mid \FcF_n}$
- Law of iterated expectations: for $0 \leq n \leq m, \Exp\SB{\Exp\SB{X\mid\FcF_m}\mid \FcF_n} = \Exp\SB{X\mid \FcF_n}$
- Independence: if $X$ is independent of $\FcF_n$, then $\Exp\SB{X \mid \FcF_n} = \Exp\SB{X}$

$Remark$

>["The smaller field wins"](https://ocw.mit.edu/courses/sloan-school-of-management/15-070j-advanced-stochastic-processes-fall-2013/lecture-notes/MIT15_070JF13_Lec9.pdf). Actually, for $0 \leq n \leq m$, or $\FcF_n \subset \FcF_m$, we have $\Exp\SB{\Exp\SB{X\mid\FcF_m}\mid \FcF_n} = \Exp\SB{X\mid \FcF_n}$ and $\Exp\SB{\Exp\SB{X\mid\FcF_n}\mid \FcF_m} = \Exp\SB{X\mid \FcF_n}$. The complete proof is not as simple as you may think, so go to see the ref link.

## Martingale

$Def$ *Martingale鞅*

A discrete time stochastic process $\CB{X_n, n = 0,1,2,\dots}$ is a $P$***-martingale***, if

1. $\Exp^P\SB{|X_n|} < \infty, \forall n$
2. $\Exp^P\SB{X_{n+1} \mid \FcF_n} = X_n, \forall n$
3. $\CB{X_n}_{n\geq 0}$ is **adapted** to the **filtration** $\CB{\FcF_n}_{n\geq 0}$, or, $X_n$ is $\FcF_n$-measurable for each $n$

**e.g.**  Discounted stock process in Binomial model

We define $\bar S_n = \ffrac{S_n} { e^{rn}}$, then $\Exp^Q\SB{\bar S_{n+1} \mid \FcF_n} = \bar{S}_n$. Thus $\bar S_n$ is a ***Q-martingale***.

$Proof$

> From the definition of $Q$***-martingale***, we first have $ S_0 = e^{-rT}\Exp^Q\SB{S_T}$. While in this chapter, we have $ \ffrac{S_0} {e^{r\cdot 0}} = \Exp^Q\SB{\big.\ffrac{S_T} {e^{rT}}\big| \FcF_T}$. And thus, for any $t$, we have $\bar S_0 = \Exp^Q\SB{\bar S_t \mid \FcF_t}$
>
>And by the property of Stochastic Process, we have $\Exp^Q\SB{\bar S_{n+1} \mid \FcF_n} = \bar{S}_n$, that's it.

$Remark$

>For the binomial model, the discounted price process of a European option is also a $Q$**-martingale**, say $c\P{t}$ the value of a European call option at time maturity $T$, then $c\P{n} = e^{-r\P{T-n}}\EE[Q]{\P{S_T - K}^+ \mid \FcF_n}$ thus $\bar c\P{n} = \ffrac{c\P{n}}{e^{rn}} = e^{-rT}\EE[Q]{\P{S_T - K}^+ \mid \FcF_n}$ is a $Q$**-martingale**.

**e.g.(V)** Simple random walk

This simple random walk $S_n$ is a $\mathbb{P}\newcommand{\abs}[1]{\left| #1 \right|}$-martingale.

>$$\Exp^P\SB{\abs{S_n}} = \Exp^P\SB{\abs{\sum X_n}} = \sum \Exp^P\SB{\abs{X_n}} = n < \infty$$
>
>$$\Exp^P\SB{S_{n+1}\mid\FcF_n} = \ffrac{1} {2}\P{S_n+1} + \ffrac{1} {2}\P{S_n-1} = S_n$$
>
>Also, $X_n$ is $\FcF_n$-measuable then so is $S_n$, for each $n$. Then all three conditions are satisfied! Done!

$Def$

***Supermartingale***: $\forall n, \Exp\SB{|X_n|} < \infty$ and $\forall n, \Exp\SB{X_{n+1}\mid\FcF_n} \leq X_n$

***Submartingale***: $\forall n, \Exp\SB{|X_n|} < \infty$ and $\forall n, \Exp\SB{X_{n+1}\mid\FcF_n} \geq X_n$

## Stopping time

$Def$

Given a sample space $\Omega$ equipped with a filtration $\CB{\FcF_n}_{n \geq 0}$, a ***stopping time*** is a random variable $\tau: \Omega \to \mathbb{Z}_+$ with the property that

$$\CB{\tau \leq n} \in \FcF_n, \forall n\geq 0$$

So we can decide whether or not $\tau\leq n$ based on the information available at time $n$.

**e.g.** Simple random walk

>We can define $\tau_1$ to be the first time that the random walk takes the value $1$, that is $\tau_1 = \inf \CB{i \geq 0: S_i = 1}$. It's a **stopping time** because
>
>$$\CB{\tau_1 \leq n} = \bigcup_{i=1}^{n}\CB{S_i = 1} \in \FcF_n$$

### American options revisited

Consider an American option that pays $g\P{\tau}$ dollars if exercised at $\tau \leq T$, that is $g\P{\tau} = \P{S_\tau - K}^+$.

The value of American options at time $t$ is given by: $V\P{t} = \d{\max_{t \leq u \leq T}}\Exp^Q\SB{\ffrac{g\P{u}} {e^{r\P{u-t}}}\mid \FcF_t}$

And the Optimal exercise time: $\tau = \min\CB{u \mid V\P{u} = g\P{u}}$ and it's obviously a **stopping time**.

For the multiperiod binomial model, the American option pricing formula is given by the dynamic programming algorithm (see the example in last chapter for more details)

$$\begin{align}
V\P{T} &= g\P{T}\\
V\P{t} &= \max\CB{g\P{t}, e^{-r}\SB{qV^{u}\P{t+1}+ \P{1-q}V^{d}\P{t+1}}}
\end{align}$$

$Lemma.3\newcommand{\AcA}{\mathscr{A}}
\newcommand{\FcF}{\mathscr{F}}$

The discounted price process of American options is a **Q-supermartingale**.

$Proof$

> Let $\tau_{t+1}$ be the optimal exercise time for price $V\P{t+1}$ and write $\Exp^Q\SB{\cdot\mid\FcF_t}$ by $\Exp^Q_t\SB{\cdot}$ for notational convenience, then
>
>$$V\P{t+1} = \max_{t+1 \leq u \leq T} \Exp^Q_{t+1}\SB{\ffrac{g\P{u}} {e^{r\P{u-\P{t+1}}}}} = \Exp^Q_{t+1}\SB{\ffrac{g\P{\tau_{t+1}}} {e^{r\P{\tau_{\void_{t+1}}\:-\P{t+1}}}}}$$
>
>Then we have the inequity for the discounted price:

>$$\begin{align}
\Exp^Q_t\SB{\ffrac{V\P{t+1}} {e^{r\P{t+1}}}} &= \Exp^Q_t\SB{ \Exp^Q_{t+1} \SB{\ffrac{g\P{\tau_{t+1}}} {e^{r\cdot\tau_{\void_{t+1}}}}}} \\
&= \Exp^Q_{t}\SB{\ffrac{g\P{\tau_{t+1}}} {e^{r\cdot\tau_{\void_{t+1}}}}} \leq \Exp^Q_{t}\SB{\ffrac{g\P{\tau_{t}}} {e^{r\cdot\tau_{\void_{t}}}}}\\
&= \max_{t\leq u\leq T} \Exp^Q_t\SB{\ffrac{g\P{u}} {e^{ru}}} = \ffrac{V\P{t}} {e^{rt}}
\end{align}$$

## Continuous Time Stochastic Processes

With tradings happen in such short time intervals, the market security prices can be approximately modeled as continuous time processes.

Basic process: Brownian motion (or the Wiener process).

$Def$

A **continuous time stochastic process** is a ***standard Brownian motion***, if

- $\forall t >s$, the increment $W_t - W_s$ has the normal distribution $\N{0,t-s}$
- The process $W_t$ has independent increments: for any set of times $0 \leq t_1 < t_2 < \cdots < t_n$, the $r.v.$ $W_{t_{\void_2}} - W_{t_{\void_1}}$, $W_{t_{\void_3}} - W_{t_{\void_2}}$, $\dots$, $W_{t_{\void_{n}}} - W_{t_{\void_{n-1}}}$ are independent.
- $W_0 = 0$
- The sample paths: $\CB{W_t: t \geq 0}$ are continuous function of $t$.

### Brownian Motion
We can make a little change in the first condition: $\forall t >s$, the increment $\P{\widehat W_t - \widehat W_s}$ has the normal distribution $\mathcal{N}\P{\mu\P{t-s},\sigma^2\P{t-s}}$. Then we call it the ***Brownian motion with drift*** $\mu$ ***and volatility*** $\sigma$ (not $\sigma^2$). And thus

$\bspace$Any Brownian motion with drift $\mu$ and volatility $\sigma$ can be written as $\widehat W_t = \mu t + \sigma W_t$ where $W_t$ is the standard Brownian motion.

### Martingale in Continuous Time
Let $\renewcommand{\FcF}{\mathcal{F}}\FcF$ be a $\sigma$-field. $\CB{\FcF_t}_{t \geq 0}$ is called **filtration** if:

- $\FcF_t$ is a sub-$\sigma$-field of $\FcF$ for all $t$
- $\FcF_s \subset \FcF_t$ for $s < t$

And we can also define the ***natural filtration*** $\FcF_t^X$ which encodes the information generated by the stochastic process $X$ on the interval $\SB{0,t}$. $A \in \FcF_t^X$ if based upon observations of the ***trajectory*** $\CB{X_s, 0 \leq s \leq t}$, it is possible to decide whether the event $A$ has occurred or not.

Here the notation for **trajectory** is: if the value of a random variable $Z$ can be completely determined given observations of the trajectory $\CB{X_s, 0 \leq s \leq t}$, then we write $Z \in \FcF_t^X$. What's more, roughly speaking, $Z \in \FcF_t^X$ $iff$ $Z$ is a "function" of $\CB{X_s, 0 \leq s \leq t}$. Namely, there is a "function" $g$ such that $Z = g\P{X_s, 0 \leq s \leq t}$

And we call a stochastic process $\CB{Y_t}_{t\geq 0}$ **adapted to** a filtration $\CB{\FcF_t^X}_{t \geq 0}$ if

$$Y_t \in \FcF_t^X, \bspace\forall t \geq 0$$

**e.g.1**

Define the event $A = \CB{X_s \leq 3, \forall s \leq 8}$, then $A\in$?

> $\FcF_8$

**e.g.2**

The stochastic process $Z_t = \d{ \int_{0}^{t} X_s\;\dd{s}}$ is adapted to $\CB{\FcF_t^X}_{t \geq 0}$

> Since for a fixed $t$, $\FcF_t$-measurable $\iff$ $Z_t$ has only one possible value

**e.g.3**

The stochastic process $M_t = \d{\max_{0\leq s\leq t+1} W_s}$ is not adapted to $\CB{\FcF_t^W}_{t \geq 0}$

> since when $s = t+1$, the information is not given... maybe it's adapted to $\CB{\FcF_{t+1}^W}_{t \geq 0}$

### Conditional Expectation

For notational simplicity, denote the Brownian filtration by $\CB{\FcF_t}_{t \geq 0}$. Let $\Exp\SB{X\mid \FcF_t}$ denotes the **conditional expectation** of $X$, given the information available at time $t$.

- Linearity: $\Exp\SB{c_1 X + c_2 Y \mid \FcF_t} = c_1 \Exp\SB{X\mid \FcF_t} + c_2\Exp\SB{X\mid \FcF_t}$
- Taking out: if $Y$ is $\FcF_t$-measurable, $\Exp\SB{YX\mid \FcF_t} = Y\Exp\SB{X \mid \FcF_t}$
- Law of iterated expectations: if $0 \leq s \leq t$, then $\Exp\SB{\Exp\SB{X\mid\FcF_t}\mid \FcF_s} = \Exp\SB{X \mid\FcF_s}$
- Independence: if $X$ is independent of $\FcF_t$, then $\Exp\SB{X \mid\FcF_t} = \Exp\SB{X}$

Let $\P{\Omega, \FcF, \mathbb{P}}$ be a probability space with filtration $\CB{\FcF_t}_{t\geq 0}$. Suppose

1. $\CB{M_t}_{t \geq 0}$ is a stochastic process adapted to $\CB{\FcF_t}_{t\geq 0}$
2. $\Exp\SB{\abs{M_t}}<\infty, \forall t \geq 0$
3. $\Exp^{\mathbb{P}}\SB{M_t\mid \FcF_s} = M_s$ for any $t\geq s\newcommand{\PP}{\mathbb{P}}$

then we call $\CB{M_t}_{t \geq 0}$ is a $\PP$**-martingale**.

$\PP$**-supermartingale** will be obtained by changing the last condition to $\Exp^{\mathbb{P}}\SB{M_t\mid \FcF_s} \leq M_s$ for any $t\geq s$, and $\PP$**-submartingale** can be obtained by changing the last condition to $\Exp^{\mathbb{P}}\SB{M_t\mid \FcF_s} \geq M_s$ for any $t\geq s$.

$Remark$

>It is possible that a process could be a martingale with respect to ($w.r.t.$) one measure but not another one.

Also, if $X$ is a $\PP$**-supermartingale**, then $\Exp^{\PP}\SB{M_t} \leq \Exp^{\PP}\SB{M_s}$ for all $t \geq s$. This can be obtained by taking the expectation on both sides of $\Exp^{\mathbb{P}}\SB{M_t\mid \FcF_s} = M_s$ and then use the law of iterated expectation.

$Proposition.7$

If $\Exp\SB{\abs{\xi}} < \infty$, let $M_t = \Exp\SB{\xi \mid \FcF_t}$, $t \geq 0$, then $\CB{M_t}_{t \geq 0}$ is a martingale.

$Proof$

>- $M_t = \Exp\SB{\xi \mid \FcF_t} \in \FcF_t$, the first condition satisfied
>- $\Exp\SB{\abs{M_t}} =\Exp\SB{\abs{\Exp\SB{\xi \mid \FcF_t}}} < \Exp\SB{\Exp\SB{\abs{\xi} \mid \FcF_t}} = \Exp\SB{\abs{\xi}} < \infty$, second done
>- $\forall t \geq s$, $\Exp\SB{M_t \mid \FcF_s} = \Exp\SB{\Exp\SB{\xi \mid \FcF_t} \mid \FcF_s}\using{\text{Law of iterated expectations}} \Exp\SB{\xi\mid \FcF_s} = M_s$, third done

$Lemma.4$

Let $\renewcommand{\FcF}{\mathcal{F}}\CB{W_t}_{t \geq 0}$ generate the filtration $\CB{\FcF_t}_{t \geq 0}$. If $\CB{W_t}_{t \geq 0}$ is a **standard Brownian motion** under the probability measure $\PP$, then

1. $W_t$ is a $\P{\PP, \CB{\FcF_t}_{t \geq 0}}$-martingale
2. $W_t^2 - t$ is a $\P{\PP, \CB{\FcF_t}_{t \geq 0}}$-martingale
3. $\exp\CB{\sigma W_t - \ffrac{1} {2} \sigma^2 t}$ is a $\P{\PP, \CB{\FcF_t}_{t \geq 0}}$-martingale, called an ***exponential martingale***.

$Remark$

>Sometimes we can do a "standardization" process to change non-martingale Stochastic process into a martingale

$Proof$

> Since $\CB{W_t}_{t \geq 0}$ generate the filtration we have that it adapted to $\CB{\FcF_t}_{t \geq 0}$ and so is the later two.

$\bspace \P{1}$

>For $t\geq s$, we need to prove that $\Exp\SB{W_t\mid \FcF_s} = W_s$ and this is equivalent to
>
>$$\Exp\SB{W_t - W_s\mid \FcF_s} = 0$$
>
>Here $W_t - W_s$ is a normal distributed $r.v.$. Thus by independency,
>
>$\Exp\SB{W_t - W_s\mid \FcF_s} = \mu = \Exp\SB{W_t - W_s} = 0$, then by 基本不等式, we have
>
>$\Exp\SB{\abs{W_t}}  \leq\Exp\SB{\ffrac{1+W_t^2} {2}} = \ffrac{t+1} {2} < \infty$

$Remark$

>For **Brownian motion** $W_t$, we have $\Exp\SB{W_t} = 0$, $\Exp\SB{W_t^2} = t$, $\Exp\SB{W_t^3} = 0$, $\Exp\SB{W_t^4} = 3t^2$

$\bspace \P{2}$

>$\Exp\SB{\abs{W_t^2-t}} \leq \Exp\SB{\abs{W_t^2}+\abs{t}} =\Exp\SB{\abs{W_t^2}} + \Exp\SB{\abs{t}} < \infty$
>
>$\begin{align}
\Exp_s\SB{W_t^2 - t} &= \Exp_s\SB{\P{W_t-W_s}^2} - W_s^2 + 2W_s\Exp_s\SB{W_t}-\Exp_s\SB{t-s}-s \\
&= \P{t-s}-W_s^2+2W_s^2-\P{t-s}-s\\
&= W_s^2-s\end{align}$

$\bspace \P{3}$

> We denote $M_t = \exp\CB{\sigma W_t - \ffrac{1} {2} \sigma^2 t}$. For the expectation part:
>
>$$\Exp\SB{\abs{M_t}} = \int_{-\infty}^{\infty} \exp\CB{\sigma W_t - \ffrac{1} {2} \sigma^2 t} \cdot\ffrac{1} {\sqrt{2\pi t}} \exp\CB{-\ffrac{x^2} {2t}}\dd{x} = 1 < \infty$$
>
>$$\Exp_s\SB{M_t} = \Exp_s\SB{M_s\cdot M_{t-s}} = M_s\Exp_s\SB{M_{t-s}}$$
>
>Now we prove that $\Exp_s\SB{M_{t-s}} = 1$

>$$
\begin{align}
\Exp_s\SB{M_{t-s}}&= \Exp\SB{M_{t-s}}\\
&= \Exp\SB{\exp\CB{\sigma\P{W_t-W_s} - \ffrac{1}{2}\sigma^2\P{t-s}}}\\
&= \text{MGF}_{W_{t-s}}\P{\sigma} \cdot \exp\CB{-\ffrac{1}{2}\sigma^2\P{t-s}} = 1
\end{align}$$

$Remark$

>Other results:
>
>- $\Exp\SB{\exp\CB{\sigma W_t}} = \exp\CB{\ffrac{1} {2} \sigma^2 t}$
>- $\Exp\SB{\exp\CB{\mu t + \sigma W_t}} = \exp\CB{\mu t + \ffrac{1} {2} \sigma^2 t}$
>- The $\text{MGF}$ for $X_t \sim \mathcal{N}\P{\mu t,\sigma^2t}$: $\Exp\SB{\exp\CB{\omega X_t}} = \exp\CB{\omega \mu t + \ffrac{1} {2} \omega^2\sigma^2t}$
***

## Stochastic Calculus

Suppose the stock price is $S_t = f\P{t,W_t}$. Using **Taylor's Theorem** we have

$$\begin{align}
f\P{t + \Delta t,W_{t+\Delta t}} - f\P{t,W_t} &= \Delta_t \ffrac{\partial f\P{t,W_t}} {\partial t} + O\P{\Delta_t^2} + \P{W_{t+\Delta t}-W_t}\ffrac{\partial f\P{t,W_t}} {\partial W} \\
&\bspace+ \ffrac{1} {2}\P{W_{t+\Delta t}-W_t}^2 \ffrac{\partial^2 f\P{t,W_t}} {\partial W^2} +\cdots \\
&= \Delta_t f_t\P{t,W_t} + O\P{\Delta_t^2} + \P{W_{t+\Delta t}-W_t} f_W\P{t,W_t} + \ffrac{1} {2}\P{W_{t+\Delta t}-W_t}^2 f_{WW}\P{t,W_t}
\end{align}$$

$W_t$ is the Brownian motion so that $\Exp\SB{\P{W_{t+\Delta t} - W_t}^2} = \Delta_t$. And as for the first derivative, if $\P{W_{t+\Delta t}-W_t}^2$ is $O\P{\Delta_t}$ then $W_{t+\Delta t}-W_t$ should be $O\P{\sqrt{\Delta t}}$ which could lead to **unbounded changes** in $S_t$ over a bounded time interval. So we conclude that

$$\dd{S_t} = f_t \P{t,W_t} \dd{t} +f_W\P{t,W_t} \dd{W_t} + \ffrac{1} {2}\: f_{WW} \P{t,W_t} \dd{t}\\
\Downarrow \\
S_t = S_0 + \int_{0}^{t} f_s\P{s,W_s}\dd{s} + \boxed{\int_{0}^{t} f_W\P{s,W_s}\dd{W_s}} + \int_{0}^{t} \ffrac{1} {2} \: f_{WW}\P{s,W_s}\dd{s}$$

Notice that the boxed term is a stochastic integral. Now we seek to make sense of that.

### Itô Integral for Simple Processes

We define the ***Itô Integral*** for the ***simple integrands*** $h\P{s,W_s}$ and then extend it to nonsimple integrands. This happens because the **Brownian motion** is *nowhere differentiable*.

First we let $\Pi = \CB{t_0,t_1,\dots,t_n}$ denote the ***partition*** of $\SB{0,T}$ and assume that $h\P{s,W_s}$ is constant in $s$ on each interval. For this process $h\P{s,W_s}$ we call it the ***simple process*** (I think this might be the same with **simple integrands**).

Let $t_0, t_1,\dots,$ be the trading dates and $h\P{s,W_s}$ can be regarded as the position taken in the asset at each trading date and held to the next trading date. Then the gains are

$$
h\P{s,W_s} = \sum_{j=0}^{n-1} \newcommand{\II}{\mathbb{I}}\mathbf{1}_{[t_{\void_j},t_{\void_{j+1}}\:)}\P{s}\cdot h\P{t_j,W_{t_{\void_j}}}
\Rightarrow
\begin{cases}
I\P{t} = h\P{t_0,W_{t_{\void_0}}} \SB{W_t - W_0} & 0 \leq t \leq t_1\\
I\P{t} = h\P{t_0,W_{t_{\void_0}}} \SB{W_{t_{\void_1}} - W_0} + h\P{t_1,W_{t_{\void_1}}} \SB{W_t - W_{t_{\void_1}}} & t_1 \leq t \leq t_2\\
\bspace \cdots & \space\\
I\P{t} = \d{\sum_{j=0}^{k-1}h\P{t_j, W_{t_j}}\SB{W_{t_{\void_{j+1}}} - W_{t_{\void_j}}}} + h\P{t_k,W_{t_{\void_k}}} \SB{W_t - W_{t_{\void_k}}} &  t_k \leq t \leq t_{\void_{k+1}}
\end{cases}$$

Thus for this simple integrand $h\P{t,W_t}$ we have its Itô integral is just this process $I\P{t}$:

$$I\P{t} = \sum_{j=0}^{k-1}h\P{t_j, W_{t_j}}\SB{W_{t_{\void_{j+1}}} - W_{t_{\void_j}}} + h\P{t_k,W_{t_{\void_k}}} \SB{W_t - W_{t_{\void_k}}} = \int_{0}^{t} h\P{s, W_s}\dd{W_s}$$

### Itô Integral for General Processes

$Assumption$

1. $h\P{t,W_t}$ is adapted to the filtration $\FcF_t$
2. $\Exp\SB{\d{\int_{0}^{T} h^2\P{t,W_t}\dd{t}}}<\infty$

then we'll construct the Itô Integral the same way, spliting the time interval and consider

$$I_n\P{t} = \sum_{j=0}^{n-1} h\P{t_j,W_{t_{\void_{j}}}}\SB{W_{t_{\void_{j+1}}} - W_{t_{\void_{j}}}}$$

Itô, this person showed that there is a limit $I(t)$ such that $\Exp\SB{\P{I_n\P{t} - I\P{t}}^2} \to\infty$, and called the limit: the ***Itô integral***

$$I\P{t} = \int_{0}^{t} h\P{s,W_s}\dd{W_s}$$

$Remark$

>A kindly notice: $W_t$ is a **Brownian motion**.

### Properties of Itô Integral

- **Continuity**: The paths of the Itô Integral $\d{I\P{t} = \int_{0}^{t} h\P{s,W_s}\dd{W_s}}$ (it do have a path! like a Brownian motion) are continuous functions of $t$.
- **Adaptivity**: For each $t$, $I\P{t}$ is $\FcF_t$ measurable.
- **Linearity**: $\d{\int_{0}^{t} \P{h_1\P{s,W_s} + h_2\P{s,W_s}}\dd{W_s} = \int_{0}^{t} h_1\P{s,W_s}\dd{W_s} + \int_{0}^{t} h_2\P{s,W_s}\dd{W_s}}$
- **Martingale**: Yeah, face the truth: $I\P{t}$ is a martingale!
- **Itô isometry**: $\d{\Exp\SB{I^2\P{t}} = \int_{0}^{t} \Exp\SB{h^2\P{s,W_s}}\dd{s}}$

$Proof$ Martingale

> Firstly, it's adapted to $\FcF_t$. And since we've assumed that $\Exp\SB{\int_{0}^{T} h^2\P{t,W_t}\dd{t}}<\infty$, we have
>
>$$\Exp\SB{\abs{I\P{t}}} \leq \Exp\SB{\ffrac{I^2\P{t}+1} {2}}<\infty$$

>Then we write $\Exp_s\SB{I\P{t}} = I\P{s} + \Exp_s\SB{I\P{t} - I\P{s}}$. Now we prove that $\Exp_s\SB{I\P{t} - I\P{s}} = 0$ by partition the interval $\SB{s,t}$ in $n$ subintervals
>
>$$\begin{align}
\Exp_s\SB{I\P{t} - I\P{s}}=& \Exp_s\SB{ \lim_{n\to\infty} \sum_{j=0}^{n-1} h\P{t_j, W_{t_{\void_{j}}}}\P{W_{t_{\void_{j+1}}} - W_{t_{\void_{j}}}} }\\[0.6em]
&\bspace \text{assume the summation is exchangable with the expectation } \\[0.5em]
=& \lim_{n\to\infty} \sum_{j=0}^{n-1} \Exp_s\SB{ h\P{t_j, W_{t_{\void_{j}}}}\P{W_{t_{\void_{j+1}}} - W_{t_{\void_{j}}}} }\\[0.6em]
&\bspace \text{condition on }\FcF_{t_{\void_{j}}}, t_j>s, \Exp_s\SB{X} = \Exp_s\SB{\Exp_t\SB{X}} \\[0.5em]
=& \lim_{n\to\infty} \sum_{j=0}^{n-1} \Exp_s\SB{ \Exp_{t_{\void_{j}}}\SB{ h\P{t_j, W_{t_{\void_{j}}}} \P{W_{t_{\void_{j+1}}} - W_{t_{\void_{j}}}} } }\\
=& \lim_{n\to\infty} \sum_{j=0}^{n-1} \Exp_s\SB{  h\P{t_j, W_{t_{\void_{j}}}} } \Exp_{t_{\void_{j}}} \SB{{W_{t_{\void_{j+1}}} - W_{t_{\void_{j}}}} } \\
=& \lim_{n\to\infty} \sum \Exp_s\SB{  h\P{t_j, W_{t_{\void_{j}}}} } \cdot 0 = 0
\end{align}$$

$Proof$ Isometry, skipped 😱😱😱

Now we need to introduce a new concept: ***Quadratic Variation***. But first we need to define the ***mesh***: for the partition of interval $\SB{0,t}$ into $n$ subintervals $\left[t_j,t_{j+1}\right)$ where $0 = t_0 \leq t_1 \leq \cdots \leq t_n = t\newcommand{\norm}[1]{\left\| #1 \right\|}$, its **mesh** is

$$\norm{\Pi} := \max_{1\,\leq \, j \, \leq \, n} \abs{\,t_j - t_{j+1}}$$

with this we have the **sampled quadratic variation** of $\CB{W_t}$ corresponding to this partition $\Pi$:

$$Q_\Pi = \sum_{j=1}^{n} \P{W_{t_{\void_{j}}} - W_{t_{\void_{j-1}}}}^2$$

then if the *limit* of $Q_\Pi$ when the **mesh** goes to $0$ exists, we call it the **quadratic variation** of $\CB{W_t}$ up to time $t$:

$$\SB{W}_t = \lim_{\norm{\Pi}\to 0}Q_{\Pi}$$

$Theorem.5$

Let $W_t$ be a standard Brownian motion, then $\SB{W}_t = t$.

$Proof$

>We are going to prove that $\d{\lim_{\norm{\Pi}\to 0} \sum_{j=1}^{n} \P{W_{t_{\void_{j}}} - W_{t_{\void_{j-1}}}}^2 } = t $. First notice that:
>
>$$\Exp\SB{\sum_{j=1}^{n}\P{W_{t_{\void_j}} - W_{t_{\void_{j-1}}}}^2} = \sum_{j=1}^{n}\Exp\SB{\P{W_{t_{\void_j}} - W_{t_{\void_{j-1}}}}^2} = \sum_{j=1}^{n} \P{t_j - t_{j-1}} = t$$
>
>Then the only thing left is to check its variance:

>$$\begin{align}
\Exp\SB{\P{\sum_{j=1}^{n}\P{W_{t_{\void_j}} - W_{t_{\void_{j-1}}}}^2 - t }^2} &=  \Var{\sum_{j=1}^{n} \P{W_{t_{\void_j}} - W_{t_{\void_{j-1}}}}^2}\\
&= \sum_{j=1}^{n} \Var{\P{W_{t_{\void_j}} - W_{t_{\void_{j-1}}}}^2} \\
&= \sum_{j=1}^{n} 2\P{t_j - t_{j-1}}^2 \to 0
\end{align}$$

$Remark$

>More on the distribution related to $W_t$
>
>$W_t - W_s \sim \N{0,t-s}$, then $\P{t-s}^{-1}\P{W_t - W_s}^2 \sim \chi^2\P{1}$ and what's more, $\Exp\SB{\chi^2\P{k}} = k $ and $\Var{\chi^2\P{k}} = 2k $
>
>And the integral and differential form of this $Theorem$
>
>$$t=\SB{W}_t = \lim_{\norm{\Pi}\to 0} \sum_{j=1}^{n} \P{W_{t_{\void_{j}}} - W_{t_{\void_{j-1}}}}^2 = \int_{0}^{t} \P{\dd{W_s}}^2 \iff \P{\dd{W_t}}^2 = \dd{t}$$

The notation of $\dd{W_t}$ is something not exactly the same with the similar one on a normal independent variable, say $\dd t$. This little $\dd{}$ means something else:

Consider the quadratic variation of function $t$.

$$\begin{align}
&\sum_{j=1}^{n} \P{t_j - t_{j-1}}^2 \leq \norm \Pi \cdot t \sum_{j=1}^{n} \P{t_j - t_{j-1}} =  \norm \Pi \cdot t \to 0 \space \text{ as } \norm \Pi \to 0\\
\Longrightarrow 0 =& \lim_{\norm{\Pi}\to 0}\sum_{j=1}^{n} \P{t_j - t_{j-1}}^2 = \int_{0}^{t}\P{\dd s}^2 \\
\Longrightarrow 0 =& \;\P{\dd t}^2
\end{align}$$

Also, by Cauchy-Schwartz inequality,

$$\begin{align}
&\abs{\sum_{j=1}^{n} \P{W_{t_{\void_j}} - W_{t_{\void_{j-1}}}}\P{t_j - t_{j-1}}}\\
\leq\space & \mathstrut{\sqrt{\sum_{j=1}^{n} \P{W_{t_{\void_j}} - W_{t_{\void_{j-1}}}}^2}} \mathstrut{ \sqrt{ \sum_{j=1}^{n} \P{t_j - t_{j-1}}^2}} \\
&\text{same derivation like the preceding one}\\
\Longrightarrow 0 =& \lim_{\norm{\Pi}\to 0}\sum_{j=1}^{n} \P{W_{t_{\void_j}} - W_{t_{\void_{j-1}}}}\P{t_j - t_{j-1}} =\int_{0}^{s} \dd{W_s}\;\dd{s}\\
\Longrightarrow 0 =& \;\dd{W_t}\dd{t}
\end{align}$$

**e.g.**

Compute $\d{\int_{0}^{T} W_t\;\dd{W_t}}$

>First we define the partition on the interval $\SB{0,T} $ with any $n$ subintervals $\left[t_j,t_{j+1}\right)$. And it's tricky to apply the results we've already got.
>
>$$\int_{0}^{T}W_t\;\dd{W_t} = \lim_{\norm\Pi\to\infty} \sum_{j=0}^{n-1} W_{t_{\void_{j-1}}} \SB{W_{t_{\void_{j+1}}} - W_{t_{\void_{j}}}}$$

>And where to obtain $W_{t_{\void_{j-1}}} \SB{W_{t_{\void_{j+1}}} - W_{t_{\void_{j}}}}$, we use the **quadratic variation** hoping to use the result $\SB{W}_t = t$
>
>$$\begin{align}
& \sum_{j=1}^{n} \P{W_{t_{\void_{j}}} - W_{t_{\void_{j-1}}}}^2\\
=\; & \sum_{j=1}^{n} \SB{\P{W_{t_{\void_j}}^2 - W_{t_{\void_{j-1}}}^2} - 2W_{t_{\void_{j-1}}}\P{W_{t_{\void_j}} - W_{t_{\void_{j-1}}}}}\\
=\; & W_T^2 - W_0^2 -\sum_{j=0}^{n-1} 2W_{t_{\void_{j-1}}}\SB{W_{t_{\void_{j+1}}} - W_{t_{\void_{j}}}}\\
\Longrightarrow \sum_{j=0}^{n-1} W_{t_{\void_{j-1}}}\SB{W_{t_{\void_{j+1}}} - W_{t_{\void_{j}}}} =&\; \ffrac{1} {2}\P{W_T^2 - W_0^2 - \sum_{j=1}^{n} \P{W_{t_{\void_{j}}} - W_{t_{\void_{j+1}}}}^2}
\end{align}$$
>
>Letting $\norm\Pi\to\infty$ we can obtain
>
>$$\int_{0}^{T}W_t\;\dd{W_t} = \ffrac{1}{2}\P{W_T^2 - W_0^2 -\SB{W}_T} = \ffrac{1}{2}\P{W_T^2 - W_0^2 -T}$$

$Remark$

>The extra term of the final answer comparing to a normal variable, say $t$, like $\d{\int_{0}^{T}t\;\dd{t} = \ffrac{1}{2}\P{T^2 -0^2}}$, is from the **quadratic variation** $\d{\lim_{\norm{\Pi}\to 0} \sum_{j=1}^{n} \P{W_{t_{\void_{j}}} - W_{t_{\void_{j-1}}}}^2 } = t$.

### Summary for Stochastic Calculus

- Classical integration theory:

$$\int_0^t h\P{s,x_s} \;\dd{x_s} = \lim_{\norm\Pi\to0}\sum_{j=0}^{n-1} h\P{t_j,x_{t_{\void_j}}}\SB{x_{t_{\void_{ j+1}}}-x_{t_{\void_j}} }$$

- Itô's integral:

$$\int_0^t h\P{s,W_s} \;\dd{W_s} = \sum_{j=0}^{n-1} h\P{t_j,W_{t_{\void_j}}}\SB{x_{W_{\void_{ j+1}}}-W_{t_{\void_j}} }$$

where the limits only exist in the $L^2$ case

And the two may look familiar, but their behaviour is far from familiar. Like the integral result.

One more thing, we calculating Itô's integral, the midpoint must be the ***left end*** of the small interval, while the normal integral can take any value in that.

## Itô processes

Suppose we have two *adapted processes*, $\CB{\mu\P{t,\cdot}}$ and $\CB{\sigma\P{t,\cdot}}$ and they satisfy some conditions, then the process $\CB{X_t}$ defined by

$$X_t = X_0 + \int_0^{t}\mu\P{s,X_s}\;\dd{s} + \int_0^{t}\sigma\P{s,X_s}\;\dd{W_s},\bspace\forall\; t>0$$

this is called a ***Itô process***. And its corresponding differential form is:

$$\dd{X_t} = \mu\P{t,X_t}\;\dd{t} + \sigma\P{t,X_t}\;\dd{W_t}$$

- $\mu\P{t,X_t}\;\dd t$ can be interpreted as the expected change in $X$ in an instant $\dd t$.
- $\mu\P{t,X_t}$ is called the ***drift*** of the process $X$ at time $t$. Also!!! If $X_t$ is a **martingale**, this **drift** must be $0$ since $\Exp\SB{\dd X_t} = 0$.
- $\sigma\P{t,X_t}$ is called the ***"diffusion" coefficient*** of $X$ at time $t$.

$Remark$

>here is that $\d{\int_{0}^{t} h\P{s,W_s}}\;\dd{W_s}$ is a **Martingale**. We can also see that its expectation is always $0$, so
>
>$$\begin{align}
\Exp\SB{\int_{0}^{t} h\;\dd W_s} &= \Exp\SB{\lim_{\norm{\Pi}\to0}\sum_{j=0}^{n-1}h\P{t_j,W_{t_{\void_{j}}}} \P{W_{t_{\void_{j+1}}}-W_{t_{\void_j}}}}\\
&= \lim_{\norm{\Pi}\to0}\sum_{j=0}^{n-1} \Exp\SB{h\P{t_j,W_{t_{\void_{j}}}} \P{W_{t_{\void_{j+1}}}-W_{t_{\void_j}}}}\\
&= \lim_{\norm{\Pi}\to0}\sum_{j=0}^{n-1} \Exp\bigg[\Exp\SB{h\P{t_j,W_{t_{\void_{j}}}} \P{W_{t_{\void_{j+1}}}-W_{t_{\void_j}}}\mid \FcF_{t_{\void_j}}}\bigg]\\
&= \lim_{\norm{\Pi}\to0}\sum_{j=0}^{n-1} \Exp\bigg[h\P{t_j,W_{t_{\void_{j}}}} \cdot \Exp\SB{ \P{W_{t_{\void_{j+1}}}-W_{t_{\void_j}}}\mid \FcF_{t_{\void_j}}}\bigg]\\
&\bspace\text{by the independency}\\
&= \lim_{\norm{\Pi}\to0}\sum_{j=0}^{n-1} \Exp\SB{h\P{t_j,W_{t_{\void_{j}}}}}\cdot \Exp\SB{W_{t_{\void_{j+1}}}-W_{t_{\void_j}}}\\
&= 0 = \int_{0}^{\mathbf{0}} h\;\dd W_s
\end{align}$$
>
>And with this we have the conclusion that for a Itô process $X_t$, given that the first two conditions of being a martingale already satisfied, to say that is is a martingale is to say its drift is $0$

### Itô's formula

Assume that the process $X$, $\dd{X_t} = \mu\P{t,X_t}\;\dd{t} + \sigma\P{t,X_t} \;\dd{W_t}$.

Consider a function $f$, twice differentiable and then, define $Y_t = f\P{t,X_t}$, then $Y$ has a stochastic differential given by

$$\dd{f\P{t,X_t}} = \ffrac{\partial f} {\partial t} \dd{t} + \ffrac{\partial f} {\partial X} \dd{X_t} + \ffrac{1}{2}\ffrac{\partial^2 f} {\partial X^2} \P{\dd X_t}^2$$

or equivalently,

$$\dd{f\P{t,X_t}} = \P{\ffrac{\partial f} {\partial t} + \mu\P{t,X_t} \ffrac{\partial f} {\partial X} +\ffrac{1} {2}\sigma^2\P{t,X_t}\ffrac{\partial^2 f} {\partial X^2} }\dd{t} + \sigma\P{t,X_t}\ffrac{\partial f} {\partial X} \dd{W_t}$$

$Proof$

> First, by **Taylor expansion**
>
>$$\begin{align}
&f\P{t+\Delta_t} - f\P{t,X_t}\\
=\;& \Delta_t f_t\P{t,X_t} + O\P{\Delta_t^2} + \P{X_{t+\Delta_t} -X_t}f_X\P{t,X_t}\\
& \bspace + \ffrac{1}{2}\P{X_{t+\Delta_t} -X_t}^2 f_{XX}\P{t,X_t} + \text{higher order terms}
\end{align}$$

>$\P{X_{t+\Delta_t} -X_t}^2$, this term will not disappear so we need to further simplify this term:
>
>$$\begin{align}
&\P{X_{t+\Delta_t} -X_t}^2\\
=\;& \SB{\mu\P{t,X_t}\Delta_t + \sigma\P{t,X_t} \P{W_{t+\Delta_t} -W_t}}^2 \\
=\;& \mu^2 \P{t,X_t} \Delta_t^2 + 2\mu \P{t,X_t} \sigma\P{t,X_t} \Delta_t \P{W_{t+\Delta_t} -W_t}\\
&\bspace +\sigma^2 \P{t,X_t} \P{W_{t+\Delta_t} -W_t}^2
\end{align}$$
>
>Recall that we've already build the relation that $\P{\dd t}^2 = 0 $, $\dd t \cdot \dd W_t = 0$ and $\P{ \dd W_t }^2 = \dd t$. Therefore, let $\Delta_t \to 0$, then
>
>$$\P{\dd X_t}^2 = \sigma^2\P{t,X_t} \dd{t}\\
\Longrightarrow \dd f\P{t,X_t} = f_t\dd{t} + f_X \dd X_t + \ffrac{1}{2} \sigma^2 \P{t,X_t} \dd{t}$$

$Remark$

>$\P{\dd X_t}^2 = \dd\SB{X}_t = \cdots = \sigma^2\P{t,X_t} \P{\dd W_t}^2 = \sigma^2\P{t,X_t} \dd t$. This can be proved using the quadratic variation.

**e.g.**

Process $X$ has a stochastic differential given by $\dd X_t = \mu_t \dd{t} + \sigma_t \dd{W}_t$

$\P{1}$ Find $\dd Y_t$ if $Y_t = e^{X_t}$

> Using the second one will be much easier cause we don't need to find $\P{\dd X_t}^2$.
>
>$$\begin{align}
\dd Y_t &= \dd\P{e^{X_t}} \\
&\bspace\text{there's no $t$ in this equation}\\
&= \P{0 + \mu\P{X_t} e^{X_t} + \ffrac{1}{2}\sigma^2\P{X_t} e^{X_t}}\dd t + \sigma\P{X_t}e^{X_t} \dd W_t\\
&\bspace\text{change all $X_t$ into the form of $Y$}\\
&= \mu_t Y_t \;\dd t+ \ffrac{1}{2} \sigma^2_t Y_t\;\dd t + \sigma_t Y_t \dd W_t
\end{align}$$
>
>If you're so determined to using the first one, you'll get
>
>$$\begin{align}
\dd Y_t &= \dd\P{e^{X_t}} \\
&= 0 \;\dd{t} + e^{X_t} \;\dd{X_t} + \ffrac{1}{2}e^{X_t} \P{\dd X_t}^2\\
&\bspace\text{the third term can be found in previous }Remark \\
&= \mu_t Y_t \;\dd t + \sigma_t Y_t \dd W_t + \ffrac{1}{2}Y_t\sigma_t^2 \dd t \\
&\bspace\text{the same}
\end{align}$$

$\P{2}$ Find $\dd Y_t$ if $Y_t = e^{-rt} e^{X_t}$

>$$\begin{align}
\dd Y_t &= \dd\P{e^{-rt} e^{X_t}} \\
&= \P{-re^{-rt}e^{X_t} + \mu\P{t,X_t} e^{-rt} e^{X_t} + \ffrac{1}{2}\sigma^2\P{t,X_t} e^{-rt}e^{X_t}}\dd t + \sigma\P{t,X_t} e^{-rt}e^{X_t} \dd W_t\\
&= Y_t\P{-r\dd t + \mu_t \dd t + \sigma_t \dd W_t + \ffrac{1}{2}\sigma_t^2 \dd t}
\end{align}$$
***

**e.g.**

Use Itô's formula to compute $\Exp\SB{W_t^6}$

> Make it in lower order using the Itô's formula, here $X_t = 0 \dd t + 1 \dd W_t$
>
>$$\begin{align}
\dd W_t^6 &= \dd X_t^6 \\
&= \P{0 + 0 + \ffrac{1}{2} \cdot 1 \cdot \P{6\times 5 \times W_t^4}}\dd t + 1 \cdot 6 W_t^5 \dd W_t \\
&= 6 W_t^5 \dd W_t + 15W_t^4 \dd t
\end{align}$$
>
>Then transform this into integrated form:
>
>$$W_t^6 -W_0^6 = \int_{0}^{t} 6W_s^5\;\dd W_s + \int_0^t 15W_s^4 \;\dd s$$
>
>Since the expectation of the stochastic integral vanishes (by the martingale property, it equals to the integral from time $0$ to $\mathbf 0$), then we have
>
>$$\begin{align}\Exp\SB{W_t^6} &= \int_{0}^t 15\Exp\SB{W_s^4}\;\dd s\\
&= 15\int_0^t 3s^2 \;\dd s\\
&= 15 t^3
\end{align}$$

$Remark$

>- $\d{I\P{t} = \int_{0}^{t} h\P{s,W_s}\dd{W_s}}$ is a martinagle $\Rightarrow \Exp\SB{\d{\int_{0}^{t} 6W_s^5\;\dd W_s}} =\d{\int_{0}^{0} 6W_s^5\;\dd W_s = 0}$
>- $\Exp\SB{W_t} = 0, \Exp\SB{W_t^2} = t,\Exp\SB{W_t^3} = 0, \Exp\SB{W_t^4} = 3t^2$
>- $\Exp\SB{\exp\CB{\mu t + \sigma W_t}} = \exp\CB{\mu t + \ffrac{1} {2} \sigma^2 t}$
***

**e.g.**

Prove $\Exp\SB{e^{\sigma W_t}} = \exp\CB{\ffrac{1}{2}\sigma^2 t}$

>Likewise, we have $\dd e^{\sigma W_t} = \sigma e^{\sigma W_t}\dd W_t + \ffrac{1}{2}\sigma^2 e^{\sigma W_t}\dd t$, so that
>
>$$\begin{align}
\Exp\SB{e^{\sigma W_t}} &= \Exp\SB{e^{\sigma W_0} + \int_0^t \sigma e^{\sigma W_s}\;\dd W_s + \int_0^t\ffrac{1}{2}\sigma^2 e^{\sigma W_s}\;\dd s}\\
&= \Exp\SB{1} + \Exp\SB{\int_0^t \sigma e^{\sigma W_s}\;\dd W_s} + \Exp\SB{\int_0^t\ffrac{1}{2}\sigma^2 e^{\sigma W_s}\;\dd s}\\
&= 1 + 0 + \int_0^t\ffrac{1}{2}\sigma^2 \Exp\SB{e^{\sigma W_s}}\;\dd s
\end{align}$$
>
>And now is the matter of ODE, we first let $\Exp\SB{e^{\sigma W_t}} = f\P{t}$, then obtain
>
>$$\begin{align}\dd f\P{t} &= \ffrac{1}{2}\sigma^2 f\P{t} + C\\
\Longrightarrow f\P{t} &= \exp\CB{\ffrac{1}{2}\sigma^2 t}-\ffrac{2C}{\sigma^2}\\
f\P{0}=1\Rightarrow f\P{t} &= \Exp\SB{e^{\sigma W_t}} = \exp\CB{\ffrac{1}{2}\sigma^2 t}
\end{align}$$

**e.g.**

The basic reference model for stock price in continuous time is

$$\ffrac{\dd S_t}{S_t} = \mu \dd t + \sigma \dd W_t \P{\neq \dd \log S_t}$$

>My thought: first write $\dd S_t = \mu \cdot S_t \dd t + \sigma \cdot S_t \dd W_t \equiv \mu_t \dd t + \sigma_t \dd W_t$. Then using the Itô's formula we have
>
>$$\begin{align}
\dd \log S_t &= \dd f\P{S_t} \\
&= \P{0 + \P{\mu S_t}\ffrac{1}{S_t} + \ffrac{1}{2}\P{\sigma S_t}^2 \P{-\ffrac{1}{S_t^2}}}\dd t + \P{\sigma S_t} \ffrac{1}{S_t} \dd W_t \\
&= \P{\mu - \ffrac{1}{2}\sigma^2}\dd t + \sigma \dd W_t
\end{align}$$
>***
>Here we can also apply the first form of Itô's formula, which yields
>
>$$\begin{align}
\dd \log S_t &= \ffrac{1}{S_t} \dd S_t - \ffrac{1}{2S_t^2} \P{\dd S_t}^2 \\
&= \mu \dd t + \sigma \dd W_t - \ffrac{1}{2}\P{\ffrac{\dd S_t}{S_t}}^2\\
&= \mu \dd t + \sigma \dd W_t - \ffrac{1}{2}\sigma^2\dd t = \P{\mu - \ffrac{1}{2}\sigma^2}\dd t + \sigma \dd W_t\\
\end{align}$$
>
>take the intergral, like the preceding example, we have
>
>$$\begin{align}
\log\ffrac{S_t}{S_0} &= \P{\mu - \ffrac{\sigma^2}{2}}t + \sigma W_t\\
S_t &= S_0 \exp\CB{\P{\mu - \ffrac{\sigma^2}{2}}t + \sigma W_t}
\end{align}$$

$Remark$

>The stochastic process $\CB{S_t }$ is a ***geometric Brownian motion***. And more generally
>
>$$S_t = S_s \exp\CB{\P{\mu - \ffrac{\sigma^2}{2}}\P{t-s} + \sigma \P{W_t-W_s}}$$

$Lemma.6$

The **geometric Brownian motion** process is a $\P{\PP ,\CB{\FcF_t}_{t\geq 0}}$ martingale $iff$ $\mu = 0$. Moreover, $\Exp\SB{S_t} = S_0 e^{\mu t}$.

$\bspace$Sufficiency $\Rightarrow)$

>Since it's a martingale
>
>$$\begin{align}
\Exp\SB{S_t\mid \FcF_s} &= \Exp\SB{S_s \exp\CB{\P{\mu - \ffrac{\sigma^2}{2}}\P{t-s}}\cdot\exp\CB{\sigma \P{W_t-W_s}} \mid \FcF_s}\\
&=S_s \exp\CB{\P{\mu - \ffrac{\sigma^2}{2}}\P{t-s}} \cdot \exp\CB{\ffrac{\sigma^2}{2}\P{t-s}} \\
&= S_s\cdot e^{\mu\P{t-s}} = S_s \Longrightarrow \mu=0
\end{align}$$
>

$\bspace$Necessity $\Leftarrow)$

>Obviously, this can be proved inversely like how we proved the sufficiency.
>***
>Then, $\Exp\SB{S_t} = \Exp\SB{S_t\mid \FcF_0} = S_0 e^{\mu t}$.

## Feynman-Kac Formula

$Theorem.7$

Assume the function $f$ solves the boundary value problem:

$$\begin{cases}
-rf\P{t,x} +\ffrac{\partial f} {\partial t}\P{t,x} + \mu\P{t,x} \ffrac{\partial f} {\partial x}\P{t,x} + \ffrac{1}{2}\sigma^2 \P{t,x} \ffrac{\partial^2 f} {\partial x^2}\P{t,x} = 0\\
f\P{T,x} = \Phi\P{x}
\end{cases}$$

And define $\CB{X_t}_{0\leq t\leq T}$ to be the solution of the ***stochastic differential equation***: $\dd{X_t} = \mu\P{t,X_t} \dd{t}+\sigma\P{t,X_t} \dd{W_t}$. Then under some regularity condition, we have

$$f\P{t,x} = \Exp\SB{e^{-r\P{T-t}}f\P{T,X_T}\mid X_t = x} = \Exp\SB{e^{-r\P{T-t}}\Phi\P{X_T}\mid X_t = x}$$

The two equation, are equivalent.

$Remark$

>This $Theorem$ makes sense in that we can consider $f\P{t,X}$ to be an option price and $\Phi\P{t}$ is its payoff, if under risk-neutral measure $Q$. $r$ could be consider the interest rate. Also $\sigma$ is the volatility, $\mu$ is the drift.
>
>And the one with the form of expectation looks somehow like to be convertable to be a **martingale**.

$Proof$

>We apply the Itô's formula to $e^{-rt}f\P{t,X_t}$ which gives
>
>$$\begin{align}
\dd{\P{e^{-rt}f\P{t,X_t}}} &= e^{-rt}\P{-rf +\ffrac{\partial f} {\partial t} +\mu\P{t,X_t}\ffrac{\partial f} {\partial X} + \ffrac{1} {2}\sigma^2 \P{t,X_t} \ffrac{\partial^2 f} {\partial X^2}}\dd{t}\\
&\bspace +e^{-rt} \sigma\P{t,X_t} \ffrac{\partial f} {\partial X} \dd{W_t} \\
&= e^{-rt} \sigma\P{t,X_t} \ffrac{\partial f} {\partial X} \dd{W_t}\\
\Longrightarrow e^{-rT}f\P{T,X_T} &= e^{-rt}f\P{t,X_t} + \int_{t}^{T} e^{-rs} \sigma\P{s,X_s} \ffrac{\partial f} {\partial X} \dd{W_s}
\end{align}$$
>
>The expecation of last term is $0$. Therefore, $e^{-rt}f\P{t,X_t}$ is a martingale and we have
>
>$$\begin{align}
e^{-rt} &= \Exp\SB{e^{-rT} f\P{T,X_T} \mid \FcF_t} = \Exp\SB{e^{-rT}\Phi\P{X_T}\mid \FcF_t}\\
&\text{and actually the left side use the informtion only in $t$}\\
&\text{however more information, before $t$ is given}\\
\Longrightarrow f\P{t,X_t} &= \Exp\SB{f\P{t,X_t}\mid\FcF_t} = \Exp\SB{\Exp\SB{e^{-rT}\Phi\P{X_T}\mid \FcF_t}\mid X_t} \\
&= \Exp\SB{e^{-r\P{T-t}}\Phi\P{X_T}\mid X_t}
\end{align}$$
>
>And it's obvious to see from the expectation form to the PDE form.
***

**e.g.** An heat equation

Use the preceding formula to solve

$$\begin{cases}
\ffrac{\partial f} {\partial t}\P{t,x} +\ffrac{1}{2}\ffrac{\partial^2 f} {\partial x^2}\P{t,x} = 0\\
f\P{T,x} = x^4
\end{cases}$$

> The corresponding stochastic differential equation is $\dd{X_t} = \dd{W_t}$ and without loss of generality, $X_t = W_t + C$ and let $C=0$. Here $\mu=0$, $\sigma=1$ and $r=0$. By the Feynman-Kac formula, we have
>
>$$\begin{align}
f\P{t,x} &= \Exp\SB{e^{-r\P{T-t}}X_T^4\mid X_t = x}\\
\Longrightarrow f\P{t,x} &= \Exp\SB{e^{-r\P{T-t}}W_T^4\mid W_t = x} \\
&\nRightarrow\text{not directly to }3\P{T-t}^2 + 6x^2\P{T-t}+x^4\\
&= \Exp\SB{e^{-r\P{T-t}}\P{W_T-W_t + x}^4\mid W_t = x}\\
&\using{\text{indep}} \Exp\SB{e^{-r\P{T-t}}\P{W_T-W_t + x}^4}\\
&= \Exp\SB{\P{W_T-W_t}^4 +4\P{W_T-W_t}^3 x+6\P{W_T-W_t}^2x^2 + 4\P{W_T-W_t}x^3 + x^4}\\
&= 3\P{T-t}^2 + 6x^2\P{T-t}+x^4
\end{align}$$
>

$Remark$

>There, we can't not directly get the answer is because the conclusion can only be made when the Brownian Motion start from $0$, however $t$, in this problem.

Then about the $Q$ measure, recall that

>In the discrete time world, we first find the risk neutral measure according to the formula $S_0=e^{-rT}\Exp^Q\SB{S_T}$, the option price is reduced to calculating the expectation under this measure $Q$. In the continuous world, the discounted price of the underlying asset is still required to be a martingale under the risk neutral measure.

To find the $Q$ measure at this time, we introduce the ***Girsanov's Theorem***, with which we can derive a risk neutral measure for pricing contingent claims.

$Theorem.8$ **Girsanov's Theorem**

Suppose that $\CB{W_t}_{t \geq0}$ is a $\PP$-Brownian motion with the natural filtration $\CB{\FcF_t}_{t\geq 0}$ and $\CB{\theta_t}_{t\geq0}$ is an $\CB{\FcF_t}_{t\geq0}$-adapted process such that

$$\Exp\SB{\exp\CB{\ffrac{1}{2}\int_{0}^{T} \theta_t^2 \;\dd{t}}}<\infty$$

Then we define $L_t = \exp\CB{\d{\mathbf-\int_{0}^{t} \theta_s \;\dd{W_s} \mathbf-\ffrac{1}{2}\int_{0}^{t}\theta_s^2 \;\dd{s}}}$ and let $\tilde P$ be the probability measure defined by

$$\tilde P\SB{A} = \int_A L_t\P{\omega} \PP\P{\dd{\omega}}$$

Then under the new probability measure $\tilde P$, the process defined by

$$\widetilde W_t = W_t \mathbf +\int_{0}^{t}\theta_s\;\dd{s}$$

is a standard Brownian motion.

$Remark$

>$L_t$ is a martingale. This conclusion is guranteed by that $\Exp\SB{\exp\CB{\ffrac{1}{2}\int_{0}^{T} \theta_t^2 \;\dd{t}}}<\infty$, the **Novikov’s condition**.
>
>$P$ is the objective probability, $\tilde P$ is the risk-neutral probability in continuous time. And the definition can be rewritten as
>
>$$\left.\ffrac{\dd \tilde P}{\dd \PP}\right|_{\FcF_t} = L_t$$
>
>This $L_t$ is the Radon-Nikodym derivative of $\tilde P$ with respect to $\PP$.
>***
>We have the distribution of $\d{\int_{0}^{t} \theta_s\;\dd{W_s}}$. It's normally distributed with mean $0$ by spliting it apart, and variance $\d{\int_{0}^{t} \Exp\SB{\theta_s^2}\;\dd{s}}$ by the Itô's isometry.

The expectation under the new measure $\tilde P$: $\Exp^{\tilde P} \SB{\Phi\P{t}} = \Exp^{\tilde P} \SB{\Phi\P{t}\mid \FcF_0}  = \Exp^\PP\SB{\Phi\P{t}L_t}$, or more generally

$$\Exp^{\tilde P} \SB{\Phi\P{t}\mid \FcF_s} = \Exp^\PP\SB{\Phi\P{t}\ffrac{L_t}{L_s}\mid\FcF_s}$$

**e.g.**

The basic reference model for stock price in continuous time is

$$\dd S_t = \mu S_t \dd t + \sigma S_t \dd W_t$$

where $\CB{W_t}_{t\geq 0}$ is a $\PP$-Brownian motion. Find a measure under which the discounted stock price $\CB{\tilde S_t}_{t\geq 0}$ is a martingale. Here, $\tilde S_t = e^{-rt}S_t$.

>By Itô's formula, under $\PP$ measure, we have
>
>$$\dd \tilde S_t =\tilde S_t \P{-r + \mu} \dd t + \sigma \tilde S_t \dd W_t$$
>
>With the intention to *zero* the **drift** $\mu_t$, we let $\tilde W_t = W_t + \ffrac{\mu - r}{\sigma}t$:
>
>$$\dd \tilde S_t =  \sigma \tilde S_t \dd \tilde W_t$$
>
>According to Girsanov’s Theorem, we can define, by letting $\theta = \ffrac{\mu-r}{\sigma}$
>
>$$\newcommand{\QQ}{\mathbb{Q}}
\left.\ffrac{\dd\QQ}{\dd\PP}\right|_{\FcF_t} = \exp\CB{-\theta W_t - \ffrac{1}{2}\theta^2 t}$$
>
>Then $\CB{\tilde W_t}_{t\geq0}$ is a $\QQ$-Brownian motion and the discounted stock price $\CB{\tilde S_t}_{t\geq 0 }$ is a $\QQ$-martingale.

$Remark$

>When $S_t$ is multiplied by $e^{-rt}$, the resulting process can be remembered as a conclusion that
>
>$$\dd \tilde S_t =\tilde S_t \P{-r + \mu} \dd t + \sigma \tilde S_t \dd W_t$$
>
>The **drift** is decreased by $r$, while the **diffusion** (volatility) remains the same.

***

FinMath/Models and Pricing of Financial Derivatives/Chap_03_Stochastic_Process.ipynb