From 618c9ab4a276c393479382b107e8ef0bc3a085bb Mon Sep 17 00:00:00 2001 From: Tongcai618 <134289249+Tongcai618@users.noreply.github.com> Date: Mon, 18 Sep 2023 21:02:26 -0400 Subject: [PATCH 1/9] Create dividend_discount_model.py This program is to pricing a company's stock using the value of next year dividend (D1), constant cost of equity capital (r), and constant growth rate in perpetuity (g). --- financial/dividend_discount_model.py | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 financial/dividend_discount_model.py diff --git a/financial/dividend_discount_model.py b/financial/dividend_discount_model.py new file mode 100644 index 000000000000..a0203f1f463e --- /dev/null +++ b/financial/dividend_discount_model.py @@ -0,0 +1,51 @@ +""" +Program using Dividend Discount Model to pricing a company's stock, given +- Value of the next year dividend +- Constant cost of equity capital +- constant growth rate in perpetuity + +Wikipedia Reference: https://en.wikipedia.org/wiki/Dividend_discount_model +""" + +def dividend_discount_model( + next_dividend: float, constant_cost: float, constant_growth: float +) -> float: + """ + Formula for Dividend Discount Model: + P = D1/(r-g) + where P is the expected price of a company's stock, r is the rate of interest per month + and n is the number of payments + + >>> dividend_discount_model(25000, 0.12, 0.03) + 277777.7777777778 + >>> dividend_discount_model(25000, 0.12, 0.10) + 1250000.0000000007 + >>> dividend_discount_model(0, 0.12, 0.03) + Traceback (most recent call last): + ... + Exception: Principal borrowed must be > 0 + >>> dividend_discount_model(25000, -0.12, 0.03) + Traceback (most recent call last): + ... + Exception: Rate of interest must be >= 0 + >>> dividend_discount_model(25000, 0.12, 0) + Traceback (most recent call last): + ... + """ + if next_dividend <= 0: + raise Exception("The next year's dividend must be > 0") + if constant_cost < 0: + raise Exception("The rate of constant cost must be >= 0") + if constant_growth <= 0: + raise Exception("The constant growth must be >= 0") + + + return (next_dividend/(constant_cost-constant_growth)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + From ab5526d4ab2f80590558c029cf0113434aa6ef5d Mon Sep 17 00:00:00 2001 From: Tongcai618 <134289249+Tongcai618@users.noreply.github.com> Date: Tue, 19 Sep 2023 10:20:44 -0400 Subject: [PATCH 2/9] Update dividend_discount_model.py --- financial/dividend_discount_model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/financial/dividend_discount_model.py b/financial/dividend_discount_model.py index a0203f1f463e..aa4f0ae593e5 100644 --- a/financial/dividend_discount_model.py +++ b/financial/dividend_discount_model.py @@ -23,11 +23,11 @@ def dividend_discount_model( >>> dividend_discount_model(0, 0.12, 0.03) Traceback (most recent call last): ... - Exception: Principal borrowed must be > 0 + Exception: The rate of constant cost must be >= 0 >>> dividend_discount_model(25000, -0.12, 0.03) Traceback (most recent call last): ... - Exception: Rate of interest must be >= 0 + Exception: The constant growth must be >= 0 >>> dividend_discount_model(25000, 0.12, 0) Traceback (most recent call last): ... From 69cac1c23b14af17767788fb558ea819e3acff6c Mon Sep 17 00:00:00 2001 From: Tongcai618 <134289249+Tongcai618@users.noreply.github.com> Date: Tue, 19 Sep 2023 13:40:06 -0400 Subject: [PATCH 3/9] Update price_plus_tax.py I change the comment above to make the inputs more clear to read. Also, I add some conditions to prevent some situations that the price or tax rate is not legal. --- financial/price_plus_tax.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/financial/price_plus_tax.py b/financial/price_plus_tax.py index 43876d35e57c..d954fdc05316 100644 --- a/financial/price_plus_tax.py +++ b/financial/price_plus_tax.py @@ -1,5 +1,7 @@ """ -Calculate price plus tax of a good or service given its price and a tax rate. +Calculate price plus tax of a good or service given +- Price +- Tax rate """ @@ -10,6 +12,13 @@ def price_plus_tax(price: float, tax_rate: float) -> float: >>> price_plus_tax(125.50, 0.05) 131.775 """ + # If the price is negative, it will raise a warning + if price <= 0: + raise Exception("The price must be >= 0") + # If the tax rate is negative or zero, it will raise a warning + if tax_rate <= 0: + raise Exception("The tax rate must be > 0") + return price * (1 + tax_rate) From 296755e5ad32e9ed473e4b9eb9e5aa7fa5d41e58 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 20 Sep 2023 19:08:24 +0000 Subject: [PATCH 4/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- financial/dividend_discount_model.py | 6 ++---- financial/price_plus_tax.py | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/financial/dividend_discount_model.py b/financial/dividend_discount_model.py index aa4f0ae593e5..7e828a4ab77d 100644 --- a/financial/dividend_discount_model.py +++ b/financial/dividend_discount_model.py @@ -7,6 +7,7 @@ Wikipedia Reference: https://en.wikipedia.org/wiki/Dividend_discount_model """ + def dividend_discount_model( next_dividend: float, constant_cost: float, constant_growth: float ) -> float: @@ -39,13 +40,10 @@ def dividend_discount_model( if constant_growth <= 0: raise Exception("The constant growth must be >= 0") - - return (next_dividend/(constant_cost-constant_growth)) + return next_dividend / (constant_cost - constant_growth) if __name__ == "__main__": import doctest doctest.testmod() - - diff --git a/financial/price_plus_tax.py b/financial/price_plus_tax.py index d954fdc05316..2ad8b293d745 100644 --- a/financial/price_plus_tax.py +++ b/financial/price_plus_tax.py @@ -1,5 +1,5 @@ """ -Calculate price plus tax of a good or service given +Calculate price plus tax of a good or service given - Price - Tax rate """ @@ -18,7 +18,7 @@ def price_plus_tax(price: float, tax_rate: float) -> float: # If the tax rate is negative or zero, it will raise a warning if tax_rate <= 0: raise Exception("The tax rate must be > 0") - + return price * (1 + tax_rate) From 3f0e1e9430e2696111934ac7b49ce16b4c6b8fa5 Mon Sep 17 00:00:00 2001 From: Tongcai618 <134289249+Tongcai618@users.noreply.github.com> Date: Wed, 20 Sep 2023 15:13:46 -0400 Subject: [PATCH 5/9] Update dividend_discount_model.py --- financial/dividend_discount_model.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/financial/dividend_discount_model.py b/financial/dividend_discount_model.py index aa4f0ae593e5..fc98ec31ca5c 100644 --- a/financial/dividend_discount_model.py +++ b/financial/dividend_discount_model.py @@ -13,7 +13,8 @@ def dividend_discount_model( """ Formula for Dividend Discount Model: P = D1/(r-g) - where P is the expected price of a company's stock, r is the rate of interest per month + where P is the expected price of a company's stock, + r is the rate of interest per month and n is the number of payments >>> dividend_discount_model(25000, 0.12, 0.03) From 7cffb9912af9b4ff232cfc1da3f2a177dd8ad142 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 20 Sep 2023 19:18:21 +0000 Subject: [PATCH 6/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- financial/dividend_discount_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/financial/dividend_discount_model.py b/financial/dividend_discount_model.py index 50ec06d08e6b..5e7e19dc9a9e 100644 --- a/financial/dividend_discount_model.py +++ b/financial/dividend_discount_model.py @@ -14,7 +14,7 @@ def dividend_discount_model( """ Formula for Dividend Discount Model: P = D1/(r-g) - where P is the expected price of a company's stock, + where P is the expected price of a company's stock, r is the rate of interest per month and n is the number of payments From 42c97a76005eadfba044b68f96c87b2fced7284e Mon Sep 17 00:00:00 2001 From: Tong Cai <134289249+Tongcai618@users.noreply.github.com> Date: Wed, 20 Sep 2023 15:27:30 -0400 Subject: [PATCH 7/9] Update dividend_discount_model.py --- financial/dividend_discount_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/financial/dividend_discount_model.py b/financial/dividend_discount_model.py index 5e7e19dc9a9e..8517c87cf992 100644 --- a/financial/dividend_discount_model.py +++ b/financial/dividend_discount_model.py @@ -15,7 +15,7 @@ def dividend_discount_model( Formula for Dividend Discount Model: P = D1/(r-g) where P is the expected price of a company's stock, - r is the rate of interest per month + r is the rate of interest per month, and n is the number of payments >>> dividend_discount_model(25000, 0.12, 0.03) From a42ad49618cc9f86b48bc340513564d66d4d146c Mon Sep 17 00:00:00 2001 From: Tongcai618 <134289249+Tongcai618@users.noreply.github.com> Date: Wed, 20 Sep 2023 22:54:01 -0400 Subject: [PATCH 8/9] Update index_of_rightmost_set_bit.py --- bit_manipulation/index_of_rightmost_set_bit.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/index_of_rightmost_set_bit.py b/bit_manipulation/index_of_rightmost_set_bit.py index c9c911660b08..2e0adb8861ed 100644 --- a/bit_manipulation/index_of_rightmost_set_bit.py +++ b/bit_manipulation/index_of_rightmost_set_bit.py @@ -4,8 +4,9 @@ def get_index_of_rightmost_set_bit(number: int) -> int: """ Take in a positive integer 'number'. - Returns the zero-based index of first set bit in that 'number' from right. - Returns -1, If no set bit found. + Return the zero-based index of the first set bit in that 'number' + starting from right. + Return -1 if the set bit is not found. >>> get_index_of_rightmost_set_bit(0) -1 From fb6dc8d382d9117a5b79f93eb87d00c408ccfc93 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sat, 30 Sep 2023 12:34:46 -0400 Subject: [PATCH 9/9] Fix typos in doctests --- financial/dividend_discount_model.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/financial/dividend_discount_model.py b/financial/dividend_discount_model.py index 8517c87cf992..674593ca0dd9 100644 --- a/financial/dividend_discount_model.py +++ b/financial/dividend_discount_model.py @@ -25,21 +25,22 @@ def dividend_discount_model( >>> dividend_discount_model(0, 0.12, 0.03) Traceback (most recent call last): ... - Exception: The rate of constant cost must be >= 0 + ValueError: The next year's dividend must be > 0 >>> dividend_discount_model(25000, -0.12, 0.03) Traceback (most recent call last): ... - Exception: The constant growth must be >= 0 + ValueError: The rate of constant cost must be >= 0 >>> dividend_discount_model(25000, 0.12, 0) Traceback (most recent call last): ... + ValueError: The constant growth must be >= 0 """ if next_dividend <= 0: - raise Exception("The next year's dividend must be > 0") + raise ValueError("The next year's dividend must be > 0") if constant_cost < 0: - raise Exception("The rate of constant cost must be >= 0") + raise ValueError("The rate of constant cost must be >= 0") if constant_growth <= 0: - raise Exception("The constant growth must be >= 0") + raise ValueError("The constant growth must be >= 0") return next_dividend / (constant_cost - constant_growth)