never executed always true always false
    1 -- GENERATED by C->Haskell Compiler, version 0.28.8 Switcheroo, 25 November 2017 (Haskell)
    2 -- Edit the ORIGNAL .chs file instead!
    3 
    4 
    5 module QuantLib.InterestRate
    6   (
    7     -- * Types
    8     InterestRate
    9   , Compounding(..)
   10   , VolatilityType(..)
   11   , AccrualPeriod(..)
   12   , EquivalentPeriod(..)
   13 
   14     -- * Constructors
   15   , interestRate
   16 
   17     -- * Inspectors
   18   , compoundFactor
   19   , discountFactor
   20   , discountFactorSecondDerivative
   21   , discountFactorFirstDerivative
   22   , equivalentRate
   23   , impliedRate
   24   , rate
   25   ) where
   26 import qualified Foreign.C.Types as C2HSImp
   27 import qualified Foreign.ForeignPtr as C2HSImp
   28 import qualified Foreign.Ptr as C2HSImp
   29 import qualified System.IO.Unsafe as C2HSImp
   30 
   31 
   32 import QuantLib.Internal
   33 import QuantLib.Time.Schedule(Frequency)
   34 import QuantLib.Internal.Type
   35 
   36 
   37 
   38 
   39 
   40 
   41 
   42 
   43 
   44 data Compounding = Simple
   45                  | Compounded
   46                  | Continuous
   47                  | SimpleThenCompounded
   48                  | CompoundedThenSimple
   49   deriving (Show,Eq,Read)
   50 instance Enum Compounding where
   51   succ Simple = Compounded
   52   succ Compounded = Continuous
   53   succ Continuous = SimpleThenCompounded
   54   succ SimpleThenCompounded = CompoundedThenSimple
   55   succ CompoundedThenSimple = error "Compounding.succ: CompoundedThenSimple has no successor"
   56 
   57   pred Compounded = Simple
   58   pred Continuous = Compounded
   59   pred SimpleThenCompounded = Continuous
   60   pred CompoundedThenSimple = SimpleThenCompounded
   61   pred Simple = error "Compounding.pred: Simple has no predecessor"
   62 
   63   enumFromTo from to = go from
   64     where
   65       end = fromEnum to
   66       go v = case compare (fromEnum v) end of
   67                  LT -> v : go (succ v)
   68                  EQ -> [v]
   69                  GT -> []
   70 
   71   enumFrom from = enumFromTo from CompoundedThenSimple
   72 
   73   fromEnum Simple = 0
   74   fromEnum Compounded = 1
   75   fromEnum Continuous = 2
   76   fromEnum SimpleThenCompounded = 3
   77   fromEnum CompoundedThenSimple = 4
   78 
   79   toEnum 0 = Simple
   80   toEnum 1 = Compounded
   81   toEnum 2 = Continuous
   82   toEnum 3 = SimpleThenCompounded
   83   toEnum 4 = CompoundedThenSimple
   84   toEnum unmatched = error ("Compounding.toEnum: Cannot match " ++ show unmatched)
   85 
   86 
   87 data VolatilityType = ShiftedLognormal
   88                     | Normal
   89   deriving (Enum,Show,Eq,Read)
   90 
   91 
   92 
   93 -- |A compounding period: a year fraction in the rate's own day counter, or a date range.
   94 data AccrualPeriod
   95   = AccrualAtTime !Double
   96   | AccrualBetween !Day !Day !(Maybe Day) !(Maybe Day) -- ^d1, d2, refStart, refEnd
   97   deriving (Eq, Show)
   98 
   99 -- |As 'AccrualPeriod', for 'equivalentRate': only the date form takes a result day counter,
  100 -- the time form keeps the rate's own.
  101 data EquivalentPeriod
  102   = EquivalentAtTime !Double
  103   | EquivalentBetween !DayCounter !Day !Day !(Maybe Day) !(Maybe Day) -- ^resultDC, d1, d2, refStart, refEnd
  104 
  105 -- |construct an interest rate from a rate value, a day counter, a compounding convention and a frequency.
  106 interestRate :: (Double) -- ^r
  107  -> (DayCounter) -> (Compounding) -> (Frequency) -> IO ((InterestRate))
  108 interestRate a1 a2 a3 a4 =
  109   let {a1' = realToFrac a1} in 
  110   withDayCounter a2 $ \a2' -> 
  111   let {a3' = (fromIntegral . fromEnum) a3} in 
  112   let {a4' = (fromIntegral . fromEnum) a4} in 
  113   preErrorCheck $ \a5' -> 
  114   interestRate'_ a1' a2' a3' a4' a5' >>= \res ->
  115   peekInterestRate res >>= \res' ->
  116   errorCheck  a5'>>
  117   return (res')
  118 
  119 
  120 
  121 -- |compound factor implied by the rate compounded between two dates
  122 -- returns the compound (a.k.a capitalization) factor implied by the rate compounded between two dates.
  123 compoundFactorBetweenRaw :: (InterestRate) -> (Day) -- ^d1
  124  -> (Day) -- ^d2
  125  -> (Maybe Day) -- ^refStart
  126  -> (Maybe Day) -- ^refEnd
  127  -> IO ((Double))
  128 compoundFactorBetweenRaw a1 a2 a3 a4 a5 =
  129   withInterestRate a1 $ \a1' -> 
  130   withDay a2 $ \a2' -> 
  131   withDay a3 $ \a3' -> 
  132   withMaybeDay a4 $ \a4' -> 
  133   withMaybeDay a5 $ \a5' -> 
  134   preErrorCheck $ \a6' -> 
  135   compoundFactorBetweenRaw'_ a1' a2' a3' a4' a5' a6' >>= \res ->
  136   let {res' = realToFrac res} in
  137   errorCheck  a6'>>
  138   return (res')
  139 
  140 
  141 
  142 -- |compound factor implied by the rate compounded at time t.
  143 -- returns the compound (a.k.a capitalization) factor implied by the rate compounded at time t. /Warning/ Time must be measured using InterestRate's own day counter.
  144 compoundFactorAtTimeRaw :: (InterestRate) -> (Double) -- ^t
  145  -> IO ((Double))
  146 compoundFactorAtTimeRaw a1 a2 =
  147   withInterestRate a1 $ \a1' -> 
  148   let {a2' = realToFrac a2} in 
  149   preErrorCheck $ \a3' -> 
  150   compoundFactorAtTimeRaw'_ a1' a2' a3' >>= \res ->
  151   let {res' = realToFrac res} in
  152   errorCheck  a3'>>
  153   return (res')
  154 
  155 
  156 
  157 -- |discount factor implied by the rate compounded between two dates
  158 discountFactorBetweenRaw :: (InterestRate) -> (Day) -- ^d1
  159  -> (Day) -- ^d2
  160  -> (Maybe Day) -- ^refStart
  161  -> (Maybe Day) -- ^refEnd
  162  -> IO ((Double))
  163 discountFactorBetweenRaw a1 a2 a3 a4 a5 =
  164   withInterestRate a1 $ \a1' -> 
  165   withDay a2 $ \a2' -> 
  166   withDay a3 $ \a3' -> 
  167   withMaybeDay a4 $ \a4' -> 
  168   withMaybeDay a5 $ \a5' -> 
  169   preErrorCheck $ \a6' -> 
  170   discountFactorBetweenRaw'_ a1' a2' a3' a4' a5' a6' >>= \res ->
  171   let {res' = realToFrac res} in
  172   errorCheck  a6'>>
  173   return (res')
  174 
  175 
  176 
  177 -- |discount factor implied by the rate compounded at time t.
  178 -- /Warning/ Time must be measured using InterestRate's own day counter.
  179 discountFactorAtTimeRaw :: (InterestRate) -> (Double) -> IO ((Double))
  180 discountFactorAtTimeRaw a1 a2 =
  181   withInterestRate a1 $ \a1' -> 
  182   let {a2' = realToFrac a2} in 
  183   preErrorCheck $ \a3' -> 
  184   discountFactorAtTimeRaw'_ a1' a2' a3' >>= \res ->
  185   let {res' = realToFrac res} in
  186   errorCheck  a3'>>
  187   return (res')
  188 
  189 
  190 
  191 -- |equivalent rate for a compounding period between two dates
  192 -- The resulting rate is calculated taking the required day-counting rule into account.
  193 equivalentRateBetweenRaw :: (InterestRate) -> (DayCounter) -- ^resultDC
  194  -> (Compounding) -> (Frequency) -> (Day) -- ^d1
  195  -> (Day) -- ^d2
  196  -> (Maybe Day) -- ^refStart
  197  -> (Maybe Day) -- ^refEnd
  198  -> IO ((InterestRate))
  199 equivalentRateBetweenRaw a1 a2 a3 a4 a5 a6 a7 a8 =
  200   withInterestRate a1 $ \a1' -> 
  201   withDayCounter a2 $ \a2' -> 
  202   let {a3' = (fromIntegral . fromEnum) a3} in 
  203   let {a4' = (fromIntegral . fromEnum) a4} in 
  204   withDay a5 $ \a5' -> 
  205   withDay a6 $ \a6' -> 
  206   withMaybeDay a7 $ \a7' -> 
  207   withMaybeDay a8 $ \a8' -> 
  208   preErrorCheck $ \a9' -> 
  209   equivalentRateBetweenRaw'_ a1' a2' a3' a4' a5' a6' a7' a8' a9' >>= \res ->
  210   peekInterestRate res >>= \res' ->
  211   errorCheck  a9'>>
  212   return (res')
  213 
  214 
  215 
  216 -- |equivalent interest rate for a compounding period t.
  217 -- The resulting InterestRate shares the same implicit day-counting rule of the original InterestRate instance. /Warning/ Time must be measured using the InterestRate's own day counter.
  218 equivalentRateAtTimeRaw :: (InterestRate) -> (Compounding) -> (Frequency) -> (Double) -- ^t
  219  -> IO ((InterestRate))
  220 equivalentRateAtTimeRaw a1 a2 a3 a4 =
  221   withInterestRate a1 $ \a1' -> 
  222   let {a2' = (fromIntegral . fromEnum) a2} in 
  223   let {a3' = (fromIntegral . fromEnum) a3} in 
  224   let {a4' = realToFrac a4} in 
  225   preErrorCheck $ \a5' -> 
  226   equivalentRateAtTimeRaw'_ a1' a2' a3' a4' a5' >>= \res ->
  227   peekInterestRate res >>= \res' ->
  228   errorCheck  a5'>>
  229   return (res')
  230 
  231 
  232 
  233 -- |implied rate for a given compound factor between two dates.
  234 -- The resulting rate is calculated taking the required day-counting rule into account.
  235 impliedRateBetweenRaw :: (InterestRate) -> (Double) -- ^compound
  236  -> (DayCounter) -> (Compounding) -> (Frequency) -> (Day) -- ^d1
  237  -> (Day) -- ^d2
  238  -> (Maybe Day) -- ^refStart
  239  -> (Maybe Day) -- ^refEnd
  240  -> IO ((InterestRate))
  241 impliedRateBetweenRaw a1 a2 a3 a4 a5 a6 a7 a8 a9 =
  242   withInterestRate a1 $ \a1' -> 
  243   let {a2' = realToFrac a2} in 
  244   withDayCounter a3 $ \a3' -> 
  245   let {a4' = (fromIntegral . fromEnum) a4} in 
  246   let {a5' = (fromIntegral . fromEnum) a5} in 
  247   withDay a6 $ \a6' -> 
  248   withDay a7 $ \a7' -> 
  249   withMaybeDay a8 $ \a8' -> 
  250   withMaybeDay a9 $ \a9' -> 
  251   preErrorCheck $ \a10' -> 
  252   impliedRateBetweenRaw'_ a1' a2' a3' a4' a5' a6' a7' a8' a9' a10' >>= \res ->
  253   peekInterestRate res >>= \res' ->
  254   errorCheck  a10'>>
  255   return (res')
  256 
  257 
  258 
  259 -- |implied interest rate for a given compound factor at a given time.
  260 -- The resulting InterestRate has the day-counter provided as input. /Warning/ Time must be measured using the day-counter provided as input.
  261 impliedRateAtTimeRaw :: (InterestRate) -> (Double) -- ^compound
  262  -> (DayCounter) -> (Compounding) -> (Frequency) -> (Double) -- ^t
  263  -> IO ((InterestRate))
  264 impliedRateAtTimeRaw a1 a2 a3 a4 a5 a6 =
  265   withInterestRate a1 $ \a1' -> 
  266   let {a2' = realToFrac a2} in 
  267   withDayCounter a3 $ \a3' -> 
  268   let {a4' = (fromIntegral . fromEnum) a4} in 
  269   let {a5' = (fromIntegral . fromEnum) a5} in 
  270   let {a6' = realToFrac a6} in 
  271   preErrorCheck $ \a7' -> 
  272   impliedRateAtTimeRaw'_ a1' a2' a3' a4' a5' a6' a7' >>= \res ->
  273   peekInterestRate res >>= \res' ->
  274   errorCheck  a7'>>
  275   return (res')
  276 
  277 
  278 
  279 -- |the rate value of an interest rate.
  280 rate :: (InterestRate) -> (Double)
  281 rate a1 =
  282   C2HSImp.unsafePerformIO $
  283   withInterestRate a1 $ \a1' -> 
  284   rate'_ a1' >>= \res ->
  285   let {res' = realToFrac res} in
  286   return (res')
  287 
  288 
  289 
  290 -- |Compound (capitalization) factor implied by the rate over the given period.
  291 -- Both supported QuantLib versions reject 'SimpleThenCompounded' and 'CompoundedThenSimple';
  292 -- use "QuantLib.CashFlow" calculations for these conventions.
  293 compoundFactor :: InterestRate -> AccrualPeriod -> IO Double
  294 compoundFactor ir period = case period of
  295   AccrualAtTime t -> compoundFactorAtTimeRaw ir t
  296   AccrualBetween d1 d2 rs re -> compoundFactorBetweenRaw ir d1 d2 rs re
  297 
  298 -- |Discount factor implied by the rate over the given period.
  299 -- Both supported QuantLib versions reject 'SimpleThenCompounded' and 'CompoundedThenSimple';
  300 -- use "QuantLib.CashFlow" calculations for these conventions.
  301 discountFactor :: InterestRate -> AccrualPeriod -> IO Double
  302 discountFactor ir period = case period of
  303   AccrualAtTime t -> discountFactorAtTimeRaw ir t
  304   AccrualBetween d1 d2 rs re -> discountFactorBetweenRaw ir d1 d2 rs re
  305 
  306 -- |Equivalent rate under a different compounding and frequency over the given period.
  307 -- Both supported QuantLib versions reject mixed source compounding and non-unit factors with
  308 -- mixed result compounding; use "QuantLib.CashFlow" for these conventions.
  309 equivalentRate :: InterestRate -> Compounding -> Frequency -> EquivalentPeriod -> IO InterestRate
  310 equivalentRate ir comp freq period = case period of
  311   EquivalentAtTime t -> equivalentRateAtTimeRaw ir comp freq t
  312   EquivalentBetween dc d1 d2 rs re -> equivalentRateBetweenRaw ir dc comp freq d1 d2 rs re
  313 
  314 -- |Rate implied by a given compound factor over the given period, in the supplied day counter.
  315 -- Both supported QuantLib versions reject mixed result compounding for non-unit compound factors;
  316 -- use "QuantLib.CashFlow" calculations for 'SimpleThenCompounded' and 'CompoundedThenSimple'.
  317 impliedRate :: InterestRate -> Double -> DayCounter -> Compounding -> Frequency -> AccrualPeriod
  318   -> IO InterestRate
  319 impliedRate ir compound dc comp freq period = case period of
  320   AccrualAtTime t -> impliedRateAtTimeRaw ir compound dc comp freq t
  321   AccrualBetween d1 d2 rs re -> impliedRateBetweenRaw ir compound dc comp freq d1 d2 rs re
  322 
  323 -- vim: set ff=unix ts=8 sts=2 sw=2 et:
  324 
  325 -- |First derivative of the discount factor with respect to the rate at year fraction @t@.
  326 -- Requires QuantLib 1.44.
  327 discountFactorFirstDerivative :: (InterestRate) -- ^rate
  328  -> (Double) -- ^t
  329  -> IO ((Double))
  330 discountFactorFirstDerivative a1 a2 =
  331   withInterestRate a1 $ \a1' -> 
  332   let {a2' = realToFrac a2} in 
  333   preErrorCheck $ \a3' -> 
  334   discountFactorFirstDerivative'_ a1' a2' a3' >>= \res ->
  335   let {res' = realToFrac res} in
  336   errorCheck  a3'>>
  337   return (res')
  338 
  339 
  340 
  341 -- |Second derivative of the discount factor with respect to the rate at year fraction @t@.
  342 -- Requires QuantLib 1.44.
  343 discountFactorSecondDerivative :: (InterestRate) -- ^rate
  344  -> (Double) -- ^t
  345  -> IO ((Double))
  346 discountFactorSecondDerivative a1 a2 =
  347   withInterestRate a1 $ \a1' -> 
  348   let {a2' = realToFrac a2} in 
  349   preErrorCheck $ \a3' -> 
  350   discountFactorSecondDerivative'_ a1' a2' a3' >>= \res ->
  351   let {res' = realToFrac res} in
  352   errorCheck  a3'>>
  353   return (res')
  354 
  355 
  356 
  357 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRate"
  358   interestRate'_ :: (C2HSImp.CDouble -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO (C2HSImp.Ptr (CInterestRate))))))))
  359 
  360 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateCompoundFactor1"
  361   compoundFactorBetweenRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO C2HSImp.CDouble)))))))
  362 
  363 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateCompoundFactor"
  364   compoundFactorAtTimeRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO C2HSImp.CDouble))))
  365 
  366 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateDiscountFactor1"
  367   discountFactorBetweenRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO C2HSImp.CDouble)))))))
  368 
  369 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateDiscountFactor"
  370   discountFactorAtTimeRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO C2HSImp.CDouble))))
  371 
  372 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateEquivalentRate1"
  373   equivalentRateBetweenRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO (C2HSImp.Ptr (CInterestRate))))))))))))
  374 
  375 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateEquivalentRate"
  376   equivalentRateAtTimeRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO (C2HSImp.Ptr (CInterestRate))))))))
  377 
  378 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateImpliedRate1"
  379   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 ())) -> (IO (C2HSImp.Ptr (CInterestRate)))))))))))))
  380 
  381 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateImpliedRate"
  382   impliedRateAtTimeRaw'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO (C2HSImp.Ptr (CInterestRate))))))))))
  383 
  384 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateRate"
  385   rate'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (IO C2HSImp.CDouble))
  386 
  387 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateDiscountFactorFirstDerivative"
  388   discountFactorFirstDerivative'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO C2HSImp.CDouble))))
  389 
  390 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateDiscountFactorSecondDerivative"
  391   discountFactorSecondDerivative'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO C2HSImp.CDouble))))