New Version 5.0!

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

Download now!

QuickStart Samples

Simple Time Series QuickStart Sample (F#)

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

C# code Visual Basic code IronPython code Back to QuickStart Samples

// Illustrates the use of the TimeSeriesCollection class to represent
// and manipulate time series data.

#light

open System

open System
open System.Data
open System.Data.OleDb

open Extreme.Statistics
open Extreme.Statistics.TimeSeriesAnalysis

// Time series collections can be created in a variety of ways.
// Here we use an ADO.NET data table:
let seriesTable =
    let filename = @"..\..\..\..\Data\MicrosoftStock.xls"
    let connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filename+";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""
    let ds = new DataSet()
    use cnn = new OleDbConnection(connectionString)
    try
        cnn.Open()
        use adapter = new OleDbDataAdapter("Select * from [MicrosoftStock$]", cnn)
        adapter.Fill(ds) |> ignore
    with :? OleDbException as ex -> printfn "%s" ex.InnerException.Message
    ds.Tables.[0]
let timeSeries = TimeSeriesCollection(seriesTable)

// The RowCount property returns the number of
// observations:
printfn "# observations: %d" timeSeries.Observations.Count

// The StartOfPeriodVariable property returns the
// DateTimeVariable that contains the start times
// for each period.
printfn "First date: %A" timeSeries.StartOfPeriodVariable.Minimum
// The EndOfPeriodVariable property returns the
// DateTimeVariable that contains the end times
// for each period.
printfn "Last date: %A" timeSeries.EndOfPeriodVariable.Maximum

// Data in a TimeSeriesCollection is always sorted 
// in ascending time order.

//
// Accessing variables
//

// Variables are accessed by name or numeric index.
// They need to be cast to the appropriate specialized
// type (NumericalVariable, DateTimeVariable, etc.)
let close = timeSeries.["Close"] :?> NumericalVariable
printfn "Average close price: $%.2f" close.Mean

// Variables can also be accessed by numeric index:
printfn "3rd variable: %s" timeSeries.[2].Name

// The GetSubset method returns the data from the specified range.
let y2004 = DateTime(2004, 1, 1)
let y2005 = DateTime(2005, 1, 1)
let series2004 = timeSeries.CreateSubset(y2004, y2005)
printfn "Opening price on the first trading day of 2004: %A"
    (series2004.["Open"].GetValue(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:
timeSeries.["Open"].Aggregator <- Aggregator.First
timeSeries.["Close"].Aggregator <- Aggregator.Last
timeSeries.["High"].Aggregator <- Aggregator.Maximum
timeSeries.["Low"].Aggregator <- Aggregator.Minimum
timeSeries.["Volume"].Aggregator <- Aggregator.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:
let monthlySeries = timeSeries.TransformFrequency(y2004, y2005, DateTimeUnit.Month)

// We can now print the results:
printfn "Monthly statistics for Microsoft Corp. (MSFT)"
printfn "Month   Open   Close  High   Low    Volume"
for row in 0..monthlySeries.Observations.Count-1 do
    printfn " %s    %.2f  %.2f  %.2f  %.2f  %10.0f"
        (monthlySeries.StartOfPeriodVariable.[row].ToString("MMM"))
        ((monthlySeries.["Open"]  :?> NumericalVariable).[row])
        ((monthlySeries.["Close"] :?> NumericalVariable).[row])
        ((monthlySeries.["High"]  :?> NumericalVariable).[row])
        ((monthlySeries.["Low"]   :?> NumericalVariable).[row])
        ((monthlySeries.["Volume"]:?> NumericalVariable).[row])

printf "Press any key to exit."
Console.ReadLine() |> ignore