# Nano-porous materials

In this notebook we'll explore nano-porous materials in the molecular dynamics scheme. We'll use Lammps to simulate our systems.

```python
import os
import sys

sys.path.append("..")

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

from read_lammps_dump import read_dump
from read_lammps_log import read_log, get_temp_lognames

sns.set(color_codes=True)
```

We start by creating a system of liquid Argon in a system of $L = 20$ unit cells with length $b = 5.72 \text{Å}$ and thermalize it at $T = 0.851$. To create the specified length, we have to convert to the correct dimensions. For $\sigma = 3.405 \text{$\mu$m}$ we do this by
\begin{align}
    b' = \frac{b}{\sigma},
\end{align}
where $b'$ is the simulation length used in the simulation. When specifying the length in the front-centered-cubic lattice Lammps uses the reduced number density $\rho'$. This is given by
\begin{align}
    \rho' = \frac{n}{{b'}^3},
\end{align}
where $n$ is the number of atoms in the specified region. For front-centered-cubic lattices this is four atoms, i.e., $n = 4$.

Having set up the system we wish to thermalize it at $T = 0.851$ (note that the temperature is already in the right dimension). We do this by using the `fix nvt`-command in Lammps, which lets us specify the temperature we wish the system to achieve. Once the temperature desired temperature is achieved we move on to creating a cylinder, or in this case, we define a cylinder region and make all atoms outside this region frozen by removing all their velocity and keeping them fixed. Thus, all the atoms inside the cylinder still "feel" the atoms outside the cylinder, but they are unable to move the fixed atoms. The cylinder we create has a radius of $R = 2 \text{nm} = 20 \text{Å}$ and is located in the center of the $yz$-plane stretching from $x = 0$ to $x = L$.

```python
%%writefile scripts/liquid_argon.in

# 3d Lennard-Jones gas
units lj
dimension 3
# Periodic boundiaries
boundary p p p
atom_style atomic

variable sigma equal 3.405
variable b equal 5.72
variable reduced_density equal 4/((${b}/${sigma})^3)
variable temperature equal 0.851

# Set fcc lattice with specified density
lattice fcc ${reduced_density}
region simbox block 0 20 0 20 0 20
create_box 2 simbox
create_atoms 1 box

# Use a radius of 2 nm (equal to 20 Å)
variable radius equal 20/${sigma}
variable centre_y equal ly/2
variable centre_z equal lz/2
region cylinder cylinder x ${centre_y} ${centre_z} ${radius} EDGE EDGE units box

mass * 1.0
velocity all create ${temperature} 87287

pair_style lj/cut 3.0
pair_coeff * * 1.0 1.0

fix 1 all nvt temp ${temperature} ${temperature} 0.5
#fix 1 all nve

thermo 100
# Thermalize for 3000 steps
run 300

# Group all atoms in the cylinder
group cylinder_group region cylinder
set group cylinder_group type 2
# Create another group from all the remaining atoms except for the ones in the cylinder
group frozen subtract all cylinder_group
# Set the velocity of the atoms outside the cylinder to zero
velocity frozen set 0 0 0
# Avoid integrating all the particles
unfix 1

# Run cylinder using nve
fix 1 cylinder_group nve

dump 1 all custom 10 dat/liquid_argon.lammpstrj id type x y z vx vy vz

run 5000
```

    Overwriting scripts/liquid_argon.in

```python
!export OMP_NUM_THREADS=4 && mpirun -np 4 lmp -in scripts/liquid_argon.in
```

    LAMMPS (4 Jan 2019)
      using 4 OpenMP thread(s) per MPI task
    Lattice spacing in x,y,z = 1.67988 1.67988 1.67988
    Created orthogonal box = (0 0 0) to (33.5977 33.5977 33.5977)
      1 by 2 by 2 MPI processor grid
    Created 32000 atoms
      Time spent = 0.000822122 secs
    Created 0 atoms
      Time spent = 5.3607e-05 secs
    Neighbor list info ...
      update every 1 steps, delay 10 steps, check yes
      max neighbors/atom: 2000, page size: 100000
      master list distance cutoff = 3.3
      ghost atom cutoff = 3.3
      binsize = 1.65, bins = 21 21 21
      1 neighbor lists, perpetual/occasional/extra = 1 0 0
      (1) pair lj/cut, perpetual
          attributes: half, newton on
          pair build: half/bin/atomonly/newton
          stencil: half/bin/3d/newton
          bin: standard
    Setting up Verlet run ...
      Unit style    : lj
      Current step  : 0
      Time step     : 0.005
    Per MPI rank memory allocation (min/avg/max) = 13.76 | 13.76 | 13.76 Mbytes
    Step Temp E_pair E_mol TotEng Press
           0        0.851   -6.9322187            0   -5.6557586   -5.7920633
         100   0.47402775   -6.3193958            0   -5.6083764   -2.4013733
         200   0.52542817   -6.2911261            0   -5.5030084   -2.2236281
         300   0.57344047   -6.2429535            0   -5.3828197   -1.9201373
    Loop time of 3.55113 on 16 procs for 300 steps with 32000 atoms

    Performance: 36495.402 tau/day, 84.480 timesteps/s
    95.6% CPU use with 4 MPI tasks x 4 OpenMP threads

    MPI task timing breakdown:
    Section |  min time  |  avg time  |  max time  |%varavg| %total
    ---------------------------------------------------------------
    Pair    | 2.0683     | 2.1059     | 2.1794     |   3.0 | 59.30
    Neigh   | 0.48603    | 0.49445    | 0.51077    |   1.4 | 13.92
    Comm    | 0.20127    | 0.29639    | 0.34715    |  10.3 |  8.35
    Output  | 0.53734    | 0.55459    | 0.57135    |   1.8 | 15.62
    Modify  | 0.072648   | 0.08042    | 0.084194   |   1.6 |  2.26
    Other   |            | 0.01942    |            |       |  0.55

    Nlocal:    8000 ave 8060 max 7975 min
    Histogram: 2 1 0 0 0 0 0 0 0 1
    Nghost:    10145.8 ave 10249 max 10063 min
    Histogram: 1 0 0 1 1 0 0 0 0 1
    Neighs:    517847 ave 522818 max 515443 min
    Histogram: 1 2 0 0 0 0 0 0 0 1

    Total # of neighbors = 2071388
    Ave neighs/atom = 64.7309
    Neighbor list builds = 29
    Dangerous builds = 26
    3031 atoms in group cylinder_group
    Setting atom values ...
      3031 settings made for type
    28969 atoms in group frozen
    Setting up Verlet run ...
      Unit style    : lj
      Current step  : 300
      Time step     : 0.005
    Per MPI rank memory allocation (min/avg/max) = 13.78 | 13.78 | 13.78 Mbytes
    Step Temp E_pair E_mol TotEng Press
         300  0.054746478   -6.2429535            0   -6.1608363   -2.3577812
         400  0.053537113   -6.2411808            0   -6.1608777   -2.3482434
         500  0.054195291   -6.2421498            0   -6.1608594   -2.3529156
         600  0.054588507   -6.2427207            0   -6.1608405   -2.3576015
         700  0.054164375    -6.242087            0    -6.160843    -2.354077
         800  0.054542731    -6.242645            0   -6.1608335    -2.356711
         900  0.053656545   -6.2413693            0    -6.160887   -2.3496066
        1000  0.053531015   -6.2411615            0   -6.1608675   -2.3488478
        1100  0.053607339   -6.2413142            0   -6.1609057    -2.347686
        1200  0.054080019    -6.241997            0   -6.1608795   -2.3487897
        1300  0.054057089   -6.2419634            0   -6.1608803   -2.3545469
        1400  0.054642785   -6.2427904            0   -6.1608288   -2.3555009
        1500  0.053960962   -6.2417896            0   -6.1608507   -2.3512594
        1600  0.054353499   -6.2423977            0     -6.16087   -2.3520278
        1700  0.053287423   -6.2408517            0    -6.160923   -2.3438486
        1800  0.054533715    -6.242672            0    -6.160874   -2.3551676
        1900  0.054404589   -6.2424711            0   -6.1608667   -2.3515031
        2000  0.052724905   -6.2400032            0   -6.1609183   -2.3399522
        2100  0.055720704   -6.2444167            0   -6.1608383   -2.3642853
        2200  0.054435896   -6.2424764            0   -6.1608251   -2.3525603
        2300  0.054272256   -6.2422707            0   -6.1608649    -2.351017
        2400  0.054679876   -6.2428261            0   -6.1608089   -2.3559871
        2500  0.055208308   -6.2436749            0   -6.1608651   -2.3605248
        2600  0.053957624   -6.2417997            0   -6.1608657   -2.3495073
        2700  0.053974238   -6.2418495            0   -6.1608907   -2.3477621
        2800  0.053464424   -6.2410467            0   -6.1608525   -2.3454992
        2900   0.05439206   -6.2424161            0   -6.1608305   -2.3507302
        3000  0.055070624   -6.2434466            0   -6.1608432   -2.3560051
        3100  0.054098655   -6.2420216            0   -6.1608761   -2.3507998
        3200  0.054502112   -6.2426174            0   -6.1608668   -2.3511833
        3300  0.053918462   -6.2417688            0   -6.1608937    -2.346089
        3400  0.053695405   -6.2414453            0   -6.1609047   -2.3482677
        3500  0.054133236   -6.2420996            0   -6.1609023   -2.3495945
        3600    0.0537576   -6.2415004            0   -6.1608666   -2.3477452
        3700  0.054337551   -6.2423583            0   -6.1608545   -2.3530167
        3800   0.05343917   -6.2411002            0   -6.1609439   -2.3443153
        3900  0.052863366   -6.2402346            0   -6.1609421   -2.3399106
        4000  0.054097377   -6.2419906            0    -6.160847   -2.3507625
        4100   0.05308042   -6.2404851            0    -6.160867   -2.3436421
        4200  0.053470067   -6.2410732            0   -6.1608706    -2.346378
        4300   0.05332083   -6.2408896            0   -6.1609109   -2.3423181
        4400  0.054121698   -6.2420992            0   -6.1609192   -2.3482353
        4500  0.053388896   -6.2409655            0   -6.1608846   -2.3439139
        4600  0.053022177   -6.2404219            0   -6.1608911    -2.342513
        4700  0.053329065   -6.2409111            0     -6.16092   -2.3418489
        4800  0.053299889   -6.2408862            0   -6.1609388   -2.3431833
        4900  0.053480763   -6.2411039            0   -6.1608852   -2.3434571
        5000  0.053519857    -6.241116            0   -6.1608388    -2.347808
        5100  0.054042209    -6.241927            0   -6.1608663   -2.3493521
        5200  0.053405979   -6.2409894            0   -6.1608829   -2.3473462
        5300  0.053157986   -6.2406561            0   -6.1609216   -2.3413953
    Loop time of 52.5528 on 16 procs for 5000 steps with 32000 atoms

    Performance: 41101.486 tau/day, 95.142 timesteps/s
    96.7% CPU use with 4 MPI tasks x 4 OpenMP threads

    MPI task timing breakdown:
    Section |  min time  |  avg time  |  max time  |%varavg| %total
    ---------------------------------------------------------------
    Pair    | 31.765     | 32.137     | 32.647     |   5.9 | 61.15
    Neigh   | 7.3477     | 7.4115     | 7.5055     |   2.1 | 14.10
    Comm    | 3.4258     | 4.1079     | 4.5784     |  20.9 |  7.82
    Output  | 8.4295     | 8.5397     | 8.6378     |   3.3 | 16.25
    Modify  | 0.15041    | 0.15486    | 0.16382    |   1.3 |  0.29
    Other   |            | 0.2017     |            |       |  0.38

    Nlocal:    8000 ave 8051 max 7974 min
    Histogram: 2 0 0 1 0 0 0 0 0 1
    Nghost:    10120.2 ave 10189 max 10032 min
    Histogram: 1 0 0 0 0 1 0 1 0 1
    Neighs:    517797 ave 522347 max 515839 min
    Histogram: 2 1 0 0 0 0 0 0 0 1

    Total # of neighbors = 2071187
    Ave neighs/atom = 64.7246
    Neighbor list builds = 496
    Dangerous builds = 464
    Total wall time: 0:00:56

