Class: TimeWise::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/time_wise/base.rb

Overview

The Base class is the foundation of all time series operations in TimeWise

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, dates = nil) ⇒ Base

Initialize a new time series object

Parameters:

  • data (Array)

    Array of numeric values representing the time series

  • dates (Array) (defaults to: nil)

    Optional array of dates corresponding to each data point



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/time_wise/base.rb', line 14

def initialize(data, dates = nil)
  validate_inputs(data, dates)

  @data = Numo::DFloat.cast(data)
  @dates = dates

  if dates&.length&.positive?
    @has_dates = true
    @frequency = detect_frequency
  else
    @has_dates = false
    @frequency = nil
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/time_wise/base.rb', line 9

def data
  @data
end

#datesObject (readonly)

Returns the value of attribute dates.



9
10
11
# File 'lib/time_wise/base.rb', line 9

def dates
  @dates
end

Instance Method Details

#lengthInteger

Length of the time series

Returns:

  • (Integer)

    Number of data points



78
79
80
# File 'lib/time_wise/base.rb', line 78

def length
  @data.size
end

#moving_averageObject

Access moving average methods



88
89
90
# File 'lib/time_wise/base.rb', line 88

def moving_average
  TimeWise::MovingAverage.new(self)
end

#plotObject

Access visualization methods



93
94
95
# File 'lib/time_wise/base.rb', line 93

def plot
  TimeWise::Visualization.new(self)
end

#slice(start_idx, end_idx) ⇒ TimeWise::Base

Get a section of the time series

Parameters:

  • start_idx (Integer)

    Starting index

  • end_idx (Integer)

    Ending index

Returns:

  • (TimeWise::Base)

    A new time series with the specified data range



43
44
45
46
47
48
49
50
# File 'lib/time_wise/base.rb', line 43

def slice(start_idx, end_idx)
  end_idx = @data.size - 1 if end_idx >= @data.size

  new_data = @data[start_idx..end_idx]
  new_dates = @has_dates ? @dates[start_idx..end_idx] : nil

  TimeWise.create(new_data.to_a, new_dates)
end

#slice_by_date(start_date, end_date) ⇒ TimeWise::Base

Get a section of the time series by date range

Parameters:

  • start_date (Date, DateTime, String)

    Starting date

  • end_date (Date, DateTime, String)

    Ending date

Returns:

  • (TimeWise::Base)

    A new time series with the specified date range

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/time_wise/base.rb', line 56

def slice_by_date(start_date, end_date)
  raise TimeWise::Errors::MissingDatesError, "Time series has no date information" unless @has_dates

  parsed_start = parse_date(start_date)
  parsed_end = parse_date(end_date)

  indices = find_indices_in_date_range(parsed_start, parsed_end)

  new_data = indices.map { |idx| @data[idx] }
  new_dates = indices.map { |idx| @dates[idx] }

  TimeWise.create(new_data, new_dates)
end

#statsObject

Apply basic statistical methods



83
84
85
# File 'lib/time_wise/base.rb', line 83

def stats
  TimeWise::Statistics.new(self)
end

#to_aArray

Convert to array

Returns:

  • (Array)

    The time series data as a standard Ruby array



72
73
74
# File 'lib/time_wise/base.rb', line 72

def to_a
  @data.to_a
end

#to_hHash

Get the time series as a hash with date and value arrays

Returns:

  • (Hash)

    Hash with :dates and :values keys



31
32
33
34
35
36
37
# File 'lib/time_wise/base.rb', line 31

def to_h
  if @has_dates
    { dates: @dates, values: @data.to_a }
  else
    { indices: (0...@data.size).to_a, values: @data.to_a }
  end
end