cds2.finance
Quantitative finance: returns, risk metrics, Black-Scholes, VaR.
Quantitative finance basics: returns, drawdowns, risk metrics and Black-Scholes.
DrawdownResult
dataclass
Deepest peak-to-trough decline of a price series.
Source code in src/cds2/finance.py
57 58 59 60 61 62 63 | |
BSResult
dataclass
Black-Scholes option price and first-order Greeks.
Source code in src/cds2/finance.py
127 128 129 130 131 132 133 134 | |
log_returns(prices)
Logarithmic returns ln(P_t / P_{t-1}); length n - 1.
Source code in src/cds2/finance.py
45 46 47 48 | |
simple_returns(prices)
Simple percentage returns P_t / P_{t-1} - 1; length n - 1.
Source code in src/cds2/finance.py
51 52 53 54 | |
max_drawdown(prices)
Most severe drawdown P_t / running_max_t - 1 with its peak/trough indices.
Source code in src/cds2/finance.py
66 67 68 69 70 71 72 73 74 75 76 77 | |
annualized_volatility(returns, periods_per_year=252)
Sample volatility scaled by sqrt(periods_per_year).
Source code in src/cds2/finance.py
86 87 88 89 90 | |
sharpe_ratio(returns, risk_free_annual=0.0, periods_per_year=252)
Annualized mean excess return over per-period sample volatility.
Source code in src/cds2/finance.py
93 94 95 96 97 98 99 100 101 102 | |
sortino_ratio(returns, risk_free_annual=0.0, periods_per_year=252, target=0.0)
Mean excess return divided by downside deviation against a per-period target.
The downside deviation averages the squared negative deviations
min(r - target, 0) over all periods; if there are none it is zero
and the ratio is infinite.
Source code in src/cds2/finance.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
black_scholes(spot, strike, rate, volatility, maturity, option='call')
Black-Scholes European call/put price with delta, gamma and vega.
Vega is quoted per unit change in volatility (not per percent). A zero maturity collapses to intrinsic value with step-function delta.
Source code in src/cds2/finance.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
historical_var(returns, confidence=0.95)
Historical Value-at-Risk as the positive loss at the given confidence level.
Source code in src/cds2/finance.py
192 193 194 195 196 197 198 | |
monte_carlo_var(current_value, mean_return, sigma, horizon_days=10, confidence=0.99, simulations=50000, seed=None)
Monte-Carlo Value-at-Risk of a lognormal terminal value over the horizon.
Terminal prices follow V0 * exp((mu - sigma^2/2) h + sigma sqrt(h) Z);
the reported number is the loss percentile of that distribution.
Source code in src/cds2/finance.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |