MIT and MITRange¶
Moment-in-time scalars and ranges. MIT carries its frequency on the type; arithmetic
between two MITs yields a Duration, while MIT + Duration (or MIT + int as a
shorthand) yields a new MIT. MITRange is the inclusive analogue of Julia's
first:last range.
tsecon.mit¶
tsecon.mit ¶
MIT (moment-in-time) and Duration types.
Mirrors TimeSeriesEcon.jl's momentintime.jl. An :class:MIT carries a
frequency and an integer value; :class:Duration is the same shape but
represents a distance between two MITs of the same frequency.
MIT - MIT→Duration(same frequency required).MIT - Duration→MIT.MIT + Duration→MIT.MIT + MITraises (semantically meaningless).- Adding/subtracting a plain
intis allowed and interpreted as a duration in the MIT's own frequency.
Constructor functions :func:qq, :func:mm, :func:yy, :func:daily,
:func:bdaily, :func:weekly are the Pythonic equivalent of the Julia
literal-suffix syntax 2020Q1, 2020M3, 2020Y, etc.
Equality with plain integers is not supported (unlike Julia, where
MIT(Quarterly, 5) == 5 is true). The Julia idiom violates Python's
__hash__/__eq__ invariant; use int(mit) or mit.value to
extract the underlying integer instead.
BDailyBias
module-attribute
¶
One of "strict", "previous", "next", "nearest".
Determines how :func:bdaily resolves a date that falls on a weekend.
"strict" (the default, matching Julia) raises rather than guessing.
MIT
dataclass
¶
A frequency-tagged moment in time.
Construct via :meth:from_yp for year/period frequencies, or via the
convenience constructors :func:qq, :func:mm, :func:yy, :func:daily,
:func:bdaily, :func:weekly. Direct MIT(frequency, value)
construction takes a raw integer offset.
Source code in src/tsecon/mit.py
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 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 | |
from_yp
classmethod
¶
Construct an MIT from year+period (only for YP frequencies).
Source code in src/tsecon/mit.py
to ¶
Return the MITRange from self through stop (inclusive).
Mirrors Julia's start:stop syntax for MITs. Python's slice/colon
cannot be overloaded outside of indexing, so this method is the
idiomatic spelling. The free function :func:tsecon.mitrange.mitrange
also works.
Source code in src/tsecon/mit.py
Duration
dataclass
¶
A frequency-tagged distance between two MITs.
Source code in src/tsecon/mit.py
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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
mit2yp ¶
Recover (year, period) from a YP- or Daily-/BDaily-frequency MIT.
For YPFrequency subclasses this is the canonical decomposition
int(mit) == N * year + (period - 1). For :class:Daily and
:class:BDaily it returns the year and the 1-based index of the day
within that year.
Source code in src/tsecon/mit.py
year ¶
period ¶
frequency_of ¶
Return the frequency of an MIT, Duration, or other frequency-bearing value.
Mirrors Julia's frequencyof. Raises TypeError for values that
don't carry a frequency.
Source code in src/tsecon/mit.py
qq ¶
mm ¶
yy ¶
daily ¶
bdaily ¶
Construct an MIT{BDaily} from a date or ISO string.
Business-daily MITs index Monday-Friday only. bias controls how a
date that lands on a weekend is resolved:
"strict"— raiseValueError."previous"— return the preceding Friday."next"— return the following Monday."nearest"— Saturday → Friday, Sunday → Monday.
When bias is not supplied, the value falls back to the global option
bdaily_creation_bias (default "strict"). Mirrors Julia's
bdaily(d::Date; bias::Symbol=getoption(:bdaily_creation_bias)).
Source code in src/tsecon/mit.py
weekly ¶
Construct an MIT{Weekly(end_day)} from a date or ISO string.
The resulting MIT identifies the week (ending on the given end_day)
that contains the provided date.
Source code in src/tsecon/mit.py
weekly_from_iso ¶
Construct an MIT{Weekly(7)} from an ISO year + week number (1..53).
Source code in src/tsecon/mit.py
mit_to_date ¶
Convert an MIT to a :class:datetime.date.
For multi-day frequencies (Yearly, HalfYearly, Quarterly, Monthly,
Weekly) ref controls whether the end of the period (default) or
its beginning (ref="begin") is returned.
Source code in src/tsecon/mit.py
tsecon.mitrange¶
tsecon.mitrange ¶
MITRange — an inclusive integer range of frequency-tagged moments in time.
Mirrors Julia's UnitRange{MIT{F}} and StepRange{MIT{F}}. A range is
defined by (start, stop, step) where start and stop are
:class:~tsecon.mit.MIT values of the same frequency and step is a
nonzero integer number of periods (or a :class:~tsecon.mit.Duration of the
same frequency). step may be negative — MITRange(10U, 1U, -1) walks
backward, mirroring Julia's 10U:-1:1U. Ranges whose endpoints do not
match the sign of step (e.g. MITRange(1U, 10U, -1)) are empty.
MITRange supports len, bidirectional iteration, indexing (by int
or MIT), slicing, and membership testing.
MITRange
dataclass
¶
An inclusive range of MITs of a single frequency.
Construct with MITRange(start, stop) for unit steps, or
MITRange(start, stop, step) for a step range. step may be a
nonzero int (interpreted in the range's frequency) or a
:class:Duration of the same frequency. Negative step walks
backward (MITRange(10U, 1U, -1) mirrors Julia's 10U:-1:1U);
ranges whose endpoints have the opposite sense to step are empty.
Source code in src/tsecon/mitrange.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 | |
is_empty ¶
first ¶
last ¶
Return the last MIT in the range. Raises if empty.
For a forward range (step > 0) this is the largest MIT not past
stop; for a reversed range (step < 0) it is the smallest
MIT not past stop. In both cases, last() == start + (len-1) * step.
Source code in src/tsecon/mitrange.py
mitrange ¶
Functional constructor for :class:MITRange.
Mirrors Julia's start:stop and start:step:stop colon syntax.
step may be an int or a :class:Duration of the same frequency.
Source code in src/tsecon/mitrange.py
rangeof ¶
rangeof(
obj: object,
*,
drop: int = 0,
method: Literal["intersect", "union"] = "intersect",
) -> MITRange
Return the range of obj, optionally dropping leading/trailing periods.
Mirrors Julia's rangeof(t; drop=n) and rangeof(w; method=intersect)
(see TimeSeriesEcon.jl/src/workspaces.jl
and src/tseries.jl). The Python equivalents — :attr:TSeries.range /
:attr:MVTSeries.range properties, :meth:Workspace.rangeof,
:func:rangeof_span — remain available; this function unifies them under
a single kwarg-bearing call site that mirrors the Julia surface 1-to-1 and
is the recommended target for line-by-line tutorial / model code ports.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
object
|
A :class: |
required |
drop
|
int
|
Skip |
0
|
method
|
Literal['intersect', 'union']
|
|
'intersect'
|
Returns:
| Type | Description |
|---|---|
MITRange
|
The (possibly shrunk) range. |
Raises:
| Type | Description |
|---|---|
TypeError
|
Unsupported |
ValueError
|
|
Examples:
>>> import tsecon as ts
>>> a = ts.TSeries(ts.qq(2020, 1), [1.0, 2.0, 3.0, 4.0])
>>> ts.rangeof(a)
2020Q1:2020Q4
>>> ts.rangeof(a, drop=1)
2020Q2:2020Q4
>>> ts.rangeof(a, drop=-1)
2020Q1:2020Q3
The tutorial-1 @rec idiom ports line-by-line::
# Julia: @rec rangeof(a, drop=1) a[t] = (1-ρ)*a_ss + ρ*a[t-1]
ts.rec(ts.rangeof(a, drop=1), a, lambda t: (1 - rho) * a_ss + rho * a[t - 1])
Workspace intersection vs union:
>>> w = ts.Workspace(a=a, b=ts.TSeries(ts.qq(2020, 3), [10.0, 20.0]))
>>> ts.rangeof(w)
2020Q3:2020Q4
>>> ts.rangeof(w, method="union")
2020Q1:2020Q4
Source code in src/tsecon/mitrange.py
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 | |
rangeof_span ¶
Return the smallest forward MITRange covering all argument ranges.
All arguments must share a single frequency. Raises TypeError if
frequencies are mixed. The returned span is always forward-stepped
(step=1) regardless of the direction of the inputs.