This is the note that I took while learning the Fundamentals of Quantitative Modeling Coursera course.


Week 1 Introduction

Purpose of the course

What is a Model

Examples of Models

Mathmatical Functions

How Models are Used

Benefits of Modelling

Key Steps in Modelling

┌──────────────────┐                      ┌────────────────────┐                                                   
│                  │                      │ ┌───────────────┐  │                                                   
│ ┌──────────────┐ │                      │ │  Sensitivity  │  │                                                   
│ │Define inputs │ │     ┌──────────┐     │ │   Analysis    │  │     ┌──────────────┐                              
│ │  & Outputs   │ │     │          │     │ └───────────────┘  │     │              │              ┌──────────────┐
│ └──────────────┘ │     │formulate │     │                    │     │   Fit for    │              │  Implement   │
│                  │────▶│  model   │────▶│ ┌───────────────┐  │────▶│   purpose?   │────[ Yes ] ─▶│    Model     │
│ ┌──────────────┐ │     │          │     │ │   Validate    │  │     │              │              └──────────────┘
│ │ Define scope │ │     └──────────┘     │ │     model     │  │     └──────────────┘                              
│ └──────────────┘ │                      │ │   forecasts   │  │             │                                     
│                  │                      │ └───────────────┘  │             │                                     
└──────────────────┘                      └────────────────────┘             │                                     
          ▲                                                                  │                                     
          │                                                                  │                                     
          └───────────────────────────────────[ No ]─────────────────────────┘                                     

A Vocabulary for Modeling

Week 2 Linear Models

Introduction

Constant Proportionate Growth

┌────────┬────────┬────────┬────────┬────────┐
│  TIME  │    0   │    1   │    2   │    3   │
├────────┼────────┼────────┼────────┼────────┤
│ AMOUNT │   P0   │  P0 Θ  │ P0 Θ^2 │ P0 Θ^3 │
└────────┴────────┴────────┴────────┴────────┘
\[S_t = P_0 \frac{1 - \theta^{t+1}}{1 - \theta}\]

Present and Future Value

If the prevailingiling interest rate (现行利率) is 4%, which options is better?

This question is equal to

The equation is,

\[P_t = P_0 \theta^t\]

since $P_t = 1000 \times 1.04^{10} = 1480.2443$, we should take 1500.

Continuous Compounding

The compounding period approachs 0, the process is continuous.

If a principle amount $P_0$ is continuously compounded at a nominal annual interest rate of $R \%$, then at year $t$, we have

\[P_t = P_0 \exp(r t)\]

where $r = R / 100$.

The exponential function also describes the beginning of an epidemic.

Optimisation

Consider the demand model which gives the relationship between the quantity $Q$ and the price $P$.

\[Q = 60000 P^{-2.5}\]

If the price of producing one unit of product is constant ($c = 2$), how do we maximise the profit?

\[\begin{aligned} \mathrm{Profit} &= \mathrm{Revenue} - \mathrm{Cost} \\ &= P \times Q - c\times Q \\ &= Q(P - c) \\ &= 60,000 \times P^{-2.5}(P - c) \\ &= 60,000 \times (P^{-1.5} - c \times P^{-2.5}) \end{aligned}\]

Taking the derivatives.

\[\begin{aligned} \frac{d\ \mathrm{Profit}}{dP} &= 60,000 \times (-1.5 \times P^{-2.5} + 2.5c \times P^{-3.5}) \rightarrow 0 \\ &\rightarrow 2.5c \times P^{-1} = 1.5 \\ &\rightarrow P = 2.5 c / 1.5 \\ & \rightarrow P_\mathrm{opt}\approx 3.333 \;(c = 2) \end{aligned}\]

generally, if the relationship follows

\[Q = a P^\beta; \mathrm{Cost} = P - c\]

then the best price is

\[P_\mathrm{opt} = \frac{c b}{1 + b}\]

Week 3 Probabilistic Models

Introduction

Regression Models

Probability Trees

Monte-Carlo Simulation

The method is used to model complicated examples. For instance, if the value of $b$ for the optimisation problem in Week 2 is a random variable following uniform distribution. We can calculate the distirubiton of the optimum price.

Example:

import numpy as np

c = 2
b = np.random.uniform(-2, -3, 100000)
popt = c * b / (1 + b)
mean, std = popt.mean(), popt.std()
print(f"The opt price is {mean:.4f}, with std of {std:.4f}")

The opt price is 3.3869, with std of 0.2797.

Markov Models

