// TODO Update with Java 8 private static boolean isPositive(DataBlock q) { for (int i = 0; i < q.getLength(); ++i) { if (q.get(i) < State.ZERO) { return false; } } return true; }
/** * @param t * @param state */ @Override public void save(final int t, final MState state) { m_var.save(t, state); m_fdata.save(t, state); DataBlock diag = state.F.diagonal(); for (int i = 0; i < state.E.getLength(); ++i) { double r = diag.get(i); if (r != 0) { addStd(state.E.get(i), r); } } }
private int check(DataBlock z) { int idx = -1; for (int i = 0; i < z.getLength(); ++i) { if (z.get(i) != 0) { if (idx != -1) { return -1; } else { idx = i; } } } return idx; }
@Override public void save(final int t, final AugmentedPredictionErrors pe) { DataBlock U = pe.getTransformedPredictionErrors(); Matrix L = pe.getCholeskyFactor(); DataBlock D = L.diagonal(); Matrix E = pe.E(); int nvars = E.getColumnsCount(); n += nvars; LogSign sld = D.sumLog(); det += sld.value; Q.subMatrix(0, nd, nd + 1, nd + 1 + nvars).copy(E.subMatrix()); Q.row(nd).range(nd + 1, nd + 1 + nvars).copy(U); ec.tstoolkit.maths.matrices.ElementaryTransformations.fastGivensTriangularize(Q.subMatrix()); }
double[] scomponent() { DisturbanceSmoother smoother = new DisturbanceSmoother(); smoother.setSsf(ssf); smoother.process(ssfdata); SmoothingResults sstates = smoother.calcSmoothedStates(); double[] cmp = new double[ssfdata.getCount()]; DataBlock z = new DataBlock(ssf.getStateDim()); DataBlock zc = z.drop(2, 0); for (int i = 0; i < cmp.length; ++i) { zc.copy(S.row(i)); cmp[i] = sstates.zcomponent(i, z); } return cmp; }
/** * @param z * @return */ public double[] zvariance(DataBlock z) { if (m_P == null) { return null; } if (m_r != z.getLength()) { return null; } int iz = check(z); double[] var = new double[m_n - m_start]; if (iz >= 0) { return componentVar(iz); } else { for (int i = 0; i < m_n - m_start; ++i) { var[i] = SymmetricMatrix.quadraticForm(m_P.matrix(i), z) * m_ser2; } } return var; }
/** * @param z * @return */ public double[] zcomponent(DataBlock z) { int iz = check(z); if (iz >= 0) { return component(iz); } if (m_a == null) { return null; } if (m_r != z.getLength()) { return null; } int n = m_n - m_start; double[] c = new double[n]; for (int i = 0; i < n; ++i) { c[i] = m_a.block(i).dot(z); } return c; }
/** * @param s * @param domain * @return */ @Override public TsData process(TsData s, TsDomain domain) { TsDomain rdomain = domain == null ? s.getDomain() : domain; TsData out = new TsData(rdomain); PeriodIterator pin = new PeriodIterator(s, domain); PeriodIterator pout = new PeriodIterator(out); int p = 0; while (pin.hasMoreElements()) { DataBlock bin = pin.nextElement().data; DataBlock bout = pout.nextElement().data; int nf = 0, len = bin.getLength(); if (filters[p] != null) { nf = filters[p].getUpperBound(); } if (filters[p] != null && 2 * nf < len && (nf < 9 || len >= 20)) { filters[p].filter(bin, bout.drop(nf, nf)); endPointsProcessors[p].process(bin, bout); } else { bout.set(bin.sum() / len); } ++p; } return out; }
@Override public void XS(int pos, DataBlock x, DataBlock xs) { xs.set(0, std() * x.get(0)); }
@Override public void addSU(int pos, DataBlock x, DataBlock u) { x.add(0, std() * u.get(0)); }
// time-varying trading days // @Test public void demoTD() { TsData s = Data.X; CompositeResults rslts = TramoSeatsProcessingFactory.process(s, TramoSeatsSpecification.RSA5); PreprocessingModel regarima = rslts.get("preprocessing", PreprocessingModel.class); SeatsResults seats = rslts.get("decomposition", SeatsResults.class); assertTrue(seats != null && regarima != null); if (regarima.isMultiplicative()) { s = s.log(); } int[] calPos = regarima.description.getRegressionVariablePositions(ComponentType.CalendarEffect); UcarimaModel ucm = seats.getUcarimaModel(); // compute the full decomposition... SsfUcarima stoch = new SsfUcarima(ucm); ExtendedSsfData xdata = new ExtendedSsfData(new SsfData(s, null)); xdata.setForecastsCount(s.getFrequency().intValue()); Matrix x = regarima .description .buildRegressionVariables() .all() .matrix(new TsDomain(s.getStart(), xdata.getCount())); RegSsf xssf = new RegSsf(stoch, x.subMatrix()); Filter filter = new Filter(); filter.setInitializer(new DiffuseSquareRootInitializer()); filter.setSsf(xssf); DiffuseFilteringResults fr = new DiffuseFilteringResults(true); fr.getVarianceFilter().setSavingP(true); fr.getFilteredData().setSavingA(true); filter.process(xdata, fr); Smoother smoother = new Smoother(); smoother.setSsf(xssf); smoother.setCalcVar(true); SmoothingResults sm = new SmoothingResults(); smoother.process(xdata, fr, sm); Smoother lsmoother = new Smoother(); lsmoother.setSsf(stoch); lsmoother.setCalcVar(true); SmoothingResults lsm = new SmoothingResults(); ExtendedSsfData xldata = new ExtendedSsfData(new SsfData(regarima.linearizedSeries(false), null)); xldata.setForecastsCount(s.getFrequency().intValue()); lsmoother.process(xldata, lsm); int spos = stoch.cmpPos(1); DataBlock Z = new DataBlock(xssf.getStateDim()); double[] v = new double[xdata.getCount()]; double[] c = new double[xdata.getCount()]; double[] svar = sm.componentVar(spos); double[] slvar = lsm.componentVar(spos); int start = regarima.description.getRegressionVariablesStartingPosition(); for (int i = 0; i < v.length; ++i) { Z.set(spos, 1); for (int j = 0; j < calPos.length; ++j) { Z.set(stoch.getStateDim() + calPos[j], x.get(i, calPos[j])); } v[i] = sm.zvariance(i, Z); Z.set(spos, 0); c[i] = sm.zvariance(i, Z); System.out.print(svar[i]); System.out.print('\t'); System.out.print(slvar[i]); System.out.print('\t'); System.out.print(c[i]); System.out.print('\t'); System.out.println(v[i]); } System.out.println(sm.P(50)); System.out.println(sm.P(svar.length - 1)); System.out.println(regarima.estimation.getLikelihood().getBVar()); }