Skip to content

Commit

Permalink
Merge pull request #166 from palatej/develop
Browse files Browse the repository at this point in the history
Small improvements for the SSF (link with R)
  • Loading branch information
palatej authored Jul 10, 2024
2 parents 088f60d + d976e5d commit 415150b
Show file tree
Hide file tree
Showing 32 changed files with 459 additions and 225 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- ![STAT] Add default matrices for the different state blocks
- ![OTHER] Add methods for an easier access to the state blocks from R

### Fixed

- ![OTHER] Fix category of advancedsa plugin
- ![OTHER] Fix invalid OpenIDE-Module-Install in jdplus-advancedsa-desktop-plugin [#154](https://github.com/jdemetra/jdplus-incubator/issues/154)
- ![UI] Fix the names of periodic components in high-frequency

## [2.1.0] - 2024-04-15

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public ArInterpreter(final String name, int degree, double defValue) {
this.domain = new Domain(degree);
}

public DoubleSeq values(){
return DoubleSeq.of(values);
}

@Override
public int dim(){
return values.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
@lombok.experimental.UtilityClass
public class AtomicModels {

public StateItem arma(final String name, double[] ar, double[] ma, double var, boolean fixed) {
return new ArmaItem(name, ar, ma, var, fixed);
public StateItem arma(final String name, double[] ar, boolean fixedar, double[] ma, boolean fixedma, double var, boolean fixedvar) {
return new ArmaItem(name, ar, fixedar, ma, fixedma, var, fixedvar);
}

public StateItem sarima(final String name, int period, int[] orders, int[] seasonal, double[] parameters, boolean fixed, double var, boolean fixedvar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ private BoundedParameterInterpreter(final String name, double value, boolean fix
this.fixed = fixed;
this.range = new ParametersRange(lbound, ubound, open);
}

public double value(){
return value;
}

@Override
public BoundedParameterInterpreter duplicate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import jdplus.toolkit.base.api.data.DoubleSeqCursor;
import jdplus.toolkit.base.core.sarima.estimation.SarimaMapping;
import jdplus.toolkit.base.api.arima.SarimaOrders;
import jdplus.toolkit.base.api.data.DoubleSeq;

/**
*
Expand All @@ -40,6 +41,10 @@ public SarimaInterpreter(final String name, final SarimaOrders spec, final doubl
this.np = spec.getParametersCount();
this.fixed = fixed;
}

public DoubleSeq values(){
return DoubleSeq.of(values);
}

@Override
public SarimaInterpreter duplicate(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public StablePolynomialInterpreter(final String name, @NonNull double[] values,
this.fixed = fixed;
this.domain = new Domain(values.length);
}

public DoubleSeq values(){
return DoubleSeq.of(values);
}

@Override
public StablePolynomialInterpreter duplicate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,34 @@ public AggregationItem(String name, StateItem[] cmps) {
super(name);
this.cmps = cmps;
}

@Override
public AggregationItem duplicate(){
StateItem[] citems=new StateItem[cmps.length];
for (int i=0; i<citems.length; ++i)
citems[i]=cmps[i].duplicate();
public AggregationItem duplicate() {
StateItem[] citems = new StateItem[cmps.length];
for (int i = 0; i < citems.length; ++i) {
citems[i] = cmps[i].duplicate();
}
return new AggregationItem(name, citems);
}

@Override
public StateComponent build(DoubleSeq p) {
CompositeState.Builder builder = CompositeState.builder();
int pos = 0;
for (int i = 0; i < cmps.length; ++i) {
int n = cmps[i].parametersCount();
builder.add(cmps[i].build(p.extract(pos, n)));
pos += n;
if (p == null) {
CompositeState.Builder builder = CompositeState.builder();
for (int i = 0; i < cmps.length; ++i) {
builder.add(cmps[i].build(null));
}
return builder.build();
} else {
CompositeState.Builder builder = CompositeState.builder();
int pos = 0;
for (int i = 0; i < cmps.length; ++i) {
int n = cmps[i].parametersCount();
builder.add(cmps[i].build(p.extract(pos, n)));
pos += n;
}
return builder.build();
}
return builder.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,14 @@ public List<ParameterInterpreter> parameters() {

@Override
public StateComponent build(DoubleSeq p) {
int n = ar.getDomain().getDim();
double[] par = p.extract(0, n).toArray();
double w = p.get(n);
return SsfAr.of(par, w, nlags, zeroinit);
if (p == null) {
return SsfAr.of(ar.values().toArray(), v.variance(), nlags, zeroinit);
} else {
int n = ar.getDomain().getDim();
double[] par = p.extract(0, n).toArray();
double w = p.get(n);
return SsfAr.of(par, w, nlags, zeroinit);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ public ArItem2(String name, double[] ar, boolean fixedar, double var, boolean fi
this.ar = new ArInterpreter(name + ".ar", ar, fixedar);
this.v = new VarianceInterpreter(name + ".var", var, fixedvar, true);
}
private ArItem2(ArItem2 item){

private ArItem2(ArItem2 item) {
super(item.name);
this.ar=item.ar.duplicate();
this.v=item.v.duplicate();
this.nlags=item.nlags;
this.nfcasts=item.nfcasts;
this.ar = item.ar.duplicate();
this.v = item.v.duplicate();
this.nlags = item.nlags;
this.nfcasts = item.nfcasts;
}

@Override
public ArItem2 duplicate(){
public ArItem2 duplicate() {
return new ArItem2(this);
}

Expand All @@ -80,10 +80,14 @@ public List<ParameterInterpreter> parameters() {

@Override
public StateComponent build(DoubleSeq p) {
int n = ar.getDomain().getDim();
double[] par = p.extract(0, n).toArray();
double w = p.get(n);
return SsfAr2.of(par, w, nlags, nfcasts);
if (p == null) {
return SsfAr2.of(ar.values().toArray(), v.variance(), nlags, nfcasts);
} else {
int n = ar.getDomain().getDim();
double[] par = p.extract(0, n).toArray();
double w = p.get(n);
return SsfAr2.of(par, w, nlags, nfcasts);
}
}

@Override
Expand All @@ -105,7 +109,7 @@ public int defaultLoadingCount() {
public int stateDim() {
int n = ar.getDomain().getDim();
if (nfcasts >= n) {
n = nfcasts+1;
n = nfcasts + 1;
}
return n + nlags;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ public ArimaItem(String name, double[] ar, boolean fixedar, double[] diff, doubl
bdiff = BackFilter.ONE;
}
}
private ArimaItem(ArimaItem item){

private ArimaItem(ArimaItem item) {
super(item.name);
this.par=item.par == null ? null : item.par.duplicate();
this.pma=item.pma == null ? null : item.pma.duplicate();
this.v=item.v.duplicate();
this.bdiff=item.bdiff;
this.par = item.par == null ? null : item.par.duplicate();
this.pma = item.pma == null ? null : item.pma.duplicate();
this.v = item.v.duplicate();
this.bdiff = item.bdiff;
}

@Override
public ArimaItem duplicate(){
public ArimaItem duplicate() {
return new ArimaItem(this);
}

Expand Down Expand Up @@ -123,22 +123,36 @@ public List<ParameterInterpreter> parameters() {
@Override
public StateComponent build(DoubleSeq p) {
BackFilter bar = BackFilter.ONE, bma = BackFilter.ONE;
int pos = 0;
if (par != null) {
int nar = par.getDomain().getDim();
Polynomial ar = Polynomial.valueOf(1, p.extract(0, nar).toArray());
bar = new BackFilter(ar);
pos += nar;
}
if (pma != null) {
int nma = pma.getDomain().getDim();
Polynomial ma = Polynomial.valueOf(1, p.extract(0, nma).toArray());
bma = new BackFilter(ma);
pos += nma;
if (p == null) {
if (par != null) {
Polynomial ar = Polynomial.valueOf(1, par.values().toArray());
bar = new BackFilter(ar);
}
if (pma != null) {
Polynomial ma = Polynomial.valueOf(1, pma.values().toArray());
bma = new BackFilter(ma);
}
double var = v.variance();
ArimaModel arima = new ArimaModel(bar, bdiff, bma, var);
return SsfArima.stateComponent(arima);
} else {
int pos = 0;
if (par != null) {
int nar = par.getDomain().getDim();
Polynomial ar = Polynomial.valueOf(1, p.extract(0, nar).toArray());
bar = new BackFilter(ar);
pos += nar;
}
if (pma != null) {
int nma = pma.getDomain().getDim();
Polynomial ma = Polynomial.valueOf(1, p.extract(0, nma).toArray());
bma = new BackFilter(ma);
pos += nma;
}
double var = p.get(pos++);
ArimaModel arima = new ArimaModel(bar, bdiff, bma, var);
return SsfArima.stateComponent(arima);
}
double var = p.get(pos++);
ArimaModel arima = new ArimaModel(bar, bdiff, bma, var);
return SsfArima.stateComponent(arima);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,31 @@ public class ArmaItem extends StateItem {
private final StablePolynomialInterpreter par, pma;
private final VarianceInterpreter v;

public ArmaItem(final String name, double[] ar, double[] ma, double var, boolean fixed) {
public ArmaItem(final String name, double[] ar, boolean fixedar, double[] ma, boolean fixedma, double var, boolean fixedvar) {
super(name);
int nar = ar == null ? 0 : ar.length, nma = ma == null ? 0 : ma.length;
if (nar > 0) {
par = new StablePolynomialInterpreter(name + ".ar", ar, fixed);
par = new StablePolynomialInterpreter(name + ".ar", ar, fixedar);
} else {
par = null;
}
if (nma > 0) {
pma = new StablePolynomialInterpreter(name + ".ma", ma, fixed);
pma = new StablePolynomialInterpreter(name + ".ma", ma, fixedma);
} else {
pma = null;
}
v = new VarianceInterpreter(name + ".var", var, true, true);
v = new VarianceInterpreter(name + ".var", var, fixedvar, true);
}

private ArmaItem(ArmaItem item){
private ArmaItem(ArmaItem item) {
super(item.name);
this.par=item.par == null ? null : item.par.duplicate();
this.pma=item.pma == null ? null : item.pma.duplicate();
this.v=item.v.duplicate();
}
this.par = item.par == null ? null : item.par.duplicate();
this.pma = item.pma == null ? null : item.pma.duplicate();
this.v = item.v.duplicate();
}

@Override
public ArmaItem duplicate(){
public ArmaItem duplicate() {
return new ArmaItem(this);
}

Expand Down Expand Up @@ -108,22 +108,36 @@ public List<ParameterInterpreter> parameters() {
@Override
public StateComponent build(DoubleSeq p) {
BackFilter bar = BackFilter.ONE, bma = BackFilter.ONE;
int pos = 0;
if (par != null) {
int nar = par.getDomain().getDim();
Polynomial ar = Polynomial.valueOf(1, p.extract(0, nar).toArray());
bar = new BackFilter(ar);
pos += nar;
}
if (pma != null) {
int nma = pma.getDomain().getDim();
Polynomial ma = Polynomial.valueOf(1, p.extract(0, nma).toArray());
bma = new BackFilter(ma);
pos += nma;
if (p == null) {
if (par != null) {
Polynomial ar = Polynomial.valueOf(1, par.values().toArray());
bar = new BackFilter(ar);
}
if (pma != null) {
Polynomial ma = Polynomial.valueOf(1, pma.values().toArray());
bma = new BackFilter(ma);
}
ArimaModel arima = new ArimaModel(bar, BackFilter.ONE, bma, v.variance());
return SsfArima.stateComponent(arima);

} else {
int pos = 0;
if (par != null) {
int nar = par.getDomain().getDim();
Polynomial ar = Polynomial.valueOf(1, p.extract(0, nar).toArray());
bar = new BackFilter(ar);
pos += nar;
}
if (pma != null) {
int nma = pma.getDomain().getDim();
Polynomial ma = Polynomial.valueOf(1, p.extract(0, nma).toArray());
bma = new BackFilter(ma);
pos += nma;
}
double n = p.get(pos++);
ArimaModel arima = new ArimaModel(bar, BackFilter.ONE, bma, n);
return SsfArima.stateComponent(arima);
}
double n = p.get(pos++);
ArimaModel arima = new ArimaModel(bar, BackFilter.ONE, bma, n);
return SsfArima.stateComponent(arima);
}

@Override
Expand All @@ -149,7 +163,7 @@ public ISsfLoading defaultLoading(int m) {
public int defaultLoadingCount() {
return 1;
}

@Override
public int stateDim() {
int p = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ public List<ParameterInterpreter> parameters() {

@Override
public StateComponent build(DoubleSeq p) {
if (p == null){
return CyclicalComponent.stateComponent(factor.value(), period.value(), v.variance());
}else{
double f = p.get(0), l = p.get(1), var = p.get(2);
return CyclicalComponent.stateComponent(f, l, var);
}
}

@Override
Expand Down
Loading

0 comments on commit 415150b

Please sign in to comment.