Spaces:
Sleeping
Sleeping
File size: 2,853 Bytes
72fc0ee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | """
Input validation helpers reused across every module.
Fail-fast with informative messages rather than propagating bad numbers.
"""
from __future__ import annotations
import math
from typing import Any
class ValidationError(ValueError):
"""Raised when a domain input fails validation."""
def require_positive(value: float, name: str) -> float:
"""Value must be > 0 and finite."""
_check_finite(value, name)
if value <= 0:
raise ValidationError(f"{name} must be positive, got {value}")
return value
def require_non_negative(value: float, name: str) -> float:
"""Value must be >= 0 and finite."""
_check_finite(value, name)
if value < 0:
raise ValidationError(f"{name} must be non-negative, got {value}")
return value
def require_probability(value: float, name: str) -> float:
"""Value must be in [0, 1]."""
_check_finite(value, name)
if not 0.0 <= value <= 1.0:
raise ValidationError(f"{name} must be in [0, 1], got {value}")
return value
def require_range(value: float, name: str, lo: float, hi: float) -> float:
"""Value must be in [lo, hi]."""
_check_finite(value, name)
if not lo <= value <= hi:
raise ValidationError(f"{name} must be in [{lo}, {hi}], got {value}")
return value
def require_ticker(ticker: Any) -> str:
"""Ticker must be a non-empty string of reasonable length."""
if not isinstance(ticker, str):
raise ValidationError(f"Ticker must be a string, got {type(ticker).__name__}")
ticker = ticker.strip().upper()
if not ticker:
raise ValidationError("Ticker cannot be empty")
if len(ticker) > 20:
raise ValidationError(f"Ticker too long ({len(ticker)} chars): {ticker!r}")
return ticker
def validate_black_scholes_inputs(
S: float, K: float, T: float, r: float, sigma: float
) -> None:
"""
Validate all Black-Scholes inputs.
Raises :class:`ValidationError` on the first failure found.
"""
require_positive(S, "Spot price (S)")
require_positive(K, "Strike price (K)")
require_non_negative(T, "Time to expiry (T)")
_check_finite(r, "Risk-free rate (r)")
require_non_negative(sigma, "Volatility (sigma)")
if sigma > 3.0: # 300% annualised vol
raise ValidationError(
f"Volatility {sigma:.1%} exceeds maximum allowed 300%. "
"Check your inputs – this value is unrealistic."
)
# ----------------------------------------------------------------- internal
def _check_finite(value: Any, name: str) -> None:
if not isinstance(value, (int, float)):
raise ValidationError(f"{name} must be numeric, got {type(value).__name__}")
if math.isnan(value):
raise ValidationError(f"{name} is NaN")
if math.isinf(value):
raise ValidationError(f"{name} is infinite")
|