use of com.alibaba.alink.common.linalg.DenseVector in project Alink by alibaba.
the class DeepARModelMapper method predictMultiVar.
@Override
protected Tuple2<Vector[], String> predictMultiVar(Timestamp[] historyTimes, Vector[] historyVals, int predictNum) {
Timestamp[] predictTimes = TimeSeriesMapper.getPredictTimes(historyTimes, predictNum);
int window = historyVals.length;
int series = 0;
DenseVector[] vectors = new DenseVector[historyVals.length];
for (int i = 0; i < window; ++i) {
vectors[i] = VectorUtil.getDenseVector(historyVals[i]);
if (vectors[i] == null) {
throw new IllegalArgumentException("history values should not be null.");
}
series = vectors[i].size();
}
FloatTensor[][] tensors = new FloatTensor[series][window];
for (int i = 0; i < series; ++i) {
tensors[i][0] = Tensor.cat(new FloatTensor[] { new FloatTensor(new float[] { 0.0f }), DeepARFeaturesGenerator.generateFromFrequency(calendar.get(), unit, historyTimes[0]) }, -1, null);
for (int j = 1; j < window; ++j) {
tensors[i][j] = Tensor.cat(new FloatTensor[] { new FloatTensor(new float[] { (float) vectors[j - 1].get(i) }), DeepARFeaturesGenerator.generateFromFrequency(calendar.get(), unit, historyTimes[j]) }, -1, null);
}
}
FloatTensor[] batch = new FloatTensor[series];
for (int i = 0; i < series; ++i) {
batch[i] = Tensor.stack(tensors[i], 0, null);
}
Vector[] result = new Vector[predictNum];
Row[] sigmas = new Row[predictNum];
for (int i = 0; i < predictNum; ++i) {
result[i] = new DenseVector(series);
sigmas[i] = Row.of(new DenseVector(series));
}
for (int i = 0; i < series; ++i) {
float mu = (float) historyVals[window - 1].get(i);
FloatTensor v = new FloatTensor(new float[] { 0.0f, 0.0f });
int nonZero = 0;
for (int j = 0; j < window; ++j) {
float cell = batch[i].getFloat(j, 0);
if (cell != 0) {
nonZero += 1;
}
v.setFloat(v.getFloat(0) + cell, 0);
}
if (mu != 0) {
nonZero += 1;
v.setFloat(v.getFloat(0) + mu, 0);
}
if (nonZero == 0) {
continue;
}
v.setFloat(v.getFloat(0) / nonZero + 1.0f, 0);
for (int j = 0; j < window; ++j) {
batch[i].setFloat(batch[i].getFloat(j, 0) / v.getFloat(0), j, 0);
}
mu = mu / v.getFloat(0);
for (int j = 0; j < predictNum; ++j) {
batch[i] = Tensor.cat(new FloatTensor[] { batch[i], Tensor.stack(new FloatTensor[] { Tensor.cat(new FloatTensor[] { new FloatTensor(new float[] { mu }), DeepARFeaturesGenerator.generateFromFrequency(calendar.get(), unit, predictTimes[j]) }, -1, null) }, 0, null) }, 0, null);
FloatTensor pred;
try {
pred = (FloatTensor) tfTableModelPredictModelMapper.map(Row.of(batch[i])).getField(0);
} catch (Exception e) {
return Tuple2.of(null, null);
}
mu = pred.getFloat(window + j, 0);
float sigma = pred.getFloat(window + j, 1);
result[j].set(i, mu * v.getFloat(0) + v.getFloat(1));
((Vector) (sigmas[j].getField(0))).set(i, sigma * v.getFloat(0));
}
}
return Tuple2.of(result, new MTable(Arrays.asList(sigmas), new String[] { "sigma" }, new TypeInformation<?>[] { VectorTypes.DENSE_VECTOR }).toString());
}
use of com.alibaba.alink.common.linalg.DenseVector in project Alink by alibaba.
the class ExpandExtendedVarsMapper method map.
@Override
protected void map(SlicedSelectedSample selection, SlicedResult result) throws Exception {
final DenseVector vector = VectorUtil.getDenseVector(VectorUtil.getVector(selection.get(0)));
final DenseVector cVector = VectorUtil.getDenseVector(VectorUtil.getVector(selection.get(1)));
if (vector == null) {
result.set(0, null);
return;
}
if (cVector == null) {
result.set(0, vector);
return;
}
final int size = vector.size();
final int cSize = cVector.size();
final int gSize = size / numVars;
final int cPlusSize = cSize + numVars;
final int finalSize = gSize * cPlusSize;
final double[] array = vector.getData();
final double[] cArray = cVector.getData();
final double[] buf = new double[finalSize];
for (int i = 0; i < gSize; ++i) {
final int s = i * cPlusSize;
for (int j = i * numVars, j1 = s; j < (i + 1) * numVars; ++j, ++j1) {
buf[j1] = array[j];
}
for (int j = 0, j1 = s + numVars; j < cSize; ++j, ++j1) {
buf[j1] = cArray[j];
}
}
result.set(0, new DenseVector(buf));
}
use of com.alibaba.alink.common.linalg.DenseVector in project Alink by alibaba.
the class LookupVectorInTimeSeriesMapper method map.
@Override
protected void map(SlicedSelectedSample selection, SlicedResult result) throws Exception {
MTable mTable = null;
if (selection.get(1) == null) {
result.set(0, null);
return;
}
if (selection.get(1) instanceof MTable) {
mTable = (MTable) selection.get(1);
} else {
mTable = new MTable((String) selection.get(1));
}
if (mTable.getNumRow() == 0) {
result.set(0, null);
return;
}
Timestamp lookupTime = (Timestamp) selection.get(0);
TableSchema schema = mTable.getTableSchema();
String timeCol = null;
String vectorCol = null;
int timeIdx = -1;
TypeInformation<?>[] colTypes = schema.getFieldTypes();
for (int i = 0; i < colTypes.length; i++) {
if (colTypes[i] == Types.SQL_TIMESTAMP) {
timeCol = schema.getFieldNames()[i];
timeIdx = i;
}
if (colTypes[i] == VectorTypes.VECTOR || colTypes[i] == VectorTypes.DENSE_VECTOR || colTypes[i] == VectorTypes.SPARSE_VECTOR) {
vectorCol = schema.getFieldNames()[i];
}
}
if (null != timeCol && null != vectorCol) {
List<Object> times = MTableUtils.getColumn(mTable, timeCol);
int idxRow = times.indexOf(lookupTime);
int idxCol = TableUtil.findColIndex(schema, vectorCol);
if (idxRow >= 0) {
result.set(0, mTable.getEntry(idxRow, idxCol));
return;
} else {
mTable.orderBy(timeIdx);
Timestamp[] timesArr = MTableUtils.getColumn(mTable, timeCol).toArray(new Timestamp[] {});
int pos = Arrays.binarySearch(timesArr, lookupTime);
if (pos == -1) {
result.set(0, mTable.getEntry(0, idxCol));
} else if (-pos == timesArr.length + 1) {
result.set(0, mTable.getEntry(timesArr.length - 1, idxCol));
} else {
int pos0 = -2 - pos;
int pos1 = -1 - pos;
long time0 = timesArr[pos0].getTime();
long time1 = timesArr[pos1].getTime();
double scale = (double) (lookupTime.getTime() - time0) / (double) (time1 - time0);
DenseVector inter = ((DenseVector) mTable.getEntry(pos0, idxCol)).scale(1 - scale);
inter.plusEqual(((DenseVector) mTable.getEntry(pos1, idxCol)).scale(scale));
result.set(0, inter);
}
return;
}
}
result.set(0, null);
}
use of com.alibaba.alink.common.linalg.DenseVector in project Alink by alibaba.
the class MleEstimate method bfgsEstimateParams.
public void bfgsEstimateParams(double[] data, int aOrder, int bOrder) {
double mean = TsMethod.mean(data);
for (int i = 0; i < data.length; i++) {
data[i] -= mean;
}
this.aOrder = aOrder;
this.bOrder = bOrder;
// initiation of parameters
double[] initAlpha = new double[aOrder];
double[] initBeta = new double[bOrder];
for (int i = 0; i < aOrder; i++) {
initAlpha[i] = 0.1;
}
for (int i = 0; i < bOrder; i++) {
initBeta[i] = 0.1;
}
double var = 0;
for (double aData : data) {
var += aData * aData;
}
var = var / data.length;
double sumAlpha = 0;
double sumBeta = 0;
for (double alpha : initAlpha) {
sumAlpha += alpha * alpha;
}
for (double beta : initBeta) {
sumBeta += beta * beta;
}
double initC = Math.sqrt(var * (1 - sumAlpha - sumBeta));
double[] ch = new double[data.length];
for (int i = 0; i < bOrder; i++) {
ch[i] = var;
}
// ///////-----------------------
// int m = this.aOrder + this.bOrder + 1 + 1;
int m = this.aOrder + this.bOrder + 1;
DenseVector lowerBound = DenseVector.zeros(m);
DenseVector upperBound = DenseVector.ones(m);
lowerBound.set(m - 1, var * 1e-8);
upperBound.set(m - 1, var * 10);
// lowerBound.set(m - 2, var * 1e-8);
// upperBound.set(m - 2, var * 10);
// lowerBound.set(m - 2, Double.NEGATIVE_INFINITY);
// upperBound.set(m - 2, Double.POSITIVE_INFINITY);
GarchGradientTarget garchGradientTarget = new GarchGradientTarget();
garchGradientTarget.fit(initAlpha, initBeta, initC, data, ch);
DenseVector result = TimeSeriesUtils.calcGarchLBFGSB(lowerBound, upperBound, garchGradientTarget, data, 500, 1e-8);
this.alpha = new double[aOrder];
this.beta = new double[bOrder];
for (int i = 0; i < aOrder; i++) {
alpha[i] = result.get(i);
}
for (int i = 0; i < bOrder; i++) {
beta[i] = result.get(i + aOrder) * result.get(i + aOrder);
}
this.c = result.get(aOrder + bOrder) * result.get(aOrder + bOrder);
this.h = garchGradientTarget.residual;
// /////////-----------------------
// //find parameters
// GarchGradientTarget garchProblem = new GarchGradientTarget();
// garchProblem.fit(initAlpha, initBeta, initC, data, ch);
// AbstractGradientTarget problem = BFGS.solve(
// garchProblem, 1000, 0.00001,
// 0.000001, new int[] {2, 3}, -1);
//
// this.alpha = new double[aOrder];
// this.beta = new double[bOrder];
// DenseMatrix result = problem.getFinalCoef();
// for (int i = 0; i < aOrder; i++) {
// alpha[i] = result.get(i, 0) * result.get(i, 0);
// }
// for (int i = 0; i < bOrder; i++) {
// beta[i] = result.get(i + aOrder, 0) * result.get(i + aOrder, 0);
// }
// this.c = result.get(aOrder + bOrder, 0) * result.get(aOrder + bOrder, 0);
// this.h = problem.getResidual();
// this.loglike = -problem.getMinValue();
// this.warn = problem.getWarn();
//
// DenseMatrix obsInformation = problem.getH();
// this.alphaStdError = new double[aOrder];
// this.betaStdError = new double[bOrder];
// for (int i = 0; i < aOrder; i++) {
// this.alphaStdError[i] = Math.sqrt(obsInformation.get(i, i));
// if (Double.isNaN(this.alphaStdError[i])) {
// this.alphaStdError[i] = -99;
// }
//
// }
// for (int i = 0; i < bOrder; i++) {
// this.betaStdError[i] = Math.sqrt(obsInformation.get(i + aOrder, i + aOrder));
// if (Double.isNaN(this.betaStdError[i])) {
// this.betaStdError[i] = -99;
// }
// }
// this.cStdError = Math.sqrt(obsInformation.get(aOrder + bOrder, aOrder + bOrder));
// if (Double.isNaN(this.cStdError)) {
// this.cStdError = -99;
// }
//
// sumAlpha = 0;
// sumBeta = 0;
// for (int i = 0; i < this.aOrder; i++) {
// sumAlpha += this.alpha[i];
// }
// for (int i = 0; i < this.bOrder; i++) {
// sumBeta += this.beta[i];
// }
//
// this.unconSigma2 = this.c / (1 - sumAlpha - sumBeta);
// this.residual = new double[this.h.length];
// for (int i = 0; i < h.length; i++) {
// this.residual[i] = data[i] / h[i];
// }
}
use of com.alibaba.alink.common.linalg.DenseVector in project Alink by alibaba.
the class HoltWinters method fit.
/**
* holt winters fit.
*
* @param data time series data.
* @param frequency time series frequency
* @param alpha alpha
* @param beta beta
* @param gamma gamma
* @param doTrend doTrend
* @param doSeasonal doSeasonal
* @param seasonalType seasonalType
* @param levelStart levelStart
* @param trendStart trendStart
* @param seasonalStart seasonalStart
* @return HoltWintersModel
*/
public static HoltWintersModel fit(double[] data, int frequency, double alpha, double beta, double gamma, boolean doTrend, boolean doSeasonal, SeasonalType seasonalType, Double levelStart, Double trendStart, double[] seasonalStart) {
if (data == null) {
throw new RuntimeException("Data is null.");
}
for (double val : data) {
if (Double.isNaN(val) || Double.isInfinite(val)) {
return null;
}
}
Tuple3<Double, Double, double[]> starts = initStart(data, frequency, doTrend, doSeasonal, seasonalType, levelStart, trendStart, seasonalStart);
levelStart = starts.f0;
trendStart = starts.f1;
seasonalStart = starts.f2;
DenseVector coefs;
try {
coefs = train(data, frequency, alpha, beta, gamma, doTrend, doSeasonal, seasonalType, levelStart, trendStart, seasonalStart);
} catch (Throwable ex) {
for (int i = 0; i < data.length; i++) {
data[i] += 1e-10;
}
coefs = train(data, frequency, alpha, beta, gamma, doTrend, doSeasonal, seasonalType, levelStart, trendStart, seasonalStart);
}
boolean isAddType = seasonalType == SeasonalType.ADDITIVE;
Tuple3<Double, Double, double[]> initialData = HoltWintersUtil.calculateInitialData(data, coefs.getData(), isAddType, frequency, levelStart, trendStart, seasonalStart);
HoltWintersModel model = new HoltWintersModel();
model.levelStart = initialData.f0;
model.trendStart = initialData.f1;
model.seasonalStart = initialData.f2;
model.coefs = coefs.getData();
model.seasonalType = seasonalType;
model.frequency = frequency;
return model;
}
Aggregations