NbbTools > Use cases

Time series creation.

Simple time series can be created following two distinct ways. The first one prioritizes the view of a time series as a collection of couples "date/value", the second one as an association between a continuous time domain and an array of values with the same length.

The first way is better suited for the creation of a time series whose domain is not well known (undefined frequency or length, ...); it also allows the building of a time series with observations at irregular intervals. The second way supposes that the domain is well known; in that case the creation process can be greatly optimized.

Creation through a TSDataCollector

A TSDataCollector is an object designed to collect any couple of "Date/Value". At any moment, it can create a time series, given a frequency (that can be "Unknown"), and an aggregation type (None, Sum, Average, Last, ...)., that defines the way to aggregate the values, if necessary. The resulting time series will contain missing values if there are some "holes" in the dates array. If the frequency is unknown, the object searches for the smaller frequency, if any, such that every period of the domain contains at most one observation.

Through the TSDataCollector, it is easy to create time series of any admissible frequency from any set, regular or not, of time observations.

C#:

References:

Code:

using Nbb.TimeSeries.SimpleTS;

...

TSDataCollector collector = new TSDataCollector();
DateTime d = DateTime.Now;
Random rnd = new Random();

while (collector.Count < 100)
{
    double val = rnd.NextDouble();
    d=d.Add(new TimeSpan(31+rnd.Next()%31, 0, 0, 0));
// one observation by month at the most
    collector.AddObservation(d, val);
}

TS ts0 = collector.Make(TSFrequency.HalfYearly, Nbb.TimeSeries.TSAggregationType.Average);
// ts0 is an half-yearly series, obtained by averaging the observations in each half-year
TS ts1 = collector.Make(TSFrequency.Undefined, Nbb.TimeSeries.TSAggregationType.None);
// ts1 is a series whose frequency has been automatically detected;
// it's thelowest frequency for which each period contains at the most one observation;
// this method fails if there are more than 1 observation during a month.
 

VB.Net:

References:

Code:

Imports Nbb.TimeSeries.SimpleTS
...
Dim collector As New TSDataCollector
Dim ts0 As TS
Dim ts1 As TS
Dim d As Date
Dim span As TimeSpan
Dim rnd As New Random
Dim val As Double

d = Now
Do While collector.Count < 100
    val = rnd.NextDouble
    span = New TimeSpan(31 + rnd.Next Mod 31, 0, 0, 0)
    d = d.Add(span)
' one observation by month at the most
        collector.AddObservation(d, val)
Loop

ts0 = collector.Make(2, TSAggregationType.Average)
ts1 = collector.Make(0, TSAggregationType.None)

 

VBA (Excel):

We assume that the time series is located in the 2 first columns of the Excel sheet (1st column for the dates and the 2nd for the observations). The dates are real Excel dates, not text.

You can download an XLS file with the example code here (requires installation off the full package with the COM interfaces).

References:

Code:

Public Sub TsCreation()
    Dim dc As New NbbTs.TSDataCollector
    Dim ts0 As NbbTs.TS
    Dim i As Integer

    For i = 1 To 100
        dc.AddObservation Cells(i, 1), Cells(i, 2))
    Next i

    Set ts0 = dc.Make(TSFrequency_Undefined, TSAggregationType_None)
End Sub

 

Direct creation

A time series can be directly created from its domain. The domain can be explicit (a TSDomain object) or implicit (a frequency, first year, first period and length or a starting TSPeriod object and a length). Once it has been created in that way, a time series contains only missing values that can of course be initialized later. This method should be the prefered one when the number of observations is known.

Be aware that the periods of the year are always 0-based indexed.
(Jan-2006 is {TSFrequency.Monthly, 2006, 0}, QIII-2006 is TSFrequency.Quarterly, 2006, 2}, ...).

C#:

References:

Code:

using Nbb.TimeSeries.SimpleTS;

...

Random rnd = new Random();
TSPeriod period = new TSPeriod(TSFrequency.Monthly);
period.Set(1985, 0);
TSDomain domain = new TSDomain(TSFrequency.Monthly, 1985, 0, 100);
TS ts0 = new TS(TSFrequency.Monthly, 1985, 0, 100);
TS ts1 = new TS(period, 100);
TS ts2 = new TS(domain);
for (int i = 0; i < 100; ++i)
{
    ts0[i] = rnd.NextDouble();
    ts1[i] = rnd.NextDouble();
    ts2[i] = rnd.NextDouble();
}

 

VB.Net:

References:

Code:

Imports Nbb.TimeSeries.SimpleTS
...
Dim ts0 As TS
Dim ts1 As TS
Dim ts2 As TS
Dim domain As TSDomain
Dim period As New TSPeriod
Dim i As Integer
Dim rnd As New Random

period.Frequency = TSFrequency.Monthly
period.Set(1985, 0)
domain = New TSDomain(TSFrequency.Monthly, 1985, 0, 100)
ts0 = New TS(TSFrequency.Monthly, 1985, 0, 100)
ts1 = New TS(period, 100)
ts2 = New TS(domain)
For i = 0 To 99
    ts0(i) = rnd.NextDouble
    ts1(i) = rnd.NextDouble
    ts2(i) = rnd.NextDouble
Next i

 

VBA (Excel):

You can download an XLS file with the example code here (requires the installation off the last full package with the COM interfaces (10/2006)).

References:

Code:

Public Sub TsCreation()
    Dim ts0 As NbbTs.TS
    Dim ts1 As NbbTs.TS
    Dim ts2 As NbbTs.TS
    Dim fac As New NbbTs.TSFactory
    Dim domain As NbbTs.TSDomain
    Dim period As New NbbTs.TSPeriod
    Dim i As Integer

    period.Frequency = TSFrequency_Monthly
    period.Set 1985, 0

    Set domain = fac.CreateDomain(12, 1985, 0, 100)

    Set ts0 = fac.Create(TSFrequency_Monthly, 1985, 0, 100)
    Set ts1 = fac.CreateFromPeriod(period, 100)
    Set ts2 = fac.CreateFromDomain(domain)

    For i = 0 To 99
        ts0.Values(i) = Cells(i + 1, 2)
        ts1.Values(i) = Cells(i + 1, 2)
        ts2.Values(i) = Cells(i + 1, 2)
    Next i
End Sub