```python
%%writefile scripts/liquid_random_argon.in

# 3d Lennard-Jones gas
units lj
dimension 3
# Periodic boundiaries
boundary p p p
atom_style atomic

variable seed equal 87287
variable sigma equal 3.405
variable b equal 5.72
variable reduced_density equal 4/((${b}/${sigma})^3)
variable temperature equal 0.851
variable num_box equal 20
variable radius_min equal 20/${sigma}
variable radius_max equal 30/${sigma}

# Set fcc lattice with specified density
lattice fcc ${reduced_density}
region simbox block 0 ${num_box} 0 ${num_box} 0 ${num_box}
create_box 2 simbox
create_atoms 1 box

variable box_len equal lx

variable a loop 20
label pore_loop

variable x_1 equal random(0,${box_len},${seed})
variable y_1 equal random(0,${box_len},${seed})
variable z_1 equal random(0,${box_len},${seed})
variable radius_1 equal random(${radius_min},${radius_max},${seed})
region $a sphere ${x_1} ${y_1} ${z_1} ${radius_1} units box

next a
jump scripts/liquid_random_argon.in pore_loop

mass * 1.0
velocity all create ${temperature} ${seed}

pair_style lj/cut 3.0
pair_coeff * * 1.0 1.0

fix 1 all nvt temp ${temperature} ${temperature} 0.5
#fix 1 all nve

thermo 100
# Thermalize for 3000 steps
run 300

group pore_group region 1

# Group all atoms in the cylinder
variable a loop 2 20
label group_loop

group pore_$a region $a
group pore_group union pore_group pore_$a

next a
jump scripts/liquid_random_argon.in group_loop

set group pore_group type 2
# Create another group from all the remaining atoms except for the ones in the cylinder
group frozen subtract all pore_group
# Set the velocity of the atoms outside the cylinder to zero
velocity frozen set 0 0 0
# Avoid integrating all the particles
unfix 1

# Run cylinder using nve
fix 1 pore_group nve

dump 1 all custom 10 dat/liquid_random_argon.lammpstrj id type x y z vx vy vz

# Compute the porosity
variable porosity equal count(pore_group)/count(all)
print ${porosity} file dat/porosity.dat

run 1000
```

    Overwriting scripts/liquid_random_argon.in

```python
!export OMP_NUM_THREADS=4 && mpirun -np 4 lmp -in scripts/liquid_random_argon.in
```

    LAMMPS (4 Jan 2019)
      using 4 OpenMP thread(s) per MPI task
    Lattice spacing in x,y,z = 1.67988 1.67988 1.67988
    Created orthogonal box = (0 0 0) to (33.5977 33.5977 33.5977)
      1 by 2 by 2 MPI processor grid
    Created 32000 atoms
      Time spent = 0.000880943 secs
    Neighbor list info ...
      update every 1 steps, delay 10 steps, check yes
      max neighbors/atom: 2000, page size: 100000
      master list distance cutoff = 3.3
      ghost atom cutoff = 3.3
      binsize = 1.65, bins = 21 21 21
      1 neighbor lists, perpetual/occasional/extra = 1 0 0
      (1) pair lj/cut, perpetual
          attributes: half, newton on
          pair build: half/bin/atomonly/newton
          stencil: half/bin/3d/newton
          bin: standard
    Setting up Verlet run ...
      Unit style    : lj
      Current step  : 0
      Time step     : 0.005
    Per MPI rank memory allocation (min/avg/max) = 11.78 | 11.78 | 11.78 Mbytes
    Step Temp E_pair E_mol TotEng Press
           0        0.851   -6.9322187            0   -5.6557586   -5.7920633
         100   0.47402775   -6.3193958            0   -5.6083764   -2.4013733
         200   0.52542817   -6.2911261            0   -5.5030084   -2.2236281
         300   0.57344047   -6.2429535            0   -5.3828197   -1.9201373
    Loop time of 3.09036 on 16 procs for 300 steps with 32000 atoms

    Performance: 41936.799 tau/day, 97.076 timesteps/s
    94.7% CPU use with 4 MPI tasks x 4 OpenMP threads

    MPI task timing breakdown:
    Section |  min time  |  avg time  |  max time  |%varavg| %total
    ---------------------------------------------------------------
    Pair    | 2.1518     | 2.1883     | 2.2453     |   2.3 | 70.81
    Neigh   | 0.49939    | 0.51156    | 0.52683    |   1.6 | 16.55
    Comm    | 0.23879    | 0.29737    | 0.3394     |   7.0 |  9.62
    Output  | 0.00011915 | 0.00018622 | 0.00038491 |   0.0 |  0.01
    Modify  | 0.075756   | 0.080528   | 0.084954   |   1.5 |  2.61
    Other   |            | 0.01242    |            |       |  0.40

    Nlocal:    8000 ave 8060 max 7975 min
    Histogram: 2 1 0 0 0 0 0 0 0 1
    Nghost:    10145.8 ave 10249 max 10063 min
    Histogram: 1 0 0 1 1 0 0 0 0 1
    Neighs:    517847 ave 522818 max 515443 min
    Histogram: 1 2 0 0 0 0 0 0 0 1

    Total # of neighbors = 2071388
    Ave neighs/atom = 64.7309
    Neighbor list builds = 29
    Dangerous builds = 26
    1923 atoms in group pore_group
    1287 atoms in group pore_2
    3210 atoms in group pore_group
    1585 atoms in group pore_3
    4795 atoms in group pore_group
    743 atoms in group pore_4
    5229 atoms in group pore_group
    922 atoms in group pore_5
    6151 atoms in group pore_group
    568 atoms in group pore_6
    6719 atoms in group pore_group
    987 atoms in group pore_7
    7481 atoms in group pore_group
    638 atoms in group pore_8
    7741 atoms in group pore_group
    1142 atoms in group pore_9
    8746 atoms in group pore_group
    1034 atoms in group pore_10
    9409 atoms in group pore_group
    1110 atoms in group pore_11
    10303 atoms in group pore_group
    477 atoms in group pore_12
    10744 atoms in group pore_group
    1826 atoms in group pore_13
    12539 atoms in group pore_group
    1494 atoms in group pore_14
    13346 atoms in group pore_group
    769 atoms in group pore_15
    13778 atoms in group pore_group
    2110 atoms in group pore_16
    14959 atoms in group pore_group
    1172 atoms in group pore_17
    15373 atoms in group pore_group
    531 atoms in group pore_18
    15865 atoms in group pore_group
    920 atoms in group pore_19
    16785 atoms in group pore_group
    946 atoms in group pore_20
    17427 atoms in group pore_group
    Setting atom values ...
      17427 settings made for type
    14573 atoms in group frozen
    0.54459375
    Setting up Verlet run ...
      Unit style    : lj
      Current step  : 300
      Time step     : 0.005
    Per MPI rank memory allocation (min/avg/max) = 13.78 | 13.78 | 13.78 Mbytes
    Step Temp E_pair E_mol TotEng Press
         300   0.31237373   -6.2429535            0   -5.7744075   -2.1404103
         400   0.30860761   -6.2374798            0   -5.7745828    -2.109514
         500   0.30855077   -6.2374273            0   -5.7746156   -2.1124759
         600     0.308571   -6.2373618            0   -5.7745197   -2.1150021
         700   0.30788742   -6.2364197            0    -5.774603   -2.1074638
         800   0.30666359   -6.2345859            0   -5.7746049   -2.0994764
         900   0.30739781   -6.2357219            0   -5.7746396   -2.1001406
        1000    0.3094111   -6.2387945            0   -5.7746923   -2.1146176
        1100   0.30684162   -6.2350072            0   -5.7747591   -2.0953547
        1200   0.30721825   -6.2355118            0   -5.7746988    -2.099055
        1300   0.30702065   -6.2352445            0   -5.7747279   -2.0923653
    Loop time of 13.431 on 16 procs for 1000 steps with 32000 atoms

    Performance: 32164.294 tau/day, 74.454 timesteps/s
    92.8% CPU use with 4 MPI tasks x 4 OpenMP threads

    MPI task timing breakdown:
    Section |  min time  |  avg time  |  max time  |%varavg| %total
    ---------------------------------------------------------------
    Pair    | 7.7158     | 7.919      | 8.2614     |   7.4 | 58.96
    Neigh   | 1.7338     | 1.7957     | 1.9023     |   4.8 | 13.37
    Comm    | 1.0932     | 1.5601     | 1.8383     |  23.5 | 11.62
    Output  | 1.8823     | 1.9327     | 1.9813     |   2.6 | 14.39
    Modify  | 0.16144    | 0.16828    | 0.17661    |   1.3 |  1.25
    Other   |            | 0.05528    |            |       |  0.41

    Nlocal:    8000 ave 8069 max 7963 min
    Histogram: 1 1 1 0 0 0 0 0 0 1
    Nghost:    10143.8 ave 10195 max 10097 min
    Histogram: 1 0 0 0 1 1 0 0 0 1
    Neighs:    517382 ave 522642 max 514350 min
    Histogram: 1 1 0 1 0 0 0 0 0 1

    Total # of neighbors = 2069529
    Ave neighs/atom = 64.6728
    Neighbor list builds = 100
    Dangerous builds = 100
    Total wall time: 0:00:16

We measure the porosity of the system by counting all the particles that are moving, i.e., that belong to the pore groups, and divide by the total amount of particles in our simulation. That is, let $V_p$ be the fraction of porous media and $V$ be the total volume. Then the porosity is computed from
\begin{align}
    \phi = \frac{V_p}{V}.
\end{align}
In the simulation above we found $\phi \approx 0.54$. This is a higher porosity than we could achieve using non-overlapping pores. We can see this fact by considering a cube with sides $L = 9$ filled by non-overlapping spheres with radius $r = \frac{3}{2} = 1.5$. We are able to place $27$ such non-overlapping spheres in this cube filling it completely. The cube then has a volume $V_c = L^3 = 729$ and a single sphere consists of a volume $V_s = \frac{4}{3}\pi r^3 \approx 14.1$. The total volume of pore space is then
\begin{align}
    V_p = 27 V_s \approx 381.7,
\end{align}
yielding a porosity of
\begin{align}
    \phi = \frac{V_p}{V_c}
    \approx 0.52.
\end{align}

We now wish to look at a fluid in the pores with a temperature at $T = 1.5$ and with a lower density. We can use Lammps to delete a fraction of atoms in a group thus lowering the density.

```python
%%writefile scripts/low_density.in

# 3d Lennard-Jones gas
units lj
dimension 3
# Periodic boundiaries
boundary p p p
atom_style atomic

variable seed equal 87287
variable sigma equal 3.405
variable b equal 5.72
variable reduced_density equal 4/((${b}/${sigma})^3)
variable temperature equal 1.5
variable num_box equal 20
variable radius_min equal 20/${sigma}
variable radius_max equal 30/${sigma}

# Set fcc lattice with specified density
lattice fcc ${reduced_density}
region simbox block 0 ${num_box} 0 ${num_box} 0 ${num_box}
create_box 2 simbox
create_atoms 1 box

variable box_len equal lx

mass * 1.0
velocity all create ${temperature} ${seed}

pair_style lj/cut 3.0
pair_coeff * * 1.0 1.0

fix 1 all nvt temp ${temperature} ${temperature} 0.5

thermo 100
run 1000

variable a loop 20
label pore_loop

variable x_1 equal random(0,${box_len},${seed})
variable y_1 equal random(0,${box_len},${seed})
variable z_1 equal random(0,${box_len},${seed})
variable radius_1 equal random(${radius_min},${radius_max},${seed})
region $a sphere ${x_1} ${y_1} ${z_1} ${radius_1} units box

# Delete half of the atoms in the region
delete_atoms porosity $a 0.5 ${seed}

next a
jump scripts/low_density.in pore_loop

# Create initial group
group pore_group region 1

# Group all atoms in the cylinder
variable a loop 2 20
label group_loop

group pore_$a region $a
group pore_group union pore_group pore_$a

next a
jump scripts/low_density.in group_loop

set group pore_group type 2
# Create another group from all the remaining atoms except for the ones in the cylinder
group frozen subtract all pore_group
# Set the velocity of the atoms outside the cylinder to zero
velocity frozen set 0 0 0
# Avoid integrating all the particles
unfix 1

# Thermalize pore_group
fix 1 pore_group nvt temp ${temperature} ${temperature} 0.5
thermo 100

run 4000

unfix 1
# Run pore_group using NVE
fix 1 pore_group nve

dump 1 all custom 10 dat/low_density.lammpstrj id type x y z vx vy vz

# Compute the porosity
variable porosity equal count(pore_group)/count(all)
print ${porosity} file dat/low_density_porosity.dat

reset_timestep 0
compute pore_temp pore_group temp
compute msd pore_group msd
variable time equal dt*step

thermo_style custom step v_time c_pore_temp etotal press c_msd[4]

thermo 10
log dat/low_density.log

run 2000
```

    Overwriting scripts/low_density.in

