New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Minor suggestions in docstr #53
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
329fe2c
[Docstr_AddingInfo] More info calculation functions docstr in tools/i…
Didou09 108687f
[Docstr_AddingInfo] removed code changes
Didou09 e0e2bb5
[Docstr_AddingInfo] removed code changes 2
Didou09 0e6e9d7
[Docstr_AddingInfo] Corrected mistake in calc_x() docstr
Didou09 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
def calc_x(Z, Y): | ||
"""Calculate the industry output x from the Z and Y matrix | ||
|
||
industry output x = flows(Z) + final demand(Y) | ||
|
||
Parameters | ||
---------- | ||
Z : pandas.DataFrame or numpy.array | ||
|
@@ -33,7 +35,9 @@ def calc_x(Z, Y): | |
The type is determined by the type of Z. If DataFrame index as Z | ||
|
||
""" | ||
x = np.reshape(np.sum(np.hstack((Z, Y)), 1), (-1, 1)) | ||
|
||
# [:, None] syntax more compact and explicit? | ||
x = np.sum(np.hstack((Z, Y)), 1)[:, None] | ||
if type(Z) is pd.DataFrame: | ||
x = pd.DataFrame(x, index=Z.index, columns=["indout"]) | ||
if type(x) is pd.Series: | ||
|
@@ -46,6 +50,10 @@ def calc_x(Z, Y): | |
def calc_x_from_L(L, y): | ||
"""Calculate the industry output x from L and a y vector | ||
|
||
x = Ly | ||
|
||
The industry output x is computed from a demand vector y | ||
|
||
Parameters | ||
---------- | ||
L : pandas.DataFrame or numpy.array | ||
|
@@ -71,6 +79,11 @@ def calc_x_from_L(L, y): | |
def calc_Z(A, x): | ||
"""calculate the Z matrix (flows) from A and x | ||
|
||
A = Z / x[None, :] => Z = A * x[None, :] | ||
|
||
By definition, the coefficient matrix A is basically the normalized flows | ||
So Z is just derived from A by un-normalizing using the industrial output x | ||
|
||
Parameters | ||
---------- | ||
A : pandas.DataFrame or numpy.array | ||
|
@@ -88,19 +101,27 @@ def calc_Z(A, x): | |
""" | ||
if (type(x) is pd.DataFrame) or (type(x) is pd.Series): | ||
x = x.values | ||
x = x.reshape((1, -1)) # use numpy broadcasting - much faster | ||
# Syntax x[None, :] is faster than x.reshape((1, -1)) | ||
# (checked using timeit) | ||
# it is also somewhat more explicit | ||
# And using directly in the final line avoids storing an intermediate value | ||
# | ||
# x = x.reshape((1, -1)) # use numpy broadcasting - much faster | ||
# (but has to ensure that x is a row vector) | ||
# old mathematical form: | ||
# return A.dot(np.diagflat(x)) | ||
if type(A) is pd.DataFrame: | ||
return pd.DataFrame(A.values * x, index=A.index, columns=A.columns) | ||
return pd.DataFrame(A.values * x[None, :], | ||
index=A.index, columns=A.columns) | ||
else: | ||
return A * x | ||
return A * x[None, :] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. x[None,:] is not doing a row vector - leads to different results |
||
|
||
|
||
def calc_A(Z, x): | ||
"""Calculate the A matrix (coefficients) from Z and x | ||
|
||
A is a normalized version of the industrial flows Z | ||
|
||
Parameters | ||
---------- | ||
Z : pandas.DataFrame or numpy.array | ||
|
@@ -127,7 +148,8 @@ def calc_A(Z, x): | |
warnings.simplefilter("ignore") | ||
recix = 1 / x | ||
recix[recix == np.inf] = 0 | ||
recix = recix.reshape((1, -1)) | ||
# recix = recix.reshape((1, -1)) | ||
recix = recix[None, :] | ||
# use numpy broadcasting - factor ten faster | ||
# Mathematical form - slow | ||
# return Z.dot(np.diagflat(recix)) | ||
|
@@ -140,6 +162,19 @@ def calc_A(Z, x): | |
def calc_L(A): | ||
"""Calculate the Leontief L from A | ||
|
||
L = inverse matrix of (I - A) | ||
|
||
Where I is an identity matrix of same shape as A | ||
|
||
Comes from: | ||
x = Ax + y => (I-A)x = y | ||
Where: | ||
A: coefficient input () - output () table | ||
x: output vector | ||
y: final demand vector | ||
|
||
Hence, L allows to derive a required output vector x for a given demand y | ||
|
||
Parameters | ||
---------- | ||
A : pandas.DataFrame or numpy.array | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thats not fully correct: it is: sum_columns(Z) + sum_columns(Y)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, corrected in the last commit