Basis



In maths, a set of vectors in a vector space is called a basis if every element can be written as a linear combination of the basis vectors. The coefficients are the components or coordinates of the vector.

A basis  for ℝn is defined as a set of linearly independent vectors that spans the entire space.

The vectors in the basis are frequently the standard unit vectors, represented as e1, e2, …, en, where each vector has a single component equal to 1, with all other components set to zero.

Any vector w in  can be expressed as a linear combination of these basis vectors.

Any basis for ℝn  must have exactly n elements.

The standard basis in ℝconsists of three vectors:


In Pyton we can represent these using NumPy array:

import numpy as np


# Standard basis vectors in R^3

e1 = np.array([1, 0, 0])

e2 = np.array([0, 1, 0])

e3 = np.array([0, 0, 1])


# Display the basis vectors

print("Standard Basis Vectors in R^3:")

print("e1 =", e1)

print("e2 =", e2)

print("e3 =", e3)

Standard Basis Vectors in R^3: e1 = [1 0 0] e2 = [0 1 0] e3 = [0 0 1]

Given a vector w  in  ℝ

import numpy as np


# Vector w 

w = np.array([3, 4, 5]) 


# Expressing w as a linear combination of the basis vectors 

w_linear_combination = 3 * e1 + 4 * e2 + 5 * e3 


# Check if w_linear_combination equals w 

print("w =", w) 

print("w (as linear combination) =", w_linear_combination)

w = [3 4 5] w (as linear combination) = [3 4 5]

To check if a set of vectors is a basis, use numpy.linalg.matrix_rank. If the matrix formed by the vectors has the same rank as the number of vectors, they are linearly independent.

import numpy as np


# Forming a matrix with the basis vectors as columns 

B = np.column_stack((e1, e2, e3)) 


# Check the rank of the matrix 

rank = np.linalg.matrix_rank(B) 

print("Matrix B:\n", B) 

print("Rank of B:", rank) 


if rank == 3

    print("The vectors are linearly independent and form a basis."

else

    print("The vectors are not linearly independent.")

Matrix B: [[1 0 0] [0 1 0] [0 0 1]] Rank of B: 3 The vectors are linearly independent and form a basis.

If you use a different basis for R3, you can express the same vector in terms of this new basis.

Let's consider a different basis:


import numpy as np


# New basis vectors 

b1 = np.array([1, 1, 0]) 

b2 = np.array([0, 1, 1]) 

b3 = np.array([1, 0, 1]) 


# Form the matrix B with new basis vectors as columns 

B_new = np.column_stack((b1, b2, b3)) 


# Solve the system B_new * x = w for x 

x = np.linalg.solve(B_new, w) 

print("Coordinates of w in the new basis:", x)

Coordinates of w in the new basis: [1. 3. 2.]

In finance, basis is used in many ways, such as valuing portfolios, building efficient portfolios and understanding risk factors. Here are a few Python examples of how basis is used in finance.

Imagine a portfolio with three assets. We can represent the portfolio's returns as a linear combination of these assets.


And a portfolio with weights w1 = 0.5, w2 = 0.3, and w3 = 0.2

import numpy as np


# Asset returns 

r1 = np.array([0.01, 0.02, 0.015]) 

r2 = np.array([0.02, 0.01, 0.025]) 

r3 = np.array([0.015, 0.02, 0.02]) 


# Portfolio weights 

w1, w2, w3 = 0.5, 0.3, 0.2 


# Portfolio return as a linear combination of asset returns 

portfolio_return = w1 * r1 + w2 * r2 + w3 * r3 

print("Portfolio Returns:", portfolio_return)

Portfolio Returns: [0.014 0.017 0.019]

PCA reduces the number of variables in a dataset while keeping the most useful information. In finance, it can be used to identify the main factors that affect asset returns.
Assume we have a dataset of daily returns for five assets. We'll use PCA to find the principal components.

from sklearn.decomposition import PCA 

# Sample daily returns data for five assets 

returns = np.array([ 

     [0.01, 0.02, 0.015, 0.03, 0.025], 

     [0.02, 0.01, 0.025, 0.02, 0.03], 

     [0.015, 0.02, 0.02, 0.025, 0.015], 

     [0.03, 0.025, 0.02, 0.01, 0.02], 

     [0.025, 0.03, 0.015, 0.02, 0.01

])


# Perform PCA 

pca = PCA(n_components=3


# Reduce to 3 principal components 

principal_components = pca.fit_transform(returns) 

print("Principal Components (basis vectors):\n", pca.components_) 

print("Explained Variance Ratio:", pca.explained_variance_ratio_)

Principal Components (basis vectors): [[ 0.54999012 0.5102341 -0.09997003 -0.43838757 -0.48476217] [ 0.43676428 -0.39233627 0.36152592 -0.533196 0.4902145 ] [-0.02997897 -0.46120659 0.55634353 0.05949734 -0.68799106]] Explained Variance Ratio: [0.57789707 0.3676873 0.04935424]

Each row shows how much of each original feature contributes to that principal component.  The first principal component is a linear combination of the original features with coefficients: 0.54999012, 0.5102341, −0.09997003, −0.43838757, and −0.48476217.
The higher the explained variance, the more important the principal component is. In this case, the first two components together explain over 94% of the variance, meaning the data can be represented in just two dimensions.

In futures markets, basis risk is when the hedge instrument (like a futures contract) isn't perfectly aligned with the underlying asset. The basis is basically the difference between the spot price and the futures price.

# Spot and futures prices 

spot_price = 100.0 

futures_price = 98.0 


# Calculate basis 

basis = spot_price - futures_price 


# Simulating spot and futures prices over time 

spot_prices = np.array([100, 102, 105, 103, 104]) 

futures_prices = np.array([98, 99, 100, 101, 102]) 


# Calculate basis over time 

basis_over_time = spot_prices - futures_prices 

print("Initial Basis:", basis) 

print("Basis Over Time:", basis_over_time)

Initial Basis: 2.0 Basis Over Time: [2 3 5 2 2]

This example shows how the basis changes over time, which is important for understanding and managing basis risk in hedging strategies. It shows how the concept of basis can be used effectively in financial analysis and modelling.

References:

  1. "Financial Engineering and Risk Management". Coursera, www.coursera.org/specializations/financialengineering. Last accessed 24 Aug. 2024.

  2. "NumPy documentation — NumPy v2.1 Manual". NumPy -, numpy.org/doc/stable. Last accessed 24 Aug 2024.

  3. "WQU | Master of Science in Financial Engineering · WQU". WQU | A Leader in Global Education · WQU, www.wqu.edu/mscfe?utm_term=worldquant%20university&utm_campaign=GA_MScFE_SEA_GKW+BKW_WW&utm_source=adwords&utm_medium=ppc&hsa_acc=1450481729&hsa_cam=18879661171&hsa_grp=142652472959&hsa_ad=634265330876&hsa_src=g&hsa_tgt=kwd-365833978550&hsa_kw=worldquant%20university&hsa_mt=b&hsa_net=adwords&hsa_ver=3&gad_source=1&gclid=CjwKCAjwiaa2BhAiEiwAQBgyHk5oR6aQvbAQDkK_znzHUT5523KyleBQZJW2wiqJB_x6reoBOECdGRoCPIsQAvD_BwE. Last accessed 24 Aug 2024.

Коментарі

Популярні дописи з цього блогу

Learn how to build games with HTML

Four Stances of Zhan Zhuang