-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from ChristianeHofer/FractionalAirlineLogs
Fractional airline estimation extension
- Loading branch information
Showing
5 changed files
with
482 additions
and
259 deletions.
There are no files selected for viewing
319 changes: 164 additions & 155 deletions
319
...re/src/main/java/jdplus/highfreq/base/core/extendedairline/ExtendedAirlineEstimation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,155 +1,164 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package jdplus.highfreq.base.core.extendedairline; | ||
|
||
import jdplus.toolkit.base.api.data.DoubleSeq; | ||
import jdplus.toolkit.base.api.data.DoubleSeqCursor; | ||
import jdplus.highfreq.base.api.ExtendedAirline; | ||
import jdplus.toolkit.base.core.stats.likelihood.LikelihoodStatistics; | ||
import jdplus.toolkit.base.api.math.matrices.Matrix; | ||
import jdplus.toolkit.base.api.modelling.OutlierDescriptor; | ||
import jdplus.toolkit.base.api.information.GenericExplorable; | ||
|
||
/** | ||
* Low-level results. Should be refined | ||
* | ||
* @author palatej | ||
*/ | ||
@lombok.Value | ||
@lombok.Builder | ||
public class ExtendedAirlineEstimation implements GenericExplorable { | ||
|
||
double[] y; | ||
Matrix x; //user-def reg var, outlier | ||
|
||
ExtendedAirline model; | ||
|
||
OutlierDescriptor[] outliers; | ||
|
||
DoubleSeq coefficients; // user-def-var,outlier | ||
Matrix coefficientsCovariance; | ||
|
||
private DoubleSeq parameters, score; | ||
private Matrix parametersCovariance; | ||
|
||
LikelihoodStatistics likelihood; | ||
DoubleSeq residuals; | ||
|
||
public double[] linearized() { | ||
|
||
double[] l = y.clone(); | ||
DoubleSeqCursor acur = coefficients.cursor(); | ||
for (int j = 0; j < x.getColumnsCount(); ++j) { | ||
double a = acur.getAndNext(); | ||
if (a != 0) { | ||
DoubleSeqCursor cursor = x.column(j).cursor(); | ||
for (int k = 0; k < l.length; ++k) { | ||
l[k] -= a * cursor.getAndNext(); | ||
} | ||
} | ||
} | ||
return l; | ||
} | ||
|
||
public double[] component_userdef_reg_variables() { | ||
|
||
double[] l = new double[y.length]; | ||
DoubleSeqCursor acur = coefficients.cursor(); | ||
|
||
for (int j = 0; j < x.getColumnsCount() - outliers.length; ++j) { | ||
double a = acur.getAndNext(); | ||
if (a != 0) { | ||
DoubleSeqCursor cursor = x.column(j).cursor(); | ||
for (int k = 0; k < l.length; ++k) { | ||
l[k] += a * cursor.getAndNext(); | ||
} | ||
} | ||
} | ||
|
||
return l; | ||
} | ||
|
||
public double[] component_outliers() { | ||
|
||
double[] l = new double[y.length]; | ||
DoubleSeqCursor acur = coefficients.cursor(); | ||
|
||
for (int i = 1; i < x.getColumnsCount() - outliers.length+1; i++) { | ||
acur.getAndNext(); | ||
} | ||
|
||
for (int j = x.getColumnsCount() - outliers.length; j < x.getColumnsCount(); ++j) { | ||
|
||
double a = acur.getAndNext(); | ||
if (a != 0) { | ||
DoubleSeqCursor cursor = x.column(j).cursor(); | ||
for (int k = 0; k < l.length; ++k) { | ||
l[k] += a * cursor.getAndNext(); | ||
} | ||
} | ||
} | ||
|
||
return l; | ||
} | ||
|
||
public double[] component_ao() { | ||
return component_outlier("AO"); | ||
} | ||
|
||
public double[] component_wo() { | ||
return component_outlier("WO"); | ||
} | ||
|
||
public double[] component_ls() { | ||
return component_outlier("LS"); | ||
} | ||
|
||
/** | ||
* @return sum (coefficients*regression variable) if ao | ||
* | ||
*/ | ||
private double[] component_outlier(String outlierTyp) { | ||
|
||
double[] l = new double[y.length]; | ||
DoubleSeqCursor acur = coefficients.cursor(); | ||
|
||
for (int i = 1; i < x.getColumnsCount() - outliers.length+1; i++) { | ||
acur.getAndNext(); | ||
} | ||
|
||
for (int j = x.getColumnsCount() - outliers.length; j < x.getColumnsCount(); ++j) { | ||
double a = acur.getAndNext(); | ||
if (outlierTyp.equalsIgnoreCase(outliers[j - x.getColumnsCount() + outliers.length].getCode())) { | ||
|
||
if (a != 0) { | ||
DoubleSeqCursor cursor = x.column(j).cursor(); | ||
for (int k = 0; k < l.length; ++k) { | ||
l[k] += a * cursor.getAndNext(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return l; | ||
} | ||
|
||
|
||
public double[] tstats() { | ||
double[] t = coefficients.toArray(); | ||
if (t == null) { | ||
return null; | ||
} | ||
DoubleSeqCursor v = coefficientsCovariance.diagonal().cursor(); | ||
for (int i = 0; i < t.length; ++i) { | ||
t[i] /= Math.sqrt(v.getAndNext()); | ||
} | ||
return t; | ||
} | ||
|
||
public int getNx() { | ||
return coefficients == null ? 0 : coefficients.length(); | ||
} | ||
} | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package jdplus.highfreq.base.core.extendedairline; | ||
|
||
import jdplus.toolkit.base.api.data.DoubleSeq; | ||
import jdplus.toolkit.base.api.data.DoubleSeqCursor; | ||
import jdplus.highfreq.base.api.ExtendedAirline; | ||
import jdplus.toolkit.base.core.stats.likelihood.LikelihoodStatistics; | ||
import jdplus.toolkit.base.api.math.matrices.Matrix; | ||
import jdplus.toolkit.base.api.modelling.OutlierDescriptor; | ||
import jdplus.toolkit.base.api.information.GenericExplorable; | ||
|
||
/** | ||
* Low-level results. Should be refined | ||
* | ||
* @author palatej | ||
*/ | ||
@lombok.Value | ||
@lombok.Builder | ||
public class ExtendedAirlineEstimation implements GenericExplorable { | ||
|
||
double[] y; | ||
Matrix x; //user-def reg var, outlier | ||
boolean log; //true if logs are taken from y | ||
ExtendedAirline model; | ||
int[] missing;// positions where missing values are replaced, null based | ||
|
||
OutlierDescriptor[] outliers; | ||
|
||
DoubleSeq coefficients; // user-def-var,outlier | ||
Matrix coefficientsCovariance; | ||
|
||
private DoubleSeq parameters, score; | ||
private Matrix parametersCovariance; | ||
|
||
LikelihoodStatistics likelihood; | ||
DoubleSeq residuals; | ||
|
||
private double[] exp(double[] a) { | ||
DoubleSeq exp = DoubleSeq.of(a).exp(); | ||
return exp.toArray(); | ||
} | ||
|
||
public double[] getY() { | ||
return log ? exp(y) : y; | ||
} | ||
|
||
public double[] linearized() { | ||
|
||
double[] l = y.clone(); | ||
DoubleSeqCursor acur = coefficients.cursor(); | ||
for (int j = 0; j < x.getColumnsCount(); ++j) { | ||
double a = acur.getAndNext(); | ||
if (a != 0) { | ||
DoubleSeqCursor cursor = x.column(j).cursor(); | ||
for (int k = 0; k < l.length; ++k) { | ||
l[k] -= a * cursor.getAndNext(); | ||
} | ||
} | ||
} | ||
return l; | ||
} | ||
|
||
public double[] component_userdef_reg_variables() { | ||
|
||
double[] l = new double[y.length]; | ||
DoubleSeqCursor acur = coefficients.cursor(); | ||
|
||
for (int j = 0; j < x.getColumnsCount() - outliers.length; ++j) { | ||
double a = acur.getAndNext(); | ||
if (a != 0) { | ||
DoubleSeqCursor cursor = x.column(j).cursor(); | ||
for (int k = 0; k < l.length; ++k) { | ||
l[k] += a * cursor.getAndNext(); | ||
} | ||
} | ||
} | ||
|
||
return log ? exp(l) : l; | ||
} | ||
|
||
public double[] component_outliers() { | ||
|
||
double[] l = new double[y.length]; | ||
DoubleSeqCursor acur = coefficients.cursor(); | ||
|
||
for (int i = 1; i < x.getColumnsCount() - outliers.length + 1; i++) { | ||
acur.getAndNext(); | ||
} | ||
|
||
for (int j = x.getColumnsCount() - outliers.length; j < x.getColumnsCount(); ++j) { | ||
|
||
double a = acur.getAndNext(); | ||
if (a != 0) { | ||
DoubleSeqCursor cursor = x.column(j).cursor(); | ||
for (int k = 0; k < l.length; ++k) { | ||
l[k] += a * cursor.getAndNext(); | ||
} | ||
} | ||
} | ||
|
||
return log ? exp(l) : l; | ||
} | ||
|
||
public double[] component_ao() { | ||
return component_outlier("AO"); | ||
} | ||
|
||
public double[] component_wo() { | ||
return component_outlier("WO"); | ||
} | ||
|
||
public double[] component_ls() { | ||
return component_outlier("LS"); | ||
} | ||
|
||
/** | ||
* @return sum (coefficients*regression variable) if ao | ||
* | ||
*/ | ||
private double[] component_outlier(String outlierTyp) { | ||
|
||
double[] l = new double[y.length]; | ||
DoubleSeqCursor acur = coefficients.cursor(); | ||
|
||
for (int i = 1; i < x.getColumnsCount() - outliers.length + 1; i++) { | ||
acur.getAndNext(); | ||
} | ||
|
||
for (int j = x.getColumnsCount() - outliers.length; j < x.getColumnsCount(); ++j) { | ||
double a = acur.getAndNext(); | ||
if (outlierTyp.equalsIgnoreCase(outliers[j - x.getColumnsCount() + outliers.length].getCode())) { | ||
|
||
if (a != 0) { | ||
DoubleSeqCursor cursor = x.column(j).cursor(); | ||
for (int k = 0; k < l.length; ++k) { | ||
l[k] += a * cursor.getAndNext(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return log ? exp(l) : l; | ||
} | ||
|
||
public double[] tstats() { | ||
double[] t = coefficients.toArray(); | ||
if (t == null) { | ||
return null; | ||
} | ||
DoubleSeqCursor v = coefficientsCovariance.diagonal().cursor(); | ||
for (int i = 0; i < t.length; ++i) { | ||
t[i] /= Math.sqrt(v.getAndNext()); | ||
} | ||
return t; | ||
} | ||
|
||
public int getNx() { | ||
return coefficients == null ? 0 : coefficients.length(); | ||
} | ||
} |
Oops, something went wrong.