Skip to content

Commit

Permalink
Merge pull request #380 from jdebacker/cs_updates
Browse files Browse the repository at this point in the history
Updates for latest package dependencies
  • Loading branch information
jdebacker authored Feb 24, 2024
2 parents fb9c9c2 + aa1d941 commit 0147866
Show file tree
Hide file tree
Showing 35 changed files with 4,503 additions and 2,852 deletions.
4 changes: 4 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# .git-blame-ignore-revs
# ignore initial move to black formatting
09acdfbf8dd3979d1d18b302b9d4277104e7b579
23c567a5a901d606511c3b60e3c780011137bf5c
6 changes: 3 additions & 3 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build Package and Test Source Code [Python 3.9, 3.10]
name: Build Package and Test Source Code [Python 3.9, 3.10, 3.11]

on: [push, pull_request]

Expand All @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.9", "3.10"]
python-version: ["3.9", "3.10", "3.11"]

steps:
- name: Checkout
Expand All @@ -34,7 +34,7 @@ jobs:
shell: bash -l {0}
working-directory: ./
run: |
pytest -m 'not needs_puf' --pycodestyle --cov=./ --cov-report=xml
pytest -m 'not needs_puf' --cov=./ --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/check_formatting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Check Black formatting

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: psf/black@stable
with:
options: "-l 79 --check"
src: "."
3 changes: 2 additions & 1 deletion ccc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""
Specify what is available to import from the ccc package.
"""

from ccc.parameters import *
from ccc.data import *
from ccc.calculator import *

__version__ = '0.0.0'
__version__ = "0.0.0"
146 changes: 78 additions & 68 deletions ccc/calcfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from ccc.constants import TAX_METHODS
from ccc.utils import str_modified

pd.set_option("future.no_silent_downcasting", True)

ENFORCE_CHECKS = True


def update_depr_methods(df, p, dp):
'''
"""
Updates depreciation methods per changes from defaults that are
specified by user.
Expand All @@ -21,38 +23,44 @@ def update_depr_methods(df, p, dp):
df (Pandas DataFrame): assets by type and tax treatment with
updated tax depreciation methods
'''
"""
# update tax_deprec_rates based on user defined parameters
# create dataframe with depreciation policy parameters
deprec_df = pd.DataFrame(dp.asset)
print("deprec_df", deprec_df.head())
# split out value into two columns
deprec_df = deprec_df.join(
pd.DataFrame(deprec_df.pop('value').values.tolist()))
print("deprec_df 2", deprec_df.head())
pd.DataFrame(deprec_df.pop("value").values.tolist())
)
# drop information duplicated in asset dataframe
deprec_df.drop(columns=['asset_name', 'minor_asset_group',
'major_asset_group'], inplace=True)
deprec_df.drop(
columns=["asset_name", "minor_asset_group", "major_asset_group"],
inplace=True,
)
# merge depreciation policy parameters to asset dataframe
df.drop(columns=deprec_df.keys(), inplace=True, errors='ignore')
df = df.merge(deprec_df, how='left', left_on='bea_asset_code',
right_on='BEA_code')
df.drop(columns=deprec_df.keys(), inplace=True, errors="ignore")
df = df.merge(
deprec_df, how="left", left_on="bea_asset_code", right_on="BEA_code"
)
# add bonus depreciation to tax deprec parameters dataframe
# ** UPDATE THIS - maybe including bonus in new asset deprec JSON**
df['bonus'] = df['GDS_life'].apply(str_modified)
df['bonus'].replace(p.bonus_deprec, inplace=True)
df["bonus"] = df["GDS_life"].apply(str_modified)
df.replace({"bonus": p.bonus_deprec}, inplace=True)
# make bonus float format
df["bonus"] = df["bonus"].astype(float)
# Compute b
df['b'] = df['method']
df['b'].replace(TAX_METHODS, inplace=True)
df.loc[df['system'] == 'ADS', 'Y'] = df.loc[df['system'] == 'ADS',
'ADS_life']
df.loc[df['system'] == 'GDS', 'Y'] = df.loc[df['system'] == 'GDS',
'GDS_life']
df["b"] = df["method"]
df.replace({"b": TAX_METHODS}, regex=True, inplace=True)
df.loc[df["system"] == "ADS", "Y"] = df.loc[
df["system"] == "ADS", "ADS_life"
]
df.loc[df["system"] == "GDS", "Y"] = df.loc[
df["system"] == "GDS", "GDS_life"
]
return df


def dbsl(Y, b, bonus, r):
r'''
r"""
Makes the calculation for the declining balance with a switch to
straight line (DBSL) method of depreciation.
Expand All @@ -72,21 +80,24 @@ def dbsl(Y, b, bonus, r):
z (array_like): net present value of depreciation deductions for
$1 of investment
'''
"""
beta = b / Y
Y_star = Y * (1 - (1 / b))
z = (
bonus + ((1 - bonus) * (((beta / (beta + r)) *
(1 - np.exp(-1 * (beta + r) * Y_star))) +
((np.exp(-1 * beta * Y_star) /
((Y - Y_star) * r)) *
(np.exp(-1 * r * Y_star) -
np.exp(-1 * r * Y))))))
z = bonus + (
(1 - bonus)
* (
((beta / (beta + r)) * (1 - np.exp(-1 * (beta + r) * Y_star)))
+ (
(np.exp(-1 * beta * Y_star) / ((Y - Y_star) * r))
* (np.exp(-1 * r * Y_star) - np.exp(-1 * r * Y))
)
)
)
return z


