beta¶
Defined in fynance.metrics
- beta(X, B, period=252)[source]
OLS slope of the strategy’s returns on the benchmark’s returns.
- Parameters:
- Xnp.ndarray[float64, ndim=1]
Strategy price/level curve.
- Bnp.ndarray[float64, ndim=1]
Benchmark price/level curve, same length as
X.- periodint, optional
Unused by
betaitself; accepted for API consistency. Default 252.
- Returns:
- float
OLS beta of the strategy against the benchmark.
0when the benchmark has zero variance and the strategy’s covariance with it is also zero,+inf/-infwhen the benchmark has zero variance and the covariance is non-zero (see_safe_ratio).
See also
alpha,roll_beta_benchmark
Notes
With \(x\) the strategy’s simple returns and \(b\) the benchmark’s simple returns (see module docstring):
\[\beta = \frac{Cov(x, b)}{Var(b)}\]perioddoes not affect \(\beta\) (a slope is scale-free); it is accepted for signature consistency with the rest of this module (e.g.alphacallsbetainternally).Examples
>>> import numpy as np >>> rng = np.random.default_rng(42) >>> b_ret = rng.normal(0., 0.01, 999) >>> B = 100. * np.cumprod(1. + b_ret) >>> B = np.concatenate([[100.], B]) >>> x_ret = 2. * _compute_returns(B, False)[1:] >>> X = 100. * np.cumprod(1. + x_ret) >>> X = np.concatenate([[100.], X]) >>> round(beta(X, B), 4) 2.0