New Version 6.0!

Try it for free with our fully functional 60-day trial version.

Download now!

QuickStart Samples

Simple Time Series QuickStart Sample (Visual Basic)

Illustrates how to perform simple operations on time series data using classes in the Extreme.Statistics.TimeSeriesAnalysis namespace in Visual Basic.

C# code F# code IronPython code Back to QuickStart Samples

Option Infer On

Imports System.Collections.Generic
Imports System.Data
Imports System.Data.OleDb

Imports Extreme.DataAnalysis
Imports Extreme.Statistics

Namespace Extreme.Numerics.QuickStart.VB
    ' Illustrates the use of the TimeSeriesCollection class to represent
    ' and manipulate time series data.
    Module SimpleTimeSeries

        Sub Main()
            ' Time series collections can be created in a variety of ways.
            ' Here we use an ADO.NET data table:
            Dim timeSeries = LoadTimeSeriesData()

            ' The RowCount property returns the number of
            ' observations:
            Console.WriteLine("# observations: {0}", timeSeries.RowCount)

            ' Accessing variables

            ' Variables are accessed by name or numeric index.
            ' They need to be cast to the appropriate specialized
            ' type (NumericalVariable, DateTimeVariable, etc.)
            Dim close = timeSeries("Close").As(Of Double)
            Console.WriteLine("Average close price: ${0:F2}", close.Mean())

            ' Variables can also be accessed by numeric index:
            Console.WriteLine("3rd variable: {0}", timeSeries(2).Name)

            ' The GetSubset method returns the data from the specified range.
            Dim y2004 As DateTime = New DateTime(2004, 1, 1)
            Dim y2005 As DateTime = New DateTime(2005, 1, 1)
            Dim series2004 = timeSeries.GetRows(y2004, y2005)
            Console.WriteLine("Opening price on the first trading day of 2004: {0}", _

            ' Transforming the Frequency

            ' The first step is to define the aggregator function
            ' for each variable. This function specifies how each
            ' observation in the new time series is calculated
            ' from the observations in the original series.

            ' The Aggregator class has a number of 
            ' pre-defined aggregator functions:
            Dim allAggregators = New Dictionary(Of String, AggregatorGroup)() From
                {"Open", Aggregators.First},
                {"Close", Aggregators.Last},
                {"High", Aggregators.Max},
                {"Low", Aggregators.Min},
                {"Volume", Aggregators.Sum}

            ' We can specify a subset of the series by providing
            ' the start and end dates.

            ' The TransformFrequency method returns a new series
            ' containing the aggregated data:

            Dim monthlySeries = timeSeries.GetRows(y2004, y2005).
                Resample(Recurrence.Monthly, allAggregators)

            ' We can now print the results:
            Console.WriteLine("Monthly statistics for Microsoft Corp. (MSFT)")

            Console.WriteLine("Press Enter key to continue.")
        End Sub

        Private Function LoadTimeSeriesData() As DataFrame(Of Date, String)
            Dim filename As String = "..\..\..\Data\MicrosoftStock.xls"
            Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
            Dim cnn As OleDbConnection = Nothing
            Dim ds As DataSet = New DataSet
                cnn = New OleDbConnection(connectionString)
                Dim adapter As OleDbDataAdapter = New OleDbDataAdapter("Select * from [MicrosoftStock$]", cnn)
            Catch ex As OleDbException

                If Not (cnn Is Nothing) Then
                End If
            End Try
            Return DataFrame.FromDataTable(Of Date)(ds.Tables(0), "Date")
        End Function

    End Module

End Namespace