TimeSeriesEcon Reference
TimeSeriesEcon.TimeSeriesEcon
— ModuleTimeSeriesEcon
This package is part of the StateSpaceEcon ecosystem. Provides the data types and functionality necessary to work with macroeconomic discrete time models.
Working with time
- Frequencies are represented by abstract type
Frequency
. - Concrete frequencies include
Yearly
,Quarterly
andMonthly
. - Moments in time are represented by data type
MIT
. - Lengths of time are represented by data type
Duration
.
Working with time series
- Data type
TSeries
represents a single time series. - Data type
MVTSeries
represents a multivariate time series.
Working with other data
- Data type
Workspace
is a general purpose dictionary-like collection of "variable"-like objects.
Tutorial
TimeSeriesEcon.M1
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.M10
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.M11
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.M12
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.M2
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.M3
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.M4
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.M5
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.M6
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.M7
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.M8
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.M9
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.Q1
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.Q2
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.Q3
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.Q4
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.U
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.Y
— ConstantConvenience constants that make MIT literal constants possible. For example, the constant Q1
makes it possible to write 2020Q1
instead of MIT{Quarterly}(2020, 1)
. Use U
for MIT{Unit}
, Y
for MIT{Yearly}
, Q1
to Q4
for MIT{Quarterly}
and M1
to M12
for MIT{Monthly}
TimeSeriesEcon.Duration
— TypeMIT{F <: Frequency}, Duration{F <: Frequency}
Two types representing a moment in time (like 2020Q1 or 2020Y) and duration (the quantity of time between two moments).
Both of these have a Frequency as a type parameter and both internally are represented by integer values.
If you imagine a time axis of the given Frequency
, MIT
values are ordinal (correspond to points) while Duration
values are cardinal (correspond to distances).
TimeSeriesEcon.Frequency
— Typeabstract type Frequency end
The abstract supertype for all frequencies.
See also: Unit
and YPFrequency
TimeSeriesEcon.MIT
— TypeMIT{F <: Frequency}, Duration{F <: Frequency}
Two types representing a moment in time (like 2020Q1 or 2020Y) and duration (the quantity of time between two moments).
Both of these have a Frequency as a type parameter and both internally are represented by integer values.
If you imagine a time axis of the given Frequency
, MIT
values are ordinal (correspond to points) while Duration
values are cardinal (correspond to distances).
TimeSeriesEcon.MIT
— MethodMIT{F}(year, period) where {F <: YPFrequency}
Construct an MIT
instance from year
and period
. This is valid only for frequencies subtyped from YPFrequency
.
TimeSeriesEcon.MVTSeries
— Typemutable struct MVTSeries{F,T,C} <: AbstractMatrix{T}
firstdate::MIT{F}
columns::OrderedDict{Symbol,TSeries{F,T}}
values::C
end
Multi-variate Time series with frequency F
with values of type T
stored in a container of type C
. By default the type is Float64
and the container is Matrix{Float64}
. The rows correspond to moments in time and the columns correspond to variables. Columns are named. The values in the field columns
are TSeries
whose storages are views into the corresponding columns of the values
matrix.
Construction:
x = MVTSeries(args...)
The standard construction is MVTSeries(firstdate::MIT, names, values)
Here names
should be a tuple of Symbol
s and values
should be a matrix of the same number of columns as there are names in names
. The range of the MVTSeries
is determined from the number of rows of values
. If values
is not provided, the MVTSeries
is constructed empty with size (0, length(names))
. If
namesis also not provided, the
MVTSeriesis constructed empty with size
(0, 0)`.
The first argument can be a range. MVTSeries(range::UnitRange{<:MIT}, names, values)
In this case the size of the MVTSeries
is determined by the lengths of range
and names
; the values
argument is interpreted as an initializer. If it is omitted or set to undef
, the storage is left uninitialized. If it is a number, the storage is filled with it. It can also be an initializer function, such as zeros
, ones
or rand
. Lastly, if the values
argument is an array, it must be 2-dimensional and of the correct size.
Another possibility is to construct from a collection of name-value pairs. MVTSeries(range; var1 = val1, var2 = val2, ...)
The range
argument is optional, if missing it'll be determined from the ranges of the given values. The values can be TSeries
, vectors or constants. Any vector must have the same length as the range.
An MVTSeries
can also be constructed with copy
, similar
, and fill
.
Indexing:
Indexing with integers, integer ranges, or boolean arrays works the same as with Matrix
. The result from slicing with integer ranges or boolean arrays is always a Matrix
, i.e., the MVTSeries
structure is lost.
Indexing with two indexes works as follows. The first index can be an MIT
or a range of MIT
and it works the same as for TSeries
. The second index can be a Symbol
or a collection of Symbol
s, such as a tuple or a vector. begin
and end
work for the first index the same as with TSeries
.
Indexing with one index depends on the type. If it is MIT
or a range of MIT
, it is treated as if the second index were :
, i.e., the entire row or multiple rows is returned. If the index is a Symbol
or a collection of Symbol
s, it is treated as if the first index were :
, i.e., entire column or multiple columns is returned as TSeries
or MVTSeries
respectively.
Columns can also be accessed using "dot" notation. For example x[:a]
is the same as x.a
.
Check out the tutorial at https://bankofcanada.github.io/DocsEcon.jl/dev/Tutorials/TimeSeriesEcon/main/
TimeSeriesEcon.Monthly
— Typestruct Monthly <: YPFrequency{12} end
A concrete frequency defined as 12 periods per year.
TimeSeriesEcon.Quarterly
— Typestruct Quarterly <: YPFrequency{4} end
A concrete frequency defined as 4 periods per year.
TimeSeriesEcon.TSeries
— Typemutable struct TSeries{F, T, C} <: AbstractVector{T}
firstdate::MIT{F}
values::C
end
Time series with frequency F
and values of type T
stored in a container of type C
. By default the type is Float64
and the container is Vector{Float64}
.
Construction:
ts = TSeries(args...)
The standard construction is TSeries(firstdate::MIT, values::AbstractVector)
. If the second argument is not given, the TSeries
is constructed empty.
Alternatively, the first argument can be a range. In this case, the second argument is interpreted as an initializer. If it is omitted or set to undef
, the storage is left uninitialized. If it is a number, the storage is filled with it. It can also be an initializer function, such as zeros
, ones
or rand
. Lastly, if the second argument is an array, it must be 1-dimensional and of the same length as the range given in the first argument.
If only an integer number is given, as in TSeries(n::Integer)
, the constructed TSeries
will have frequency Unit
, first date 1U
and length n
. An initialization argument is not allowed in this case, so the storage remains uninitialized.
A TSeries
can also be constructed with copy
, similar
, and fill
, ones
, zeros
.
Indexing:
Indexing with an MIT
or a range of MIT
works as you'd expect.
Indexing with Integer
s works the same as with Vector
.
Indexing with Bool
-array works as you'd expect. For example, s[s .< 0.0] .*= -1
multiplies in place the negative entries of s
by -1, so effectively it's the same as s .= abs.(s)
.
There are important differences between indexing with MIT and not using MIT (i.e., using Integer
or Bool
-array).
with MIT-range we return a
TSeries
, otherwise we return aVector
.the range can be extended (the
TSeries
resized appropriately) by assigning outside the current range. This works only withMIT
. With anything else you get a BoundsError if you try to assign outside the Integer range.begin
andend
areMIT
, so either use both or none of them. For examples[2:end]
doesn't work because 2 is anInt
andend
is anMIT
. You should uses[begin+1:end]
.
Check out the tutorial at https://bankofcanada.github.io/DocsEcon.jl/dev/Tutorials/TimeSeriesEcon/main/
TimeSeriesEcon.Unit
— Typestruct Unit <: Frequency end
Represents a non-dimensional frequency (not associated with the calendar).
See also: Frequency
, YPFrequency
TimeSeriesEcon.Workspace
— Typestruct Workspace
…
end
A collection of variables. Workspace
s can store data of any kind, including numbers, MIT
s, ranges, strings, TSeries
, MVTSeries
, even nested Workspace
s.
Construction
Easiest is to start with and empty Workspace
and fill it up later. Otherwise, content can be provided at construction time as a collection of name-value pairs, where the name must be a Symbol
and the value can be anything.
Access
Members of the Workspace
can be accessed using "dot" notation or using []
indexing, like a dictionary.
TimeSeriesEcon.YPFrequency
— TypeTimeSeriesEcon.Yearly
— Typestruct Yearly <: YPFrequency{1} end
A concrete frequency defined as 1 period per year.
Base.diff
— Functiondiff(x::TSeries)
diff(x::TSeries, k)
Construct the first difference, or the k
-th difference, of time series t
. If y = diff(x,k)
then y[t] = x[t] - x[t+k]
. A negative value of k
means that we subtract a lag and positive value means that we subtract a lead. k
not given is the same as k=-1
, which matches the standard definition of first difference.
Base.fill
— MethodBase.pairs
— Methodpairs(data::MVTSeries; copy = false)
Returns an iterator over the named columns of data
. Each iteration gives a name-value pair where name is a Symbol
and value is a TSeries
.
Setting copy=true
is equivalent to pairs(copy(data))
but slightly more efficient.
Base.resize!
— Methodresize!(t::TSeries, n::Integer)
Extend or shrink the allocated storage for t
to n
entries. The first date of t
does not change. If allocation is extended, the new entries are set to NaN
.
Base.resize!
— Methodresize!(t::TSeries, rng)
Extend or shrink the allocated storage for t
so that the new range of t
equals the given rng
. If t
is extended, new entries are set to NaN
, or the appropriate Not-A-Number value (see typenan
).
Base.similar
— Methodsimilar(t::MVTSeries, [eltype], [shape])
similar(array, [eltype], shape)
similar(array_type, [eltype], shape)
Create an uninitialized MVTSeries
with the given element type and shape
.
If the first argument is an MVTSeries
then the element type and shape of the output will match those of the input, unless they are explicitly given in subsequent arguments. If the first argument is another array or an array type, then shape
must be given in the form of a tuple where the first element is an MIT range and the second is a list of column names. The element type, eltype
, also can be given optionally; if not given it will be deduced from the first argument.
Example:
similar(Array{Float64}, (2000Q1:2001Q4, (:a, :b)))
Base.similar
— Methodsimilar(t::TSeries, [eltype], [range])
similar(array, [eltype], range)
similar(array_type, [eltype], range)
Create an uninitialized TSeries
with the given element type and range.
If the first argument is a TSeries
then the element type and range of the output will match those of the input, unless they are explicitly given in subsequent arguments. If the first argument is another array or an array type, then range
must be given. The element type, eltype
, can be given; if not it will be deduced from the first argument.
Base.strip
— Methodstrip(t:TSeries)
Remove leading and trailing NaN
from the given time series. This version creates a new TSeries
instance.
TimeSeriesEcon.apct
— Methodapct(x::TSeries, islog::Bool)
Annualised percent rate of change in x
.
Examples
julia> x = TSeries(qq(2018, 1), Vector(1:8));
julia> apct(x)
TSeries{Quarterly} of length 7
2018Q2: 1500.0
2018Q3: 406.25
2018Q4: 216.04938271604937
2019Q1: 144.140625
2019Q2: 107.35999999999999
2019Q3: 85.26234567901243
2019Q4: 70.59558517284461
See also: pct
TimeSeriesEcon.colnames
— Methodcolnames(x::MVTSeries)
Return the names of the columns of x
as an iterable.
TimeSeriesEcon.columns
— Methodcolumns(x::MVTSeries)
Return the columns of x
as a dictionary.
TimeSeriesEcon.compare
— Function@compare x y [options] compare(x, y [; options])
Compare two Workspace
recursively and print out the differences. MVTSeries
and Dict
with keys of type Symbol
are treated like Workspace
. TSeries
and other Vector
are compared using isapprox
, so feel free to supply rtol
or atol
.
Optional argument name
can be used for the top name. Default is "_"
.
Parameter showequal=true
causes the report to include objects that are the same. Default behaviour, with showequal=false
, is to report only the differences.
Parameter ignoremissing=true
causes objects that appear in one but not the other workspace to be ignored. That is, they are not printed and do not affect the return value true
or false
. Default is ignoremissing=false
meaning they will be printed and return value will be false
.
TimeSeriesEcon.fconvert
— Methodfconvert(F, t)
Convert the time series t
to the desired frequency F
.
TimeSeriesEcon.fconvert
— Methodfconvert(F1, x::TSeries{F2}; method) where {F1 <: YPFrequency, F2 <: YPFrequency}
Convert between frequencies derived from YPFrequency
.
Currently this works only when the periods per year of the higher frequency is an exact multiple of the periods per year of the lower frequency.
Converting to Higher Frequency
The only method available is method=:const
, where the value at each period of the higher frequency is the value of the period of the lower frequency it belongs to.
x = TSeries(2000Q1:2000Q3, collect(Float64, 1:3))
fconvert(Monthly, x)
Converting to Lower Frequency
The range of the result includes periods that are fully included in the range of the input. For each period of the lower frequency we aggregate all periods of the higher frequency within it. We have 4 methods currently available: :mean
, :sum
, :begin
, and :end
. The default is :mean
.
x = TSeries(2000M1:2000M7, collect(Float64, 1:7))
fconvert(Quarterly, x; method = :sum)
TimeSeriesEcon.firstdate
— MethodTimeSeriesEcon.frequencyof
— Functionfrequencyof(x)
frequencyof(T)
Return the Frequency
type of the given value x
or type T
.
TimeSeriesEcon.istypenan
— Methodistypenan(x)
Return true
if the given x
is a not-n-number of its type, otherwise return false
.
TimeSeriesEcon.lag
— Functionlag(x::TSeries, k=1)
Shift the dates of x
by k
period to produce the k
-th lag of x
. This is the same shift(x, -k)
.
TimeSeriesEcon.lag!
— Functionlag!(x::TSeries, k=1)
In-place version of lag
TimeSeriesEcon.lastdate
— MethodTimeSeriesEcon.lead
— Functionlead(x::TSeries, k=1)
Shift the dates of x
by k
period to produce the k
-th lead of x
. This is the same shift(x, k)
.
TimeSeriesEcon.lead!
— Functionlead!(x::TSeries, k=1)
In-place version of lead
TimeSeriesEcon.mit2yp
— Functionmit2yp(x::MIT)
Recover the year and period from the given MIT
value. This is valid only for frequencies subtyped from YPFrequency
.
TimeSeriesEcon.mm
— Methodmm(year, period)
Construct an MIT{Monthly}
from an year and a period.
TimeSeriesEcon.moving
— Functionmoving(x, n)
Compute the moving average of x
over a window of n
periods. If n > 0
the window is backward-looking (-n+1:0)
and if n < 0
the window is forward-looking (0:-n-1)
.
TimeSeriesEcon.overlay
— Functionoverlay(arg1, args...)
Return the first argument, from left to right, that is valid. At least one argument must be given. Validity is determined by calling istypenan
. If it returns true
, the observation is not valid; false
means it is.
TimeSeriesEcon.overlay
— Methodoverlay([rng,] t1, t2, ...)
Construct a TSeries
in which each observation is taken from the first valid observation in the list of arguments. A valid observation is one for which istypenan
returns false
.
All TSeries
in the arguments list must be of the same frequency. The data type of the resulting TSeries
is decided by the standard promotion of numerical types in Julia. Its range is the union of the ranges of the arguments, unless the optional rng
is ginven in which case it becomes the range.
TimeSeriesEcon.overlay
— Methodoverlay(data1, data2, ...)
When overlaying Workspace
s and MVTSeries
the result is a Workspace
and each member is overlaid recursively.
TimeSeriesEcon.pct
— Functionpct(x; islog=false)
Observation-to-observation percent rate of change in x.
TimeSeriesEcon.period
— Methodperiod(mit)
Return the period of an MIT
. This is valid only for frequencies subtyped from YPFrequency
.
TimeSeriesEcon.ppy
— Functionppy(x)
ppy(T)
Return the periods per year for the frequency associated with the given value x
or type T
. This is valid only for frequencies subtyped from YPFrequency
.
TimeSeriesEcon.qq
— Methodqq(year, period)
Construct an MIT{Quarterly}
from an year and a period.
TimeSeriesEcon.rangeof
— FunctionTimeSeriesEcon.rangeof
— Methodrangeof(s; drop::Integer)
Return the stored range of s
adjusted by dropping drop
periods. If drop
is positive, we drop from the beginning and if drop
is negative we drop from the end. This adds convenience when using @rec
Example
julia> q = TSeries(20Q1:21Q4);
julia> rangeof(q; drop=1)
20Q2:21Q4
julia> rangeof(q; drop=-4)
20Q1:20Q4
julia> q[begin:begin+1] .= 1;
julia> @rec rangeof(q; drop=2) q[t] = q[t-1] + q[t-2];
julia> q
8-element TSeries{Quarterly} with range 20Q1:21Q4:
20Q1 : 1.0
20Q2 : 1.0
20Q3 : 2.0
20Q4 : 3.0
21Q1 : 5.0
21Q2 : 8.0
21Q3 : 13.0
21Q4 : 21.0
TimeSeriesEcon.rangeof
— MethodTimeSeriesEcon.rawdata
— MethodTimeSeriesEcon.reindex
— Functionreindex(ts, from => to; copy = false)
reindex(w, from => to; copy = false)
The function reindex
re-indexes the TSeries
or MVTSeries
ts
or those contained in the Workspace
w
so that the MIT
from
becomes the MIT
to
leaving the data unchanged. For a Workspace
, only the TSeries
with the same frequency as the first element of the pair will be reindexed.
By default, the data is not copied.
Example: With a TSeries
or an MVTSeries
ts = MVTSeries(2020Q1,(:y1,:y2),randn(10,2))
ts2 = reindex(ts,2021Q1 => 1U; copy = true)
ts2.y2[3U] = 9999
ts
ts2
With a Workspace
w = Workspace();
w.a = TSeries(2020Q1,randn(10))
w.b = TSeries(2021Q1,randn(10))
w.c = 1
w.d = "string"
w1 = reindex(w, 2021Q1 => 1U)
w2 = reindex(w, 2021Q1 => 1U; copy = true)
w.a[2020Q1] = 9999
MVTSeries(; w1_a = w1.a, w2_a = w2.a)
TimeSeriesEcon.shift!
— Methodshift!(x::TSeries, n)
In-place version of shift
.
TimeSeriesEcon.shift
— Methodshift(x::TSeries, n)
Shift the dates of x
by n
periods. By convention positive n
gives the lead and negative n
gives the lag. shift
creates a new TSeries
and copies the data over. See shift!
for in-place version.
For example:
julia> shift(TSeries(2020Q1, 1:4), 1)
TSeries{Quarterly} of length 4
2019Q4: 1.0
2020Q1: 2.0
2020Q2: 3.0
2020Q3: 4.0
julia> shift(TSeries(2020Q1, 1:4), -1)
TSeries{Quarterly} of length 4
2020Q2: 1.0
2020Q3: 2.0
2020Q4: 3.0
2021Q1: 4.0
TimeSeriesEcon.strip!
— Methodstrip!(t::TSeries)
Remove leading and training NaN
from the given time series. This is done in-place.
TimeSeriesEcon.strip!
— Methodstrip!(w::Workspace; recursive=true)
Apply strip!
to all TSeries members of the given workspace. This includes nested workspaces, unless recursive=false
.
TimeSeriesEcon.typenan
— Functiontypenan(x)
typenan(T)
Return a value that indicates not-a-number of the same type as the given x
or of the given type T
.
For floating point types, this is NaN
. For integer types, we use typemax()
. This is not ideal, but it'll do for now.
TimeSeriesEcon.undiff
— Functionundiff(dvar, [date => value])
undiff!(var, dvar; fromdate=firstdate(dvar)-1)
Inverse of diff
, i.e. var
remains unchanged under undiff!(var, diff(var))
or undiff(diff(var), firstdate(var)=>first(var))
. This is the same as cumsum
, but specific to time series.
In the case of undiff
the second argument is an "anchor" Pair
specifying a known value at some time period. Typically this will be the period just before the first date of dvar
, but doesn't have to be. If the date falls outside the rangeof(dvar)
we extend dvar with zeros as necessary. If missing, this argument defaults to firstdate(dvar)-1 => 0
.
In the case of undiff!
, the var
argument provides the "anchor" value and the storage location for the result. The fromdate
parameter specifies the date of the "anchor" and the anchor value is taken from var
. See important note below.
The in-place version (undiff!
) works only with TSeries
. The other version (undiff
) works with MVTSeries
as well as TSeries
. In the case of MVTSeries
the anchor value
must be a Vector
, or a Martix
with 1 row, of the same length as the number of columns of dvar
.
In the case of undiff!
the meaning of parameter fromdate
is different from the meaning of date
in the second argument of undiff
. This only matters if fromdate
falls somewhere in the middle of the range of dvar
.
In the case of undiff!
, all values of dvar
at, and prior to, fromdate
are ignored (considered zero). Effectively, values of var
up to, and including, fromdate
remain unchanged.
By contrast, in undiff
with date => value
somewhere in the middle of the range of dvar
, the operation is applied over the full range of dvar
, both before and after date
, and then the result is adjusted by adding or subtracting a constant such that in the end we have result[date]=value
.
TimeSeriesEcon.undiff!
— Functionundiff(dvar, [date => value])
undiff!(var, dvar; fromdate=firstdate(dvar)-1)
Inverse of diff
, i.e. var
remains unchanged under undiff!(var, diff(var))
or undiff(diff(var), firstdate(var)=>first(var))
. This is the same as cumsum
, but specific to time series.
In the case of undiff
the second argument is an "anchor" Pair
specifying a known value at some time period. Typically this will be the period just before the first date of dvar
, but doesn't have to be. If the date falls outside the rangeof(dvar)
we extend dvar with zeros as necessary. If missing, this argument defaults to firstdate(dvar)-1 => 0
.
In the case of undiff!
, the var
argument provides the "anchor" value and the storage location for the result. The fromdate
parameter specifies the date of the "anchor" and the anchor value is taken from var
. See important note below.
The in-place version (undiff!
) works only with TSeries
. The other version (undiff
) works with MVTSeries
as well as TSeries
. In the case of MVTSeries
the anchor value
must be a Vector
, or a Martix
with 1 row, of the same length as the number of columns of dvar
.
In the case of undiff!
the meaning of parameter fromdate
is different from the meaning of date
in the second argument of undiff
. This only matters if fromdate
falls somewhere in the middle of the range of dvar
.
In the case of undiff!
, all values of dvar
at, and prior to, fromdate
are ignored (considered zero). Effectively, values of var
up to, and including, fromdate
remain unchanged.
By contrast, in undiff
with date => value
somewhere in the middle of the range of dvar
, the operation is applied over the full range of dvar
, both before and after date
, and then the result is adjusted by adding or subtracting a constant such that in the end we have result[date]=value
.
TimeSeriesEcon.year
— Methodyear(mit)
Return the year of an MIT
. This is valid only for frequencies subtyped from YPFrequency
.
TimeSeriesEcon.ytypct
— Methodytypct(x)
Year-to-year percent change in x.
TimeSeriesEcon.yy
— Functionyy(year, period)
Construct an MIT{Yearly}
from an year and a period.
TimeSeriesEcon.@compare
— Macro@compare x y [options] compare(x, y [; options])
Compare two Workspace
recursively and print out the differences. MVTSeries
and Dict
with keys of type Symbol
are treated like Workspace
. TSeries
and other Vector
are compared using isapprox
, so feel free to supply rtol
or atol
.
Optional argument name
can be used for the top name. Default is "_"
.
Parameter showequal=true
causes the report to include objects that are the same. Default behaviour, with showequal=false
, is to report only the differences.
Parameter ignoremissing=true
causes objects that appear in one but not the other workspace to be ignored. That is, they are not printed and do not affect the return value true
or false
. Default is ignoremissing=false
meaning they will be printed and return value will be false
.
TimeSeriesEcon.@rec
— Macro@rec [index=]range expression
Compute recursive operations on time series. The first argument is the range and the second argument is an expression to be evaluated over that range.
The expression is meant to be an assignment, but it doesn't have to be.
The range specification can include an optional indexing variable name. If not given, the variable name defaults to t
.
Examples
julia> s = TSeries(1U)
Empty TSeries{Unit} starting 5U
julia> s[1U] = s[2U] = 1; s
2-element TSeries{Unit} with range 1U:2U:
1U : 1.0
2U : 1.0
julia> @rec t=3U:10U s[t] = s[t-1] + s[t-2]
julia> s
10-element TSeries{Unit} with range 1U:10U:
1U : 1.0
2U : 1.0
3U : 2.0
4U : 3.0
5U : 5.0
6U : 8.0
7U : 13.0
8U : 21.0
9U : 34.0
10U : 55.0
TimeSeriesEcon.@showall
— Macro@showall X
Print all data in X
without truncating the output to fit the size of the screen.