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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
190
191
192
193
194
195
196
197
198
199
200
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
"""
Bachelier Option Pricing Model
==============================

Implementation of the Bachelier (1900) model for option pricing under
arithmetic Brownian motion. This is the first mathematical model of
derivatives pricing, predating Black-Scholes by 73 years.

Mathematical Foundation
-----------------------
The Bachelier model assumes the underlying asset follows arithmetic Brownian motion:
    dF = σ dW

Where:
    F: Forward/futures price
    σ: Absolute volatility (not percentage)
    W: Wiener process

Closed-Form Solutions
--------------------
Call Option:
    C = (F - K)×Φ(d) + σ√T×φ(d)

Put Option:
    P = (K - F)×Φ(-d) + σ√T×φ(d)

Where:
    d = (F - K) / (σ√T)
    Φ: Standard normal CDF
    φ: Standard normal PDF

Use Cases
---------
1. Futures and forwards pricing (constant strike)
2. Low or negative interest rate environments
3. Short-dated options near at-the-money
4. Historical analysis and academic research
5. Situations where absolute price changes are more relevant than percentages

References
----------
- Bachelier, L. (1900). "Théorie de la spéculation."
  Annales scientifiques de l'École normale supérieure, 17, 21-86.

Author: Extracted from Q-Fin, Enhanced for QuantLib Pro
Date: February 24, 2026
Version: 1.0.0
"""

from dataclasses import dataclass
from typing import Tuple, List, Optional, Literal
import numpy as np
from scipy.stats import norm


@dataclass
class BachelierParams:
    """
    Parameters for the Bachelier model.
    
    Attributes
    ----------
    sigma : float
        Absolute volatility (not percentage). Must be positive.
        Typical values: 10-50 for equity indices, 0.5-2.0 for FX.
    """
    sigma: float
    
    def __post_init__(self):
        """Validate parameters."""
        if self.sigma <= 0:
            raise ValueError(f"Sigma must be positive, got {self.sigma}")
        if np.isnan(self.sigma) or np.isinf(self.sigma):
            raise ValueError(f"Sigma must be finite, got {self.sigma}")