```python
!export OMP_NUM_THREADS=4 && mpirun -np 2 lmp -in scripts/low_density.in
```

    LAMMPS (4 Jan 2019)
    Lattice spacing in x,y,z = 1.67988 1.67988 1.67988
    Created orthogonal box = (0 0 0) to (33.5977 33.5977 33.5977)
      1 by 1 by 2 MPI processor grid
    Created 32000 atoms
      Time spent = 0.001909 secs
    Neighbor list info ...
      update every 1 steps, delay 10 steps, check yes
      max neighbors/atom: 2000, page size: 100000
      master list distance cutoff = 3.3
      ghost atom cutoff = 3.3
      binsize = 1.65, bins = 21 21 21
      1 neighbor lists, perpetual/occasional/extra = 1 0 0
      (1) pair lj/cut, perpetual
          attributes: half, newton on
          pair build: half/bin/atomonly/newton
          stencil: half/bin/3d/newton
          bin: standard
    Setting up Verlet run ...
      Unit style    : lj
      Current step  : 0
      Time step     : 0.005
    Per MPI rank memory allocation (min/avg/max) = 10.02 | 10.02 | 10.02 Mbytes
    Step Temp E_pair E_mol TotEng Press
           0          1.5   -6.9322187            0    -4.682289   -5.2444747
         100   0.83477058   -5.8432169            0   -4.5911002   0.42024456
         200   0.89433697   -5.7525149            0   -4.4110513   0.95945149
         300   0.95836808   -5.6406183            0   -4.2031111    1.5922005
         400    1.0323133   -5.5229934            0   -3.9745718    2.2449421
         500     1.122926   -5.4150121            0   -3.7306758    2.8623904
         600    1.2254954   -5.3175187            0   -3.4793331    3.3988102
         700    1.3213421   -5.2141462            0    -3.232195    3.9681891
         800    1.4182294   -5.1332709            0   -3.0059932    4.4305869
         900    1.4988561   -5.0698896            0   -2.8216758     4.785953
        1000    1.5361724   -5.0186643            0   -2.7144777     5.035095
    Loop time of 22.9242 on 2 procs for 1000 steps with 32000 atoms

    Performance: 18844.709 tau/day, 43.622 timesteps/s
    98.5% CPU use with 2 MPI tasks x no OpenMP threads

    MPI task timing breakdown:
    Section |  min time  |  avg time  |  max time  |%varavg| %total
    ---------------------------------------------------------------
    Pair    | 17.936     | 17.99      | 18.043     |   1.3 | 78.48
    Neigh   | 3.6484     | 3.6531     | 3.6578     |   0.2 | 15.94
    Comm    | 0.65071    | 0.71124    | 0.77178    |   7.2 |  3.10
    Output  | 0.000627   | 0.0007305  | 0.000834   |   0.0 |  0.00
    Modify  | 0.47998    | 0.48402    | 0.48806    |   0.6 |  2.11
    Other   |            | 0.08531    |            |       |  0.37

    Nlocal:    16000 ave 16036 max 15964 min
    Histogram: 1 0 0 0 0 0 0 0 0 1
    Nghost:    15877 ave 15899 max 15855 min
    Histogram: 1 0 0 0 0 0 0 0 0 1
    Neighs:    1.01408e+06 ave 1.01796e+06 max 1.0102e+06 min
    Histogram: 1 0 0 0 0 0 0 0 0 1

    Total # of neighbors = 2028159
    Ave neighs/atom = 63.38
    Neighbor list builds = 100
    Dangerous builds = 100
    Deleted 973 atoms, new total = 31027
    Deleted 633 atoms, new total = 30394
    Deleted 769 atoms, new total = 29625
    Deleted 306 atoms, new total = 29319
    Deleted 459 atoms, new total = 28860
    Deleted 298 atoms, new total = 28562
    Deleted 442 atoms, new total = 28120
    Deleted 217 atoms, new total = 27903
    Deleted 533 atoms, new total = 27370
    Deleted 425 atoms, new total = 26945
    Deleted 497 atoms, new total = 26448
    Deleted 224 atoms, new total = 26224
    Deleted 900 atoms, new total = 25324
    Deleted 553 atoms, new total = 24771
    Deleted 303 atoms, new total = 24468
    Deleted 796 atoms, new total = 23672
    Deleted 378 atoms, new total = 23294
    Deleted 257 atoms, new total = 23037
    Deleted 448 atoms, new total = 22589
    Deleted 381 atoms, new total = 22208
    884 atoms in group pore_group
    566 atoms in group pore_2
    1450 atoms in group pore_group
    748 atoms in group pore_3
    2198 atoms in group pore_group
    268 atoms in group pore_4
    2375 atoms in group pore_group
    476 atoms in group pore_5
    2851 atoms in group pore_group
    142 atoms in group pore_6
    2993 atoms in group pore_group
    342 atoms in group pore_7
    3300 atoms in group pore_group
    208 atoms in group pore_8
    3432 atoms in group pore_group
    371 atoms in group pore_9
    3774 atoms in group pore_group
    337 atoms in group pore_10
    4037 atoms in group pore_group
    343 atoms in group pore_11
    4338 atoms in group pore_group
    116 atoms in group pore_12
    4448 atoms in group pore_group
    840 atoms in group pore_13
    5284 atoms in group pore_group
    536 atoms in group pore_14
    5663 atoms in group pore_group
    190 atoms in group pore_15
    5799 atoms in group pore_group
    816 atoms in group pore_16
    6396 atoms in group pore_group
    362 atoms in group pore_17
    6593 atoms in group pore_group
    259 atoms in group pore_18
    6841 atoms in group pore_group
    461 atoms in group pore_19
    7302 atoms in group pore_group
    395 atoms in group pore_20
    7640 atoms in group pore_group
    Setting atom values ...
      7640 settings made for type
    14568 atoms in group frozen
    Setting up Verlet run ...
      Unit style    : lj
      Current step  : 1000
      Time step     : 0.005
    Per MPI rank memory allocation (min/avg/max) = 10.05 | 10.05 | 10.05 Mbytes
    Step Temp E_pair E_mol TotEng Press
        1000    0.5321663   -3.9762007            0   -3.1779872    2.4426748
        1100   0.53547061   -4.0132711            0   -3.2101013    1.8197738
        1200   0.52324944   -4.0268667            0   -3.2420279    1.8130869
        1300   0.51017112   -4.0289043            0    -3.263682    1.8148468
        1400   0.51173462   -4.0386067            0   -3.2710394    1.8101125
        1500   0.51544836   -4.0388542            0   -3.2657165    1.7974796
        1600   0.51910684   -4.0354038            0   -3.2567786    1.8246737
        1700   0.51862457   -4.0363121            0   -3.2584102    1.8207909
        1800   0.51347771   -4.0393447            0   -3.2691628    1.8043572
        1900   0.51187574   -4.0408316            0   -3.2730525    1.8193031
        2000   0.51574638   -4.0401347            0     -3.26655     1.810246
        2100   0.51973926   -4.0343343            0   -3.2547605    1.8109767
        2200   0.52150429    -4.036471            0   -3.2542498    1.8259612
        2300   0.51123962    -4.041745            0   -3.2749201    1.7888058
        2400   0.51129408   -4.0402638            0   -3.2733572    1.8026295
        2500   0.51421894   -4.0341507            0    -3.262857    1.8124201
        2600   0.51966875    -4.037444            0    -3.257976    1.7924145
        2700   0.51954301   -4.0407314            0    -3.261452    1.7843355
        2800   0.50755352   -4.0391179            0   -3.2778219    1.8046174
        2900   0.50500582   -4.0410657            0   -3.2835911    1.7966094
        3000   0.51370584   -4.0387214            0   -3.2681974    1.8204237
        3100   0.51777068   -4.0382702            0   -3.2616491    1.8018882
        3200   0.51313886   -4.0291744            0   -3.2595008    1.8166979
        3300   0.52169645   -4.0381623            0   -3.2556529    1.8152747
        3400   0.51659085   -4.0418275            0   -3.2669761    1.7943361
        3500    0.5095503   -4.0400014            0   -3.2757103    1.7881514
        3600   0.51243545   -4.0325059            0   -3.2638874    1.8104957
        3700   0.51604362   -4.0298653            0   -3.2558348    1.8213373
        3800   0.51405376   -4.0275115            0   -3.2564656    1.8155557
        3900   0.51732563   -4.0338269            0   -3.2578734     1.799005
        4000   0.51276276   -4.0302541            0   -3.2611445    1.7998781
        4100   0.51501651   -4.0335091            0   -3.2610191     1.816589
        4200   0.52222055   -4.0377783            0   -3.2544827    1.8030866
        4300   0.52519484    -4.041492            0   -3.2537352    1.7943771
        4400   0.51365715   -4.0373506            0   -3.2668995    1.8008933
        4500   0.51099054    -4.037662            0   -3.2712107    1.7895268
        4600   0.51914284   -4.0347963            0   -3.2561171    1.8047697
        4700   0.52692613   -4.0376142            0   -3.2472606    1.8398295
        4800   0.51670581    -4.035099            0   -3.2600751    1.8213018
        4900   0.51368187   -4.0372424            0   -3.2667543    1.7990528
        5000    0.5164691   -4.0395399            0   -3.2648711    1.8143265
    Loop time of 52.2697 on 2 procs for 4000 steps with 22208 atoms

    Performance: 33059.331 tau/day, 76.526 timesteps/s
    97.3% CPU use with 2 MPI tasks x no OpenMP threads

    MPI task timing breakdown:
    Section |  min time  |  avg time  |  max time  |%varavg| %total
    ---------------------------------------------------------------
    Pair    | 35.275     | 37.631     | 39.988     |  38.4 | 71.99
    Neigh   | 8.3017     | 8.7764     | 9.2511     |  16.0 | 16.79
    Comm    | 1.624      | 4.4769     | 7.3298     | 134.8 |  8.57
    Output  | 0.002085   | 0.0025395  | 0.002994   |   0.9 |  0.00
    Modify  | 1.1453     | 1.1648     | 1.1842     |   1.8 |  2.23
    Other   |            | 0.2176     |            |       |  0.42

    Nlocal:    11104 ave 11588 max 10620 min
    Histogram: 1 0 0 0 0 0 0 0 0 1
    Nghost:    11898.5 ave 12016 max 11781 min
    Histogram: 1 0 0 0 0 0 0 0 0 1
    Neighs:    545414 ave 582189 max 508638 min
    Histogram: 1 0 0 0 0 0 0 0 0 1

    Total # of neighbors = 1090827
    Ave neighs/atom = 49.1187
    Neighbor list builds = 400
    Dangerous builds = 400
    0.344020172910663
    Setting up Verlet run ...
      Unit style    : lj
      Current step  : 0
      Time step     : 0.005
    Per MPI rank memory allocation (min/avg/max) = 13 | 13 | 13 Mbytes
    Step v_time c_pore_temp TotEng Press c_msd[4]
           0            0    1.5014045   -3.2648711    1.8143265            0
          10         0.05    1.5038898   -3.2648621    1.8109131  0.010997185
          20          0.1    1.5146326   -3.2648882    1.7975831  0.041692254
          30         0.15    1.5023211   -3.2649063    1.8172732  0.087594452
          40          0.2    1.5096983   -3.2649594    1.8092793   0.14427516
          50         0.25    1.5065906   -3.2649745    1.8173559   0.20947975
          60          0.3    1.4994294   -3.2650246    1.8361641   0.28138782
          70         0.35    1.4988473   -3.2649975    1.8422572   0.35848342
          80          0.4    1.5033468   -3.2650108    1.8354825   0.43998605
          90         0.45    1.5056617   -3.2650305    1.8276617   0.52511359
         100          0.5    1.5108247   -3.2649554    1.8177077     0.613705
         110         0.55    1.5024751   -3.2649261    1.8285549   0.70563551
         120          0.6      1.50654   -3.2649327    1.8217675   0.79991369
         130         0.65       1.5149    -3.264995    1.8055424   0.89572642
         140          0.7    1.5068238   -3.2649126    1.8107346   0.99328527
         150         0.75    1.5068153   -3.2648747    1.8071672    1.0921202
         160          0.8    1.5083357     -3.26484    1.8024051    1.1923917
         170         0.85     1.503117   -3.2648363    1.8070482    1.2939533
         180          0.9    1.5078564   -3.2648709    1.7986695    1.3967808
         190         0.95    1.5102244   -3.2648243     1.792913    1.5007965
         200            1    1.5015944   -3.2647569    1.8051449    1.6063329
         210         1.05    1.5005078   -3.2647601    1.8090453    1.7124783
         220          1.1    1.4985082   -3.2647286    1.8101552    1.8176029
         230         1.15    1.5155164    -3.264788    1.7896205    1.9214106
         240          1.2     1.502029   -3.2647573    1.8161384    2.0279232
         250         1.25    1.5084997   -3.2648131     1.817705    2.1375299
         260          1.3    1.4901903   -3.2648395    1.8471741    2.2491691
         270         1.35    1.4971022   -3.2648899    1.8362428      2.36079
         280          1.4    1.5089529   -3.2649049    1.8166792    2.4706404
         290         1.45    1.4923202    -3.264866    1.8355314    2.5806626
         300          1.5    1.5062264   -3.2648899    1.8103261    2.6909562
         310         1.55    1.5098507   -3.2648872    1.8017187    2.8023142
         320          1.6    1.5068584   -3.2648512    1.8062061    2.9145666
         330         1.65    1.5135878   -3.2648757    1.7962169    3.0269374
         340          1.7    1.5062025   -3.2648861    1.8088116     3.139713
         350         1.75    1.4979151   -3.2648804    1.8264394    3.2532081
         360          1.8     1.502048   -3.2648925     1.818952    3.3662483
         370         1.85    1.5003671   -3.2648952    1.8130923     3.479233
         380          1.9    1.5047913   -3.2648709    1.8048991    3.5935279
         390         1.95    1.5089217    -3.264844    1.7995434    3.7101964
         400            2    1.5040106   -3.2648459     1.810987    3.8273019
         410         2.05    1.4873367   -3.2649419    1.8375604    3.9442953
         420          2.1     1.507283   -3.2649427    1.8070796    4.0598544
         430         2.15    1.5042437   -3.2649428      1.80793    4.1756321
         440          2.2    1.5172677   -3.2650029    1.7942358    4.2924566
         450         2.25    1.5044051   -3.2649785    1.8210657    4.4087305
         460          2.3    1.4958788   -3.2649589    1.8386641    4.5209444
         470         2.35     1.513223   -3.2649543    1.8157922    4.6297325
         480          2.4    1.5099648   -3.2649348    1.8208015    4.7385022
         490         2.45    1.5037401   -3.2648612    1.8279721    4.8462897
         500          2.5    1.5091739   -3.2648624    1.8180117    4.9528695
         510         2.55    1.5169234   -3.2648949    1.8032945    5.0604771
         520          2.6    1.5154209   -3.2649407    1.8044557    5.1702088
         530         2.65     1.508536   -3.2649623    1.8175706    5.2804316
         540          2.7     1.502165   -3.2649367     1.828595    5.3908483
         550         2.75    1.5013401   -3.2649713    1.8293581    5.5026926
         560          2.8     1.504919   -3.2648929    1.8218294    5.6150793
         570         2.85    1.5143372   -3.2649299    1.8058723      5.72574
         580          2.9    1.5017896    -3.264902    1.8227485    5.8362007
         590         2.95    1.5061967   -3.2649216    1.8167711    5.9450885
         600            3    1.4983529   -3.2648929    1.8257698    6.0531095
         610         3.05    1.5070447   -3.2649133    1.8129489    6.1602179
         620          3.1    1.5140042   -3.2648639    1.8000564     6.267506
         630         3.15    1.5158457   -3.2648572    1.7975592    6.3756822
         640          3.2    1.5128528   -3.2648636    1.8026362    6.4856184
         650         3.25    1.5070574   -3.2648071    1.8086381    6.5976652
         660          3.3    1.5098573   -3.2648543    1.8010876    6.7098805
         670         3.35    1.5055461   -3.2648173     1.803323    6.8225469
         680          3.4     1.506042   -3.2648707    1.8010592     6.937699
         690         3.45     1.503086   -3.2648716    1.8031342    7.0542436
         700          3.5    1.5066065     -3.26485    1.7978185    7.1675664
         710         3.55    1.5040944   -3.2647955    1.8013781     7.277941
         720          3.6    1.4974528   -3.2648038    1.8118843    7.3856663
         730         3.65    1.4984581   -3.2647868    1.8127739    7.4885354
         740          3.7    1.4946527   -3.2647816    1.8206459    7.5904245
         750         3.75     1.491574   -3.2648028     1.824468    7.6939461
         760          3.8    1.4985856   -3.2648146     1.812084    7.7982701
         770         3.85    1.4963323   -3.2648226    1.8107633    7.9016529
         780          3.9    1.4999606   -3.2647369    1.8028162    8.0038923
         790         3.95    1.4903661   -3.2647124     1.818283    8.1084248
         800            4    1.4977683   -3.2647227    1.8118021    8.2157165
         810         4.05    1.5055596   -3.2647065    1.8035778    8.3244374
         820          4.1    1.4897455   -3.2646842    1.8269139    8.4334918
         830         4.15    1.4938544   -3.2647482    1.8201755    8.5403067
         840          4.2    1.4936394    -3.264727    1.8151835     8.644772
         850         4.25    1.5047716   -3.2647265    1.7941817    8.7467475
         860          4.3    1.5030115   -3.2646883    1.7927307    8.8486847
         870         4.35    1.4993627   -3.2646523    1.7951407    8.9542124
         880          4.4    1.4999472   -3.2646404    1.7947197    9.0626446
         890         4.45    1.4963045   -3.2646704    1.8014645    9.1721484
         900          4.5    1.4951743   -3.2646411     1.801305    9.2800807
         910         4.55    1.5029425   -3.2646414    1.7895587    9.3871169
         920          4.6     1.497832   -3.2646567    1.7988449    9.4948584
         930         4.65    1.4978499   -3.2647115    1.8041799    9.6029044
         940          4.7     1.502695   -3.2647362    1.8010691    9.7094289
         950         4.75    1.4942258   -3.2647886    1.8168294    9.8151889
         960          4.8    1.4961801   -3.2648076    1.8196332    9.9192498
         970         4.85    1.4995801   -3.2648606    1.8190139    10.022847
         980          4.9    1.5035408   -3.2648869    1.8185109    10.123962
         990         4.95    1.4867506    -3.264862    1.8458266    10.223021
        1000            5    1.4997275   -3.2648479    1.8302293    10.322844
        1010         5.05    1.5047138   -3.2648726    1.8202359    10.422351
        1020          5.1    1.5103145   -3.2649094    1.8117341    10.521457
        1030         5.15    1.4967497    -3.264977    1.8305481    10.624174
        1040          5.2    1.5017557   -3.2649965    1.8231923    10.730107
        1050         5.25    1.5028482   -3.2649632    1.8191593    10.838045
        1060          5.3    1.5044668   -3.2649327    1.8167651    10.947784
        1070         5.35    1.4996838   -3.2648945    1.8258897    11.057777
        1080          5.4    1.4966061    -3.264911    1.8301931    11.165077
        1090         5.45    1.5046294   -3.2649291    1.8157094    11.271099
        1100          5.5    1.5036473   -3.2649584     1.812486    11.374479
        1110         5.55     1.497556    -3.264936    1.8148476    11.474348
        1120          5.6    1.5094671   -3.2649562     1.796127    11.573697
        1130         5.65    1.4959203   -3.2649338    1.8160557    11.677545
        1140          5.7     1.490887    -3.264905    1.8263148    11.784723
        1150         5.75    1.4969858   -3.2649054     1.816099    11.894946
        1160          5.8    1.5042009   -3.2648494    1.8029397    12.003191
        1170         5.85    1.4977773   -3.2648229    1.8138122    12.110474
        1180          5.9    1.5009323   -3.2648365    1.8120667    12.220461
        1190         5.95    1.4915044   -3.2648518    1.8227269    12.329878
        1200            6    1.4983318   -3.2648609    1.8118318    12.435163
        1210         6.05     1.496014   -3.2648741    1.8128012    12.540913
        1220          6.1    1.5031862   -3.2648787    1.8009713    12.649623
        1230         6.15    1.5093393    -3.264924     1.794889    12.762227
        1240          6.2     1.503386   -3.2648912    1.8115794    12.874141
        1250         6.25    1.5031547   -3.2649093    1.8232632    12.983814
        1260          6.3    1.5081169   -3.2649467    1.8276423    13.094136
        1270         6.35    1.5029995   -3.2649226    1.8404492    13.203134
        1280          6.4    1.4986114   -3.2649428    1.8435225    13.308515
        1290         6.45    1.5013497   -3.2649509    1.8313884    13.410188
        1300          6.5    1.5002215   -3.2649402    1.8242865    13.510807
        1310         6.55    1.5017654   -3.2648967    1.8163461    13.612014
        1320          6.6    1.5071303   -3.2648982    1.8063213    13.711908
        1330         6.65    1.5051516   -3.2648624    1.8085902    13.809363
        1340          6.7    1.4972324   -3.2648424    1.8176755    13.908054
        1350         6.75    1.5048679    -3.264849     1.804573    14.005517
        1360          6.8    1.4994058   -3.2648641    1.8135485    14.099162
        1370         6.85    1.4907844   -3.2648556    1.8294856    14.190854
        1380          6.9     1.494299   -3.2648596    1.8252927    14.281175
        1390         6.95    1.4958034    -3.264821    1.8196783    14.370585
        1400            7     1.497038   -3.2647812    1.8131925    14.462349
        1410         7.05    1.5003314   -3.2647307    1.8067399    14.556711
        1420          7.1    1.4895887   -3.2647278    1.8229473    14.653724
        1430         7.15    1.4830713    -3.264702    1.8316191    14.749837
        1440          7.2    1.4888682   -3.2646943    1.8203544     14.84551
        1450         7.25     1.494856   -3.2646762    1.8071593    14.942009
        1460          7.3    1.4973138   -3.2646816    1.7976311    15.040517
        1470         7.35    1.4940656    -3.264658    1.7960929    15.138546
        1480          7.4    1.4943421   -3.2646276    1.7929077    15.238546
        1490         7.45    1.4949826   -3.2646144    1.7888957    15.340657
        1500          7.5    1.4960234   -3.2646091    1.7842876    15.443917
        1510         7.55    1.4887131   -3.2646492    1.7950805    15.547321
        1520          7.6    1.4838099   -3.2646034    1.8017914    15.650412
        1530         7.65     1.486824   -3.2645524    1.7945814    15.753516
        1540          7.7    1.4850316   -3.2645562     1.798311    15.858801
        1550         7.75    1.4909648   -3.2645873    1.7943433    15.962746
        1560          7.8    1.4928499   -3.2646444    1.7957145    16.064921
        1570         7.85    1.4835045   -3.2646427    1.8107625    16.167424
        1580          7.9    1.4950456   -3.2646262      1.79554    16.268773
        1590         7.95    1.5003357   -3.2646025    1.7874511    16.366981
        1600            8    1.5006257     -3.26463    1.7912367    16.462892
        1610         8.05    1.4924731   -3.2646524    1.8091834    16.563136
        1620          8.1    1.4893251   -3.2646579    1.8198812    16.668269
        1630         8.15    1.4859165   -3.2647045    1.8280756    16.771332
        1640          8.2    1.4955952   -3.2647699    1.8158045     16.87382
        1650         8.25    1.4945079   -3.2647726    1.8172933     16.97828
        1660          8.3    1.5020184   -3.2648025    1.8083931    17.082993
        1670         8.35    1.4995145   -3.2648568    1.8140498     17.19028
        1680          8.4     1.500243   -3.2648722    1.8155559    17.296421
        1690         8.45    1.5081468   -3.2649156    1.8076968    17.401419
        1700          8.5    1.4933857   -3.2648869    1.8285978     17.50577
        1710         8.55    1.4985692   -3.2649289     1.822014    17.608891
        1720          8.6    1.5038108   -3.2649463    1.8158923    17.710839
        1730         8.65    1.5159208   -3.2649659      1.80265    17.813083
        1740          8.7    1.5052054   -3.2650108    1.8211564    17.914976
        1750         8.75    1.5021157    -3.265015    1.8251508    18.014307
        1760          8.8    1.5060217   -3.2650244    1.8169803    18.110967
        1770         8.85    1.5022868   -3.2649733    1.8200602    18.209789
        1780          8.9    1.5031886   -3.2650506    1.8207723    18.311814
        1790         8.95    1.4955237   -3.2650658    1.8299595    18.415723
        1800            9    1.4949766   -3.2650547    1.8264771     18.51547
        1810         9.05    1.5034971   -3.2650835    1.8147698    18.611092
        1820          9.1     1.507442   -3.2651003    1.8132813    18.705721
        1830         9.15     1.505529   -3.2650768    1.8208227    18.801512
        1840          9.2    1.5011888   -3.2650476    1.8301218    18.898617
        1850         9.25     1.498989   -3.2650703    1.8338242    18.994701
        1860          9.3    1.5081624   -3.2651021    1.8216548    19.089147
        1870         9.35    1.5127713   -3.2650972    1.8148266    19.184435
        1880          9.4     1.515145   -3.2650705    1.8103365    19.281858
        1890         9.45    1.5129557   -3.2651048    1.8098786    19.379561
        1900          9.5     1.504134   -3.2651162    1.8201943    19.478273
        1910         9.55    1.4961574   -3.2650961    1.8287157    19.581541
        1920          9.6    1.5006236   -3.2650516    1.8182906    19.688348
        1930         9.65    1.5037481    -3.265077    1.8130028    19.792583
        1940          9.7     1.508408   -3.2651315    1.8095777    19.894438
        1950         9.75      1.50204   -3.2651338    1.8208053    19.994927
        1960          9.8    1.5000665   -3.2651115    1.8228639    20.093319
        1970         9.85     1.506897   -3.2651244    1.8112979    20.190283
        1980          9.9    1.5062023   -3.2651082    1.8118898    20.285432
        1990         9.95     1.497569   -3.2650786    1.8216823     20.38026
        2000           10    1.4965047   -3.2650866    1.8183794    20.477813
    Loop time of 36.9742 on 2 procs for 2000 steps with 22208 atoms

    Performance: 23367.623 tau/day, 54.092 timesteps/s
    89.7% CPU use with 2 MPI tasks x no OpenMP threads

    MPI task timing breakdown:
    Section |  min time  |  avg time  |  max time  |%varavg| %total
    ---------------------------------------------------------------
    Pair    | 22.185     | 23.559     | 24.933     |  28.3 | 63.72
    Neigh   | 4.6377     | 4.8949     | 5.1522     |  11.6 | 13.24
    Comm    | 1.3341     | 2.952      | 4.5698     |  94.2 |  7.98
    Output  | 5.0728     | 5.0756     | 5.0785     |   0.1 | 13.73
    Modify  | 0.35346    | 0.35808    | 0.3627     |   0.8 |  0.97
    Other   |            | 0.1346     |            |       |  0.36

    Nlocal:    11104 ave 11536 max 10672 min
    Histogram: 1 0 0 0 0 0 0 0 0 1
    Nghost:    11918.5 ave 12059 max 11778 min
    Histogram: 1 0 0 0 0 0 0 0 0 1
    Neighs:    546034 ave 579919 max 512150 min
    Histogram: 1 0 0 0 0 0 0 0 0 1

    Total # of neighbors = 1092069
    Ave neighs/atom = 49.1746
    Neighbor list builds = 200
    Dangerous builds = 200
    Total wall time: 0:01:52

