|
15 | 15 | from quantflow.utils import plot |
16 | 16 |
|
17 | 17 | from ..bs import implied_black_volatility |
| 18 | +from ..moneyness import log_strike_from_moneyness, moneyness_from_log_strike |
18 | 19 | from ..pricer import OptionPricerBase |
19 | 20 | from ..surface import OptionPrice, VolSurface |
20 | 21 |
|
@@ -150,7 +151,9 @@ def model_post_init(self, _ctx: Any) -> None: |
150 | 151 | entries = tuple(self.options.values()) |
151 | 152 | self._log_strikes = np.asarray([e.log_strike for e in entries]) |
152 | 153 | self._ttms = np.asarray([e.ttm for e in entries]) |
153 | | - self._moneyness = self._log_strikes / np.sqrt(self._ttms) |
| 154 | + self._moneyness = np.asarray( |
| 155 | + moneyness_from_log_strike(self._log_strikes, self._ttms) |
| 156 | + ) |
154 | 157 | self._mid_prices = np.asarray([e.mid_price() for e in entries]) |
155 | 158 | self._mid_ivs = np.asarray([e.mid_iv() for e in entries]) |
156 | 159 |
|
@@ -223,7 +226,7 @@ def cost_weight(self, ttm: float, log_strike: float) -> float: |
223 | 226 | Up-weights wing options via `exp(moneyness_weight * moneyness**2)`, |
224 | 227 | capped at `max_cost_weight`. The quadratic form mimics `1/vega`. |
225 | 228 | """ |
226 | | - moneyness = log_strike / np.sqrt(ttm) |
| 229 | + moneyness = float(moneyness_from_log_strike(log_strike, ttm)) |
227 | 230 | weight = np.exp(self.moneyness_weight * moneyness * moneyness) |
228 | 231 | return float(min(weight, self.max_cost_weight)) |
229 | 232 |
|
@@ -281,7 +284,11 @@ def cost_function(self, params: np.ndarray) -> float: |
281 | 284 | def _model_grid( |
282 | 285 | self, ttm: float, max_moneyness: float, support: int |
283 | 286 | ) -> pd.DataFrame: |
284 | | - log_strikes = np.linspace(-max_moneyness, max_moneyness, support) * np.sqrt(ttm) |
| 287 | + log_strikes = np.asarray( |
| 288 | + log_strike_from_moneyness( |
| 289 | + np.linspace(-max_moneyness, max_moneyness, support), ttm |
| 290 | + ) |
| 291 | + ) |
285 | 292 | return self.pricer.maturity(ttm).prices(log_strikes) |
286 | 293 |
|
287 | 294 | def plot( |
|
0 commit comments