common financial tools for risk and pricing


License
Other
Install
pip install fin-tools==0.1.9

Documentation

fin-tools

PyPI version Twitter

Overview

Common tools used in finance - risk, pricing, etc

pypi registry - https://pypi.python.org/pypi/fin-tools

Installation

pip install fin-tools

Dependencies

fin-tools requires:

numpy==1.12.1 or later
pandas==0.19.2 or later

API

Pricing

class cds(num_periods: int, discount_factors: List[float], default_probabilities: float, recovery_rate: float, notional: float, coupon_frequency: float, issue_premium: float)

cds.get_price()
returns price of CDS using probability tree model. if a list length 1 is passed for discount_factors and default_probabilities, price is computed using these values as the fixed discount factor and default probability for each period up to num_periods. Otherwise, these parameters should be lists of length num_periods

Risk

vol(returns) -> float

standard deviation of returns

beta(returns, market) -> float

beta of returns

var(returns, alpha) -> float

historical var of returns (takes t historical returns, orders them, and takes the loss at the point in the list which corresponds to alpha)

cvar(returns, alpha) -> float

conditional var of returns

lower_partial_moment(returns, threshold, order) -> float

measures of risk-adjusted return based on vol treat all deviations from the mean as risk, whereas measures of risk-adjusted return based on lower partial moments consider only deviations below some predefined minimum return threshold, t as risk

higher_partial_moment(returns, threshold, order) -> float

drawdown(returns, tau) -> float

maximum decrease in the value of the portfolio over a specific period of time

max_drawdown(returns) -> float

average_drawdown(returns, periods) -> float

average_drawdown_squared(returns, periods) -> float

treynor_ratio(er, returns, market, rf) -> float

excess returns generated by a portfolio, discounted by portfolio beta

sharpe_ratio(er, returns, rf) -> float

discounts expected excess returns by vol

information_ratio(returns, benchmark) -> float

extension of Sharpe ratio - replaces risk-free rate of return with the scalar expected return of a benchmark portfolio E(rb)

modigliani_ratio(er, returns, benchmark, rf) -> float

combination of the Sharpe and information ratios: adjusts the expected excess returns of the portfolio above the risk free rate by the expected excess returns of a benchmark portfolio, above the risk free rate

excess_var(er, returns, rf, alpha) -> float

excess return on value at risk discounts the excess return of the portfolio above the risk-free rate by the value at risk of the portfolio

conditional_sharpe_ratio(er, returns, rf, alpha) -> float

discounts the excess return of the portfolio above the risk-free rate by the conditional VaR of the portfolio

omega_ratio(er, returns, rf, target=0) -> float

discounts the excess returns of a portfolio above the target threshold (usually risk-free rate), by the first-order lower partial moment of the returns. first-order lower partial moment corresponds to the average expeceted loss aka downside risk

sortino_ratio(er, returns, rf, target=0) -> float

modification of sharpe - only uses downside vol (delta)

kappa_three_ratio(er, returns, rf, target=0) -> float

generalization of omega and sortino ratios if j = 1, kappa is omega, j = 2, kappa is sortino

gain_loss_ratio(returns, target=0) -> float

discounts first order higher partial moment of a portfolio's returns, upside potential, by the first-order lower partial moment of a portfolio's returns, downside risk

upside_potential_ratio(returns, target=0) -> float

discounts first order higher partial moment of a portfolio's returns, upside potential, by the second-order lower partial moment of a portfolio's returns, downside variation

calmar_ratio(er, returns, rf) -> float

discounts expected excess return of a portfolio by the worst expected maximum draw down for that portfolio

sterling_ratio(er, returns, rf, periods) -> float

discounts the expected excess return of a portfolio by the average of the N worst expected maximum drawdowns for that portfolio

burke_ratio(er, returns, rf, periods) -> float

similar to sterling, but less sensitive to outliers discounts the expected excess return of a portfolio by the square root of the average of the N worst expected maximum drawdowns for that portfolio

Mortgage

amortization_table(loan_amount: float, rate: float, monthly_payment: float) -> float

takes an initial loan amount, a fixed rate, and monthly payment, and returns the amortization table for the loan, showing monthly capital and interest repayment. term is calculated based on period required to fully amortize the loan.

Assume monthly compounding, payments made at end of month i.e. interest accrues in period, followed by monthly payment to cancel interest and reduce outstanding capital.