```python
log_df = read_log("dat/low_density.log")
```

```python
fig = plt.figure(figsize=(14, 10))

plt.plot(log_df["v_time"], log_df["c_pore_temp"])
plt.xlabel(r"$t$")
plt.ylabel(r"$T(t)$")
plt.title(r"Pore temperature as a function of time")
plt.show()
```

```python
fig = plt.figure(figsize=(14, 10))

plt.plot(log_df["v_time"], log_df["c_msd[4]"])
plt.xlabel(r"$t$")
plt.ylabel(r"$\langle r^2(t) \rangle$")
plt.show()
```

Here we see how the mean squared displacement is linear when $t$ is large. Initially we observe the expected anomalous behaviour.


# Portfolio Optimization

```python
# Import libraries

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

# !pip install pandas-datareader
import pandas_datareader.data as web
import datetime

```

### Collect Data
Our investment universe consists of 20 stocks (n=20) all of which are constituents of the S&P 500. The stock symbols are F (Ford Motor Co.), CAT (Catepillar Inc.), DIS, MCD, KO, PEP, WMT, C, WFC, JPM, AAPL, IBM, PFE, JNJ, XOM, MRO, ED, T, VZ and NEM.

```python
stocks = ["F", "CAT", "DIS", "MCD", "KO", "PEP", "WMT", "C", "WFC", "JPM", "AAPL", "IBM",
          "PFE", "JNJ", "XOM", "MRO", "ED", "T", "VZ", "NEM"]
```

