cointoss.series¶
series
¶
Time-versioned Series over lythonic value types.
Implements ADR-0003. lythonic owns the values -- Universe, ExposureMatrix, KeyedVector --
and cointoss owns time. A Series is a system of record: the truth about what a scope or a
classification was on a date. That is the distinction from a Snapshot, which is a
materialized cache of something derivable and is never truth.
Pure: no network, no clock, no database. Axes are plain strings; in production they hold Instrument Ids, but nothing here imports or validates them.
Two rules are worth stating before the code, because both are deliberate irritants:
as_of is a step function and a date before the first entry raises. Returning an empty value
would make an un-started Series indistinguishable from an empty one, and every aggregate
downstream would then compute over nothing and report zero.
Entries hold whole values, never diffs. A diff is derivable, whereas storing diffs makes every read a fold from the beginning and lets one corrupted early diff poison every later date.
SUM_TOLERANCE = 1e-09
module-attribute
¶
How far a declared sum-to-one row may drift before it is a violation.
SeriesError
¶
NotYetStarted
¶
Bases: SeriesError
A date precedes the first entry, so the Series has nothing to say about it.
EntryOrder
¶
Bases: SeriesError
Entries are out of date order, or two of them claim one date.
TargetOutsideAxis
¶
Bases: SeriesError
An entry carries a Target the Series did not declare.
TargetAxisMismatch
¶
Bases: SeriesError
Two matrices or Series do not share a Target axis.
RowSumViolation
¶
Bases: SeriesError
A row breaks a declared expectation that Exposures sum to one.
SemanticsMismatch
¶
Bases: SeriesError
Two Series measure different things and must not be combined.
MissingPrice
¶
Bases: SeriesError
A position was held in a classified subject with no price to value it at.
ExposureSemantics
¶
Bases: StrEnum
What the numbers in an Exposure Series mean.
lythonic's matrix claims nothing about its values, so the meaning lives here.
Source code in src/cointoss/series.py
DatedUniverse
¶
Bases: BaseModel
One whole Universe, in force from its date until the next entry.
revision is the Revision Stamp (ADR-0007): the Universe Definition revision the entry was
produced under. None for a Series built by hand rather than evaluated from a Definition.
Source code in src/cointoss/series.py
DatedMatrix
¶
Bases: BaseModel
One whole ExposureMatrix, in force from its date until the next entry.
Source code in src/cointoss/series.py
UniverseChange
¶
Bases: BaseModel
What joined and what left a Universe Series between two dates.
Source code in src/cointoss/series.py
MatrixChange
¶
Bases: BaseModel
One cell whose Exposure differs between two dates. before/after are cell values.
Source code in src/cointoss/series.py
UniverseSeries
¶
Bases: BaseModel
A Series of instrument lists: the scope of data gathering and research over time.
Construction may be rule-based or hand-curated; the Series records the outcome either way, so nothing downstream branches on provenance.
>>> s = UniverseSeries(name="midcap").append(date(2024, 1, 1), ["AAPL", "MSFT"])
>>> s = s.append(date(2024, 4, 1), ["AAPL", "NVDA"])
>>> list(s.as_of(date(2024, 2, 15)))
['AAPL', 'MSFT']
>>> s.change(date(2024, 1, 1), date(2024, 4, 1))
UniverseChange(admitted=('NVDA',), dropped=('MSFT',))
Source code in src/cointoss/series.py
dates()
¶
as_of(when)
¶
The Universe in force on when. A date before the first entry raises.
append(when, universe, *, revision=None)
¶
A new Series with one more entry, or this one if membership did not change.
Series are immutable. An unchanged membership is not an entry: dates are the dates the
scope changed, and the store records no interval rows for a quiet evaluation, so the
fold must not either. Membership is compared as a set, so reordering is not a change.
An earlier date is refused before that comparison, so a misordered replay fails even
when it would have changed nothing.
revision stamps the new entry. A different revision alone is not a change either: the
stamp records which recipe produced the membership in force, so an edit that leaves the
membership as it was keeps the stamp of the entry that produced it.
Source code in src/cointoss/series.py
change(start, end)
¶
What joined and what left between two dates. Derived, never stored.
Source code in src/cointoss/series.py
ExposureSeries
¶
Bases: BaseModel
A named Series of exposure matrices over a fixed Target axis.
The Target axis is declared once and fixed for the life of the Series, so a Target means the same thing at every date and a cross-time comparison compares like with like. Where a genuinely new Target appears -- a revised sector taxonomy, say -- the right move is a new Series, because that is a different classification rather than a new date in the old one.
The Subject axis is free to grow, since instruments enter and leave scope constantly.
>>> from lythonic.exposure import ExposureMatrixBuilder
>>> b = ExposureMatrixBuilder(targets=["tech", "energy"])
>>> b.set_exposure("AAPL", "tech", 1.0)
>>> s = ExposureSeries(
... name="sector", targets=["tech", "energy"], semantics=ExposureSemantics.PERCENT
... ).append(date(2024, 1, 1), b.build())
>>> s.as_of(date(2024, 6, 1)).exposure("AAPL", "tech")
1.0
Source code in src/cointoss/series.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
dates()
¶
as_of(when)
¶
The ExposureMatrix in force on when. A date before the first entry raises.
append(when, matrix)
¶
A new Series with one more entry, cast onto the declared Target axis.
Casting rather than merely checking is what makes every entry share one axis, so two dates can be compared cell by cell without realigning them first.
Source code in src/cointoss/series.py
trace(subject, target)
¶
One subject's Exposure to one Target at every date the Series holds.
The Subject axis grows, so a subject is absent from the entries that predate it. Those dates read as the matrix's own cell fill rather than raising, because a subject outside the classification has no exposure, which is a value rather than an error.
Source code in src/cointoss/series.py
change(start, end)
¶
Every cell whose Exposure differs between two dates. Derived, never stored.
A cell absent from either side reads as that matrix's own fill, so a Series whose fill is not zero does not report a phantom change against zero.
Source code in src/cointoss/series.py
require_comparable(other)
¶
Refuse two Series that cannot legitimately be combined.
Exposure Semantics exists to stop a currency value being added to a percentage, and a shared Target axis is what makes two cells the same cell. Every operation that puts two Series together must call this first; none exists yet, and this is the check the first one will owe.
Source code in src/cointoss/series.py
cast_to(scope, when)
¶
The matrix in force on when, restricted to the scope in force on the same date.
Both sides are read at one date, which is the point: aligning a classification to a scope from another date would compare different worlds.
Source code in src/cointoss/series.py
portfolio_exposure(positions, prices, when)
¶
Valued exposure per Target, computed from positions at when.
Never stored on a position. The classification used is the one in force at the date being valued, not the latest one -- a historical valuation uses its own era's view.
Source code in src/cointoss/series.py
series_covering(subject, series, when)
¶
Names of the Series that classify subject on when, in the order given.
An instrument advertises which classifications exist for it; it never owns them. This is a lookup across Series rather than a field on the instrument, so changing a classification touches no instrument.