packages feed

quantfin 0.1.0.0 → 0.1.0.1

raw patch · 4 files changed

+41/−8 lines, 4 files

Files

quantfin.cabal view
@@ -10,7 +10,7 @@ -- PVP summary:      +-+------- breaking API changes
 --                   | | +----- non-breaking API additions
 --                   | | | +--- code changes with no API change
-version:             0.1.0.0
+version:             0.1.0.1
 
 -- A short (one-line) description of the package.
 synopsis:            Quant finance library in pure Haskell.
src/Quant/ContingentClaim.hs view
@@ -36,10 +36,13 @@ -- | 'ContingentClaim'' is the underlying type of contingent claims.
 data ContingentClaim' = ContingentClaim' {
     payoutTime   :: Double                               -- ^ Payout time for cash flow
-  , collector    :: [U.Vector Double] -> U.Vector Double -- ^ Function to collect observations and transform them into a cash flow.
-  , observations :: [( Double                            -- ^ Time of observation
-                     , Observables  -> U.Vector Double   -- ^ Function to access specific observable.
-                     , Double       -> Double) ]         -- ^ Function to run to transform observations.
+  , collector    :: [U.Vector Double] -> U.Vector Double 
+  , observations :: [( Double                            
+                     , Observables  -> U.Vector Double   
+                     , Double       -> Double) ]         {- ^ List containing:
+                                                         -- Time of observation, 
+                                                         -- Function to access specific observable, 
+                                                         -- Function to collect observations and transform them into a cash flow. -}
 }
 
 -- | 'ContingentClaim' is just a list of the underlying 'ContingentClaim''s.
src/Quant/Models/Black.hs view
@@ -8,7 +8,6 @@ 
 import Quant.YieldCurve
 import Data.Random
-import Quant.Models
 import Control.Monad.State
 import Quant.MonteCarlo
 import Quant.ContingentClaim
src/Quant/VolSurf.hs view
@@ -4,6 +4,8 @@ ) where
 
 
+import Quant.YieldCurve
+
 {- | The 'VolSurf' class defines the
 basic operations of a volatility surface.
 
@@ -18,8 +20,37 @@     var vs s t = v*v*t
         where v = vol vs s t
 
--- |A flat curve is just a flat curve with one continuously 
--- compounded rate at all points on the curve.
+    -- | Calculates Dupire local vol for a given strike/maturity/forward generating yield curve.
+    localVol :: (VolSurf a, YieldCurve b) => a       -- ^ Volatility surface
+                                          -> Double  -- ^ Initial stock price
+                                          -> b       -- ^ 'YieldCurve' to generate forwards
+                                          -> Double  -- ^ Current stock level
+                                          -> Double  -- ^ Time
+                                          -> Double  -- ^ Local volatility
+    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)
+
+-- |A flat surface has one volatility at all times/maturities.
 data FlatSurf = FlatSurf Double
 
 instance VolSurf FlatSurf where