def sl(Y, bonus, r):
r'''
r"""
Makes the calculation for straight line (SL) method of depreciation.
.. math::
Expand All @@ -101,13 +112,13 @@ def sl(Y, bonus, r):
z (array_like): net present value of depreciation deductions for
$1 of investment
'''
"""
z = bonus + ((1 - bonus) * ((1 - np.exp(-1 * r * Y)) / (r * Y)))
return z


def econ(delta, bonus, r, pi):
r'''
r"""
Makes the calculation for the NPV of depreciation deductions using
economic depreciation rates.
Expand All @@ -124,13 +135,13 @@ def econ(delta, bonus, r, pi):
z (array_like): net present value of depreciation deductions for
$1 of investment
'''
"""
z = bonus + ((1 - bonus) * (delta / (delta + r - pi)))
return z


def npv_tax_depr(df, r, pi, land_expensing):
'''
"""
Depending on the method of depreciation, makes calls to either
the straight line or declining balance calculations.
Expand All @@ -144,28 +155,28 @@ def npv_tax_depr(df, r, pi, land_expensing):
z (Pandas series): NPV of depreciation deductions for all asset
types and tax treatments
'''
idx = (df['method'] == 'DB 200%') | (df['method'] == 'DB 150%')
df.loc[idx, 'z'] = dbsl(df.loc[idx, 'Y'], df.loc[idx, 'b'],
df.loc[idx, 'bonus'], r)
idx = df['method'] == 'SL'
df.loc[idx, 'z'] = sl(df.loc[idx, 'Y'], df.loc[idx, 'bonus'], r)
idx = df['method'] == 'Economic'
df.loc[idx, 'z'] = econ(df.loc[idx, 'delta'], df.loc[idx, 'bonus'],
r, pi)
idx = df['method'] == 'Expensing'
df.loc[idx, 'z'] = 1.0
idx = df['asset_name'] == 'Land'
df.loc[idx, 'z'] = np.squeeze(land_expensing)
idx = df['asset_name'] == 'Inventories'
df.loc[idx, 'z'] = 0.0 # not sure why I have to do this with changes
z = df['z']
"""
idx = (df["method"] == "DB 200%") | (df["method"] == "DB 150%")
df.loc[idx, "z"] = dbsl(
df.loc[idx, "Y"], df.loc[idx, "b"], df.loc[idx, "bonus"], r
)
idx = df["method"] == "SL"
df.loc[idx, "z"] = sl(df.loc[idx, "Y"], df.loc[idx, "bonus"], r)
idx = df["method"] == "Economic"
df.loc[idx, "z"] = econ(df.loc[idx, "delta"], df.loc[idx, "bonus"], r, pi)
idx = df["method"] == "Expensing"
df.loc[idx, "z"] = 1.0
idx = df["asset_name"] == "Land"
df.loc[idx, "z"] = np.squeeze(land_expensing)
idx = df["asset_name"] == "Inventories"
df.loc[idx, "z"] = 0.0 # not sure why I have to do this with changes
z = df["z"]

