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
8 Compounding(..)
9 , VolatilityType(..)
10
11 , InterestRate
12 , interestRate
13 , compoundFactor
14 , compoundFactor'
15 , discountFactor
16 , discountFactor'
17 , equivalentRate
18 , equivalentRate'
19 , impliedRate
20 , impliedRate'
21 , rate
22 ) where
23 import qualified Foreign.C.Types as C2HSImp
24 import qualified Foreign.ForeignPtr as C2HSImp
25 import qualified Foreign.Ptr as C2HSImp
26 import qualified System.IO.Unsafe as C2HSImp
27
28
29 import QuantLib.Internal
30 import QuantLib.Time.Schedule(Frequency)
31 import QuantLib.Internal.Type
32
33
34
35
36
37
38
39
40
41 data Compounding = Simple
42 | Compounded
43 | Continuous
44 | SimpleThenCompounded
45 | CompoundedThenSimple
46 deriving (Show,Eq,Read)
47 instance Enum Compounding where
48 succ Simple = Compounded
49 succ Compounded = Continuous
50 succ Continuous = SimpleThenCompounded
51 succ SimpleThenCompounded = CompoundedThenSimple
52 succ CompoundedThenSimple = error "Compounding.succ: CompoundedThenSimple has no successor"
53
54 pred Compounded = Simple
55 pred Continuous = Compounded
56 pred SimpleThenCompounded = Continuous
57 pred CompoundedThenSimple = SimpleThenCompounded
58 pred Simple = error "Compounding.pred: Simple has no predecessor"
59
60 enumFromTo from to = go from
61 where
62 end = fromEnum to
63 go v = case compare (fromEnum v) end of
64 LT -> v : go (succ v)
65 EQ -> [v]
66 GT -> []
67
68 enumFrom from = enumFromTo from CompoundedThenSimple
69
70 fromEnum Simple = 0
71 fromEnum Compounded = 1
72 fromEnum Continuous = 2
73 fromEnum SimpleThenCompounded = 3
74 fromEnum CompoundedThenSimple = 4
75
76 toEnum 0 = Simple
77 toEnum 1 = Compounded
78 toEnum 2 = Continuous
79 toEnum 3 = SimpleThenCompounded
80 toEnum 4 = CompoundedThenSimple
81 toEnum unmatched = error ("Compounding.toEnum: Cannot match " ++ show unmatched)
82
83
84 data VolatilityType = ShiftedLognormal
85 | Normal
86 deriving (Enum,Show,Eq,Read)
87
88
89
90 -- |construct an interest rate from a rate value, a day counter, a compounding convention and a frequency.
91 interestRate :: (Double) -- ^r
92 -> (DayCounter) -> (Compounding) -> (Frequency) -> IO ((InterestRate))
93 interestRate a1 a2 a3 a4 =
94 let {a1' = realToFrac a1} in
95 withDayCounter a2 $ \a2' ->
96 let {a3' = (fromIntegral . fromEnum) a3} in
97 let {a4' = (fromIntegral . fromEnum) a4} in
98 preErrorCheck $ \a5' ->
99 interestRate'_ a1' a2' a3' a4' a5' >>= \res ->
100 peekInterestRate res >>= \res' ->
101 errorCheck a5'>>
102 return (res')
103
104
105
106 -- |compound factor implied by the rate compounded between two dates
107 -- returns the compound (a.k.a capitalization) factor implied by the rate compounded between two dates.
108 compoundFactor' :: (InterestRate) -> (Day) -- ^d1
109 -> (Day) -- ^d2
110 -> (Day) -- ^refStart
111 -> (Day) -- ^refEnd
112 -> IO ((Double))
113 compoundFactor' a1 a2 a3 a4 a5 =
114 withInterestRate a1 $ \a1' ->
115 withDay a2 $ \a2' ->
116 withDay a3 $ \a3' ->
117 withDay a4 $ \a4' ->
118 withDay a5 $ \a5' ->
119 preErrorCheck $ \a6' ->
120 compoundFactor''_ a1' a2' a3' a4' a5' a6' >>= \res ->
121 let {res' = realToFrac res} in
122 errorCheck a6'>>
123 return (res')
124
125
126
127 -- |compound factor implied by the rate compounded at time t.
128 -- 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.
129 compoundFactor :: (InterestRate) -> (Double) -- ^t
130 -> IO ((Double))
131 compoundFactor a1 a2 =
132 withInterestRate a1 $ \a1' ->
133 let {a2' = realToFrac a2} in
134 preErrorCheck $ \a3' ->
135 compoundFactor'_ a1' a2' a3' >>= \res ->
136 let {res' = realToFrac res} in
137 errorCheck a3'>>
138 return (res')
139
140
141
142 -- |discount factor implied by the rate compounded between two dates
143 discountFactor' :: (InterestRate) -> (Day) -- ^d1
144 -> (Day) -- ^d2
145 -> (Day) -- ^refStart
146 -> (Day) -- ^refEnd
147 -> IO ((Double))
148 discountFactor' a1 a2 a3 a4 a5 =
149 withInterestRate a1 $ \a1' ->
150 withDay a2 $ \a2' ->
151 withDay a3 $ \a3' ->
152 withDay a4 $ \a4' ->
153 withDay a5 $ \a5' ->
154 preErrorCheck $ \a6' ->
155 discountFactor''_ a1' a2' a3' a4' a5' a6' >>= \res ->
156 let {res' = realToFrac res} in
157 errorCheck a6'>>
158 return (res')
159
160
161
162 -- |discount factor implied by the rate compounded at time t.
163 -- /Warning/ Time must be measured using InterestRate's own day counter.
164 discountFactor :: (InterestRate) -> (Double) -> IO ((Double))
165 discountFactor a1 a2 =
166 withInterestRate a1 $ \a1' ->
167 let {a2' = realToFrac a2} in
168 preErrorCheck $ \a3' ->
169 discountFactor'_ a1' a2' a3' >>= \res ->
170 let {res' = realToFrac res} in
171 errorCheck a3'>>
172 return (res')
173
174
175
176 -- |equivalent rate for a compounding period between two dates
177 -- The resulting rate is calculated taking the required day-counting rule into account.
178 equivalentRate' :: (InterestRate) -> (DayCounter) -- ^resultDC
179 -> (Compounding) -> (Frequency) -> (Day) -- ^d1
180 -> (Day) -- ^d2
181 -> (Day) -- ^refStart
182 -> (Day) -- ^refEnd
183 -> IO ((InterestRate))
184 equivalentRate' a1 a2 a3 a4 a5 a6 a7 a8 =
185 withInterestRate a1 $ \a1' ->
186 withDayCounter a2 $ \a2' ->
187 let {a3' = (fromIntegral . fromEnum) a3} in
188 let {a4' = (fromIntegral . fromEnum) a4} in
189 withDay a5 $ \a5' ->
190 withDay a6 $ \a6' ->
191 withDay a7 $ \a7' ->
192 withDay a8 $ \a8' ->
193 preErrorCheck $ \a9' ->
194 equivalentRate''_ a1' a2' a3' a4' a5' a6' a7' a8' a9' >>= \res ->
195 peekInterestRate res >>= \res' ->
196 errorCheck a9'>>
197 return (res')
198
199
200
201 -- |equivalent interest rate for a compounding period t.
202 -- 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.
203 equivalentRate :: (InterestRate) -> (Compounding) -> (Frequency) -> (Double) -- ^t
204 -> IO ((InterestRate))
205 equivalentRate a1 a2 a3 a4 =
206 withInterestRate a1 $ \a1' ->
207 let {a2' = (fromIntegral . fromEnum) a2} in
208 let {a3' = (fromIntegral . fromEnum) a3} in
209 let {a4' = realToFrac a4} in
210 preErrorCheck $ \a5' ->
211 equivalentRate'_ a1' a2' a3' a4' a5' >>= \res ->
212 peekInterestRate res >>= \res' ->
213 errorCheck a5'>>
214 return (res')
215
216
217
218 -- |implied rate for a given compound factor between two dates.
219 -- The resulting rate is calculated taking the required day-counting rule into account.
220 impliedRate' :: (InterestRate) -> (Double) -- ^compound
221 -> (DayCounter) -> (Compounding) -> (Frequency) -> (Day) -- ^d1
222 -> (Day) -- ^d2
223 -> (Day) -- ^refStart
224 -> (Day) -- ^refEnd
225 -> IO ((InterestRate))
226 impliedRate' a1 a2 a3 a4 a5 a6 a7 a8 a9 =
227 withInterestRate a1 $ \a1' ->
228 let {a2' = realToFrac a2} in
229 withDayCounter a3 $ \a3' ->
230 let {a4' = (fromIntegral . fromEnum) a4} in
231 let {a5' = (fromIntegral . fromEnum) a5} in
232 withDay a6 $ \a6' ->
233 withDay a7 $ \a7' ->
234 withDay a8 $ \a8' ->
235 withDay a9 $ \a9' ->
236 preErrorCheck $ \a10' ->
237 impliedRate''_ a1' a2' a3' a4' a5' a6' a7' a8' a9' a10' >>= \res ->
238 peekInterestRate res >>= \res' ->
239 errorCheck a10'>>
240 return (res')
241
242
243
244 -- |implied interest rate for a given compound factor at a given time.
245 -- The resulting InterestRate has the day-counter provided as input. /Warning/ Time must be measured using the day-counter provided as input.
246 impliedRate :: (InterestRate) -> (Double) -- ^compound
247 -> (DayCounter) -> (Compounding) -> (Frequency) -> (Double) -- ^t
248 -> IO ((InterestRate))
249 impliedRate a1 a2 a3 a4 a5 a6 =
250 withInterestRate a1 $ \a1' ->
251 let {a2' = realToFrac a2} in
252 withDayCounter a3 $ \a3' ->
253 let {a4' = (fromIntegral . fromEnum) a4} in
254 let {a5' = (fromIntegral . fromEnum) a5} in
255 let {a6' = realToFrac a6} in
256 preErrorCheck $ \a7' ->
257 impliedRate'_ a1' a2' a3' a4' a5' a6' a7' >>= \res ->
258 peekInterestRate res >>= \res' ->
259 errorCheck a7'>>
260 return (res')
261
262
263
264 -- |the rate value of an interest rate.
265 rate :: (InterestRate) -> (Double)
266 rate a1 =
267 C2HSImp.unsafePerformIO $
268 withInterestRate a1 $ \a1' ->
269 rate'_ a1' >>= \res ->
270 let {res' = realToFrac res} in
271 return (res')
272
273
274
275 -- vim: set ff=unix ts=8 sts=2 sw=2 et:
276
277 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRate"
278 interestRate'_ :: (C2HSImp.CDouble -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CInterestRate))))))))
279
280 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateCompoundFactor1"
281 compoundFactor''_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))))))
282
283 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateCompoundFactor"
284 compoundFactor'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble))))
285
286 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateDiscountFactor1"
287 discountFactor''_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble)))))))
288
289 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateDiscountFactor"
290 discountFactor'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO C2HSImp.CDouble))))
291
292 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateEquivalentRate1"
293 equivalentRate''_ :: ((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))))))))))))
294
295 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateEquivalentRate"
296 equivalentRate'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CInterestRate))))))))
297
298 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateImpliedRate1"
299 impliedRate''_ :: ((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)))))))))))))
300
301 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateImpliedRate"
302 impliedRate'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (CDayCounter)) -> (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CDouble -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> (IO (C2HSImp.Ptr (CInterestRate))))))))))
303
304 foreign import ccall safe "QuantLib/InterestRate.chs.h qlInterestRateRate"
305 rate'_ :: ((C2HSImp.Ptr (CInterestRate)) -> (IO C2HSImp.CDouble))