Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Monte Carlo methods are implemented and partially tested.
Consume too much of memory now.
- Loading branch information
Pavel
committed
Apr 11, 2011
1 parent
4707d00
commit 7b0eced
Showing
5 changed files
with
84 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#/usr/bash | ||
|
||
cabal clean | ||
cabal configure | ||
cabal build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-} | ||
module Main where | ||
|
||
import Control.Monad | ||
import qualified Data.Map as M | ||
import QuantLib.Methods.MonteCarlo | ||
import QuantLib.Stochastic | ||
import GSL.Random.Gen | ||
import Data.List | ||
|
||
data MaxMinClosePricer = MMCP { | ||
mmcpHigh :: Double, | ||
mmcpLow :: Double, | ||
mmcpClose :: Double | ||
} deriving (Show) | ||
|
||
instance PathPricer MaxMinClosePricer where | ||
ppPrice _ path = MMCP high low close | ||
where close = last xs | ||
high = maximum xs | ||
low = minimum xs | ||
xs = map getX path | ||
|
||
data HistoSummary = HS (M.Map Double Int) | ||
deriving (Show) | ||
|
||
addOnePath :: HistoSummary->MaxMinClosePricer->HistoSummary | ||
addOnePath (HS m) (MMCP _ _ close) = HS newM | ||
where (_, newM) = M.insertLookupWithKey inserter roundedClose 1 m | ||
roundedClose = ((fromIntegral . round) (close*10000))/10000 | ||
inserter _ new_value old_value = old_value+new_value | ||
|
||
instance Summary HistoSummary MaxMinClosePricer where | ||
sNorm _ _ = 0.0 -- we don't care about convergence now | ||
sSummarize m paths = foldl' addOnePath m paths | ||
|
||
printMap :: HistoSummary->IO () | ||
printMap (HS m) = do | ||
forM_ list printPlain | ||
where | ||
printPlain (a, b) = do | ||
putStrLn $ (show a)++","++(show b) | ||
list = M.toList m | ||
|
||
main :: IO () | ||
main = do | ||
let summary = HS M.empty | ||
let mmcp = MMCP 0.0 0.0 0.0 | ||
let start = Dot 0.0 1.0 | ||
let sp = GeometricBrownian 0.0 1.0 | ||
let discrete= Euler 0.01 | ||
rng <- newRNG mt19937 | ||
let generator=createNormalGen rng | ||
let pg = ProcessGenerator start 100 sp generator discrete | ||
let pmc = PathMonteCarlo summary mmcp pg | ||
s <- monteCarlo pmc 10000 | ||
printMap s |