_index.org

Numerical Stability

Last edited: August 8, 2025

“being careful”

numerically stable vector norms

consider:

\begin{equation} \Vert x \Vert_{2} = \sqrt{x_1^{2} + \dots + x_{m}^{2}} \end{equation}

squaring it could easily go over MAX-float for large \(m\). Instead, consider:

\begin{equation} \Vert x \Vert_{2} = z \sqrt{\qty(\frac{x_1}{z})^{2} + \dots \qty(\frac{x_{m}}{z})^{2}} \end{equation}

where \(z = \max_{i} |x_{i}|\). Notice how now we are squaring numbers which are less than one, meaning this will never blow up. Yet, the results will be the same. This is bounded, therefore, by \(m\).

NUS-ECON320 CAPM Problem Set

Last edited: August 8, 2025

NUS-ECON320 Currency Arbitrage

Last edited: August 8, 2025

Let’s import some tools.

import pandas as pd
from scipy.optimize import minimize
import numpy as np
from datetime import datetime
from tqdm import tqdm
import torch
tqdm.pandas()

And load our data:

df = pd.read_csv("./currency_signal.csv", index_col=0, header=None, parse_dates=[0])
df

Let’s rename the headers

df.index.rename("date", True)
df.columns = ["value"]

Awesome. For the rest of the calculations, we will hide the 2020 data from the model:

data = df[df.index < datetime(2020, 1,1)]
data
               value
date
2006-03-01  0.000050
2006-03-02  0.001778
2006-03-03  0.000116
2006-03-06 -0.001038
2006-03-07 -0.001197
...              ...
2019-12-25 -0.010659
2019-12-26 -0.000869
2019-12-27  0.000075
2019-12-30  0.000033
2019-12-31  0.000944

[3610 rows x 1 columns]

we will add a column of randomness to this, to serve as the seed of our epsilon:

NUS-ECON320 Inter-Temporal Choice

Last edited: August 8, 2025

We want to construct a combined agent

\begin{equation} (k_1+k_2)x^{*}(k_1+k_2, \gamma^{*}) = x^{*}(k_1,\gamma_{1})k_1+x^{*}(k_2, \gamma_{2})k_2 \end{equation}

which combines the relative risk of \(\gamma_{1}, \gamma_{2}\) into some new \(\gamma^{*}\), which produces the same combined consumption of both agents \(k_1+k_2\).

Let us create some CAS tools to solve the inter-temporal choice problem generically for 10 steps in the past.

We do this by solving backwards. We will create a variable \(k\) to measure asset, and \(k_{t}\) the remaining asset at time \(t\).

NUS-ECON320 Linearity Tests

Last edited: August 8, 2025

Let’s begin. We want to create test for the linearity of a few assets, for whether or not they follow the CAPM.

Note that we will be using the Sharpe-Linter version of CAPM:

\begin{equation} E[R_{i}-R_{f}] = \beta_{im} E[(R_{m}-R_{f})] \end{equation}

\begin{equation} \beta_{im} := \frac{Cov[(R_{i}-R_{f}),(R_{m}-R_{f})]}{Var[R_{m}-R_{f}]} \end{equation}

Recall that we declare \(R_{f}\) (the risk-free rate) to be non-stochastic.

Let us begin. We will create a generic function to analyze some given stock.

We will first import our utilities