-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Quant finance library in pure Haskell. -- @package quantfin @version 0.1.0.1 module Quant.ContingentClaim -- | ContingentClaim is just a list of the underlying -- ContingentClaim's. type ContingentClaim = [ContingentClaim'] -- | ContingentClaim' is the underlying type of contingent claims. data ContingentClaim' ContingentClaim' :: Double -> ([Vector Double] -> Vector Double) -> [(Double, Observables -> Vector Double, Double -> Double)] -> ContingentClaim' -- | Payout time for cash flow payoutTime :: ContingentClaim' -> Double collector :: ContingentClaim' -> [Vector Double] -> Vector Double -- | List containing: -- Time of observation, -- Function to access -- specific observable, -- Function to collect observations and transform -- them into a cash flow. observations :: ContingentClaim' -> [(Double, Observables -> Vector Double, Double -> Double)] -- | Observables are the observables available in a Monte Carlo simulation. -- Most basic MCs will have one observables (Black-Scholes) whereas more -- complex ones will have multiple (i.e. Heston-Hull-White). data Observables Observables :: [Vector Double] -> Observables -- | Used to compile claims for the Monte Carlo engine. data ContingentClaimBasket ContingentClaimBasket :: ContingentClaim -> [Double] -> ContingentClaimBasket -- | ADT for Put or Calls data OptionType Put :: OptionType Call :: OptionType -- | Converts a ContingentClaim into a ContingentClaimBasket -- for use by the MC engine. ccBasket :: ContingentClaim -> ContingentClaimBasket -- | Takes an OptionType, a strike, and a time to maturity and generates a -- vanilla option. vanillaOption :: OptionType -> Double -> Double -> ContingentClaim -- | Takes an OptionType, a strike, a payout amount and a time to maturity -- and generates a vanilla option. binaryOption :: OptionType -> Double -> Double -> Double -> ContingentClaim -- | A straddle is a put and a call with the same time to maturity / -- strike. straddle :: Double -> Double -> ContingentClaim -- | Takes an OptionType, a strike, observation times, time to maturity and -- generates an arithmetic Asian option. arithmeticAsianOption :: OptionType -> Double -> [Double] -> Double -> ContingentClaim -- | Takes an OptionType, a strike, observation times, time to maturity and -- generates a geometric Asian option. geometricAsianOption :: OptionType -> Double -> [Double] -> Double -> ContingentClaim -- | A call spread is a long position in a low-strike call and a short -- position in a high strike call. callSpread :: Double -> Double -> Double -> ContingentClaim -- | A put spread is a long position in a high strike put and a short -- position in a low strike put. putSpread :: Double -> Double -> Double -> ContingentClaim -- | Takes a time to maturity and generates a forward contract. forwardContract :: Double -> ContingentClaim -- | Takes an amount and a time and generates a fixed cash flow. fixed :: Double -> Double -> ContingentClaim -- | Scales up a contingent claim by a multiplier. multiplier :: Double -> ContingentClaim -> ContingentClaim -- | Flips the signs in a contingent claim to make it a short position. short :: ContingentClaim -> ContingentClaim -- | Just combines two contingent claims into one. combine :: ContingentClaim -> ContingentClaim -> ContingentClaim -- | Takes a maturity time and a function and generates a ContingentClaim -- dependent only on the terminal value of the observable. terminalOnly :: Double -> (Double -> Double) -> ContingentClaim -- | Offers the ability to change the function on the observable an option -- is based on. All options default to being based on the first -- observable. changeObservableFct :: ContingentClaim -> (Observables -> Vector Double) -> ContingentClaim -- | Utility function for when the observable function is just !! obsNum :: ContingentClaim -> Int -> ContingentClaim -- | Utility function to pull the head of a basket of observables. obsHead :: Observables -> Vector Double instance Eq Observables instance Show Observables instance Eq OptionType instance Show OptionType module Quant.MonteCarlo -- | Wraps the Identity monad in the MonteCarloT transformer. type MonteCarlo s a = MonteCarloT Identity s a -- | A monad transformer for Monte-Carlo calculations. type MonteCarloT m s = StateT s (RVarT m) -- | Runs a MonteCarlo calculation and provides the result of the -- computation. runMC :: MonadRandom (StateT b Identity) => MonteCarlo s c -> b -> s -> c -- | The Discretize class defines those models on which Monte Carlo -- simulations can be performed. -- -- Minimal complete definition: initialize, discounter, -- forwardGen and evolve'. class Discretize a where evolve mdl t2 anti = do { (_, t1) <- get; let ms = maxStep mdl; if (t2 - t1) < ms then evolve' mdl t2 anti else do { evolve' mdl (t1 + ms) anti; evolve mdl t2 anti } } maxStep _ = 1 / 250 simulateState modl (ContingentClaimBasket cs ts) trials anti = do { initialize modl trials; avg <$> process empty (replicate trials 0) cs ts } where process m cfs ccs@(c@(ContingentClaim' t _ _) : cs') (obsT : ts') = if t >= obsT then do { evolve modl obsT anti; obs <- gets fst; let m' = insert obsT obs m; process m' cfs ccs ts' } else do { evolve modl t anti; let cfs' = processClaimWithMap c m; d <- discounter modl obsT; let cfs'' = cfs' |*| d; process m (cfs |+| cfs'') cs' (obsT : ts') } process m cfs (c : cs') [] = do { d <- discounter modl (payoutTime c); let cfs' = d |*| processClaimWithMap c m; process m (cfs |+| cfs') cs' [] } process _ cfs _ _ = return cfs v1 |+| v2 = zipWith (+) v1 v2 v1 |*| v2 = zipWith (*) v1 v2 avg v = sum v / fromIntegral (length v) runSimulation modl ccs seed trials anti = runMC run seed (Observables [], 0) where run = simulateState modl (ccBasket ccs) trials anti runSimulationAnti modl ccs seed trials = (runSim True + runSim False) / 2 where runSim = runSimulation modl ccs seed (trials `div` 2) quickSim mdl opts trials = runSimulation mdl opts (pureMT 500) trials False quickSimAnti mdl opts trials = runSimulationAnti mdl opts (pureMT 500) trials initialize :: (Discretize a, Discretize a) => a -> Int -> MonteCarlo (Observables, Double) () evolve :: (Discretize a, Discretize a) => a -> Double -> Bool -> MonteCarlo (Observables, Double) () discounter :: (Discretize a, Discretize a) => a -> Double -> MonteCarlo (Observables, Double) (Vector Double) forwardGen :: (Discretize a, Discretize a) => a -> Double -> MonteCarlo (Observables, Double) (Vector Double) evolve' :: (Discretize a, Discretize a) => a -> Double -> Bool -> MonteCarlo (Observables, Double) () maxStep :: (Discretize a, Discretize a) => a -> Double simulateState :: (Discretize a, Discretize a) => a -> ContingentClaimBasket -> Int -> Bool -> MonteCarlo (Observables, Double) Double runSimulation :: (Discretize a, Discretize a, MonadRandom (StateT b Identity)) => a -> ContingentClaim -> b -> Int -> Bool -> Double runSimulationAnti :: (Discretize a, Discretize a, MonadRandom (StateT b Identity)) => a -> ContingentClaim -> b -> Int -> Double quickSim :: (Discretize a, Discretize a) => a -> ContingentClaim -> Int -> Double quickSimAnti :: (Discretize a, Discretize a) => a -> ContingentClaim -> Int -> Double -- | ADT for Put or Calls data OptionType Put :: OptionType Call :: OptionType -- | Utility function to get the number of trials. getTrials :: MonteCarlo (Observables, Double) Int module Quant.Math.Integration -- | A function, a lower bound, an upper bound and returns the integrated -- value. type Integrator = (Double -> Double) -> Double -> Double -> Double -- | Midpoint integration. midpoint :: Int -> Integrator -- | Trapezoidal integration. trapezoid :: Int -> Integrator -- | Integration using Simpson's rule. simpson :: Int -> Integrator module Quant.YieldCurve -- | The YieldCurve class defines the basic operations of a yield -- curve. -- -- Minimal complete definition: disc. class YieldCurve a where forward yc t1 t2 = (/ (t2 - t1)) $ log $ disc yc t1 / disc yc t2 spot yc t = forward yc 0 t disc :: (YieldCurve a, YieldCurve a) => a -> Double -> Double forward :: (YieldCurve a, YieldCurve a) => a -> Double -> Double -> Double spot :: (YieldCurve a, YieldCurve a) => a -> Double -> Double -- | A flat curve is just a flat curve with one continuously compounded -- rate at all points on the curve. data FlatCurve FlatCurve :: Double -> FlatCurve -- | YieldCurve that represents the difference between two -- YieldCurves. data NetYC a NetYC :: a -> a -> NetYC a instance YieldCurve a => YieldCurve (NetYC a) instance YieldCurve FlatCurve module Quant.VolSurf -- | The VolSurf class defines the basic operations of a volatility -- surface. -- -- Minimal complete definition: vol. class VolSurf a where var vs s t = v * v * t where v = vol vs s t localVol v s0 rcurve k t | w == 0.0 || solution < 0.0 = sqrt dwdt | otherwise = sqrt solution where dr = disc rcurve t f = s0 / dr y = log $ k / f dy = 1.0E-6 [kp, km] = [k * exp dy, k / exp dy] [w, wp, wm] = map (\ x -> var v (x / s0) t) [k, kp, km] dwdy = (wp - wm) / 2.0 / dy d2wdy2 = (wp - 2.0 * w + wm) / dy / dy dt = min 0.0001 (t / 2.0) dwdt = let strikept = k * dr / drpt strikemt = k * dr / drmt drpt = disc rcurve $ t + dt drmt = disc rcurve $ t - dt in case t of { 0 -> (var v (strikept / s0) (t + dt) - w) / dt _ -> (var v (strikept / s0) (t + dt) - var v (strikemt / s0) (t - dt)) / 2.0 / dt } solution = dwdt / (1.0 - y / w * dwdy + 0.25 * (- 0.25 - 1.0 / w + y * y / w / w) * dwdy * dwdy + 0.5 * d2wdy2) vol :: (VolSurf a, VolSurf a) => a -> Double -> Double -> Double var :: (VolSurf a, VolSurf a) => a -> Double -> Double -> Double localVol :: (VolSurf a, VolSurf a, YieldCurve b) => a -> Double -> b -> Double -> Double -> Double -- | A flat surface has one volatility at all times/maturities. data FlatSurf FlatSurf :: Double -> FlatSurf instance VolSurf FlatSurf module Quant.Models -- | The CharFunc class defines those models which have closed-form -- characteristic functions. -- -- Minimal complete definition: charFunc. -- -- Still under construction. class CharFunc a where charFuncMart model fg t k = exp (i * r * k) * baseCF k where i = 0 :+ 1 baseCF = charFunc model t r = forward fg 0 t :+ 0 charFuncOption model fg yc intF strike tmat damp = intF f where f v' = realPart $ exp (i * v * k) * leftTerm * rightTerm where v = v' :+ 0 damp' = damp :+ 0 k = log strike :+ 0 i = 0 :+ 1 leftTerm = d / (damp' + i * v) / (damp' + i * v + (1 :+ 0)) rightTerm = cf $ v - i * (damp' + 1) d = disc yc tmat :+ 0 cf x = charFuncMart model fg tmat x charFunc :: (CharFunc a, CharFunc a) => a -> Double -> Complex Double -> Complex Double charFuncMart :: (CharFunc a, CharFunc a, YieldCurve b) => a -> b -> Double -> Complex Double -> Complex Double charFuncOption :: (CharFunc a, CharFunc a, YieldCurve b, YieldCurve c) => a -> b -> c -> ((Double -> Double) -> Double) -> Double -> Double -> Double -> Double module Quant.Models.Black -- | Black represents a Black-Scholes model. data Black -- | YieldCurve to handle discounting Black :: Double -> Double -> a -> b -> Black -- | Initial asset level. blackInit :: Black -> Double -- | Volatility. blackVol :: Black -> Double -- | YieldCurve to generate forwards blackForwardGen :: Black -> a blackYieldCurve :: Black -> b instance Discretize Black module Quant.Models.Merton -- | Merton represents a Merton model (Black-Scholes w/ jumps). data Merton -- | YieldCurve to generate discount rates Merton :: Double -> Double -> Double -> Double -> Double -> a -> b -> Merton -- | Initial asset level mertonInitial :: Merton -> Double -- | Asset volatility mertonVol :: Merton -> Double -- | Intensity of Poisson process mertonIntensity :: Merton -> Double -- | Average size of jump mertonJumpMean :: Merton -> Double -- | Volatility of jumps mertonJumpVol :: Merton -> Double -- | YieldCurve to generate forwards mertonForwardGen :: Merton -> a mertonDiscounter :: Merton -> b instance Discretize Merton module Quant.Models.Dupire -- | Dupire represents a Dupire-style local vol model. data Dupire -- | YieldCurve to generate discount rates Dupire :: Double -> (Double -> Double -> Double) -> a -> b -> Dupire -- | Initial asset level dupireInitial :: Dupire -> Double -- | Local vol function taking a time to maturity and a level dupireFunc :: Dupire -> Double -> Double -> Double -- | YieldCurve to generate forwards mertonForwardGen :: Dupire -> a mertonDiscounter :: Dupire -> b instance Discretize Dupire module Quant.Models.Heston -- | Heston represents a Heston model (i.e. stochastic volatility). data Heston -- | YieldCurve to generate discounts Heston :: Double -> Double -> Double -> Double -> Double -> Double -> a -> b -> Heston -- | Initial asset level. hestonInit :: Heston -> Double -- | Initial variance hestonV0 :: Heston -> Double -- | Mean-reversion variance hestonVF :: Heston -> Double -- | Vol-vol hestonLambda :: Heston -> Double -- | Correlation between processes hestonCorrel :: Heston -> Double -- | Mean reversion speed hestonMeanRev :: Heston -> Double -- | YieldCurve to generate forwards hestonForwardGen :: Heston -> a hestonDisc :: Heston -> b instance Discretize Heston module Quant.Test baseYC :: FlatCurve val :: Double black :: Black opt :: ContingentClaim val' :: Double val'' :: Double val''' :: Double val'''' :: Double heston :: Heston opt' :: ContingentClaim opt'' :: ContingentClaim