Collect historical stock price from 30-Dec-2004 to 30-Sep-2008

```python
start = datetime.datetime(2004,12,30)
end = datetime.datetime(2008,9,30)

# pandas_datareader offers remote data access
daily_price = {}

for i in stocks:
    info = web.DataReader(i, "yahoo", start, end)
    # extract daily adjusted closing prices
    info_price = {i : info['Adj Close']}
    daily_price.update(info_price)
```

```python
daily_price = pd.DataFrame(daily_price)
daily_price
```

      F
      CAT
      DIS
      MCD
      KO
      PEP
      WMT
      C
      WFC
      JPM
      AAPL
      IBM
      PFE
      JNJ
      XOM
      MRO
      ED
      T
      VZ
      NEM

      Date

      2004-12-30
      9.328557
      31.554874
      22.636421
      20.282452
      12.732676
      33.171490
      37.107845
      350.587158
      19.444393
      25.094088
      0.995030
      63.082661
      13.276471
      39.717316
      30.052114
      7.949283
      21.314610
      10.499263
      16.724615
      34.624271

      2004-12-31
      9.227709
      31.545166
      22.571474
      20.082001
      12.729613
      33.139755
      36.939999
      351.682037
      19.428762
      25.049150
      0.988888
      63.262360
      13.217483
      39.636074
      30.187553
      7.987513
      21.102385
      10.503343
      16.613880
      34.523235

      2005-01-03
      9.271832
      30.755819
      22.612068
      19.931664
      12.699042
      32.974693
      37.310658
      352.338928
      19.353729
      25.139050
      0.971844
      62.729687
      13.001202
      39.311089
      29.498526
      7.768762
      20.885326
      10.429976
      16.605675
      32.929596

      2005-01-04
      9.240314
      30.416130
      22.368490
      20.094528
      12.521735
      32.739799
      37.219749
      349.346252
      19.369364
      24.879946
      0.981825
      62.055878
      13.001202
      39.186104
      29.298313
      7.681689
      20.822618
      10.319929
      16.437527
      32.315483

      2005-01-05
      9.095345
      29.833824
      22.246700
      19.937931
      12.463651
      32.777882
      37.268692
      353.725891
      19.391243
      24.931757
      0.990424
      61.927532
      12.912727
      39.161087
      29.145182
      7.588243
      20.595921
      10.368834
      16.412918
      32.393227

      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...
      ...

      2008-09-24
      3.409708
      42.777790
      26.953121
      42.091274
      16.945087
      47.980503
      43.706726
      163.172943
      24.390308
      29.565556
      3.952789
      78.445915
      10.367218
      46.545868
      49.260204
      18.414692
      25.205294
      14.006592
      15.820449
      33.879978

      2008-09-25
      3.375813
      42.930218
      27.546986
      42.666851
      17.373867
      48.801853
      44.596882
      167.045776
      24.283545
      31.726408
      4.051679
      80.904495
      10.741798
      47.344494
      50.926838
      19.023474
      25.555365
      14.533408
      16.209890
      33.389900

      2008-09-26
      3.260575
      44.433735
      27.784529
      43.304077
      17.718231
      49.082481
      45.034542
      173.414307
      26.553904
      35.215881
      3.938355
      80.439705
      10.753325
      47.371792
      50.914211
      18.689325
      25.473686
      14.499574
      16.275639
      32.939976

      2008-09-29
      2.826735
      41.495979
      25.222418
      41.221081
      17.228685
      47.679348
      43.358074
      152.759460
      23.664356
      29.930584
      3.232621
      77.098701
      10.171285
      45.665306
      46.753971
      17.155916
      24.971912
      13.412107
      15.486642
      31.622381

      2008-09-30
      3.524946
      41.295029
      26.036863
      42.276287
      17.853281
      48.781342
      44.426273
      176.512527
      26.710480
      34.091648
      3.490591
      78.782700
      10.626545
      47.289864
      49.026638
      18.249905
      25.065271
      13.494273
      16.230124
      31.140335

945 rows × 20 columns

### Parameter Estimation (Monthly)
Using monthly (last trading day of each month) adjusted closing prices for
each stock from 30-Dec-2004 to 30-Sep-2008 compute the sample mean and
sample variance for each stock. Also, compute sample covariances.