return z


def eq_coc(delta, z, w, u, inv_tax_credit, pi, r):
r'''
r"""
Compute the cost of capital
.. math::
Expand All @@ -185,15 +196,16 @@ def eq_coc(delta, z, w, u, inv_tax_credit, pi, r):
Returns:
rho (array_like): the cost of capital
'''
rho = (((r - pi + delta) / (1 - u)) *
(1 - inv_tax_credit - u * z) + w - delta)
"""
rho = (
((r - pi + delta) / (1 - u)) * (1 - inv_tax_credit - u * z) + w - delta
)

return rho


def eq_coc_inventory(u, phi, Y_v, pi, r):
r'''
r"""
Compute the cost of capital for inventories
.. math::
Expand All @@ -210,18 +222,16 @@ def eq_coc_inventory(u, phi, Y_v, pi, r):
Returns:
rho (scalar): cost of capital for inventories
'''
rho_FIFO = (((1 / Y_v) * np.log((np.exp(r * Y_v) - u) /
(1 - u))) - pi)
rho_LIFO = ((1 / Y_v) * np.log((np.exp((r - pi) * Y_v) - u) /
(1 - u)))
"""
rho_FIFO = ((1 / Y_v) * np.log((np.exp(r * Y_v) - u) / (1 - u))) - pi
rho_LIFO = (1 / Y_v) * np.log((np.exp((r - pi) * Y_v) - u) / (1 - u))
rho = phi * rho_FIFO + (1 - phi) * rho_LIFO

return rho


def eq_ucc(rho, delta):
r'''
r"""
Compute the user cost of capital
.. math::
Expand All @@ -234,13 +244,13 @@ def eq_ucc(rho, delta):
Returns:
ucc (array_like): the user cost of capital
'''
"""
ucc = rho + delta
return ucc


def eq_metr(rho, r_prime, pi):
r'''
r"""
Compute the marginal effective tax rate (METR)
.. math::
Expand All @@ -254,13 +264,13 @@ def eq_metr(rho, r_prime, pi):
Returns:
metr (array_like): METR
'''
"""
metr = (rho - (r_prime - pi)) / rho
return metr


def eq_mettr(rho, s):
r'''
r"""
Compute the marginal effective total tax rate (METTR)
.. math::
Expand All @@ -273,13 +283,13 @@ def eq_mettr(rho, s):
Returns:
mettr (array_like): METTR
'''
"""
mettr = (rho - s) / rho
return mettr


def eq_tax_wedge(rho, s):
r'''
r"""
Compute the tax wedge
.. math::
Expand All @@ -292,13 +302,13 @@ def eq_tax_wedge(rho, s):
Returns:
wedge (array_like): tax wedge
'''
"""
wedge = rho - s
return wedge


def eq_eatr(rho, metr, p, u):
r'''
r"""
Compute the effective average tax rate (EATR).
.. math::
Expand All @@ -315,6 +325,6 @@ def eq_eatr(rho, metr, p, u):
Returns:
eatr (array_like): EATR
'''
"""
eatr = ((p - rho) / p) * u + (rho / p) * metr
return eatr
Loading

0 comments on commit 0147866

Please sign in to comment.