Simple Time Series in C# QuickStart Sample
Illustrates how to perform simple operations on time series data using classes in the Numerics.NET.Statistics.TimeSeriesAnalysis namespace in C#.
This sample is also available in: Visual Basic, F#, IronPython.
Overview
This QuickStart sample demonstrates how to work with time series data using Numerics.NET. It shows basic operations for loading, analyzing and transforming financial market data.
The sample loads historical stock price data from a CSV file into a time series data frame. It demonstrates several key operations:
- Loading time series data from CSV files
- Accessing individual variables (columns) like Open, High, Low, Close prices and Volume
- Calculating basic statistics like mean prices
- Selecting data for specific time ranges
- Resampling time series to different frequencies (daily to monthly)
- Using different aggregation functions for each variable when resampling
- First price of period for Open
- Last price of period for Close
- Maximum price for High
- Minimum price for Low
- Sum for Volume
The code provides a practical example of handling financial market data, but the techniques shown can be applied to any time-stamped data series.
The code
using System;
using System.Collections.Generic;
using Numerics.NET.Data.Text;
using Numerics.NET.DataAnalysis;
using Numerics.NET.Statistics;
// Illustrates the use of the TimeSeriesCollection class to represent
// and manipulate time series data.
// The license is verified at runtime. We're using
// a 30 day trial key here. For more information, see
// https://numerics.net/trial-key
Numerics.NET.License.Verify("your-trial-key-here");
// Time series data frames can be created in a variety of ways.
// Here we read from a CSV file and specify the column to use as the index:
var timeSeries = DelimitedTextFile.ReadDataFrame<DateTime>(
@"..\..\..\..\..\Data\MicrosoftStock.csv", "Date");
// The RowCount property returns the number of
// observations:
Console.WriteLine($"# observations: {timeSeries.RowCount}");
//
// Accessing variables
//
// Variables are accessed by name or numeric index.
// They need to be cast to the appropriate specialized
// type using the As() method:
var close = timeSeries["Close"].As<double>();
Console.WriteLine($"Average close price: ${close.Mean():F2}");
// Variables can also be accessed by numeric index:
Console.WriteLine($"3rd variable: {timeSeries[2].Name}");
// The GetRows method returns the data from the specified range.
DateTime y2004 = new DateTime(2004, 1, 1);
DateTime y2005 = new DateTime(2005, 1, 1);
var series2004 = timeSeries.GetRows(y2004, y2005);
Console.WriteLine("Opening price on the first trading day of 2004: {0}",
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 Aggregators class has a number of
// pre-defined aggregator functions.
// We create a dictionary that maps column names
// to aggregators:
var aggregators = new Dictionary<string, AggregatorGroup>()
{
{ "Open", Aggregators.First },
{ "Close", Aggregators.Last },
{ "High", Aggregators.Max },
{ "Low", Aggregators.Min },
{ "Volume", Aggregators.Sum }
};
// We can then resample the data frame in accordance with
// a recurrence pattern we specify, in this case monthly:
var monthlySeries = timeSeries.Resample(Recurrence.Monthly, aggregators);
// We can specify a subset of the series by selecting it
// from the data frame first:
monthlySeries = timeSeries.GetRows(y2004, y2005)
.Resample(Recurrence.Monthly, aggregators);
// We can now print the results:
Console.WriteLine("Monthly statistics for Microsoft Corp. (MSFT)");
Console.WriteLine(monthlySeries.ToString());
Console.Write("Press any key to exit.");
Console.ReadLine();