qablet-basic 0.4.6__cp38-cp38-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- qablet/__init__.py +0 -0
- qablet/_qablet.cp38-win_amd64.pyd +0 -0
- qablet/base/__init__.py +0 -0
- qablet/base/base.py +70 -0
- qablet/base/cf.py +67 -0
- qablet/base/fixed.py +13 -0
- qablet/base/flags.py +8 -0
- qablet/base/mc.py +38 -0
- qablet/black_scholes/__init__.py +0 -0
- qablet/black_scholes/fd.py +17 -0
- qablet/hullwhite/fd.py +17 -0
- qablet/local_vol/mc.py +17 -0
- qablet_basic-0.4.6.dist-info/METADATA +5 -0
- qablet_basic-0.4.6.dist-info/RECORD +15 -0
- qablet_basic-0.4.6.dist-info/WHEEL +4 -0
qablet/__init__.py
ADDED
|
File without changes
|
|
Binary file
|
qablet/base/__init__.py
ADDED
|
File without changes
|
qablet/base/base.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Define Base Class for Models
|
|
2
|
+
from abc import ABC, abstractmethod
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
|
|
5
|
+
from pyarrow import RecordBatch as rb
|
|
6
|
+
from qablet_contracts.timetable import TS_EVENT_SCHEMA, py_to_ts
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def convert_to_arrow(timetable, dataset):
|
|
10
|
+
"""If required, convert timetable and dataset to arrow format."""
|
|
11
|
+
if isinstance(dataset["PRICING_TS"], datetime):
|
|
12
|
+
dataset = dataset.copy() # shallow copy
|
|
13
|
+
dataset["PRICING_TS"] = py_to_ts(dataset["PRICING_TS"]).value
|
|
14
|
+
if isinstance(timetable["events"], list):
|
|
15
|
+
timetable = timetable.copy() # shallow copy
|
|
16
|
+
timetable["events"] = rb.from_pylist(
|
|
17
|
+
timetable["events"], schema=TS_EVENT_SCHEMA
|
|
18
|
+
)
|
|
19
|
+
return timetable, dataset
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# Define Base Class for State Object for all Models
|
|
23
|
+
class ModelStateBase(ABC):
|
|
24
|
+
"""Class to maintain the state during a model execution."""
|
|
25
|
+
|
|
26
|
+
stats: dict = {}
|
|
27
|
+
|
|
28
|
+
def __init__(self, timetable, dataset):
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
def set_stat(self, key: str, val):
|
|
32
|
+
self.stats[key] = val
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class Model(ABC):
|
|
36
|
+
"""Base class for all models."""
|
|
37
|
+
|
|
38
|
+
@abstractmethod
|
|
39
|
+
def state_class(self):
|
|
40
|
+
"""The class that maintains state for this model."""
|
|
41
|
+
...
|
|
42
|
+
|
|
43
|
+
@abstractmethod
|
|
44
|
+
def price_method(self):
|
|
45
|
+
"""The method that calculates price."""
|
|
46
|
+
...
|
|
47
|
+
|
|
48
|
+
def price(self, timetable, dataset):
|
|
49
|
+
"""Calculate price of contract.
|
|
50
|
+
|
|
51
|
+
Parameters:
|
|
52
|
+
timetable (dict): timetable for the contract.
|
|
53
|
+
dataset (dict): dataset for the model.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
price (float): price of contract
|
|
57
|
+
stats (dict): stats such as standard error
|
|
58
|
+
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
timetable, dataset = convert_to_arrow(timetable, dataset)
|
|
62
|
+
model_state = (self.state_class())(timetable, dataset)
|
|
63
|
+
price = self.price_method()(
|
|
64
|
+
timetable["events"],
|
|
65
|
+
model_state,
|
|
66
|
+
dataset,
|
|
67
|
+
timetable.get("expressions", {}),
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
return price, model_state.stats
|
qablet/base/cf.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Define Base Class for Cashflow Models
|
|
2
|
+
from abc import ABC, abstractmethod
|
|
3
|
+
|
|
4
|
+
import polars as pl
|
|
5
|
+
|
|
6
|
+
from .._qablet import backtest_csv, backtest_py
|
|
7
|
+
|
|
8
|
+
MS_IN_DAY = 1000 * 3600 * 24
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class CFModelPyBase(ABC):
|
|
12
|
+
"""Base class for all Cashflow models delegating data to py class."""
|
|
13
|
+
|
|
14
|
+
def __init__(self, base):
|
|
15
|
+
self.base = base
|
|
16
|
+
self.stats = {}
|
|
17
|
+
|
|
18
|
+
def set_stat(self, key: str, val):
|
|
19
|
+
self.stats[key] = val
|
|
20
|
+
|
|
21
|
+
@abstractmethod
|
|
22
|
+
def get_value(self, unit, ts):
|
|
23
|
+
"""Return value for given unit on given ts."""
|
|
24
|
+
...
|
|
25
|
+
|
|
26
|
+
def cashflow(self, timetable):
|
|
27
|
+
backtest_py(
|
|
28
|
+
timetable["events"],
|
|
29
|
+
self,
|
|
30
|
+
timetable.get("expressions", {}),
|
|
31
|
+
self.base,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
return self.stats["CASHFLOW"][0].sort_by("index")
|
|
35
|
+
|
|
36
|
+
def cashflow_by_ts(self, timetable):
|
|
37
|
+
cf = self.cashflow(timetable)
|
|
38
|
+
|
|
39
|
+
# convert to polars dataframe
|
|
40
|
+
cf_df = pl.from_arrow(cf)
|
|
41
|
+
ts_col = pl.from_arrow(
|
|
42
|
+
timetable["events"]["time"], schema={"time": pl.Int64}
|
|
43
|
+
)[cf_df["index"]]
|
|
44
|
+
return cf_df.with_columns(ts=ts_col)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class CFModelCSV:
|
|
48
|
+
"""A cashflow model using backtest_csv (currently trivially implemented)"""
|
|
49
|
+
|
|
50
|
+
def __init__(self, filename, base):
|
|
51
|
+
self.base = base
|
|
52
|
+
self.filename = filename
|
|
53
|
+
self.stats = {}
|
|
54
|
+
|
|
55
|
+
def set_stat(self, key: str, val):
|
|
56
|
+
self.stats[key] = val
|
|
57
|
+
|
|
58
|
+
def cashflow(self, timetable):
|
|
59
|
+
backtest_csv(
|
|
60
|
+
timetable["events"],
|
|
61
|
+
self,
|
|
62
|
+
self.filename,
|
|
63
|
+
self.base,
|
|
64
|
+
timetable.get("expressions", {}),
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
return self.stats["CASHFLOW"][0].sort_by("index")
|
qablet/base/fixed.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Define the fixed model
|
|
2
|
+
|
|
3
|
+
from .._qablet import fixed_price
|
|
4
|
+
from .base import Model, ModelStateBase
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# Define a determinitic Model that just uses forwards
|
|
8
|
+
class FixedModel(Model):
|
|
9
|
+
def state_class(self):
|
|
10
|
+
return ModelStateBase
|
|
11
|
+
|
|
12
|
+
def price_method(self):
|
|
13
|
+
return fixed_price
|
qablet/base/flags.py
ADDED
qablet/base/mc.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Generic MC model
|
|
2
|
+
|
|
3
|
+
from abc import ABC
|
|
4
|
+
|
|
5
|
+
from .._qablet import mc_price
|
|
6
|
+
from .base import convert_to_arrow
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class MCPricer(ABC):
|
|
10
|
+
"""MC Pricer that uses a Py Model."""
|
|
11
|
+
|
|
12
|
+
def __init__(self, state_class):
|
|
13
|
+
self.state_class = state_class
|
|
14
|
+
|
|
15
|
+
def price(self, timetable, dataset):
|
|
16
|
+
"""Calculate price of contract.
|
|
17
|
+
|
|
18
|
+
Parameters:
|
|
19
|
+
timetable (dict): timetable for the contract.
|
|
20
|
+
dataset (dict): dataset for the model.
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
price (float): price of contract
|
|
24
|
+
stats (dict): stats such as standard error
|
|
25
|
+
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
timetable, dataset = convert_to_arrow(timetable, dataset)
|
|
29
|
+
model_state = self.state_class(dataset)
|
|
30
|
+
model_state.reset()
|
|
31
|
+
price = mc_price(
|
|
32
|
+
timetable["events"],
|
|
33
|
+
model_state,
|
|
34
|
+
dataset,
|
|
35
|
+
timetable.get("expressions", {}),
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
return price, model_state.stats
|
|
File without changes
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Black Scholes model using finite difference method
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
from qablet.base.base import Model, ModelStateBase
|
|
5
|
+
|
|
6
|
+
from .._qablet import fd_blackscholes_price
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Define the Model Class
|
|
10
|
+
class BSFDModel(Model):
|
|
11
|
+
__PARAM_SCHEMA_NAME__ = "BS"
|
|
12
|
+
|
|
13
|
+
def state_class(self):
|
|
14
|
+
return ModelStateBase
|
|
15
|
+
|
|
16
|
+
def price_method(self):
|
|
17
|
+
return fd_blackscholes_price
|
qablet/hullwhite/fd.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Hullwhite model using finite difference method
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
from qablet.base.base import Model, ModelStateBase
|
|
5
|
+
|
|
6
|
+
from .._qablet import fd_hullwhite_price
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Define the Model Class
|
|
10
|
+
class HWFDModel(Model):
|
|
11
|
+
__PARAM_SCHEMA_NAME__ = "HW"
|
|
12
|
+
|
|
13
|
+
def state_class(self):
|
|
14
|
+
return ModelStateBase
|
|
15
|
+
|
|
16
|
+
def price_method(self):
|
|
17
|
+
return fd_hullwhite_price
|
qablet/local_vol/mc.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Local Vol model using Monte Carlo method
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
from qablet.base.base import Model, ModelStateBase
|
|
5
|
+
|
|
6
|
+
from .. import _qablet
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Define the Model Class
|
|
10
|
+
class LVMCModel(Model):
|
|
11
|
+
__PARAM_SCHEMA_NAME__ = "LV"
|
|
12
|
+
|
|
13
|
+
def state_class(self):
|
|
14
|
+
return ModelStateBase
|
|
15
|
+
|
|
16
|
+
def price_method(self):
|
|
17
|
+
return _qablet.mc_lv_price
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
qablet_basic-0.4.6.dist-info/METADATA,sha256=ZxwkCg4qExZ1rLOxxZ9G0yyrVYjbZ6jGdR3boW4SON8,215
|
|
2
|
+
qablet_basic-0.4.6.dist-info/WHEEL,sha256=dmlmVrg43J3mJzkGmG6YeG3qWwtCluTVS5kljaN-U-M,94
|
|
3
|
+
qablet/base/base.py,sha256=HTjZILjcZAXXgfJDKojYEJpIAq61aDIBLRuMrwemQPg,2065
|
|
4
|
+
qablet/base/cf.py,sha256=rer-7y82frVX4w1b30lowCsAlNiqQAQUq0ub6-e9SsE,1754
|
|
5
|
+
qablet/base/fixed.py,sha256=whP8JoNtAMFvWigKJsQ0suhSr9pwvBk3JI9g9vx_E6Q,307
|
|
6
|
+
qablet/base/flags.py,sha256=GQ1d1ProTJh4ivcG2DK_kmKkiTAyd4iyg2rAGpZt0XM,121
|
|
7
|
+
qablet/base/mc.py,sha256=8ShapO6Ym7hO6msn6llzoETobMhV_I5YSnz_h6HqiAo,975
|
|
8
|
+
qablet/base/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
qablet/black_scholes/fd.py,sha256=nLP4rAALKav2uQjDQtEVaskvQoQt1MVuh7NEzYysJRU,376
|
|
10
|
+
qablet/black_scholes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
+
qablet/hullwhite/fd.py,sha256=9RqUMnbQMUx6U5eaWb-RYQNpdu-FF7oBUCa31904Wg0,366
|
|
12
|
+
qablet/local_vol/mc.py,sha256=Isf5fUd7Md_8ZoNAbIP6jA7isDFOfRFpsUEuijBrrEQ,343
|
|
13
|
+
qablet/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
14
|
+
qablet/_qablet.cp38-win_amd64.pyd,sha256=Y14V4qWMcEc3YtPSIpnYATtXQlfCjZyQ8K5WYp5s3rw,1639424
|
|
15
|
+
qablet_basic-0.4.6.dist-info/RECORD,,
|