Skip to content
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

ENH: Add capm to financial.py #11058

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions numpy/lib/financial.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,3 +759,51 @@ def mirr(values, finance_rate, reinvest_rate):
numer = np.abs(npv(reinvest_rate, values*pos))
denom = np.abs(npv(finance_rate, values*neg))
return (numer/denom)**(1/(n - 1))*(1 + reinvest_rate) - 1

def capm(rf, beta, rm):
"""
calculate the minimum rate of return using CAPM
(Capital Asset Pricing Model).

parameters:
-----------
rf: The risk free rate of retun.
beta: The company's beta factor which measures the sensitivity
of an investment return to market movement
rm: market rate of return

Notes.
------
The CAPM is an alternative method of calculating cost of equity
capital for an investment. it is made up of two sides; Risk free
rate i.e. the basic rate which all projects must earn if
it is completely free from risk. and the Risk Premium which
is gotten after applying the project
beta to the difference between market return and risk rate of return.

Example:
--------
XYZ limited wants to determine its minimum required rate of return if
the risk free rate is 7%, market rate of return is 10% and the company
has a beta factor of 12.

Solution
--------
rf = 0.07, beta = 12, rm = 0.10

min.required rate of return = 0.07 + 1.2(0.10 - 0.07)
=0.106
=10.6%

"""
try:
rf >= 1 or rm >= 1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SyntaxError here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops! I missed out the if statement there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"I think there's a general reluctance to add new financial functions to numpy: #2880."
Does it mean contributing to the finance library is void for now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olamilekan000 indeed. I've added a comment to gh-2880 to that effect.

return "values cannot be greater than or equals 1"
else:
ke = rf + beta * (rm - rf)
return ke
except Exception as e:
raise e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This try/except can be removed




5 changes: 5 additions & 0 deletions numpy/lib/tests/test_financial.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from decimal import Decimal

import numpy as np
import unittest
from numpy.testing import (
assert_, assert_almost_equal, assert_allclose, assert_equal, assert_raises
)
Expand Down Expand Up @@ -338,3 +339,7 @@ def test_broadcast_decimal(self):
Decimal('0'), [Decimal('0'), Decimal('0'), Decimal('1'), 'end', 'begin']),
[Decimal('-74.998201'), Decimal('-75.62318601'), Decimal('-75.62318601'),
Decimal('-76.88882405'), Decimal('-76.88882405')], 4)

def test_capm(self):
self.assertAlmostEequal(np.capm(0.07, 1.2, 0.10), 0.106)
self.assertAlmostEequal(np.capm(0.02, 2, 10), 0.18)