class BachelierModel:
    """
    Bachelier option pricing model with arithmetic Brownian motion.
    
    This model is particularly useful for:
    - Futures/forwards (no carry cost complications)
    - Low/negative rate environments
    - Short-dated ATM options
    - Academic research and historical analysis
    
    Examples
    --------
    >>> # Create model with absolute volatility of 20
    >>> model = BachelierModel(sigma=20.0)
    >>> 
    >>> # Price a call option
    >>> call_price = model.price(F0=100, K=100, T=1.0, option_type='call')
    >>> print(f"Call price: {call_price:.4f}")
    Call price: 7.9788
    >>> 
    >>> # Price a put option (put-call parity verified)
    >>> put_price = model.price(F0=100, K=100, T=1.0, option_type='put')
    >>> print(f"Put price: {put_price:.4f}")
    Put price: 7.9788
    >>> 
    >>> # Simulate paths
    >>> paths = model.simulate(F0=100, n_paths=1000, n_steps=252, T=1.0, seed=42)
    >>> print(f"Terminal mean: {np.mean([p[-1] for p in paths]):.2f}")
    Terminal mean: 100.12
    """
    
    def __init__(self, sigma: float):
        """
        Initialize Bachelier model.
        
        Parameters
        ----------
        sigma : float
            Absolute volatility (must be positive).
        """
        self.params = BachelierParams(sigma=sigma)
    
    @property
    def sigma(self) -> float:
        """Get current volatility parameter."""
        return self.params.sigma
    
    def price(
        self,
        F0: float,
        K: float,
        T: float,
        option_type: Literal['call', 'put'] = 'call'
    ) -> float:
        """
        Price a European option using the Bachelier formula.
        
        Parameters
        ----------
        F0 : float
            Current forward/futures price (must be positive).
        K : float
            Strike price (must be positive).
        T : float
            Time to maturity in years (must be positive).
        option_type : {'call', 'put'}, default='call'
            Type of option to price.
        
        Returns
        -------
        float
            Option price.
        
        Raises
        ------
        ValueError
            If any parameter is invalid.
        
        Examples
        --------
        >>> model = BachelierModel(sigma=20.0)
        >>> call = model.price(F0=100, K=100, T=1.0, option_type='call')
        >>> put = model.price(F0=100, K=100, T=1.0, option_type='put')
        >>> print(f"ATM Call: {call:.4f}, ATM Put: {put:.4f}")
        ATM Call: 7.9788, ATM Put: 7.9788
        """
        # Input validation
        if F0 <= 0:
            raise ValueError(f"Forward price F0 must be positive, got {F0}")
        if K <= 0:
            raise ValueError(f"Strike K must be positive, got {K}")
        if T <= 0:
            raise ValueError(f"Time to maturity T must be positive, got {T}")
        if option_type not in ['call', 'put']:
            raise ValueError(f"option_type must be 'call' or 'put', got {option_type}")
        
        # Handle edge case: T very close to 0
        if T < 1e-10:
            if option_type == 'call':
                return max(F0 - K, 0.0)
            else:
                return max(K - F0, 0.0)
        
        # Calculate d parameter
        sigma_sqrt_T = self.params.sigma * np.sqrt(T)
        d = (F0 - K) / sigma_sqrt_T
        
        # Calculate option price
        if option_type == 'call':
            # C = (F - K)×Φ(d) + σ√T×φ(d)
            price = (F0 - K) * norm.cdf(d) + sigma_sqrt_T * norm.pdf(d)
        else:  # put
            # P = (K - F)×Φ(-d) + σ√T×φ(d)
            # Note: φ(d) = φ(-d) (symmetric)
            price = (K - F0) * norm.cdf(-d) + sigma_sqrt_T * norm.pdf(d)
        
        return float(price)
    
    def delta(
        self,
        F0: float,
        K: float,
        T: float,
        option_type: Literal['call', 'put'] = 'call'
    ) -> float:
        """
        Calculate option delta (∂C/∂F).
        
        For Bachelier model:
        - Call delta: Δ_C = Φ(d)
        - Put delta: Δ_P = Φ(d) - 1
        
        Parameters
        ----------
        F0 : float
            Current forward price.
        K : float
            Strike price.
        T : float
            Time to maturity in years.
        option_type : {'call', 'put'}, default='call'
            Type of option.
        
        Returns
        -------
        float
            Option delta.
        """
        if T <= 0:
            raise ValueError(f"Time to maturity T must be positive, got {T}")
        
        sigma_sqrt_T = self.params.sigma * np.sqrt(T)
        d = (F0 - K) / sigma_sqrt_T
        
        if option_type == 'call':
            return float(norm.cdf(d))
        else:  # put
            return float(norm.cdf(d) - 1.0)
    
    def gamma(self, F0: float, K: float, T: float) -> float:
        """
        Calculate option gamma (∂²C/∂F²).
        
        For Bachelier model:
        - Γ = φ(d) / (σ√T)
        - Same for both calls and puts
        
        Parameters
        ----------
        F0 : float
            Current forward price.
        K : float
            Strike price.
        T : float
            Time to maturity in years.
        
        Returns
        -------
        float
            Option gamma.
        """
        if T <= 0:
            raise ValueError(f"Time to maturity T must be positive, got {T}")
        
        sigma_sqrt_T = self.params.sigma * np.sqrt(T)
        d = (F0 - K) / sigma_sqrt_T
        
        return float(norm.pdf(d) / sigma_sqrt_T)
    
    def vega(self, F0: float, K: float, T: float) -> float:
        """
        Calculate option vega (∂C/∂σ).
        
        For Bachelier model:
        - ν = √T × φ(d)
        - Same for both calls and puts
        
        Parameters
        ----------
        F0 : float
            Current forward price.
        K : float
            Strike price.
        T : float
            Time to maturity in years.
        
        Returns
        -------
        float
            Option vega (per unit of absolute volatility).
        """
        if T <= 0:
            raise ValueError(f"Time to maturity T must be positive, got {T}")
        
        sqrt_T = np.sqrt(T)
        d = (F0 - K) / (self.params.sigma * sqrt_T)
        
        return float(sqrt_T * norm.pdf(d))
    
    def theta(
        self,
        F0: float,
        K: float,
        T: float,
        option_type: Literal['call', 'put'] = 'call'
    ) -> float:
        """
        Calculate option theta (∂C/∂T).
        
        For Bachelier model:
        - Θ = -σ×φ(d) / (2√T)
        - Same for both calls and puts (no drift term)
        
        Parameters
        ----------
        F0 : float
            Current forward price.
        K : float
            Strike price.
        T : float
            Time to maturity in years.
        option_type : {'call', 'put'}, default='call'
            Type of option.
        
        Returns
        -------
        float
            Option theta (per year).
        """
        if T <= 0:
            raise ValueError(f"Time to maturity T must be positive, got {T}")
        
        sqrt_T = np.sqrt(T)
        d = (F0 - K) / (self.params.sigma * sqrt_T)
        
        # Theta is negative (time decay)
        return float(-self.params.sigma * norm.pdf(d) / (2 * sqrt_T))
    
    def implied_volatility(
        self,
        price: float,
        F0: float,
        K: float,
        T: float,
        option_type: Literal['call', 'put'] = 'call',
        tol: float = 1e-6,
        max_iter: int = 100
    ) -> float:
        """
        Calculate implied volatility from option price using Newton-Raphson.
        
        Parameters
        ----------
        price : float
            Observed option price.
        F0 : float
            Current forward price.
        K : float
            Strike price.
        T : float
            Time to maturity.
        option_type : {'call', 'put'}, default='call'
            Type of option.
        tol : float, default=1e-6
            Convergence tolerance.
        max_iter : int, default=100
            Maximum iterations.
        
        Returns
        -------
        float
            Implied absolute volatility.
        
        Raises
        ------
        ValueError
            If Newton-Raphson fails to converge.
        """
        from scipy.optimize import newton
        
        def objective(sigma):
            temp_model = BachelierModel(sigma=sigma)
            return temp_model.price(F0, K, T, option_type) - price
        
        def derivative(sigma):
            temp_model = BachelierModel(sigma=sigma)
            return temp_model.vega(F0, K, T)
        
        # Initial guess: use intrinsic value relationship
        intrinsic = max(F0 - K, 0) if option_type == 'call' else max(K - F0, 0)
        time_value = price - intrinsic
        initial_guess = max(time_value / np.sqrt(T * 2 * np.pi), 0.1)
        
        try:
            result = newton(
                objective,
                x0=initial_guess,
                fprime=derivative,
                tol=tol,
                maxiter=max_iter
            )
            return float(result)
        except RuntimeError as e:
            raise ValueError(f"Failed to converge: {e}")
    
    def simulate(
        self,
        F0: float,
        n_paths: int,
        n_steps: int,
        T: float,
        seed: Optional[int] = None
    ) -> List[np.ndarray]:
        """
        Simulate price paths using arithmetic Brownian motion.
        
        Discretization:
            F_{t+dt} = F_t + σ×√dt×Z
        
        Where Z ~ N(0,1)
        
        Parameters
        ----------
        F0 : float
            Initial forward price.
        n_paths : int
            Number of paths to simulate.
        n_steps : int
            Number of time steps per path.
        T : float
            Time to maturity in years.
        seed : int, optional
            Random seed for reproducibility.
        
        Returns
        -------
        List[np.ndarray]
            List of simulated price paths, each of length (n_steps + 1).
        
        Examples
        --------
        >>> model = BachelierModel(sigma=20.0)
        >>> paths = model.simulate(F0=100, n_paths=1000, n_steps=252, T=1.0, seed=42)
        >>> terminal_prices = [path[-1] for path in paths]
        >>> print(f"Mean: {np.mean(terminal_prices):.2f}, Std: {np.std(terminal_prices):.2f}")
        Mean: 100.12, Std: 20.04
        """
        if F0 <= 0:
            raise ValueError(f"Initial price F0 must be positive, got {F0}")
        if n_paths <= 0:
            raise ValueError(f"Number of paths must be positive, got {n_paths}")
        if n_steps <= 0:
            raise ValueError(f"Number of steps must be positive, got {n_steps}")
        if T <= 0:
            raise ValueError(f"Time to maturity T must be positive, got {T}")
        
        # Set random seed for reproducibility
        if seed is not None:
            np.random.seed(seed)
        
        dt = T / n_steps
        sqrt_dt = np.sqrt(dt)
        
        paths = []
        for _ in range(n_paths):
            path = np.zeros(n_steps + 1)
            path[0] = F0
            
            for t in range(n_steps):
                dW = np.random.standard_normal()
                path[t + 1] = path[t] + self.params.sigma * sqrt_dt * dW
            
            paths.append(path)
        
        return paths
    
    def simulate_vectorized(
        self,
        F0: float,
        n_paths: int,
        n_steps: int,
        T: float,
        seed: Optional[int] = None
    ) -> np.ndarray:
        """
        Simulate price paths using vectorized operations (faster).
        
        Parameters
        ----------
        F0 : float
            Initial forward price.
        n_paths : int
            Number of paths to simulate.
        n_steps : int
            Number of time steps per path.
        T : float
            Time to maturity in years.
        seed : int, optional
            Random seed for reproducibility.
        
        Returns
        -------
        np.ndarray
            Array of shape (n_paths, n_steps + 1) containing simulated paths.
        
        Examples
        --------
        >>> model = BachelierModel(sigma=20.0)
        >>> paths = model.simulate_vectorized(F0=100, n_paths=10000, n_steps=252, T=1.0)
        >>> print(f"Shape: {paths.shape}")
        Shape: (10000, 253)
        """
        if seed is not None:
            np.random.seed(seed)
        
        dt = T / n_steps
        sqrt_dt = np.sqrt(dt)
        
        # Generate all random numbers at once
        dW = np.random.standard_normal((n_paths, n_steps))
        
        # Calculate increments
        increments = self.params.sigma * sqrt_dt * dW
        
        # Cumulative sum to get paths
        paths = np.zeros((n_paths, n_steps + 1))
        paths[:, 0] = F0
        paths[:, 1:] = F0 + np.cumsum(increments, axis=1)
        
        return paths
    
    def __repr__(self) -> str:
        return f"BachelierModel(sigma={self.params.sigma})"
    
    def __str__(self) -> str:
        return (
            f"Bachelier Model (Arithmetic Brownian Motion)\n"
            f"  Absolute Volatility (σ): {self.params.sigma:.4f}\n"
            f"  Model Type: European Options Pricing\n"
            f"  Underlying Process: dF = σ dW"
        )


