Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
palatej committed Apr 5, 2024
1 parent f7f35ef commit 5d7e6af
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 133 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 @@ -102,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, 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 @@ -169,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.regularSplineComponent("sy", 1967, new double[]{1,5,10,15,20,30,40,50}, 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 @@ -149,25 +149,25 @@ public StateItem periodicComponent(String name, double period, int[] k, double c
return new PeriodicItem(name, period, k, cvar, fixedvar);
}

public StateItem regularSplineComponent(String name, double period, int startpos, double cvar, boolean 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 regularSplineComponent(String name, double period, int nnodes, int startpos, double cvar, boolean 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 regularSplineComponent(String name, double period, double[] nodes, int startpos, double cvar, boolean 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 dailySplineComponent(String name, int startYear, int[] pos, int startpos, double cvar, boolean 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static CompositeModelEstimation estimationOf(CompositeModel model, FastMa
.build();
MstsMapping mapping = model.mapping();
monitor.process(data, mapping, parameters == null ? null : DoubleSeq.of(parameters));
rslt.model = monitor.getModel();
rslt.model = model;
rslt.likelihood = monitor.getLikelihood();
rslt.ssf = monitor.getSsf();
rslt.cmpPos = rslt.getSsf().componentsPosition();
Expand All @@ -73,6 +73,7 @@ public static CompositeModelEstimation estimationOf(CompositeModel model, FastMa

public static CompositeModelEstimation computationOf(CompositeModel model, FastMatrix data, DoubleSeq fullParameters, boolean marginal, boolean concentrated) {
CompositeModelEstimation rslt = new CompositeModelEstimation();
rslt.model = model;
rslt.data = data;
rslt.fullParameters = fullParameters.toArray();
MstsMapping mapping = model.mapping();
Expand All @@ -98,7 +99,7 @@ public static CompositeModelEstimation computationOf(CompositeModel model, FastM
private double[] fullParameters, parameters;
private String[] parametersName, cmpName;
private StateStorage smoothedStates, filteredStates, filteringStates;
private MstsMapping model;
private CompositeModel model;

public StateStorage getSmoothedStates() {
if (smoothedStates == null) {
Expand Down Expand Up @@ -232,31 +233,73 @@ public StateStorage getFilteringStates() {
return filteringStates;
}

public DoubleSeq getFilteringComponent(int equation, int cmp) {

return DoubleSeq.empty();
}

public DoubleSeq getFilteringComponentStdev(int equation, int cmp) {

return DoubleSeq.empty();
private static int find(String[] ss, String s) {
for (int i = 0; i < ss.length; ++i) {
if (ss[i].equalsIgnoreCase(s)) {
return i;
}
}
return -1;
}

public DoubleSeq getSmoothedComponent(int equation, int cmp) {

return DoubleSeq.empty();
public FastMatrix getSmoothedComponents(int p) {
ModelEquation equation = model.getEquation(p);
StateStorage ss = getSmoothedStates();
int nr = ss.size();
int nc = equation.getItemsCount();
FastMatrix C = FastMatrix.make(nr, nc);
int[] cmpDim = ssf.componentsDimension();

for (int i = 0; i < nc; ++i) {
ModelEquation.Item item = equation.getItem(i);
String cmp = item.getCmp();
int pos = find(cmpName, cmp);
if (pos >= 0) {
ISsfLoading loading = item.getLoading();
int start = cmpPos[pos], end = start + cmpDim[pos];
DataBlock col = C.column(i);
for (int j = 0; j < nr; ++j) {
col.set(j, loading.ZX(j, ss.a(j).range(start, end)));
}
LoadingInterpreter li = item.getC();
if (li != null) {
col.mul(li.value());
}
}
}
return C;
}

public DoubleSeq getSmoothedComponentStdev(int equation, int cmp) {
public FastMatrix getSmoothedComponentVariance(int eq) {
ModelEquation equation = model.getEquation(eq);
StateStorage ss = getSmoothedStates();
if (ss == null) {
return DoubleSeq.empty();
int nr = ss.size();
int nc = equation.getItemsCount();
FastMatrix C = FastMatrix.make(nr, nc);
int[] cmpDim = ssf.componentsDimension();

for (int i = 0; i < nc; ++i) {
ModelEquation.Item item = equation.getItem(i);
String cmp = item.getCmp();
int pos = find(cmpName, cmp);
if (pos >= 0) {
ISsfLoading loading = item.getLoading();
int start = cmpPos[pos];
DataBlock col = C.column(i);
for (int j = 0; j < nr; ++j) {
double v = loading.ZVZ(j, ss.P(j).extract(start, cmpDim[pos], start, cmpDim[pos]));
col.set(j, v < 0 ? 0 : v);
}
LoadingInterpreter li = item.getC();
if (li != null) {
col.mul(li.value()*li.value());
}
}
}
ISsfLoading loading = ssf.loading(equation);
return null;
return C;
}

public MstsMapping getModel() {
public CompositeModel getModel() {
return model;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,17 @@ public DoubleSeq nodes() {
@Override
public IntSeq observations(int cycle) {
// we need to find the integers in [cycle*period, (cycle+1)*period[
if (cycle >= 0) {
int i0 = (int) Math.floor(period * cycle + EPS),
i1 = (int) Math.floor(period * (cycle + 1) + EPS);
return IntSeq.sequential(i0, i1);
} else {
int i0 = (int) Math.ceil(period * cycle - EPS),
i1 = (int) Math.ceil(period * (cycle + 1) - EPS);
return IntSeq.sequential(i0, i1);
}
}

@Override
public int cycleFor(int obs) {
if (obs >= 0) {
int c=(int) Math.floor((obs + EPS) / period);
if (obs>=(int) Math.floor(period * (c + 1) + EPS))
++c;
return c;
} else {
double dc = Math.ceil((obs + EPS) / period);
return -1 + (int) dc;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ public void Z(int pos, DataBlock z) {

@Override
public double ZX(int pos, DataBlock m) {
// try{
return z(pos).dot(m);
// }catch (Exception ex){
// return 0;
// }
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public void testProd() {
rslts.getFinals().getSeries(ComponentType.Seasonal, ComponentInformation.Value),
rslts.getFinals().getSeries(ComponentType.Irregular, ComponentInformation.Value)
));
System.out.println(table);
System.out.println(rslts.getSts().getBsm());
// System.out.println(table);
// System.out.println(rslts.getSts().getBsm());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ public void testVAT_NOBUG() {
M.mul(10);
CompositeModelEstimation rslt = model.estimate(M, false, false, SsfInitialization.SqrtDiffuse, Optimizer.LevenbergMarquardt, 1e-15, null);
StateStorage states = rslt.getSmoothedStates();
System.out.println(states.getComponent(0));
// System.out.println(states.getComponent(0));
// System.out.println(states.getComponent(2));
System.out.println(DoubleSeq.of(rslt.getFullParameters()));
// System.out.println(DoubleSeq.of(rslt.getFullParameters()));
// System.out.println(rslt.getLikelihood().factor());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package jdplus.sts.base.core.splines;

import jdplus.sts.base.core.splines.DailySpline;
import jdplus.sts.base.core.splines.IntSeq;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,43 +74,43 @@ public void testDouble2() {
assertTrue(n == 291000);
}

// @Test
// public void testNegative() {
// RegularSpline rs = RegularSpline.of(24.6);
// int n = 0;
// for (int i = 1; i < 11; ++i) {
// IntSeq observations = rs.observations(-i);
// int nc = observations.length();
// for (int j = 0; j < nc; ++j) {
// assertTrue(rs.cycleFor(observations.pos(j)) == -i);
// }
//// System.out.println(nc);
// n += nc;
// }
// assertTrue(n == 246);
// rs = RegularSpline.of(24);
// n = 0;
// for (int i = 1; i < 11; ++i) {
// IntSeq observations = rs.observations(-i);
// int nc = observations.length();
// for (int j = 0; j < nc; ++j) {
// assertTrue(rs.cycleFor(observations.pos(j)) == -i);
// }
//// System.out.println(nc);
// n += nc;
// }
// assertTrue(n == 240);
// rs = RegularSpline.of(24.25);
// n = 0;
// for (int i = 0; i < 12; ++i) {
// IntSeq observations = rs.observations(-i);
// int nc = observations.length();
// for (int j = 0; j < nc; ++j) {
// assertTrue(rs.cycleFor(observations.pos(j)) == -i);
// }
//// System.out.println(nc);
// n += nc;
// }
// assertTrue(n == 291);
// }
@Test
public void testNegative() {
RegularSpline rs = RegularSpline.of(24.6);
int n = 0;
for (int i = 1; i < 10001; ++i) {
IntSeq observations = rs.observations(-i);
int nc = observations.length();
for (int j = 0; j < nc; ++j) {
assertTrue(rs.cycleFor(observations.pos(j)) == -i);
}
// System.out.println(nc);
n += nc;
}
assertTrue(n == 246000);
rs = RegularSpline.of(24);
n = 0;
for (int i = 1; i < 10001; ++i) {
IntSeq observations = rs.observations(-i);
int nc = observations.length();
for (int j = 0; j < nc; ++j) {
assertTrue(rs.cycleFor(observations.pos(j)) == -i);
}
// System.out.println(nc);
n += nc;
}
assertTrue(n == 240000);
rs = RegularSpline.of(24.25);
n = 0;
for (int i = 0; i < 12000; ++i) {
IntSeq observations = rs.observations(-i);
int nc = observations.length();
for (int j = 0; j < nc; ++j) {
assertTrue(rs.cycleFor(observations.pos(j)) == -i);
}
// System.out.println(nc);
n += nc;
}
assertTrue(n == 291000);
}
}
Loading

0 comments on commit 5d7e6af

Please sign in to comment.