Skip to content

Commit 13c6862

Browse files
committed
add multipathgenerator
1 parent 52e9cd2 commit 13c6862

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from libcpp cimport bool
2+
from quantlib.handle cimport shared_ptr
3+
4+
from quantlib._time_grid cimport TimeGrid
5+
from libcpp.vector cimport vector
6+
from ._multipath cimport MultiPath
7+
from ._sample cimport Sample
8+
from quantlib._stochastic_process cimport StochasticProcess
9+
10+
cdef extern from 'ql/methods/montecarlo/multipathgenerator.hpp' namespace 'QuantLib' nogil:
11+
cdef cppclass MultiPathGenerator[GSG]:
12+
ctypedef Sample[MultiPath] sample_type
13+
MultiPathGenerator(const shared_ptr[StochasticProcess]&,
14+
const TimeGrid&,
15+
GSG generator,
16+
bool brownianBridge) # = false)
17+
const sample_type& next()
18+
const sample_type& antithetic()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from . cimport _multipathgenerator as _mpg
2+
from quantlib.math.randomnumbers._randomsequencegenerator cimport RandomSequenceGenerator
3+
from quantlib.math.randomnumbers._rngtraits cimport PseudoRandom as QlPseudoRandom, LowDiscrepancy as QlLowDiscrepancy, ZigguratPseudoRandom
4+
5+
6+
cdef class PseudoRandomMultiPathGenerator:
7+
cdef _mpg.MultiPathGenerator[QlPseudoRandom.rsg_type]* _thisptr
8+
9+
cdef class LowDiscrepancyMultiPathGenerator:
10+
cdef _mpg.MultiPathGenerator[QlLowDiscrepancy.rsg_type]* _thisptr
11+
12+
cdef class ZigguratMultiPathGenerator:
13+
cdef _mpg.MultiPathGenerator[ZigguratPseudoRandom]* _thisptr
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from cython.operator cimport dereference as deref
2+
from libcpp cimport bool
3+
from quantlib.stochastic_process cimport StochasticProcess
4+
from quantlib.time_grid cimport TimeGrid
5+
from quantlib.math.randomnumbers.rngtraits cimport PseudoRandom, LowDiscrepancy, FastPseudoRandom
6+
from ._sample cimport Sample
7+
from .multipath cimport MultiPath
8+
from . cimport _multipath as _mp
9+
10+
cdef class PseudoRandomMultiPathGenerator:
11+
def __init__(self, StochasticProcess process, TimeGrid time_grid, PseudoRandom gen, bool brownian_bridge):
12+
self._thisptr = new _mpg.MultiPathGenerator[QlPseudoRandom.rsg_type](process._thisptr, time_grid._thisptr, deref(gen._thisptr), brownian_bridge)
13+
14+
def __next__(self):
15+
cdef Sample[_mp.MultiPath]* r = <Sample[_mp.MultiPath]*>&self._thisptr.next()
16+
cdef MultiPath mp = MultiPath.__new__(MultiPath)
17+
mp._thisptr = new _mp.MultiPath(r.value)
18+
return (r.weight, mp)
19+
20+
def __dealloc__(self):
21+
del self._thisptr
22+
23+
24+
25+
cdef class LowDiscrepancyMultiPathGenerator:
26+
def __init__(self, StochasticProcess process, TimeGrid time_grid, LowDiscrepancy gen, bool brownian_bridge):
27+
self._thisptr = new _mpg.MultiPathGenerator[QlLowDiscrepancy.rsg_type](process._thisptr, time_grid._thisptr, deref(gen._thisptr), brownian_bridge)
28+
29+
def __next__(self):
30+
cdef Sample[_mp.MultiPath]* r = <Sample[_mp.MultiPath]*>&self._thisptr.next()
31+
cdef MultiPath mp = MultiPath.__new__(MultiPath)
32+
mp._thisptr = new _mp.MultiPath(r.value)
33+
return (r.weight, mp)
34+
35+
def __dealloc__(self):
36+
del self._thisptr
37+
38+
39+
cdef class ZigguratMultiPathGenerator:
40+
def __init__(self, StochasticProcess process, TimeGrid time_grid, FastPseudoRandom gen, bool brownian_bridge):
41+
self._thisptr = new _mpg.MultiPathGenerator[ZigguratPseudoRandom](process._thisptr, time_grid._thisptr, deref(gen._thisptr), brownian_bridge)
42+
43+
def __next__(self):
44+
cdef Sample[_mp.MultiPath]* r = <Sample[_mp.MultiPath]*>&self._thisptr.next()
45+
cdef MultiPath mp = MultiPath.__new__(MultiPath)
46+
mp._thisptr = new _mp.MultiPath(r.value)
47+
return (r.weight, mp)
48+
49+
def __dealloc__(self):
50+
del self._thisptr

0 commit comments

Comments
 (0)