```python
# Only require adjusted closing prices of last trading day of each month
asset_monthly_return = daily_price.resample('M').ffill().pct_change()
asset_monthly_return
```

      F
      CAT
      DIS
      MCD
      KO
      PEP
      WMT
      C
      WFC
      JPM
      AAPL
      IBM
      PFE
      JNJ
      XOM
      MRO
      ED
      T
      VZ
      NEM

      Date

      2004-12-31
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN
      NaN

      2005-01-31
      -0.093558
      -0.082231
      0.029856
      0.010293
      -0.003602
      0.028735
      -0.007951
      0.018057
      -0.013677
      -0.034683
      0.194099
      -0.052343
      -0.101525
      0.020183
      0.006633
      0.029779
      0.002743
      -0.066142
      -0.112918
      -0.063500

      2005-02-28
      -0.039484
      0.066779
      -0.024101
      0.021303
      0.031573
      0.002980
      -0.015077
      -0.018422
      -0.023738
      -0.020895
      0.166710
      -0.007101
      0.096315
      0.018299
      0.232947
      0.230378
      -0.012915
      0.012627
      0.010677
      0.081991

      2005-03-31
      -0.104348
      -0.037980
      0.028275
      -0.058646
      -0.020030
      -0.011187
      -0.026202
      -0.058256
      0.007073
      -0.053352
      -0.071110
      -0.012962
      -0.000761
      0.023781
      -0.058600
      -0.008872
      -0.013333
      -0.015378
      -0.013066
      -0.059020

      2005-04-30
      -0.187331
      -0.032568
      -0.081100
      -0.058767
      0.042476
      0.049218
      -0.059270
      0.054814
      0.002341
      0.036007
      -0.134629
      -0.164149
      0.034259
      0.021888
      -0.043121
      -0.007460
      0.026079
      0.018233
      0.019999
      -0.101302

      2005-05-31
      0.095500
      0.068825
      0.039394
      0.055613
      0.027394
      0.011143
      0.005117
      0.003194
      0.015929
      0.007326
      0.102607
      -0.008239
      0.033971
      -0.017501
      -0.009535
      0.047699
      0.064897
      -0.017647
      -0.011732
      -0.016532

      2005-06-30
      0.026052
      0.012751
      -0.082362
      -0.103103
      -0.058533
      -0.036936
      0.020539
      -0.018679
      0.019368
      -0.012028
      -0.074195
      -0.017869
      -0.011470
      -0.031296
      0.022598
      0.100639
      0.029224
      0.015825
      -0.023459
      0.048067

      2005-07-31
      0.058540
      0.136578
      0.018268
      0.123243
      0.048144
      0.011126
      0.023858
      -0.049633
      -0.003898
      0.004574
      0.158652
      0.124798
      -0.039159
      -0.016000
      0.022273
      0.093498
      0.028181
      0.043458
      0.002363
      -0.037920

      2005-08-31
      -0.071695
      0.029308
      -0.017551
      0.041065
      0.005485
      0.005868
      -0.086075
      0.006207
      -0.019782
      -0.035572
      0.099414
      -0.031705
      -0.031599
      -0.003795
      0.024571
      0.107718
      -0.014125
      -0.015133
      -0.044406
      0.054061

      2005-09-30
      -0.011033
      0.058749
      -0.042080
      0.032049
      -0.012158
      0.038805
      -0.025356
      0.039982
      -0.017611
      0.001180
      0.143314
      -0.004961
      -0.020015
      -0.001735
      0.060768
      0.071840
      0.034961
      -0.004568
      -0.000612
      0.194682

      2005-10-31
      -0.145691
      -0.100795
      0.009946
      -0.056434
      -0.009493
      0.041792
      0.079644
      0.005712
      0.027830
      0.090144
      0.074240
      0.020693
      -0.129356
      -0.010430
      -0.116462
      -0.127231
      -0.062822
      0.008590
      -0.023826
      -0.096883

      2005-11-30
      -0.022836
      0.098688
      0.022979
      0.093177
      0.004471
      0.002031
      0.026421
      0.070851
      0.053164
      0.044511
      0.177635
      0.088339
      -0.016447
      -0.008729
      0.038942
      -0.008748
      0.013465
      0.044445
      0.014915
      0.084905

      2005-12-31
      -0.050431
      -0.000172
      -0.028287
      -0.003840
      -0.055751
      0.002338
      -0.033312
      -0.000412
      -0.000318
      0.037647
      0.060012
      -0.075366
      0.100000
      -0.026721
      -0.032052
      0.028336
      0.017347
      -0.016861
      -0.058161
      0.157850

      2006-01-31
      0.124444
      0.180036
      0.055903
      0.038256
      0.026544
      -0.032160
      -0.014744
      -0.040181
      -0.007480
      0.010056
      0.050355
      -0.010949
      0.101201
      -0.042595
      0.117144
      0.260784
      0.014677
      0.073950
      0.064762
      0.157303

      2006-02-28
      -0.071096
      0.076289
      0.105887
      -0.002856
      0.014258
      0.033753
      -0.016265
      0.006133
      0.038163
      0.034969
      -0.092968
      -0.010560
      0.029674
      0.007587
      -0.048838
      -0.077050
      -0.011937
      0.063199
      0.064434
      -0.143689

      2006-03-31
      -0.001255
      -0.017378
      -0.003573
      -0.015755
      0.004901
      -0.018059
      0.045302
      0.018546
      -0.005140
      0.012154
      -0.084246
      0.027791
      -0.048492
      0.027233
      0.025097
      0.078895
      -0.051668
      -0.019935
      0.010683
      -0.017619

      2006-04-30
      -0.114587
      0.058077
      0.002510
      0.006112
      0.002150
      0.007787
      -0.046782
      0.068556
      0.075466
      0.098727
      0.122290
      -0.001576
      0.016453
      -0.010300
      0.036477
      0.041880
      -0.008735
      -0.018632
      -0.018701
      0.124687

      2006-05-31
      0.030216
      -0.036837
      0.090844
      -0.040498
      0.049333
      0.038118
      0.079723
      -0.013013
      -0.026317
      -0.060379
      -0.150874
      -0.026125
      -0.057058
      0.033847
      -0.029544
      -0.049457
      0.036370
      -0.005723
      -0.055102
      -0.106408

      2006-06-30
      -0.032123
      0.020973
      -0.016393
      0.012964
      -0.015895
      -0.002000
      -0.005779
      -0.021298
      0.010697
      -0.015009
      -0.041827
      -0.038548
      -0.008031
      -0.004982
      0.007224
      0.109926
      0.007709
      0.070223
      0.073054
      0.016891

      2006-07-31
      -0.030482
      -0.044325
      -0.010333
      0.053274
      0.034403
      0.055629
      -0.076188
      0.001243
      0.078414
      0.095055
      0.186660
      0.007681
      0.107371
      0.043892
      0.104157
      0.088115
      0.054680
      0.088285
      0.022291
      -0.032118

      2006-08-31
      0.254873
      -0.063779
      -0.001347
      0.014411
      0.006967
      0.029978
      0.008748
      0.031906
      -0.031785
      0.000877
      -0.001619
      0.050160
      0.070227
      0.039732
      0.003601
      -0.074630
      -0.001762
      0.038013
      0.040213
      0.000391

      2006-09-30
      -0.033453
      -0.008290
      0.042496
      0.089693
      0.004049
      0.004315
      0.102862
      0.006484
      0.041151
      0.028471
      0.134562
      0.011980
      0.029027
      0.004331
      -0.008423
      -0.079042
      0.000000
      0.045937
      0.055429
      -0.164222

      2006-10-31
      0.023486
      -0.073331
      0.017794
      0.071574
      0.045658
      -0.027888
      -0.000811
      0.009865
      0.003040
      0.017467
      0.053260
      0.126800
      -0.060296
      0.037881
      0.064381
      0.123538
      0.046536
      0.062719
      0.007286
      0.058948

      2006-11-30
      -0.018116
      0.021743
      0.050540
      0.025630
      0.009056
      -0.023171
      -0.064529
      -0.001597
      -0.021383
      -0.024452
      0.130488
      -0.001210
      0.040672
      -0.016584
      0.080132
      0.097276
      0.009328
      -0.009927
      -0.019740
      0.036227

      2006-12-31
      -0.076261
      -0.011285
      0.046304
      0.056230
      0.030322
      0.014100
      0.005435
      0.123210
      0.009081
      0.043647
      -0.074405
      0.056897
      -0.057840
      0.001669
      -0.002343
      -0.019919
      -0.003111
      0.054261
      0.065827
      -0.035479

      2007-01-31
      0.082556
      0.049934
      0.026262
      0.000451
      -0.007668
      0.043006
      0.032699
      -0.010234
      0.018064
      0.061927
      0.010490
      0.020587
      0.013128
      0.011815
      -0.033016
      -0.023351
      0.004369
      0.063707
      0.045701
      -0.001108

      2007-02-28
      -0.027060
      0.005619
      -0.026159
      -0.014881
      -0.025063
      -0.032036
      0.013000
      -0.077304
      -0.033964
      -0.030238
      -0.013064
      -0.059808
      -0.038387
      -0.052449
      -0.028539
      0.008627
      0.018259
      -0.022057
      -0.029076
      0.000000

      2007-03-31
      -0.002529
      0.040354
      0.005255
      0.031129
      0.035622
      0.011288
      -0.023499
      0.019258
      -0.007781
      -0.020449
      0.098097
      0.014203
      0.012019
      -0.042428
      0.052595
      0.089396
      0.051050
      0.071467
      0.013905
      -0.066793

      2007-04-30
      0.019012
      0.088101
      0.015974
      0.071698
      0.087292
      0.039805
      0.020660
      0.044410
      0.042405
      0.084535
      0.074158
      0.084341
      0.047506
      0.065715
      0.052087
      0.027522
      0.003917
      -0.009039
      0.017697
      -0.006906

      2007-05-31
      0.037313
      0.082071
      0.013151
      0.047018
      0.015328
      0.033893
      -0.002068
      0.026433
      0.013446
      -0.005182
      0.214328
      0.047010
      0.050061
      -0.008344
      0.052268
      0.224677
      -0.036638
      0.067666
      0.140126
      -0.024460

      2007-06-30
      0.129497
      -0.003563
      -0.023499
      0.004154
      -0.006225
      -0.045637
      0.010715
      -0.058726
      -0.025492
      -0.065213
      0.007014
      -0.012664
      -0.069844
      -0.026079
      0.008537
      -0.031419
      -0.075789
      0.003871
      -0.054215
      -0.037500

      2007-07-31
      -0.096603
      0.010593
      -0.033392
      -0.056935
      -0.003823
      0.011874
      -0.044897
      -0.092026
      -0.039807
      -0.084563
      0.079646
      0.051306
      -0.080563
      -0.018176
      0.014902
      -0.079387
      -0.031915
      -0.048136
      0.045304
      0.068868

      2007-08-31
      -0.082256
      -0.038452
      0.018182
      0.028828
      0.032047
      0.036727
      -0.045699
      0.018394
      0.091726
      0.011589
      0.051002
      0.058316
      0.069307
      0.028192
      0.011074
      -0.019354
      0.064760
      0.018130
      -0.017363
      0.012216

      2007-09-30
      0.087068
      0.035106
      0.023512
      0.105990
      0.075181
      0.082796
      0.000459
      -0.004479
      -0.025178
      0.029200
      0.108247
      0.009512
      -0.016506
      0.063279
      0.079669
      0.058081
      0.007837
      0.061200
      0.057307
      0.060958

      2007-10-31
      0.044759
      -0.044245
      0.006979
      0.096935
      0.074648
      0.006279
      0.035739
      -0.102207
      -0.045199
      0.034086
      0.237701
      -0.014261
      0.007368
      -0.008067
      -0.006158
      0.037005
      0.017062
      -0.003869
      0.050369
      0.137939

      2007-11-30
      -0.153326
      -0.036322
      -0.042738
      0.004311
      0.010963
      0.046934
      0.059500
      -0.194874
      -0.037401
      -0.029362
      -0.040695
      -0.091001
      -0.022695
      0.045900
      -0.026996
      -0.050664
      0.041698
      -0.085666
      -0.062080
      -0.023772

      2007-12-31
      -0.103861
      0.009179
      -0.015725
      0.007526
      -0.011755
      -0.011750
      -0.003252
      -0.115916
      -0.069072
      -0.043183
      0.087038
      0.027763
      -0.043350
      -0.015353
      0.050808
      0.088730
      0.008256
      0.087674
      0.011109
      -0.015324

      2008-01-31
      -0.013374
      -0.016126
      -0.075589
      -0.090477
      -0.038618
      -0.103426
      0.067536
      -0.032029
      0.128851
      0.095447
      -0.316640
      -0.009158
      0.027717
      -0.053374
      -0.085281
      -0.226586
      -0.108086
      -0.064840
      -0.102334
      0.110998

      2008-02-29
      -0.016566
      0.018876
      0.086126
      0.016783
      -0.009152
      0.022189
      -0.022665
      -0.158324
      -0.133497
      -0.142405
      -0.076389
      0.067084
      -0.032752
      -0.012179
      0.019668
      0.134751
      -0.048678
      -0.095090
      -0.064641
      -0.056774

      2008-03-31
      -0.124043
      0.082400
      -0.031780
      0.030678
      0.047921
      0.043540
      0.067366
      -0.096584
      -0.004447
      0.056581
      0.147816
      0.011242
      -0.060593
      0.046966
      -0.027928
      -0.142212
      -0.029103
      0.099627
      0.003579
      -0.112949

      2008-04-30
      0.444056
      0.050642
      0.033461
      0.068317
      -0.032857
      -0.050831
      0.100608
      0.179738
      0.022336
      0.118473
      0.212195
      0.048289
      -0.039178
      0.034222
      0.100378
      -0.000658
      0.047859
      0.021176
      0.072939
      -0.024062

      2008-05-31
      -0.176756
      0.009282
      0.036078
      -0.004364
      -0.027349
      -0.003356
      0.000062
      -0.122645
      -0.063997
      -0.097587
      0.085082
      0.076711
      -0.021968
      0.001795
      -0.042048
      0.132654
      0.006829
      0.030741
      -0.000260
      0.075096

      2008-06-30
      -0.292647
      -0.106728
      -0.071428
      -0.046089
      -0.086225
      -0.063022
      -0.026671
      -0.234354
      -0.138557
      -0.202093
      -0.112900
      -0.084215
      -0.097624
      -0.035961
      -0.007098
      0.009340
      -0.053511
      -0.155639
      -0.079802
      0.099752

      2008-07-31
      -0.002079
      -0.052608
      -0.027244
      0.063500
      -0.009234
      0.046705
      0.043060
      0.134455
      0.274526
      0.197465
      -0.050705
      0.079726
      0.068689
      0.064190
      -0.087371
      -0.046269
      0.015605
      -0.074153
      -0.026605
      -0.080521

      2008-08-31
      -0.070833
      0.017405
      0.065898
      0.043265
      0.011068
      0.028846
      0.011740
      0.016051
      0.010898
      -0.052670
      0.066561
      -0.045131
      0.040459
      0.035307
      -0.000141
      -0.084042
      0.044957
      0.038299
      0.031728
      -0.059633

      2008-09-30
      0.165919
      -0.157359
      -0.051314
      -0.004839
      0.022779
      0.047149
      0.013882
      0.080042
      0.239841
      0.213302
      -0.329558
      -0.039185
      -0.035060
      -0.016329
      -0.029371
      -0.115376
      0.050367
      -0.127227
      -0.086276
      -0.138570

```python
# Compute Geometric & Arithmetic Mean and sample variance
return_add1 = (asset_monthly_return + 1 )
Geo_mean = return_add1.prod(axis=0) ** (1/45) - 1
Ari_mean = asset_monthly_return.mean()

# Arithmetic Mean and sample variance
pd.DataFrame({'Arithmetic Mean' : Ari_mean,
              'Geometric Mean' : Geo_mean,
              'Sample variance' : asset_monthly_return.var()
               })
```

      Arithmetic Mean
      Geometric Mean
      Sample variance

      F
      -0.014276
      -0.021158
      0.014721

      CAT
      0.008088
      0.006003
      0.004283

      DIS
      0.004131
      0.003179
      0.001953

      MCD
      0.017955
      0.016680
      0.002626

      KO
      0.008147
      0.007545
      0.001234

      PEP
      0.009273
      0.008628
      0.001304

      WMT
      0.005057
      0.004109
      0.001960

      C
      -0.011948
      -0.015202
      0.006352

      WFC
      0.009518
      0.007098
      0.005304

      JPM
      0.009691
      0.006873
      0.005800

      AAPL
      0.037063
      0.028424
      0.016627

      IBM
      0.006492
      0.004888
      0.003253

      PFE
      -0.003225
      -0.004837
      0.003286

      JNJ
      0.004407
      0.003931
      0.000979

      XOM
      0.012579
      0.010835
      0.003717

      MRO
      0.023319
      0.018531
      0.009963

      ED
      0.004553
      0.003832
      0.001452

      T
      0.007281
      0.005584
      0.003385

      VZ
      0.000829
      -0.000519
      0.002751

      NEM
      0.001306
      -0.002289
      0.007417

```python
# use geometric mean as expected return
mu = Geo_mean

# Save file
# mu.to_csv('mu.csv')
```