# Convenience functions for quick pricing without instantiating model

def bachelier_call(F0: float, K: float, T: float, sigma: float) -> float:
    """
    Quick Bachelier call option pricing.
    
    Parameters
    ----------
    F0 : float
        Forward price.
    K : float
        Strike price.
    T : float
        Time to maturity.
    sigma : float
        Absolute volatility.
    
    Returns
    -------
    float
        Call option price.
    """
    model = BachelierModel(sigma=sigma)
    return model.price(F0, K, T, 'call')


def bachelier_put(F0: float, K: float, T: float, sigma: float) -> float:
    """
    Quick Bachelier put option pricing.
    
    Parameters
    ----------
    F0 : float
        Forward price.
    K : float
        Strike price.
    T : float
        Time to maturity.
    sigma : float
        Absolute volatility.
    
    Returns
    -------
    float
        Put option price.
    """
    model = BachelierModel(sigma=sigma)
    return model.price(F0, K, T, 'put')


if __name__ == "__main__":
    # Example usage and verification
    print("Bachelier Option Pricing Model")
    print("=" * 50)
    
    # Create model
    model = BachelierModel(sigma=20.0)
    print(model)
    print()
    
    # Price options
    F0, K, T = 100, 100, 1.0
    call_price = model.price(F0, K, T, 'call')
    put_price = model.price(F0, K, T, 'put')
    
    print(f"ATM Option Pricing (F0={F0}, K={K}, T={T}):")
    print(f"  Call Price: {call_price:.6f}")
    print(f"  Put Price:  {put_price:.6f}")
    print(f"  Put-Call Parity Check: {abs(call_price - put_price):.2e}")
    print()
    
    # Calculate Greeks
    print("Greeks:")
    print(f"  Delta (Call): {model.delta(F0, K, T, 'call'):.6f}")
    print(f"  Delta (Put):  {model.delta(F0, K, T, 'put'):.6f}")
    print(f"  Gamma:        {model.gamma(F0, K, T):.6f}")
    print(f"  Vega:         {model.vega(F0, K, T):.6f}")
    print(f"  Theta:        {model.theta(F0, K, T):.6f}")
    print()
    
    # Simulate paths
    print("Monte Carlo Simulation (1000 paths, 252 steps):")
    paths = model.simulate_vectorized(F0, 1000, 252, T, seed=42)
    terminal = paths[:, -1]
    print(f"  Mean Terminal Price: {np.mean(terminal):.2f} (Expected: {F0:.2f})")
    print(f"  Std Dev:             {np.std(terminal):.2f} (Expected: {model.sigma:.2f})")