{-# LINE 1 "./QuantLib/InterestRate.chs" #-}
module QuantLib.InterestRate
(
InterestRate
, Compounding(..)
, VolatilityType(..)
, AccrualPeriod(..)
, EquivalentPeriod(..)
, interestRate
, compoundFactor
, discountFactor
, equivalentRate
, impliedRate
, rate
) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.ForeignPtr as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import qualified System.IO.Unsafe as C2HSImp
import QuantLib.Internal
import QuantLib.Time.Schedule(Frequency)
import QuantLib.Internal.Type
{-# LINE 29 "./QuantLib/InterestRate.chs" #-}
data Compounding = Simple
| Compounded
| Continuous
| SimpleThenCompounded
| CompoundedThenSimple
deriving (Show,Eq,Read)
instance Enum Compounding where
succ :: Compounding -> Compounding
succ Compounding
Simple = Compounding
Compounded
succ Compounding
Compounded = Compounding
Continuous
succ Continuous = SimpleThenCompounded
succ SimpleThenCompounded = CompoundedThenSimple
succ Compounding
CompoundedThenSimple = String -> Compounding
forall a. HasCallStack => String -> a
error String
"Compounding.succ: CompoundedThenSimple has no successor"
pred :: Compounding -> Compounding
pred Compounding
Compounded = Compounding
Simple
pred Compounding
Continuous = Compounding
Compounded
pred SimpleThenCompounded = Continuous
pred Compounding
CompoundedThenSimple = Compounding
SimpleThenCompounded
pred Compounding
Simple = String -> Compounding
forall a. HasCallStack => String -> a
error String
"Compounding.pred: Simple has no predecessor"
enumFromTo from to = go from
where
end = fromEnum to
go v = case compare (fromEnum v) end of
LT -> v : go (succ v)
EQ -> [v]
GT -> []
enumFrom from = enumFromTo from CompoundedThenSimple
fromEnum :: Compounding -> Int
fromEnum Compounding
Simple = Int
0
fromEnum Compounding
Compounded = Int
1
fromEnum Compounding
Continuous = Int
2
fromEnum SimpleThenCompounded = 3
fromEnum Compounding
CompoundedThenSimple = Int
4
toEnum :: Int -> Compounding
toEnum Int
0 = Compounding
Simple
toEnum Int
1 = Compounding
Compounded
toEnum Int
2 = Compounding
Continuous
toEnum Int
3 = Compounding
SimpleThenCompounded
toEnum 4 = CompoundedThenSimple
toEnum unmatched = error ("Compounding.toEnum: Cannot match " ++ show unmatched)
{-# LINE 31 "./QuantLib/InterestRate.chs" #-}
data VolatilityType = ShiftedLognormal
| Normal
deriving (Enum,Show,Eq,Read)
{-# LINE 32 "./QuantLib/InterestRate.chs" #-}
data AccrualPeriod
= AccrualAtTime !Double
| AccrualBetween !Day !Day !(Maybe Day) !(Maybe Day)
deriving (Eq, Show)
data EquivalentPeriod
= EquivalentAtTime !Double
| EquivalentBetween !DayCounter !Day !Day !(Maybe Day) !(Maybe Day)
interestRate :: (Double)
-> (DayCounter) -> (Compounding) -> (Frequency) -> IO ((InterestRate))
interestRate a1 a2 a3 a4 =
let {a1' = realToFrac a1} in
withDayCounter a2 $ \a2' ->
let {a3' = (fromIntegral . fromEnum) a3} in
let {a4' = (fromIntegral . fromEnum) a4} in
preErrorCheck $ \a5' ->
interestRate'_ a1' a2' a3' a4' a5' >>= \res ->
peekInterestRate res >>= \res' ->
errorCheck a5'>>
return (res')
{-# LINE 48 "./QuantLib/InterestRate.chs" #-}
compoundFactorBetweenRaw :: (InterestRate) -> (Day)
-> (Day)
-> (Maybe Day)
-> (Maybe Day)
-> IO ((Double))
compoundFactorBetweenRaw a1 a2 a3 a4 a5 =
withInterestRate a1 $ \a1' ->
withDay a2 $ \a2' ->
withDay a3 $ \a3' ->
withMaybeDay a4 $ \a4' ->
withMaybeDay a5 $ \a5' ->
preErrorCheck $ \a6' ->
compoundFactorBetweenRaw'_ a1' a2' a3' a4' a5' a6' >>= \res ->
let {res' = realToFrac res} in
errorCheck a6'>>
return (res')
{-# LINE 56 "./QuantLib/InterestRate.chs" #-}
compoundFactorAtTimeRaw :: (InterestRate) -> (Double)
-> IO ((Double))
compoundFactorAtTimeRaw a1 a2 =
withInterestRate a1 $ \a1' ->
let {a2' = realToFrac a2} in
preErrorCheck $ \a3' ->
compoundFactorAtTimeRaw'_ a1' a2' a3' >>= \res ->
let {res' = realToFrac res} in
errorCheck a3'>>
return (res')
{-# LINE 61 "./QuantLib/InterestRate.chs" #-}
discountFactorBetweenRaw :: (InterestRate) -> (Day)
-> (Day)
-> (Maybe Day)
-> (Maybe Day)
-> IO ((Double))
discountFactorBetweenRaw a1 a2 a3 a4 a5 =
withInterestRate a1 $ \a1' ->
withDay a2 $ \a2' ->
withDay a3 $ \a3' ->
withMaybeDay a4 $ \a4' ->
withMaybeDay a5 $ \a5' ->
preErrorCheck $ \a6' ->
discountFactorBetweenRaw'_ a1' a2' a3' a4' a5' a6' >>= \res ->
let {res' = realToFrac res} in
errorCheck a6'>>
return (res')
{-# LINE 68 "./QuantLib/InterestRate.chs" #-}
discountFactorAtTimeRaw :: (InterestRate) -> (Double) -> IO ((Double))
discountFactorAtTimeRaw a1 a2 =
withInterestRate a1 $ \a1' ->
let {a2' = realToFrac a2} in
preErrorCheck $ \a3' ->
discountFactorAtTimeRaw'_ a1' a2' a3' >>= \res ->
let {res' = realToFrac res} in
errorCheck a3'>>
return (res')
{-# LINE 72 "./QuantLib/InterestRate.chs" #-}
equivalentRateBetweenRaw :: (InterestRate) -> (DayCounter)
-> (Compounding) -> (Frequency) -> (Day)
-> (Day)
-> (Maybe Day)
-> (Maybe Day)
-> IO ((InterestRate))
equivalentRateBetweenRaw :: InterestRate
-> DayCounter
-> Compounding
-> Frequency
-> Day
-> Day
-> Maybe Day
-> Maybe Day
-> IO InterestRate
equivalentRateBetweenRaw InterestRate
a1 DayCounter
a2 Compounding
a3 Frequency
a4 Day
a5 Day
a6 Maybe Day
a7 Maybe Day
a8 =
InterestRate
-> (Ptr CInterestRate -> IO InterestRate) -> IO InterestRate
forall b. InterestRate -> (Ptr CInterestRate -> IO b) -> IO b
withInterestRate InterestRate
a1 ((Ptr CInterestRate -> IO InterestRate) -> IO InterestRate)
-> (Ptr CInterestRate -> IO InterestRate) -> IO InterestRate
forall a b. (a -> b) -> a -> b
$ \Ptr CInterestRate
a1' ->
DayCounter
-> (Ptr CDayCounter -> IO InterestRate) -> IO InterestRate
forall b. DayCounter -> (Ptr CDayCounter -> IO b) -> IO b
withDayCounter DayCounter
a2 ((Ptr CDayCounter -> IO InterestRate) -> IO InterestRate)
-> (Ptr CDayCounter -> IO InterestRate) -> IO InterestRate
forall a b. (a -> b) -> a -> b
$ \Ptr CDayCounter
a2' ->
let {a3' :: CInt
a3' = (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CInt) -> (Compounding -> Int) -> Compounding -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Compounding -> Int
forall a. Enum a => a -> Int
fromEnum) Compounding
a3} in
let {a4' :: CInt
a4' = (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CInt) -> (Frequency -> Int) -> Frequency -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Frequency -> Int
forall a. Enum a => a -> Int
fromEnum) Frequency
a4} in
Day -> (CInt -> IO InterestRate) -> IO InterestRate
forall a. Day -> (CInt -> IO a) -> IO a
withDay Day
a5 ((CInt -> IO InterestRate) -> IO InterestRate)
-> (CInt -> IO InterestRate) -> IO InterestRate
forall a b. (a -> b) -> a -> b
$ \CInt
a5' ->
Day -> (CInt -> IO InterestRate) -> IO InterestRate
forall a. Day -> (CInt -> IO a) -> IO a
withDay Day
a6 ((CInt -> IO InterestRate) -> IO InterestRate)
-> (CInt -> IO InterestRate) -> IO InterestRate
forall a b. (a -> b) -> a -> b
$ \CInt
a6' ->
Maybe Day -> (CInt -> IO InterestRate) -> IO InterestRate
forall a. Maybe Day -> (CInt -> IO a) -> IO a
withMaybeDay Maybe Day
a7 ((CInt -> IO InterestRate) -> IO InterestRate)
-> (CInt -> IO InterestRate) -> IO InterestRate
forall a b. (a -> b) -> a -> b
$ \CInt
a7' ->
Maybe Day -> (CInt -> IO InterestRate) -> IO InterestRate
forall a. Maybe Day -> (CInt -> IO a) -> IO a
withMaybeDay Maybe Day
a8 ((CInt -> IO InterestRate) -> IO InterestRate)
-> (CInt -> IO InterestRate) -> IO InterestRate
forall a b. (a -> b) -> a -> b
$ \CInt
a8' ->
(Ptr (Ptr CChar) -> IO InterestRate) -> IO InterestRate
forall a b. (Ptr (Ptr a) -> IO b) -> IO b
preErrorCheck ((Ptr (Ptr CChar) -> IO InterestRate) -> IO InterestRate)
-> (Ptr (Ptr CChar) -> IO InterestRate) -> IO InterestRate
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr CChar)
a9' ->
Ptr CInterestRate
-> Ptr CDayCounter
-> CInt
-> CInt
-> CInt
-> CInt
-> CInt
-> CInt
-> Ptr (Ptr CChar)
-> IO (Ptr CInterestRate)
equivalentRateBetweenRaw'_ Ptr CInterestRate
a1' Ptr CDayCounter
a2' CInt
a3' CInt
a4' CInt
a5' CInt
a6' CInt
a7' CInt
a8' Ptr (Ptr CChar)
a9' IO (Ptr CInterestRate)
-> (Ptr CInterestRate -> IO InterestRate) -> IO InterestRate
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Ptr CInterestRate
res ->
Ptr CInterestRate -> IO InterestRate
peekInterestRate Ptr CInterestRate
res IO InterestRate
-> (InterestRate -> IO InterestRate) -> IO InterestRate
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \InterestRate
res' ->
Ptr (Ptr CChar) -> IO ()
errorCheck Ptr (Ptr CChar)
a9'IO () -> IO InterestRate -> IO InterestRate
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
InterestRate -> IO InterestRate
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (InterestRate
res')
{-# LINE 81 "./QuantLib/InterestRate.chs" #-}
equivalentRateAtTimeRaw :: (InterestRate) -> (Compounding) -> (Frequency) -> (Double)
-> IO ((InterestRate))
equivalentRateAtTimeRaw a1 a2 a3 a4 =
withInterestRate a1 $ \a1' ->
let {a2' = (fromIntegral . fromEnum) a2} in
let {a3' = (fromIntegral . fromEnum) a3} in
let {a4' = realToFrac a4} in
preErrorCheck $ \a5' ->
equivalentRateAtTimeRaw'_ a1' a2' a3' a4' a5' >>= \res ->
peekInterestRate res >>= \res' ->
errorCheck a5'>>
return (res')
{-# LINE 86 "./QuantLib/InterestRate.chs" #-}
impliedRateBetweenRaw :: (InterestRate) -> (Double)
-> (DayCounter) -> (Compounding) -> (Frequency) -> (Day)
-> (Day)
-> (Maybe Day)
-> (Maybe Day)
-> IO ((InterestRate))
impliedRateBetweenRaw a1 a2 a3 a4 a5 a6 a7 a8 a9 =
withInterestRate a1 $ \a1' ->
let {a2' = realToFrac a2} in
withDayCounter a3 $ \a3' ->
let {a4' = (fromIntegral . fromEnum) a4} in
let {a5' = (fromIntegral . fromEnum) a5} in
withDay a6 $ \a6' ->
withDay a7 $ \a7' ->
withMaybeDay a8 $ \a8' ->
withMaybeDay a9 $ \a9' ->
preErrorCheck $ \a10' ->
impliedRateBetweenRaw'_ a1' a2' a3' a4' a5' a6' a7' a8' a9' a10' >>= \res ->
peekInterestRate res >>= \res' ->
errorCheck a10'>>
return (res')
{-# LINE 95 "./QuantLib/InterestRate.chs" #-}
impliedRateAtTimeRaw :: (InterestRate) -> (Double)
-> (DayCounter) -> (Compounding) -> (Frequency) -> (Double)
-> IO ((InterestRate))
impliedRateAtTimeRaw a1 a2 a3 a4 a5 a6 =
withInterestRate a1 $ \a1' ->
let {a2' = realToFrac a2} in
withDayCounter a3 $ \a3' ->
let {a4' = (fromIntegral . fromEnum) a4} in
let {a5' = (fromIntegral . fromEnum) a5} in
let {a6' = realToFrac a6} in
preErrorCheck $ \a7' ->
impliedRateAtTimeRaw'_ a1' a2' a3' a4' a5' a6' a7' >>= \res ->
peekInterestRate res >>= \res' ->
errorCheck a7'>>
return (res')
{-# LINE 101 "./QuantLib/InterestRate.chs" #-}
rate :: (InterestRate) -> (Double)
rate a1 =
C2HSImp.unsafePerformIO $
withInterestRate a1 $ \a1' ->
rate'_ a1' >>= \res ->
let {res' = realToFrac res} in
return (res')
{-# LINE 104 "./QuantLib/InterestRate.chs" #-}
compoundFactor :: InterestRate -> AccrualPeriod -> IO Double
compoundFactor ir period = case period of
AccrualAtTime t -> compoundFactorAtTimeRaw ir t
AccrualBetween d1 d2 rs re -> compoundFactorBetweenRaw ir d1 d2 rs re
discountFactor :: InterestRate -> AccrualPeriod -> IO Double
discountFactor ir period = case period of
AccrualAtTime t -> discountFactorAtTimeRaw ir t
AccrualBetween d1 d2 rs re -> discountFactorBetweenRaw ir d1 d2 rs re
equivalentRate :: InterestRate -> Compounding -> Frequency -> EquivalentPeriod -> IO InterestRate
equivalentRate :: InterestRate
-> Compounding -> Frequency -> EquivalentPeriod -> IO InterestRate
equivalentRate InterestRate
ir Compounding
comp Frequency
freq EquivalentPeriod
period = case EquivalentPeriod
period of
EquivalentAtTime Double
t -> InterestRate
-> Compounding -> Frequency -> Double -> IO InterestRate
equivalentRateAtTimeRaw InterestRate
ir Compounding
comp Frequency
freq Double
t
EquivalentBetween DayCounter
dc Day
d1 Day
d2 Maybe Day
rs Maybe Day
re -> InterestRate
-> DayCounter
-> Compounding
-> Frequency
-> Day
-> Day
-> Maybe Day
-> Maybe Day
-> IO InterestRate
equivalentRateBetweenRaw InterestRate
ir DayCounter
dc Compounding
comp Frequency
freq Day
d1 Day
d2 Maybe Day
rs Maybe Day
re
impliedRate :: InterestRate -> Double -> DayCounter -> Compounding -> Frequency -> AccrualPeriod
-> IO InterestRate
impliedRate :: InterestRate
-> Double
-> DayCounter
-> Compounding
-> Frequency
-> AccrualPeriod
-> IO InterestRate
impliedRate InterestRate
ir Double
compound DayCounter
dc Compounding
comp Frequency
freq AccrualPeriod
period = case AccrualPeriod
period of
AccrualAtTime Double
t -> InterestRate
-> Double
-> DayCounter
-> Compounding
-> Frequency
-> Double
-> IO InterestRate
impliedRateAtTimeRaw InterestRate
ir Double
compound DayCounter
dc Compounding
comp Frequency
freq Double
t
AccrualBetween Day
d1 Day
d2 Maybe Day
rs Maybe Day
re -> InterestRate
-> Double
-> DayCounter
-> Compounding
-> Frequency
-> Day
-> Day
-> Maybe Day
-> Maybe Day
-> IO InterestRate
impliedRateBetweenRaw InterestRate
ir Double
compound DayCounter
dc Compounding
comp Frequency
freq Day
d1 Day
d2 Maybe Day
rs Maybe Day
re
foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRate"
interestRate'_ :: (C2HSImp.CDouble -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CInterestRate))))))))
foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateCompoundFactor1"
compoundFactorBetweenRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))))))
foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateCompoundFactor"
compoundFactorAtTimeRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble))))
foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateDiscountFactor1"
discountFactorBetweenRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))))))
foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateDiscountFactor"
discountFactorAtTimeRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble))))
foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateEquivalentRate1"
equivalentRateBetweenRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CInterestRate))))))))))))
foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateEquivalentRate"
equivalentRateAtTimeRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CInterestRate))))))))
foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateImpliedRate1"
impliedRateBetweenRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CInterestRate)))))))))))))
foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateImpliedRate"
impliedRateAtTimeRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CInterestRate))))))))))
foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateRate"
rate'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (IO C2HSImp.CDouble))