Engine¶
Correlation-aware risk/cash position optimizer (the "Basanos" engine).
tinycta.engine
¶
Engine for correlation-aware risk position optimization.
Engine
dataclass
¶
Correlation-aware risk position optimizer (Basanos engine).
Source code in src/tinycta/engine.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
assets
property
¶
List numeric asset column names, excluding the date column.
cash_position
property
¶
Correlation-shrinkage-optimized cash positions for each timestamp.
Walks forward through time, and at each timestamp t:
- Mask assets with a finite price at
tso the optimisation only sees currently-tradable instruments. - Shrink the EWMA correlation matrix towards the identity by
cfg.shrink(via :func:~tinycta.signal.shrink2id) for numerical stability, then restrict it to the masked assets. - Solve the shrunk system for the expected returns
muand normalise byinv_a_norm(mu, matrix)so the raw risk position has unit norm under the correlation metric (zeroed when the denominator is non-finite/degenerate ormuis all-zero). - Scale the risk position by a running EWMA estimate of realised
profit variance (decay
lamb=0.99), which down-weights positions after volatile P&L, then divide by per-asset EWMA volatility (self.vola) to convert the risk position into a cash position.
Returns:
| Type | Description |
|---|---|
DataFrame
|
pl.DataFrame: The input |
Example
import polars as pl from tinycta.config import Config from tinycta.engine import Engine prices = pl.DataFrame({"date": [1, 2, 3], "A": [100.0, 101.0, 102.0]}) mu = pl.DataFrame({"date": [1, 2, 3], "A": [0.0, 0.1, 0.2]}) engine = Engine(prices=prices, mu=mu, cfg=Config(vola=2, corr=2, clip=4.2, shrink=0.5)) positions = engine.cash_position
cor
property
¶
Per-timestamp EWMA correlation matrices, keyed by index value.
Each key is a value of the date column (a datetime.date in normal
use, but any hashable index value such as an integer is supported, hence
the Hashable key type). Each value is the EWMA covariance matrix at
that timestamp normalised to a correlation matrix (unit diagonal).
Contract
- Warmup: the first
cfg.corrtimestamps are omitted — a key exists only once at least one matrix cell is finite (see :func:~tinycta.ewm_cov.ewm_covariance). - NaN cells: a cell is
NaNwhile either asset is still in its own warmup, and a zero-variance asset (outer == 0) yieldsNaNcorrelations rather than a divide-by-zero.
ret_adj
property
¶
Per-asset EWMA-volatility-adjusted log returns clipped by cfg.clip.
vola
property
¶
Per-asset EWMA volatility of percentage returns.
__post_init__()
¶
Validate that prices and mu are aligned and both contain a date column.