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 B 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 ℝn can be expressed as a linear combination of these basis vectors.
Any basis for ℝn must have exactly n elements.
The standard basis in ℝ3 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
ℝ
3
import numpy as np w = np.array([3, 4, 5])
w_linear_combination = 3 * e1 + 4 * e2 + 5 * e3
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 B = np.column_stack((e1, e2, e3))
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
b1 = np.array([1, 1, 0]) b2 = np.array([0, 1, 1]) b3 = np.array([1, 0, 1])
B_new = np.column_stack((b1, b2, b3))
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
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]) w1, w2, w3 = 0.5, 0.3, 0.2
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 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] ])
pca = PCA(n_components=3)
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_price = 100.0 futures_price = 98.0 basis = spot_price - futures_price spot_prices = np.array([100, 102, 105, 103, 104]) futures_prices = np.array([98, 99, 100, 101, 102]) 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.
Коментарі
Дописати коментар