WMA: Create a streamer for calculating the weighted moving average

Description Format Methods Examples

Description

WMA creates a streaming algorithm that can be used to keep track of the weighted mean of incoming values.

In an n-day WMA the latest day has weight n, the second latest n-1, etc., down to one.

Format

An R6Class generator object

Methods

Public methods


Method new()

Creates a new WMA streamer object.

Usage
WMA$new(x = NULL)
Arguments
x

values to be used during initialisation (optional)

Returns

The new WMA (invisibly)

Examples
weighted_mean <- WMA$new()


Method update()

Resets the WMA streamer object.

Usage
WMA$update(x)
Arguments
x

values to be added to the stream

Returns

The updated WMA (invisibly)

Examples
weighted_mean <- WMA$new()
weighted_mean$update(c(1, 2))


Method clone()

The objects of this class are cloneable with this method.

Usage
WMA$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
mean <- WMA$new(c(1, 2))
mean$update(c(3, 4))
mean$value
#> [1] 7.5


## ------------------------------------------------
## Method `WMA$new`
## ------------------------------------------------

weighted_mean <- WMA$new()


## ------------------------------------------------
## Method `WMA$update`
## ------------------------------------------------

weighted_mean <- WMA$new()
weighted_mean$update(c(1, 2))

THargreaves/onlineoceanarium documentation built on Jan. 13, 2022, 10:41 p.m.