dividedDifference
Syntax
dividedDifference(X, Y, resampleRule, [closed='left'], [origin='start_day'],
[outputX=false])
Details
Resample X based on the specified resampleRule, closed and origin. Perform divided difference interpolation on Y based on the resampled X.
If outputX is unspecified, return a vector of Y after the interpolation.
If outputX=true, return a tuple where the first element is the vector of resampled X and the second element is a vector of Y after the interpolation.
Parameters
X is a strictly increasing vector of temporal type.
Y is a numeric vector of the same length as X.
resampleRule is a string. See the parameter rule of function resample for the optional values.
closed and origin are the same as the parameters closed and origin of function resample.
outputX is a Boolean value indicating whether to output the resampled X. The default value is false.
Examples
dividedDifference([2016.02.14 00:00:00, 2016.02.15 00:00:00, 2016.02.16 00:00:00], [1.0, 2.0, 4.0], resampleRule=`60min);
/* output
[1,1.0217,1.0451,1.0703,1.0972,1.1259,1.1562,1.1884,1.2222,1.2578,
1.2951,1.3342,1.375,1.4175,1.4618,1.5078,1.5556,1.605,1.6562,1.7092,
1.7639,1.8203,1.8785,1.9384,2,2.0634,2.1285,2.1953,2.2639,2.3342,
2.4062,2.48,2.5556,2.6328,2.7118,2.7925,2.875,2.9592,3.0451,3.1328,
3.2222,3.3134,3.4062,3.5009,3.5972,3.6953,3.7951,3.8967,4]
*/// Data points fall exactly on 3-minute boundaries
X = 2022.01.01T00:00:00 + [0, 3, 6, 9] * 60 // 00:00, 00:03, 00:06, 00:09
Y = [1.0, 3.0, 7.0, 13.0]
// closed='left' (default): interval [t, t+3min), left-closed right-open
// 00:03 belongs to [00:03, 00:06), the start of the second bucket
dividedDifference(X, Y, `3min, closed=`left)
// closed='right': interval (t, t+3min], left-open right-closed
// 00:03 belongs to (00:00, 00:03], the end of the first bucket
// Different bucket boundaries lead to different interpolation results
dividedDifference(X, Y, `3min, closed=`right)// Data starts at 00:00:30 and is not aligned to whole minutes
X = 2022.01.01T00:00:30 + (0..4) * 60 // 00:00:30, 00:01:30, ..., 00:04:30
Y = [2.0, 4.0, 7.0, 11.0, 16.0]
// origin='start_day' (default): align to 00:00:00 of the same day
// Time buckets: [00:00:00, 00:01:00), [00:01:00, 00:02:00), ...
// Resampled X: 00:00:00, 00:01:00, 00:02:00, 00:03:00, 00:04:00
dividedDifference(X, Y, `1min, origin=`start_day, outputX=true)
// origin='start': align to the first data point 00:00:30
// Time buckets: [00:00:30, 00:01:30), [00:01:30, 00:02:30), ...
// Resampled X: 00:00:30, 00:01:30, 00:02:30, 00:03:30, 00:04:30
dividedDifference(X, Y, `1min, origin=`start, outputX=true)
// origin=custom timestamp: align to 00:00:10
// Time buckets: [00:00:10, 00:01:10), [00:01:10, 00:02:10), ...
// Resampled X: 00:00:10, 00:01:10, 00:02:10, 00:03:10, 00:04:10
dividedDifference(X, Y, `1min, origin=2022.01.01T00:00:10, outputX=true)X = [2016.02.14T00:00:00, 2016.02.15T00:00:00, 2016.02.16T00:00:00]
Y = [1.0, 2.0, 4.0]
// outputX=false (default): return only the interpolated Y vector
dividedDifference(X, Y, `60min)
// Output: [1, 1.0217, ..., 4] (49 points)
// outputX=true: return a tuple; [0] is the resampled X, [1] is the interpolated Y
result = dividedDifference(X, Y, `60min, outputX=true)
result[0] // Hourly timestamps, 49 in total
result[1] // Corresponding interpolated Y values, 49 in total