Daru allows for a host of functions for analyzing and visualizing time series data. In this notebook we'll go over a few with examples.
For details on using statistical analysis functions offered by daru see this blog post.
require 'distribution'
require 'daru'
require 'gnuplotrb'
rng = Distribution::Normal.rng
index = Daru::DateTimeIndex.date_range(:start => '2012-4-2', :periods => 1000, :freq => 'D')
vector = Daru::Vector.new(1000.times.map {rng.call}, index: index)
vector = vector.cumsum
Daru::Vector has a bunch of functions for performing useful statistical analysis of time series data. See this blog post for a comprehensive overview of the statistics functions available on Daru::Vector.
For example, you can calculate the rolling mean of a Vector with the #rolling_mean
function and pass in the loopback length as the argument:
rolling = vector.rolling_mean 60
rolling.tail
Using the gnuplotRB gem, it is also possible to directly plot the vector and its rolling mean as line plots on the same graph:
GnuplotRB::Plot.new([vector, with: 'lines', title: 'Vector'], [rolling, with: 'lines', title: 'Rolling Mean'])
df = Daru::DataFrame.new({
a: 1000.times.map {rng.call},
b: 1000.times.map {rng.call},
c: 1000.times.map {rng.call}
}, index: index)
df = df.cumsum
rs = df.rolling_sum(60)
plots = []
rs.each_vector_with_index do |vec,n|
plots << GnuplotRB::Plot.new([vec, with: 'lines', title: n])
end
GnuplotRB::Multiplot.new(*plots, layout: [3,1], title: 'Rolling sums')