Skip to content

Commit

Permalink
Merge pull request #125 from palatej/develop
Browse files Browse the repository at this point in the history
Ssf splines
  • Loading branch information
palatej authored Apr 5, 2024
2 parents c7a6920 + 5d7e6af commit b1aafb2
Show file tree
Hide file tree
Showing 30 changed files with 1,609 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void testSymmetric() {
.nleads(4)
.build();
FSTFilter.Results rslt = ff.make(.1 * i, 0, false);
System.out.println(DoubleSeq.of(rslt.getFilter().weightsToArray()));
// System.out.println(DoubleSeq.of(rslt.getFilter().weightsToArray()));
}
}

Expand All @@ -45,11 +45,11 @@ public void testASymmetric() {
.nleads(i)
.build();
FSTFilter.Results rslt = ff.make(1, 0, true);
System.out.print(rslt.getS());
System.out.print('\t');
System.out.print(rslt.getF());
System.out.print('\t');
System.out.println(DoubleSeq.of(rslt.getFilter().weightsToArray()));
// System.out.print(rslt.getS());
// System.out.print('\t');
// System.out.print(rslt.getF());
// System.out.print('\t');
// System.out.println(DoubleSeq.of(rslt.getFilter().weightsToArray()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void testAR_int() {
ExtendedAirlineMapping mapping = ExtendedAirlineMapping.of(spec);
DoubleSeq p = mapping.getDefaultParameters();
ArimaModel m = mapping.map(p);
System.out.println(m);
// System.out.println(m);
DoubleSeq np = mapping.parametersOf(m);
assertEquals(p, np);
}
Expand All @@ -61,7 +61,7 @@ public void testAR_noint() {
ExtendedAirlineMapping mapping = ExtendedAirlineMapping.of(spec);
DoubleSeq p = mapping.getDefaultParameters();
ArimaModel m = mapping.map(p);
System.out.println(m);
// System.out.println(m);
DoubleSeq np = mapping.parametersOf(m);
assertTrue(p.distance(np) < 1.e-9);
}
Expand All @@ -78,7 +78,7 @@ public void test_int() {
ExtendedAirlineMapping mapping = ExtendedAirlineMapping.of(spec);
DoubleSeq p = mapping.getDefaultParameters();
ArimaModel m = mapping.map(p);
System.out.println(m);
// System.out.println(m);
DoubleSeq np = mapping.parametersOf(m);
assertEquals(p, np);
}
Expand All @@ -95,7 +95,7 @@ public void test_noint() {
ExtendedAirlineMapping mapping = ExtendedAirlineMapping.of(spec);
DoubleSeq p = mapping.getDefaultParameters();
ArimaModel m = mapping.map(p);
System.out.println(m);
// System.out.println(m);
DoubleSeq np = mapping.parametersOf(m);
assertTrue(p.distance(np) < 1.e-9);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import jdplus.toolkit.base.core.ssf.ISsfLoading;
import jdplus.toolkit.base.core.ssf.StateStorage;
import jdplus.toolkit.base.core.timeseries.calendars.HolidaysUtility;
import tck.demetra.data.Data;

/**
*
Expand All @@ -55,7 +56,7 @@ public class SplinesTest {
static {
DoubleSeq y;
try {
InputStream stream = ExtendedAirlineMapping.class.getResourceAsStream("/births.txt");
InputStream stream = Data.class.getResourceAsStream("/births.txt");
Matrix edf = MatrixSerializer.read(stream);
y = edf.column(0);
} catch (IOException ex) {
Expand Down Expand Up @@ -101,7 +102,7 @@ public static void main(String[] args) {
//StateItem l = AtomicModels.localLevel("l", .01, false, Double.NaN);
StateItem l = AtomicModels.localLinearTrend("l", .01, .01, false, false);
StateItem sw = AtomicModels.seasonalComponent("sw", "HarrisonStevens", 7, .01, false);
StateItem sy = AtomicModels.dailySplineComponent("sy", 1968, 2024, pos, 0, .01, false);
StateItem sy = AtomicModels.dailySplines("sy", 1968, pos, 0, .01, false);
StateItem reg = AtomicModels.timeVaryingRegression("reg", X, 0.01, false);
StateItem n = AtomicModels.noise("n", .01, false);
ModelEquation eq = new ModelEquation("eq1", 0, true);
Expand Down Expand Up @@ -149,7 +150,7 @@ public static void main(String[] args) {
public static void main2(String[] args) {
DoubleSeq y;
try {
InputStream stream = ExtendedAirlineMapping.class.getResourceAsStream("/usclaims.txt");
InputStream stream = Data.class.getResourceAsStream("/usclaims.txt");
Matrix us = MatrixSerializer.read(stream);
y = us.column(0);
} catch (IOException ex) {
Expand All @@ -168,7 +169,7 @@ public static void main2(String[] args) {
// StateItem l = AtomicModels.localLevel("l", .01, false, Double.NaN);
StateItem l = AtomicModels.localLinearTrend("l", .01, .01, false, false);
// StateItem sw = AtomicModels.seasonalComponent("sw", "HarrisonStevens", 7, .01, false);
StateItem sy = AtomicModels.weeklySplineComponent("sy", 1967, 2025, pos, 6, 0, .01, false);
StateItem sy = AtomicModels.regularSplines("sy", 1967, new double[]{1,5,10,15,20,30,40,50}, 0, .01, false);
// StateItem reg=AtomicModels.timeVaryingRegression("reg", X, 0.01, false);
StateItem n = AtomicModels.noise("n", .01, false);
ModelEquation eq = new ModelEquation("eq1", 0, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testDefault() {
spec.setNs(13);
spec.setNt(23);
spec.setNi(2);
spec.setNo(1);
spec.setNo(0);
spec.setNsjump(0);
spec.setNtjump(0);
spec.setNljump(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
@lombok.experimental.UtilityClass
public class StlDecomposition {

public Matrix stl(double[] data, int period, boolean mul, int swindow, int twindow, int lwindow, int sdegree, int tdegree, int ldegree, int sjump, int tjump, int ljump, int nin, int nout, double weightThreshold, String weightsFunction, boolean legacy) {
if (nin < 1) {
nin = 1;
Expand All @@ -54,9 +54,9 @@ public Matrix stl(double[] data, int period, boolean mul, int swindow, int twind
return stl_new(data, period, mul, swindow, twindow, lwindow, sdegree, tdegree, ldegree, sjump, tjump, ljump, nin, nout, weightThreshold, weightsFunction);
}
}

private Matrix stl_legacy(double[] data, int period, boolean mul, int swindow, int twindow, int lwindow, int sdegree, int tdegree, int ldegree, int sjump, int tjump, int ljump, int nin, int nout, double weightThreshold, String weightsFunction) {

StlLegacySpec spec = StlLegacySpec.defaultSpec(period, swindow, true);
spec.setMultiplicative(mul);
spec.setLegacy(true);
Expand All @@ -75,14 +75,14 @@ private Matrix stl_legacy(double[] data, int period, boolean mul, int swindow, i
spec.setWfn(WeightFunction.valueOf(weightsFunction).asFunction());
StlLegacy stl = new StlLegacy(spec);
DoubleSeq y = DoubleSeq.of(data).cleanExtremities();

int n = y.length();
if (!stl.process(y)) {
return null;
}

FastMatrix M = FastMatrix.make(n, 7);

M.column(0).copy(y);
M.column(2).copyFrom(stl.getTrend(), 0);
M.column(3).copyFrom(stl.getSeason(), 0);
Expand All @@ -102,7 +102,7 @@ private Matrix stl_legacy(double[] data, int period, boolean mul, int swindow, i
}
return M;
}

private Matrix stl_new(double[] data, int period, boolean mul, int swindow, int twindow, int lwindow, int sdegree, int tdegree, int ldegree, int sjump, int tjump, int ljump, int nin, int nout, double weightThreshold, String weightsFunction) {
LoessSpec tspec = LoessSpec.builder()
.window(twindow)
Expand All @@ -119,7 +119,7 @@ private Matrix stl_new(double[] data, int period, boolean mul, int swindow, int
.jump(ljump)
.degree(ldegree)
.build();

StlSpec spec = StlSpec.builder()
.multiplicative(mul)
.trendSpec(tspec)
Expand All @@ -133,25 +133,29 @@ private Matrix stl_new(double[] data, int period, boolean mul, int swindow, int
.robustWeightThreshold(weightThreshold)
.robustWeightFunction(WeightFunction.valueOf(weightsFunction))
.build();

RawStlKernel stl = new RawStlKernel(spec);
DoubleSeq y = DoubleSeq.of(data).cleanExtremities();

int n = y.length();
RawStlResults rslt = stl.process(DoubleSeq.of(data));

FastMatrix M = FastMatrix.make(n, 7);

M.column(0).copy(y);
M.column(2).copy(rslt.getTrend());
M.column(3).copy(rslt.getSeasonal());
M.column(4).copy(rslt.getIrregular());
M.column(5).copy(rslt.getFit());
M.column(6).copy(rslt.getWeights());
if (!rslt.getWeights().isEmpty()) {
M.column(6).copy(rslt.getWeights());
} else {
M.column(6).set(1);
}
M.column(1).copy(rslt.getSa());
return M;
}

private int max(int[] v) {
int m = v[0];
for (int i = 1; i < v.length; ++i) {
Expand All @@ -161,23 +165,23 @@ private int max(int[] v) {
}
return m;
}

public Matrix mstl(double[] data, int[] periods, boolean mul, int[] swindow, int twindow, int nin, int nout, boolean nojump, double weightThreshold, String weightsFunction) {
if (periods == null || (swindow != null && periods.length != swindow.length)) {
return null;
}
if (twindow == 0) {
twindow = LoessSpec.defaultTrendWindow(max(periods));
}

MStlSpec.Builder builder = MStlSpec.builder()
.innerLoopsCount(nin)
.outerLoopsCount(nout)
.multiplicative(mul)
.trendSpec(LoessSpec.of(twindow, 1, nojump))
.robustWeightThreshold(weightThreshold)
.robustWeightFunction(WeightFunction.valueOf(weightsFunction));

if (swindow == null) {
for (int i = 0; i < periods.length; ++i) {
builder.seasonalSpec(SeasonalSpec.createDefault(periods[i], nojump));
Expand All @@ -190,17 +194,17 @@ public Matrix mstl(double[] data, int[] periods, boolean mul, int[] swindow, int
for (int i = 0; i < periods.length; ++i) {
builder.seasonalSpec(new SeasonalSpec(periods[i], swindow[i], nojump));
}

}
MStlSpec spec = builder.build();

MStlKernel stl = MStlKernel.of(spec);
DoubleSeq y = DoubleSeq.of(data).cleanExtremities();
stl.process(y);

int n = y.length();
FastMatrix M = FastMatrix.make(n, 6 + periods.length);

M.column(0).copyFrom(stl.getY(), 0);
M.column(1).copy(M.column(0));
M.column(2).copyFrom(stl.getTrend(), 0);
Expand All @@ -218,22 +222,22 @@ public Matrix mstl(double[] data, int[] periods, boolean mul, int[] swindow, int
M.column(j).copyFrom(stl.getWeights(), 0);
return M;
}

public Matrix istl(double[] data, int[] periods, boolean mul, int[] swindow, int[] twindow, int nin, int nout, boolean nojump, double weightThreshold, String weightsFunction) {
if (periods == null || (swindow != null && periods.length != swindow.length)) {
return null;
}
if (twindow != null && twindow.length != periods.length) {
return null;
}

IStlSpec.Builder builder = IStlSpec.builder()
.innerLoopsCount(nin)
.outerLoopsCount(nout)
.multiplicative(mul)
.robustWeightThreshold(weightThreshold)
.robustWeightFunction(WeightFunction.valueOf(weightsFunction));

for (int i = 0; i < periods.length; ++i) {
SeasonalSpec sspec;
if (swindow == null) {
Expand All @@ -250,16 +254,16 @@ public Matrix istl(double[] data, int[] periods, boolean mul, int[] swindow, int
tspec = LoessSpec.of(twindow[i], 1, nojump);
}
builder.periodSpec(new IStlSpec.PeriodSpec(tspec, sspec));

}
IStlSpec spec = builder.build();

DoubleSeq y = DoubleSeq.of(data).cleanExtremities();
MStlResults rslt = IStlKernel.process(y, spec);

int n = y.length();
FastMatrix M = FastMatrix.make(n, 6 + periods.length);

M.column(0).copy(y);
M.column(1).copy(M.column(0));
M.column(2).copy(rslt.getTrend());
Expand All @@ -277,13 +281,13 @@ public Matrix istl(double[] data, int[] periods, boolean mul, int[] swindow, int
M.column(++j).copy(rslt.getWeights());
return M;
}

public double[] loess(double[] y, int window, int degree, int jump) {
LoessSpec spec = LoessSpec.of(window, degree, jump, null);
LoessFilter filter = new LoessFilter(spec);
double[] z = new double[y.length];
filter.filter(IDataGetter.of(y), null, IDataSelector.of(z));
return z;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public void testFilter() {

@Test
public void testStl() {
Matrix decomp = StlDecomposition.stl(Data.ABS_RETAIL, 12, false, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2, 5, 0.1, WeightFunction.TRICUBE.name(), false);
Matrix decomp = StlDecomposition.stl(Data.ABS_RETAIL, 12, false, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0.1, WeightFunction.TRICUBE.name(), false);
// System.out.println(decomp);
assertTrue(null != decomp);
}

@Test
public void testStl_legacy() {
Matrix decomp = StlDecomposition.stl(Data.ABS_RETAIL, 12, false, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2, 5, 0.1, WeightFunction.TRICUBE.name(), true);
Matrix decomp = StlDecomposition.stl(Data.ABS_RETAIL, 12, false, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0.1, WeightFunction.TRICUBE.name(), true);
// System.out.println(decomp);
assertTrue(null != decomp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package jdplus.sts.base.core.msts;

import jdplus.sts.base.core.splines.DailySpline;
import jdplus.sts.base.core.splines.RegularSpline;
import jdplus.sts.base.core.splines.SplineData;
import jdplus.sts.base.core.msts.internal.ArItem;
import jdplus.sts.base.core.msts.internal.ArItem2;
import jdplus.sts.base.core.msts.internal.ArimaItem;
Expand All @@ -39,12 +42,9 @@
import jdplus.sts.base.core.msts.internal.VarSeasonalComponentItem;
import jdplus.sts.base.core.msts.internal.VarNoiseItem;
import jdplus.toolkit.base.api.math.matrices.Matrix;
import jdplus.sts.base.core.msts.internal.RegularSplineItem;
import jdplus.sts.base.core.msts.internal.SplineItem;
import jdplus.sts.base.core.msts.internal.VarRegressionItem;
import jdplus.toolkit.base.core.ssf.sts.splines.DailySpline;
import jdplus.toolkit.base.core.ssf.sts.splines.SplineData;
import jdplus.toolkit.base.core.ssf.sts.splines.WeeklySpline;
import jdplus.toolkit.base.api.data.DoubleSeq;

/**
*
Expand Down Expand Up @@ -149,15 +149,27 @@ public StateItem periodicComponent(String name, double period, int[] k, double c
return new PeriodicItem(name, period, k, cvar, fixedvar);
}

public StateItem regularSplineComponent(String name, int[] pos, int startpos, double cvar, boolean fixedvar) {
return new RegularSplineItem(name, pos, startpos, cvar, fixedvar);
public StateItem regularSplines(String name, double period, int startpos, double cvar, boolean fixedvar) {
RegularSpline rs = RegularSpline.of(period);
SplineData sd = new SplineData(rs);
return new SplineItem(name, sd, startpos, cvar, fixedvar);
}

public StateItem dailySplineComponent(String name, int startYear, int endYear, int[] pos, int startpos, double cvar, boolean fixedvar) {
return new SplineItem(name, SplineData.of(new DailySpline(startYear, pos), endYear-startYear+1), startpos, cvar, fixedvar);

public StateItem regularSplines(String name, double period, int nnodes, int startpos, double cvar, boolean fixedvar) {
RegularSpline rs = RegularSpline.of(period, nnodes);
SplineData sd = new SplineData(rs);
return new SplineItem(name, sd, startpos, cvar, fixedvar);
}

public StateItem regularSplines(String name, double period, double[] nodes, int startpos, double cvar, boolean fixedvar) {
RegularSpline rs = RegularSpline.of(period, DoubleSeq.of(nodes));
SplineData sd = new SplineData(rs);
return new SplineItem(name, sd, startpos, cvar, fixedvar);
}

public StateItem weeklySplineComponent(String name, int startYear, int endYear, int[] pos, int startPos, int modelStart, double cvar, boolean fixedvar) {
return new SplineItem(name, SplineData.of(new WeeklySpline(startYear, startPos, pos), endYear-startYear+1), modelStart, cvar, fixedvar);
public StateItem dailySplines(String name, int startYear, int[] pos, int startpos, double cvar, boolean fixedvar) {
DailySpline rs = new DailySpline(startYear, pos);
SplineData sd = new SplineData(rs);
return new SplineItem(name, sd, startpos, cvar, fixedvar);
}
}
Loading

0 comments on commit b1aafb2

Please sign in to comment.