```python
# Compute sample covariance
Q = asset_monthly_return.cov()
Q

# Save file
# Q.to_csv('Q.csv')
```

      F
      CAT
      DIS
      MCD
      KO
      PEP
      WMT
      C
      WFC
      JPM
      AAPL
      IBM
      PFE
      JNJ
      XOM
      MRO
      ED
      T
      VZ
      NEM

      F
      0.014721
      0.001548
      0.001057
      0.002166
      7.086874e-04
      -0.000391
      1.724197e-03
      0.004618
      0.001966
      0.003574
      0.001562
      0.002104
      0.001282
      0.000321
      0.002238
      0.000780
      0.001087
      0.001222
      0.001768
      -0.000203

      CAT
      0.001548
      0.004283
      0.000862
      0.001250
      4.969156e-04
      -0.000104
      -1.611960e-04
      0.000337
      -0.000827
      -0.000236
      0.003675
      0.000983
      0.001181
      -0.000261
      0.001743
      0.002795
      0.000097
      0.001910
      0.001639
      0.001442

      DIS
      0.001057
      0.000862
      0.001953
      0.000913
      5.041601e-04
      0.000338
      1.726771e-04
      0.000499
      -0.000515
      -0.000423
      0.001410
      0.000938
      0.000169
      0.000273
      0.000244
      0.000657
      0.000264
      0.000797
      0.000725
      -0.001018

      MCD
      0.002166
      0.001250
      0.000913
      0.002626
      1.013901e-03
      0.000560
      1.547019e-04
      0.001143
      0.000282
      0.000909
      0.004075
      0.001398
      0.000633
      0.000538
      0.001275
      0.001455
      0.000758
      0.001092
      0.001138
      0.000067

      KO
      0.000709
      0.000497
      0.000504
      0.001014
      1.234342e-03
      0.000708
      6.927632e-07
      0.000575
      0.000264
      0.000730
      0.001176
      0.000475
      0.000368
      0.000519
      0.000583
      0.000352
      0.000434
      0.000777
      0.000624
      -0.000654

      PEP
      -0.000391
      -0.000104
      0.000338
      0.000560
      7.084121e-04
      0.001304
      -1.565357e-04
      0.000575
      0.000483
      0.000724
      0.000849
      0.000035
      0.000283
      0.000677
      -0.000149
      -0.000256
      0.000508
      0.000373
      0.000397
      -0.001172

      WMT
      0.001724
      -0.000161
      0.000173
      0.000155
      6.927632e-07
      -0.000157
      1.959695e-03
      0.000282
      0.000560
      0.000986
      -0.000582
      0.000463
      -0.000710
      0.000232
      -0.000838
      -0.001979
      -0.000210
      0.000028
      0.000078
      -0.001251

      C
      0.004618
      0.000337
      0.000499
      0.001143
      5.748997e-04
      0.000575
      2.816591e-04
      0.006352
      0.003721
      0.004288
      0.000854
      0.000991
      0.001176
      0.000687
      0.000538
      -0.000697
      0.000907
      0.001081
      0.001191
      -0.001138

      WFC
      0.001966
      -0.000827
      -0.000515
      0.000282
      2.643581e-04
      0.000483
      5.599563e-04
      0.003721
      0.005304
      0.004728
      -0.002496
      0.000556
      0.001344
      0.000434
      -0.001066
      -0.002649
      0.000527
      -0.000344
      -0.000262
      -0.001545

      JPM
      0.003574
      -0.000236
      -0.000423
      0.000909
      7.300467e-04
      0.000724
      9.855057e-04
      0.004288
      0.004728
      0.005800
      -0.000598
      0.000676
      0.001252
      0.000640
      -0.000544
      -0.002404
      0.000610
      0.000524
      0.000478
      -0.001168

      AAPL
      0.001562
      0.003675
      0.001410
      0.004075
      1.176363e-03
      0.000849
      -5.819182e-04
      0.000854
      -0.002496
      -0.000598
      0.016627
      0.002268
      0.000831
      0.000626
      0.003989
      0.005465
      0.001147
      0.003454
      0.002867
      0.002568

      IBM
      0.002104
      0.000983
      0.000938
      0.001398
      4.750712e-04
      0.000035
      4.633211e-04
      0.000991
      0.000556
      0.000676
      0.002268
      0.003253
      -0.000132
      0.000353
      0.000659
      0.000863
      -0.000026
      0.000997
      0.000919
      0.000087

      PFE
      0.001282
      0.001181
      0.000169
      0.000633
      3.679896e-04
      0.000283
      -7.104404e-04
      0.001176
      0.001344
      0.001252
      0.000831
      -0.000132
      0.003286
      0.000234
      0.001042
      0.001469
      0.000661
      0.000852
      0.000886
      0.000911

      JNJ
      0.000321
      -0.000261
      0.000273
      0.000538
      5.186153e-04
      0.000677
      2.319626e-04
      0.000687
      0.000434
      0.000640
      0.000626
      0.000353
      0.000234
      0.000979
      0.000139
      -0.000403
      0.000322
      0.000302
      0.000330
      -0.000860

      XOM
      0.002238
      0.001743
      0.000244
      0.001275
      5.834055e-04
      -0.000149
      -8.377282e-04
      0.000538
      -0.001066
      -0.000544
      0.003989
      0.000659
      0.001042
      0.000139
      0.003717
      0.004230
      0.000540
      0.001075
      0.001062
      0.001898

      MRO
      0.000780
      0.002795
      0.000657
      0.001455
      3.519736e-04
      -0.000256
      -1.978513e-03
      -0.000697
      -0.002649
      -0.002404
      0.005465
      0.000863
      0.001469
      -0.000403
      0.004230
      0.009963
      0.000757
      0.001478
      0.001551
      0.003391

      ED
      0.001087
      0.000097
      0.000264
      0.000758
      4.336833e-04
      0.000508
      -2.099479e-04
      0.000907
      0.000527
      0.000610
      0.001147
      -0.000026
      0.000661
      0.000322
      0.000540
      0.000757
      0.001452
      0.000498
      0.000310
      -0.000200

      T
      0.001222
      0.001910
      0.000797
      0.001092
      7.768483e-04
      0.000373
      2.837188e-05
      0.001081
      -0.000344
      0.000524
      0.003454
      0.000997
      0.000852
      0.000302
      0.001075
      0.001478
      0.000498
      0.003385
      0.002197
      -0.000363

      VZ
      0.001768
      0.001639
      0.000725
      0.001138
      6.244782e-04
      0.000397
      7.827954e-05
      0.001191
      -0.000262
      0.000478
      0.002867
      0.000919
      0.000886
      0.000330
      0.001062
      0.001551
      0.000310
      0.002197
      0.002751
      -0.000063

      NEM
      -0.000203
      0.001442
      -0.001018
      0.000067
      -6.541535e-04
      -0.001172
      -1.250594e-03
      -0.001138
      -0.001545
      -0.001168
      0.002568
      0.000087
      0.000911
      -0.000860
      0.001898
      0.003391
      -0.000200
      -0.000363
      -0.000063
      0.007417

### Risk-Free Rate

```python
# Compute risk-free rate by using historical data of 13 Week Treasury Bill

IRX_daily_price = web.DataReader("^IRX", "yahoo", start, end)['Adj Close'][:-1]
IRX_monthly_price = IRX_daily_price.resample('M').ffill().mean()

rf = IRX_monthly_price / (12*100)
rf
```

    0.002953550731358321

### Risk Aversion
set to the quantity that Idzorek (see Black-Litterman paper and class slides) suggests (in this case the market (mkt) is the group of 20 stocks listed above)

```python
# Collect historical market capitalization of assets from yahoo finance (US site)
# 30-Sep-2008

asset_market_cap = np.array([12350000000.0, 35952736685.0, 55944801000.0, 68767419589.0, 122232012230.0,
                             111965170000.0, 235605845278.0, 111770063349.0, 124645335146.0, 174048427281.0,
                             100688632743.0, 157130846042.0, 124339728868.0, 193602657107.0, 395029171280.0,
                             28148220000.0, 11750739423.0, 164532560000.0, 95230608718.0, 17024528133.0])

mkt_portfolio = asset_market_cap / asset_market_cap.sum()
mkt_portfolio
```

    array([0.00527607, 0.01535943, 0.02390028, 0.02937825, 0.05221895,
           0.04783284, 0.10065359, 0.04774949, 0.05324995, 0.07435554,
           0.04301537, 0.06712815, 0.0531194 , 0.08270933, 0.16876111,
           0.01202525, 0.00502005, 0.07029025, 0.04068364, 0.00727308])

```python
# Estimate market return and variance

expected_mkt__return = mu @ mkt_portfolio
mkt_var = mkt_portfolio.T @ Q @ mkt_portfolio
```

```python
# Compute risk aversion
risk_aversion = (expected_mkt__return - rf) / mkt_var

# Results
pd.DataFrame({'Output' : [rf,expected_mkt__return,mkt_var,risk_aversion]
               },index=['monthly risk-free rate','Expected Market return',
                       'Market Variance','risk_aversion'])
```

      Output

      monthly risk-free rate
      0.002954

      Expected Market return
      0.005974

      Market Variance
      0.000841

      risk_aversion
      3.593889

## Portfolio Optimization (use Matlab)

#### (1) Mean-variance optimization (MVO)

\begin{equation}
\begin{array}{rl}
\displaystyle \min_{x} & \lambda x^TQx-\mu ^Tx \\
s.t. & 1^Tx = 1\\
\end{array}
\end{equation}

where $\mu \in R^n$is the vector of expected returns, $Q \in R^{n \times n}$ is the covariance matrix, and $e \in R^n$ is a vector of n ones. Short selling is allowed. $\lambda > 0$ is a
risk aversion parameter.

```python
MVO_with_short = np.array([-0.2009, -0.0264, 0.6748, 1.2014, -0.9811, 0.6750, 0.0692,
                  -1.4137, 1.5304, 0.5058, 0.0411, -0.7729, -1.2104, -0.0935,
                  0.9144, 0.3581, -0.5307, 0.6501, -0.3298, -0.0609])

MVO_without_short = np.array([0.0000, 0.0001, 0.0001, 0.3881, 0.0001, 0.1978, 0.0769,
                     0.0000, 0.1203, 0.0001, 0.0630, 0.0000, 0.0000, 0.0001,
                     0.0001, 0.1531, 0.0001, 0.0001, 0.0000, 0.0000])
```

#### (2) Robust mean-variance optimization （Ellipsoidal uncertainty sets）

\begin{equation}
\begin{array}{rl}
\displaystyle \min_{x} & \lambda x^TQx-\mu ^Tx + \epsilon_2 |\Theta^{1/2}x|_2\\
s.t. & 1^Tx = 1\\
& (x \geq 0)
\end{array}
\end{equation}

```python
# Confidence levels of 90%
RMVO_90_with_short = np.array([-0.0247, 0.0320, 0.0554, 0.1094, 0.1351, 0.1553, 0.0917,
                               -0.0377, 0.0416, 0.0295, 0.0230, 0.0366, -0.0089, 0.1291,
                               0.0604, 0.0350, 0.0843, 0.0342, 0.0057, 0.0129])

RMVO_90_without_short = np.array([0.0000, 0.0302, 0.0531, 0.0997, 0.1280, 0.1469, 0.0843,
                                  0.0000, 0.0356, 0.0243, 0.0213, 0.0341, 0.0002, 0.1238,
                                  0.0550, 0.0324, 0.0790, 0.0323, 0.0067, 0.0131])

# Confidence levels of 95%
RMVO_95_with_short = np.array([-0.0226, 0.0320, 0.0564, 0.1056, 0.1338, 0.1522, 0.0901,
                               -0.0340, 0.0402, 0.0288, 0.0221, 0.0370, -0.0057, 0.1301,
                               0.0588,  0.0336, 0.0850, 0.0345, 0.0085, 0.0134])

RMVO_95_without_short = np.array([0.0000, 0.0304, 0.0542, 0.0974, 0.1275, 0.1451, 0.0837,
                                  0.0000, 0.0351, 0.0243, 0.0207, 0.0347, 0.0003, 0.1252,
                                  0.0543, 0.0315, 0.0802, 0.0328, 0.0090, 0.0135])

```

#### (3) Risk Parity optimization with no short selling

\begin{equation}
\begin{array}{rl}
\displaystyle \min_{x,\theta} & \sum_{i=1}^n (x_i(Qx)_i -\theta)^2\\
s.t. & 1^Tx = 1\\
& (x \geq 0)
\end{array}
\end{equation}

```python
RP = np.array([0.0450, 0.0500, 0.0512, 0.0500, 0.0513, 0.0515, 0.0513, 0.0495, 0.0518,
               0.0509, 0.0425, 0.0507, 0.0510, 0.0514, 0.0499, 0.0491, 0.0513, 0.0503,
               0.0504, 0.0511])
```

#### (4) Market Portfolio (based on market capitalizations)

```python
# more detail is presented in the Parameter Estimation part
mkt_portfolio
```

    array([0.00527607, 0.01535943, 0.02390028, 0.02937825, 0.05221895,
           0.04783284, 0.10065359, 0.04774949, 0.05324995, 0.07435554,
           0.04301537, 0.06712815, 0.0531194 , 0.08270933, 0.16876111,
           0.01202525, 0.00502005, 0.07029025, 0.04068364, 0.00727308])

