_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 Stochastic Integration

Last edited: August 8, 2025

The code created for this problem can be found here.

Problem 1

Let’s begin with a normal function:

\begin{equation} f(x) = (\sqrt{x}-1)^{2} \end{equation}

Taking just a normal Riemann sum, we see that, as expected, it converges to about \(0.167\) by the following values between bounds \([0,1]\) at different \(N\):

NValue
100.23
1000.172
10000.167
100000.167
1000000.167

Problem 2

First, as we are implementing a discrete random walk, here’s a fun example; \(p=0.51\), \(\epsilon=0.001\).