finds.recipes.finance

Financial functions

Copyright 2022, Terence Lim

MIT License

finds.recipes.finance.bond_price(coupon: float, n: int, m: int, yields: float | List[float], par: float = 1, **kwargs) float[source]

Compute present value of bond given spot rates or yield-to-maturity

Parameters:
  • coupon – Annual coupon rate

  • n – Number of remaining coupons

  • m – Number of compounding periods per year

  • yields – Simple annual yield-to-maturity or spot rates each period

  • par – face or par value of bond

Returns:

Present value of bond

finds.recipes.finance.bootstrap_risk(X: Series, alpha: float, n: int) DataFrame[source]

Returned bootstrapped risk statistics

Parameters:
  • X – Series of observed returns

  • alpha – VaR level (e.g. 0.95)

  • n – Number of bootstrap samples

Returns:

DataFrame of n bootstrapped samples of risk statistics

finds.recipes.finance.bootstrap_spot(coupon: float, spots: List[float], m: int, price: float = 1) float[source]

Compute spot rate to maturity of par bond from yield and sequence of spots

Parameters:
  • coupon – Annual coupon rate

  • spots – Simple annual spot rates each period (excl last period before maturity

  • m – Number of compounding periods per year

  • price – Present value of bond

Returns:

Simple spot interest rate till maturity

finds.recipes.finance.efficient_portfolio(mu: ndarray, sigma: ndarray, target: float)[source]

Returns weights of minimum variance portfolio that exceeds target return

Parameters:
  • sigma – covariance matrix

  • mu – vector of expected excess (of the risk-free) returns

  • target – required minimum expected return of portfolio

Returns:

Dict of weights, volatility and mean of efficient portfolio that achieves target

finds.recipes.finance.forwards_from_spots(spots: List[float], m: int, skip: int = 0) List[float][source]

Compute forward rates given spot rates

Parameters:
  • spots – Sequence of simple annual spot rates

  • m – Number of compounding periods per year

  • skip – Number of initial periods skipped

Returns:

List of forward rates, excluding first period of spot rates input

finds.recipes.finance.gmv_portfolio(sigma: ndarray, mu: ndarray | None = None)[source]

Returns position weights of global minimum variance portfolio

Parameters:
  • sigma – covariance matrix

  • mu – vector of expected excess (of the risk-free) returns

Returns:

Dict of weights, volatility and mean (if mu provided) of the GMV

finds.recipes.finance.halflife(alpha)[source]

Returns halflife from alpha = -ln(2)/ln(lambda), where lambda=1-alpha

finds.recipes.finance.historical_risk(X: Series, alpha: float)[source]

Calculate historical VaR, ES, and sample moments

Parameters:
  • X – Series of observed returns

  • alpha – Var level (e.g. 0.95)

finds.recipes.finance.hl_vol(high: DataFrame, low: DataFrame, last: DataFrame = None) DataFrame[source]

Compute Parkinson volatility from high and low prices

Parameters:
  • high – DataFrame of high prices (observations x stocks)

  • low – DataFrame of low prices (observations x stocks)

  • last – DataFrame of last prices, for forward filling if high low missing

  • Returns – Estimated volatility

finds.recipes.finance.kupiec(X: Series, VaR: Series, alpha: float) Dict[source]

Kupiec proportion of failures likelihood ratio test of VaR

Parameters:
  • X – Series of observed returns

  • VaR – Series of predicted VaR

  • alpha – VaR level (e.g. 0.95)

Returns:

Dict of likelihood statistic and pvalue

finds.recipes.finance.kupiec_LR(alpha: float, s: int, n: int)[source]

Compute Kupiec likelihood ratio given s violations in n trials

Parameters:
  • s – number of violations

  • n – number of observations

  • alpha – VaR level (e.g. 0.95)

finds.recipes.finance.macaulay_duration(coupon: float, n: int, m: int, price: float, yields: float | List[float], par: float = 1, **kwargs) float[source]

Compute macaulay duration of a bond given spot rates or yield-to-maturity

Parameters:
  • coupon – Annual coupon rate

  • n – Number of remaining coupons

  • m – Number of compounding periods per year

  • price – current market price of bond

  • yields – Simple annual yield-to-maturity or spot rates each period

  • par – face or par value of bond

Returns:

Macaulay duration

finds.recipes.finance.maximum_drawdown(x: Series, is_price_level: bool = False) Series[source]

Compute max drawdown (max loss from previous high) period and returns

Parameters:
  • x – Returns or price level series

  • is_price_level – Whether input are price index levels, or returns

Returns:

Series with start and end levels, keyed by dates, of maximum drawdown

Notes

MDD = (Trough - Peak) / Peak

finds.recipes.finance.modified_convexity(coupon: float, n: int, m: int, price: float, yields: float | List[float], par: float = 1, **kwargs) float[source]

Compute mocified convexity of a bond given spot rates or yield-to-maturity

Parameters:
  • coupon – Annual coupon rate

  • n – Number of remaining coupons

  • m – Number of compounding periods per year

  • price – current market price of bond

  • yields – Simple annual yield-to-maturity or spot rates each period

  • par – face or par value of bond

Returns:

Modified convexity

finds.recipes.finance.modified_duration(coupon: float, n: int, m: int, price: float, yields: float | List[float], par: float = 1, **kwargs) float[source]

Compute modified duration of a bond given spot rates or yield-to-maturity

Parameters:
  • coupon – Annual coupon rate

  • n – Number of remaining coupons

  • m – Number of compounding periods per year

  • price – current market price of bond

  • yields – Simple annual yield-to-maturity or spot rates each period

  • par – face or par value of bond

Returns:

Modified duration

finds.recipes.finance.ohlc_vol(first: DataFrame, high: DataFrame, low: DataFrame, last: DataFrame, ffill: bool = False, zero_mean: bool = True) DataFrame[source]

Compute Garman-Klass or Rogers-Satchell (non zero mean) OHLC vol

Parameters:
  • first – DataFrame of open prices (observations x stocks)

  • high – DataFrame of high prices (observations x stocks)

  • low – DataFrame of low prices (observations x stocks)

  • last – DataFrame of close prices (observations x stocks)

Returns:

Estimated volatility

finds.recipes.finance.parametric_risk(sigma: float | Series, alpha: float) Dict[source]

Calculate parametric gaussian VaR and ES

Parameters:
  • sigma – predicted volatility, or a Series

  • alpha – Var level (e.g. 0.95)

finds.recipes.finance.quadprog(sigma)[source]

Quadratic solver for portfolio optimization

finds.recipes.finance.tangency_portfolio(mu: ndarray, sigma: ndarray)[source]

Returns weights of tangency portfolio with largest slope (sharpe ratio)

Parameters:
  • sigma – covariance matrix

  • mu – vector of expected excess (of the risk-free) returns

Returns:

Dict of weights, volatility and mean of the tangency portfolio