Capsule Based Back Prediction or Back-Casting
Scenario: Instead of forecasting data into the future, there may be a need to extrapolate a signal back in time based on data from an event or period of interest. The following steps will allow you to backcast a target signal from every capsule within a condition.
Data
Target Signal – a signal that you would like to backcast.
Event – a condition that encapsulates the event or period of interest from which you would like to backcast the target signal. The target signal must have sufficient sample points within each capsule to create an accurate regression model.
Method
Step 1. Create a new extended event that will combine the capsules from the original event with a prediction window for backcasting. In this example, the prediction window is 1 hr and a maximum capsule duration of 40h is defined.
$prediction_window = $event.beforeStart(1h)
$prediction_window.join($event, 40h)
Step 2. Create a new time since signal that quantifies the time since the beginning of each capsule in the extended event condition. This new signal will be the independent variable in the regression model.
$extended_event.timeSince(1min)
Replace 1min with a sample frequency sufficient for your use case.
Step 3. In formula, use the example below to create a regression model for the target signal, with data from the event as training data, and the time since signal as an independent variable. Assign the regression model coefficients as capsule properties for a new condition called regression condition.
$event.transform($cap->
{$model=$target_signal.validValues().regressionModelOLS( group($cap),false,$time_since,$time_since^2)
$cap
.setProperty('m1',$model.get('coefficient1'))
.setProperty('m2',$model.get('coefficient2'))
.setProperty('c',$model.get('intercept'))})
The formula above creates a second-order polynomial ordinary least squares regression model. The order of the polynomial can be modified (from linear up to 9th) by adding sequential 'timesince^n' statements on line 2 and defining all coefficients as is on lines 4 and 5. See the example below of how to adjust the formula for a third-order polynomial model.
Step 4. Using the regression model coefficients from the regression condition, and the time since signal, the target signal can then be backcast over the prediction window.
$c = $regression_condition.toSignal('c',durationKey()).aggregate(average(),$extended_event,durationKey())
$m1 = $regression_condition.toSignal('m1',durationKey()).aggregate(average(),$extended_event,durationKey())
$m2 = $regression_condition.toSignal('m2',durationKey()).aggregate(average(),$extended_event,durationKey())
return $m1*$time_since+$m2*$time_since^2 + $c
The example above is for a second-order polynomial and the formula needs to be modified depending on the order of the polynomial defined in Step 3. See the example below for a linear model. Note that it may be required to manually set the units (using setunits() function) of each part of the polynomial equation.
Result
The result is a new signal which backcasts the target signal for the duration of the prediction window prior to the event or period of interest.