The Markov model is a dynamic model for discrete state transition. The model is expressed as probability transition matrix.

For instance, a person may change its state between employed, unemployed, and looking for job. These states were illustrated in the following graph.

                                          
                               ┌──[ 0.2 ]──┐                  
                               │           │                  
                               ▼           │                  
                     ┌──────────────────┐  │                  
                     │                  │  │                  
             ┌──────▶│    Unemployed    │──┴─────┐            
             │       │       (1)        │        │            
             │       └──────────────────┘        │            
          [ 0.2 ]                             [ 0.8 ]         
             │                                   │            
             │                                   │            
             │                                   ▼            
   ┌──────────────────┐                ┌──────────────────┐   
   │                  │                │     Looking      │   
┌─▶│     Employed     │◀────[ 0.5 ]────│     for Job      │◀─┐
│  │        (3)       │                │       (2)        │  │
│  └──────────────────┘                └──────────────────┘  │
│            │                                   │           │
│            │                                   │           │
└──[ 0.8 ]───┘                                   └──[ 0.5 ]──┘

The corresponding transition matrix is \(\left( \begin{matrix} 0.2 & 0.8 & 0.0 \\ 0.0 & 0.5 & 0.5 \\ 0.2 & 0.0 & 0.8 \end{matrix} \right)\) Markov chain model is characterised by the lack of memory, meaning the history of the chain will not affect the probability for the next state. This is the assumption of Markov chains.

Example

import numpy as np


def change_state(state, rand_num):
    new_state = None
    if state == 1:
        if rand_num < 0.2: new_state = 1
        else: new_state = 2
    elif state == 2:
        if rand_num < 0.5: new_state = 2
        else: new_state = 3
    elif state == 3:
        if rand_num < 0.8: new_state = 3
        else: new_state = 1
    return new_state


def simulate(state_init, n_sample):
    rand_nums = np.random.random(n_sample)
    result = np.zeros(n_sample)
    state = state_init
    for i in range(n_sample):
        state = change_state(state, rand_nums[i])
        result[i] = state
    return result

if __name__ == "__main__":
    n_sample = 5000000
    state_init = 1
    result = simulate(state_init, n_sample).astype(int)
    
    print("The stationary probabilities are (Simulation)")
    for i in range(3):
        print(f"P({i+1})={np.sum(result == i+1) / len(result):.4f}", end='; ')
        
    # calculating from transition matrix
    P = np.array((
        (0.2, 0.8, 0.0),  # pij ---> state i to j
        (0.0, 0.5, 0.5),
        (0.2, 0.0, 0.8),
    )).T  # pij ---> state j to i
    s_init = np.array((1.0, 0.0, 0.0))
    s = s_init.copy()
    for _ in range(1000):
        s = P @ s
    print("\n\nStationary Probabilities are (MCMC):")
    for i in range(3):
        print(f"P({i+1})={s[i]:.4f}", end='; ')
    
    # calculating from the eigen vector
    eigvals, eigvecs = np.linalg.eig(P)
    v_eq = np.abs(eigvecs.T[np.argmin(np.abs(eigvals - 1.0))])
    v_eq = v_eq / v_eq.sum()  # probability sum --> 1
    print(
        "\n\nStationary Probabilities are (Analytical):"
    )
    for i in range(3):
        print(f"P({i+1})={v_eq[i]:.4f}", end='; ')

Result:

The stationary probabilities are (Simulation)
P(1)=0.1515; P(2)=0.2428; P(3)=0.6057; 

Stationary Probabilities are (MCMC):
P(1)=0.1515; P(2)=0.2424; P(3)=0.6061; 

Stationary Probabilities are (Analytical):
P(1)=0.1515; P(2)=0.2424; P(3)=0.6061;

Common Probability Distributions

The emprical rule

For normal distribution, the probability in these following ranges are

Example: if the daily return of Apple’s stock follows normal distribution with $\mu = 0.13\%, \sigma = 2.34\%$, what is the probability that tomorrow Apple’s stock price increase by more than 2.47%?

  1. calculate Z score: $Z = (2.47 - 0.13) / 2.34 = 1$
  2. since the increase is 1 $\sigma$ on the right hand side of the distribution, its probability is $100\% - 50\% - 68\%/2 = 16\%$

Week 4 Regression Model

Regression Models

Multiple Regression

Logistic Regression

\[\mathbb{E}(Y=1\vert X=x) = \frac{\exp(x\beta)}{1 + \exp(x\beta)}\]