module QuantLib.Methods.MonteCarlo
( module QuantLib.Methods.MonteCarlo
) where
import Control.Monad()
import Control.Parallel.Strategies
import QuantLib.Stochastic.Process
import QuantLib.Stochastic.Random
class PathPricer p => Summary m p | m->p where
sSummarize :: m->[p]->m
sNorm :: m->m->Double
class PathGenerator m where
pgMkNew :: m->IO m
pgGenerate :: m->IO Path
class PathPricer m where
ppPrice :: m->Path->m
monteCarlo :: (Summary s p, PathPricer p, PathGenerator g) => PathMonteCarlo s p g->Int->IO s
monteCarlo (PathMonteCarlo s p g) size = do
priced <- mapM (\_ -> pricing) [1..size]
return $ sSummarize s priced
where pricing = do
!path <- pgGenerate g
return $! ppPrice p path
monteCarloParallel :: (Summary s p, PathPricer p, PathGenerator g) => PathMonteCarlo s p g->Int->IO s
monteCarloParallel (PathMonteCarlo s p g) size = do
priced <- mapM (\_ -> pricing) [1..size] `using` rpar
return $ sSummarize s priced
where pricing = do
!path <- pgGenerate g
return $! ppPrice p path
data PathMonteCarlo s p g =
PathMonteCarlo {
pmcSummary :: s,
pmcPricer :: p,
pmcGenerator :: g
}
data LastPointPricer = LastPointPricer Dot
instance PathPricer LastPointPricer where
ppPrice _ path = LastPointPricer (last path)
data ProcessGenerator sp b d =
ProcessGenerator {
pgStart :: Dot,
pgLength :: Int,
pgProcess :: sp,
pgGenerator :: b,
pgDiscretize :: d
}
instance (StochasticProcess sp, NormalGenerator b, Discretize d) => PathGenerator (ProcessGenerator sp b d) where
pgMkNew (ProcessGenerator start len process rnd d) = do
newRnd <- ngMkNew rnd
return $! ProcessGenerator start len process newRnd d
pgGenerate (ProcessGenerator start len sp b d) = generatePath b d sp len start