Combine all portfolios

```python
all_portfolios = pd.DataFrame({'MVO no short' : MVO_without_short,
                               'MVO short' : MVO_with_short,

                               'RMVO_90 no short' : RMVO_90_without_short,
                               'RMVO_95 no short' : RMVO_95_without_short,
                               'RMVO_90 short' : RMVO_90_with_short,
                               'RMVO_95 short' : RMVO_95_with_short,

                               'ERC' : RP,
                               'Market Portfolio':mkt_portfolio
                              },index=stocks)
all_portfolios
```

      MVO no short
      MVO short
      RMVO_90 no short
      RMVO_95 no short
      RMVO_90 short
      RMVO_95 short
      ERC
      Market Portfolio

      F
      0.0000
      -0.2009
      0.0000
      0.0000
      -0.0247
      -0.0226
      0.0450
      0.005276

      CAT
      0.0001
      -0.0264
      0.0302
      0.0304
      0.0320
      0.0320
      0.0500
      0.015359

      DIS
      0.0001
      0.6748
      0.0531
      0.0542
      0.0554
      0.0564
      0.0512
      0.023900

      MCD
      0.3881
      1.2014
      0.0997
      0.0974
      0.1094
      0.1056
      0.0500
      0.029378

      KO
      0.0001
      -0.9811
      0.1280
      0.1275
      0.1351
      0.1338
      0.0513
      0.052219

      PEP
      0.1978
      0.6750
      0.1469
      0.1451
      0.1553
      0.1522
      0.0515
      0.047833

      WMT
      0.0769
      0.0692
      0.0843
      0.0837
      0.0917
      0.0901
      0.0513
      0.100654

      C
      0.0000
      -1.4137
      0.0000
      0.0000
      -0.0377
      -0.0340
      0.0495
      0.047749

      WFC
      0.1203
      1.5304
      0.0356
      0.0351
      0.0416
      0.0402
      0.0518
      0.053250

      JPM
      0.0001
      0.5058
      0.0243
      0.0243
      0.0295
      0.0288
      0.0509
      0.074356

      AAPL
      0.0630
      0.0411
      0.0213
      0.0207
      0.0230
      0.0221
      0.0425
      0.043015

      IBM
      0.0000
      -0.7729
      0.0341
      0.0347
      0.0366
      0.0370
      0.0507
      0.067128

      PFE
      0.0000
      -1.2104
      0.0002
      0.0003
      -0.0089
      -0.0057
      0.0510
      0.053119

      JNJ
      0.0001
      -0.0935
      0.1238
      0.1252
      0.1291
      0.1301
      0.0514
      0.082709

      XOM
      0.0001
      0.9144
      0.0550
      0.0543
      0.0604
      0.0588
      0.0499
      0.168761

      MRO
      0.1531
      0.3581
      0.0324
      0.0315
      0.0350
      0.0336
      0.0491
      0.012025

      ED
      0.0001
      -0.5307
      0.0790
      0.0802
      0.0843
      0.0850
      0.0513
      0.005020

      T
      0.0001
      0.6501
      0.0323
      0.0328
      0.0342
      0.0345
      0.0503
      0.070290

      VZ
      0.0000
      -0.3298
      0.0067
      0.0090
      0.0057
      0.0085
      0.0504
      0.040684

      NEM
      0.0000
      -0.0609
      0.0131
      0.0135
      0.0129
      0.0134
      0.0511
      0.007273

##### Plots

```python
# Plot Portfolio Weights of MVO without short selling
all_portfolios.iloc[:,0:1].T.plot.bar(figsize=(10,6), stacked=True, title ='')
plt.title('Portfolio Weights')
plt.xticks(rotation=0.1)
plt.show()
```

```python
# Plot Portfolio Weights of robust MVO with and without short selling

all_portfolios.iloc[:,2:6].T.plot.bar(figsize=(10,6), stacked=True, title ='')
# plt.ylim([-10,10])
plt.title('Portfolio Weights')
plt.xticks(rotation=0.1)
plt.show()
```

```python
# Plot Portfolio Weights of ERC and market portfolio

all_portfolios.iloc[:,6:].T.plot.bar(figsize=(10,6), stacked=True, title ='')
plt.title('Portfolio Weights')
plt.xticks(rotation=0.1)
plt.show()
```

## PART A

```python
end2 = datetime.datetime(2008,11,5)

# pandas_datareader offers remote data access
daily_price2 = {}

for i in stocks:
    info = web.DataReader(i, "yahoo", start, end2)
    # extract daily adjusted closing prices
    info_price = {i : info['Adj Close']}
    daily_price2.update(info_price)

daily_price2 = pd.DataFrame(daily_price2)
```

```python
# Compute asset monthly return
asset_monthly_return2 = daily_price2.resample('M').ffill().pct_change()
asset_monthly_return2.tail()
```

      F
      CAT
      DIS
      MCD
      KO
      PEP
      WMT
      C
      WFC
      JPM
      AAPL
      IBM
      PFE
      JNJ
      XOM
      MRO
      ED
      T
      VZ
      NEM

      Date

      2008-07-31
      -0.002079
      -0.052608
      -0.027244
      0.063500
      -0.009234
      0.046705
      0.043060
      0.134455
      0.274526
      0.197465
      -0.050705
      0.079726
      0.068689
      0.064190
      -0.087371
      -0.046269
      0.015605
      -0.074153
      -0.025812
      -0.080521

      2008-08-31
      -0.070833
      0.017405
      0.065898
      0.043265
      0.011068
      0.028846
      0.011740
      0.016051
      0.010898
      -0.052670
      0.066561
      -0.045131
      0.040459
      0.035307
      -0.000141
      -0.084042
      0.044957
      0.038299
      0.031727
      -0.059633

      2008-09-30
      0.165919
      -0.157359
      -0.051314
      -0.004839
      0.022779
      0.047149
      0.013882
      0.080042
      0.239841
      0.213302
      -0.329558
      -0.039185
      -0.035060
      -0.016329
      -0.029371
      -0.115376
      0.050367
      -0.127227
      -0.086275
      -0.138570

      2008-10-31
      -0.578846
      -0.352596
      -0.155751
      -0.061102
      -0.166793
      -0.200085
      -0.068125
      -0.326119
      -0.092726
      -0.109887
      -0.053405
      -0.205113
      -0.039588
      -0.114607
      -0.045583
      -0.270128
      0.008380
      -0.026054
      -0.059445
      -0.320433

      2008-11-30
      -0.045662
      0.057592
      -0.064840
      -0.033316
      0.015206
      0.000000
      -0.030102
      -0.074725
      -0.060505
      -0.049212
      -0.039874
      -0.032591
      -0.023110
      -0.024291
      -0.005802
      0.029553
      0.005309
      0.014195
      0.039434
      0.055809

##### Compute the major portfolio quantities for each portfolio for the month of Oct. 2008
1. portfolio return
2. portfolio variance and standard deviation
3. Sharpe ratio.

Discuss the results i.e. explain why you think a portfolio did better than another on these portfolio dimensions e.g. why was the Sharpe ratio of a portfolio from a particular strategy better than a portfolio generated by a di¤erent strategy? and which portfolio did best
(worst) on return and why?

```python
def compute_quantity (asset_return):
    # 1. portfolio return
    port_return = all_portfolios.T @ asset_return
    port_return = np.array(port_return)

    # 2. portfolio variance and standard deviation
    port_var = np.diag(all_portfolios.T @ Q @ all_portfolios)
    port_sd = np.sqrt(port_var)

    # 3. Sharpe ratio
    sharp_ratio = (port_return - rf) / port_sd

    results = pd.DataFrame({'portfolio return' : port_return,
                               'portfolio var' : port_var,
                               'portfolio sd' : port_sd,
                               'Sharpe ratio' : sharp_ratio},
                              index = portfolios)
    return results
```

```python
portfolios =  ["MVO_without_short", "MVO_with_short", "RMVO_90_without_short",
               "RMVO_95_without_short", "RMVO_90_with_short", "RMVO_95_with_short",
               "RP", "mkt_portfolio" ]
```

```python
# Realized return for each stock for the month of Oct of 2008
oct_asset_return = asset_monthly_return2.iloc[-2,:]

# Save file
# oct_asset_return.to_csv('oct_asset_return.csv')

oct_result = compute_quantity (oct_asset_return)
oct_result
```

      portfolio return
      portfolio var
      portfolio sd
      Sharpe ratio

      MVO_without_short
      -0.124502
      0.001281
      0.035796
      -3.560637

      MVO_with_short
      0.328795
      0.010965
      0.104712
      3.111786

      RMVO_90_without_short
      -0.125830
      0.000586
      0.024207
      -5.320151

      RMVO_95_without_short
      -0.125649
      0.000582
      0.024131
      -5.329426

      RMVO_90_with_short
      -0.107001
      0.000570
      0.023870
      -4.606427

      RMVO_95_with_short
      -0.108346
      0.000564
      0.023755
      -4.685286

      RP
      -0.159970
      0.000894
      0.029898
      -5.449223

      mkt_portfolio
      -0.112932
      0.000841
      0.028992
      -3.997221

## PART B

```python
# Realized return for each stock for the month of Nov of 2008
nov_asset_return = asset_monthly_return2.iloc[-1,:]
nov_result = compute_quantity (nov_asset_return)
nov_result
```

      portfolio return
      portfolio var
      portfolio sd
      Sharpe ratio

      MVO_without_short
      -0.020516
      0.001281
      0.035796
      -0.655660

      MVO_with_short
      -0.055900
      0.010965
      0.104712
      -0.562048

      RMVO_90_without_short
      -0.011427
      0.000586
      0.024207
      -0.594079

      RMVO_95_without_short
      -0.011298
      0.000582
      0.024131
      -0.590585

      RMVO_90_with_short
      -0.008602
      0.000570
      0.023870
      -0.484116

      RMVO_95_with_short
      -0.008726
      0.000564
      0.023755
      -0.491681

      RP
      -0.013050
      0.000894
      0.029898
      -0.535257

      mkt_portfolio
      -0.019298
      0.000841
      0.028992
      -0.767534

```python
# Realized return for each stock for selected date before crisis
# asset_monthly_return2.iloc[11,:]
select_result = compute_quantity (asset_monthly_return2.iloc[11,:])
select_result
```

      portfolio return
      portfolio var
      portfolio sd
      Sharpe ratio

      MVO_without_short
      0.054869
      0.001281
      0.035796
      1.450327

      MVO_with_short
      0.135842
      0.010965
      0.104712
      1.269081

      RMVO_90_without_short
      0.030844
      0.000586
      0.024207
      1.152194

      RMVO_95_without_short
      0.030647
      0.000582
      0.024131
      1.147632

      RMVO_90_with_short
      0.031604
      0.000570
      0.023870
      1.200286

      RMVO_95_with_short
      0.031198
      0.000564
      0.023755
      1.188988

      RP
      0.040229
      0.000894
      0.029898
      1.246743

      mkt_portfolio
      0.040054
      0.000841
      0.028992
      1.279683

```python
# Compare Sharpe ratio of different periods

pd.DataFrame({'Oct 2008' : oct_result['Sharpe ratio'],
              'Nov 2008' : nov_result['Sharpe ratio'],
              'Before Crisis' : select_result['Sharpe ratio']
             } )
```

      Oct 2008
      Nov 2008
      Before Crisis

      MVO_without_short
      -3.560637
      -0.655660
      1.450327

      MVO_with_short
      3.111786
      -0.562048
      1.269081

      RMVO_90_without_short
      -5.320151
      -0.594079
      1.152194

      RMVO_95_without_short
      -5.329426
      -0.590585
      1.147632

      RMVO_90_with_short
      -4.606427
      -0.484116
      1.200286

      RMVO_95_with_short
      -4.685286
      -0.491681
      1.188988

      RP
      -5.449223
      -0.535257
      1.246743

      mkt_portfolio
      -3.997221
      -0.767534
      1.279683

## Discussion

##### Daily asset price

```python
end3 = datetime.datetime(2012,11,5)

# pandas_datareader offers remote data access
daily_price3 = {}

for i in stocks:
    info = web.DataReader(i, "yahoo", start, end3)
    # extract daily adjusted closing prices
    info_price = {i : info['Adj Close']}
    daily_price3.update(info_price)

daily_price3 = pd.DataFrame(daily_price3)
```

```python
# plot daily adjusted closing price of assets
daily_price3.plot(figsize=(12,7),title='Daily Adjusted Closing Price')
# plt.ylim([0,200])
plt.show()
```

```python
# zoomed plot
daily_price3.plot(figsize=(12,7),title='Daily Adjusted Closing Price')
plt.ylim([0,175])
plt.show()
```

```python

```

```python

```

```python

```

```python

```