Want to improve this question? As written, this question is lacking some of the information it needs to be answered. If the author adds details in comments, consider editing them into the question. Once there's sufficient detail to answer, vote to reopen the question.
Closed 8 days ago.
This post was edited and submitted for review 8 days ago.
I am using python to work on a project related to Leontief inversion and so on
So tl;dr the I am using linalg.inv to get the inversion of the matrix, and the calculations works near perfectly for a small set
but when I do the same on a larger set the calculations are off
I am suspecting that the issue comes from the mathematical algorithm that inverts matrices
But I don't have much experience in that field can someone give their thoughts about this.
These are the functions that I am using (Working on getting a sample input files link online)
import pandas as pd
import numpy as np
PATH = r""
# function to calcuate the inversion of the matrix
def create_inversion_matrix(matrix_df: pd.DataFrame, year:int, save_excel: bool = False) -> pd.DataFrame: # noqa
try:
inverse_values = np.linalg.inv(matrix_df.values)
except np.linalg.LinAlgError:
print("Matrix cannot be inverted")
return None
leontief_inverted_matrix_df = pd.DataFrame(
inverse_values,
columns=matrix_df.columns
)
if (save_excel):
leontief_inverted_matrix_df.to_excel(
f"{PATH}/Intermediate-Results/{year}/Inverted-Matrix-{year}.xlsx",
index=False
) # noqa
return leontief_inverted_matrix_df
# this function gets the inverted matrix
# and then does a matrix multiplication
# with vector base to get the intended values
def create_x_matrix(
leontief_inverted_matrix: pd.DataFrame,
Vector_base_matrix: pd.DataFrame,
year: int,
save_excel: bool = False
):
# these label columns/df are just for the result
# to be labeld should not affect calculations
label_columns = Vector_base_matrix.columns[:5]
labels_df = Vector_base_matrix[label_columns].reset_index(drop=True)
vectors_base_matrix_numerical = Vector_base_matrix.iloc[:, 5:]
old_vector_base_columns = [col for col in vectors_base_matrix_numerical.columns if 'OLD' in col]
old_vector_base = vectors_base_matrix_numerical.loc[:, old_vector_base_columns]
x_matrix_old = leontief_inverted_matrix.to_numpy() @ old_vector_base.to_numpy()
x_matrix_old_df = pd.DataFrame(
x_matrix_old,
index=leontief_inverted_matrix.index,
columns=old_vector_base_columns
)
x_matrix_df = pd.concat(
[labels_df, x_matrix_old_df.reset_index(drop=True)],
axis=1
)
if save_excel:
x_matrix_df.to_excel(f"{PATH}/Intermediate-Results/{year}/Matrix_X_Demand-{year}.xlsx", index=False)
return x_matrix_df
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4