Commit 0974808a authored by Sören Henning's avatar Sören Henning

Redesign WeightedForecaster

parent 6a880f31
package teead.forecast;
import teead.timeseries.EquidistantTimeSeries;
import teead.timeseries.TimeSeriesPoint;
/**
* @author Sören Henning, Christian Claus Wiechmann
*
*/
public abstract class AbstractWeightedForecaster implements Forecaster {
public AbstractWeightedForecaster() {}
public AbstractWeightedForecaster(final ForecasterConfiguration configuration) {
this();
}
@Override
public double forecast(final EquidistantTimeSeries timeSeries) {
double weightedSum = 0;
double totalWeights = 0;
// more recent entry means more weight
int position = 1; // We just want to have values > 0
int size = timeSeries.size();
for (TimeSeriesPoint point : timeSeries) {
final double weight = getWeight(position, size);
totalWeights += weight;
weightedSum += point.getValue() * weight;
position++;
}
return weightedSum / totalWeights;
}
protected abstract double getWeight(final int position, final int size);
}
package teead.forecast;
public class ExponentialWeightedForecaster extends AbstractWeightedForecaster {
public ExponentialWeightedForecaster() {
super();
}
public ExponentialWeightedForecaster(final ForecasterConfiguration configuration) {
super(configuration);
}
@Override
protected double getWeight(final int position, final int size) {
// We need to do this scaling here to avoid overflows (Double.POSITIVE_INFINITY)
return Math.exp(position - size);
}
}
package teead.forecast;
public class LinearWeightedForecaster extends AbstractWeightedForecaster {
public LinearWeightedForecaster() {
super();
}
public LinearWeightedForecaster(final ForecasterConfiguration configuration) {
super(configuration);
}
@Override
protected double getWeight(final int position, final int size) {
return position;
}
}
package teead.forecast;
public class LogarithmicWeightedForecaster extends AbstractWeightedForecaster {
public LogarithmicWeightedForecaster() {
super();
}
public LogarithmicWeightedForecaster(final ForecasterConfiguration configuration) {
super(configuration);
}
@Override
protected double getWeight(final int position, final int size) {
return Math.log(position);
}
}
......@@ -6,7 +6,9 @@ import teead.timeseries.TimeSeriesPoint;
/**
* @author Christian Claus Wiechmann, Sören Henning
*
* @deprecated use {@link AbstractWeightedForecaster} and its subclasses instead
*/
@Deprecated
public class WeightedForecaster implements Forecaster {
public final static String WEIGHT_METHOD_CONFIGURATION_KEY = "weightMethod";
......
......@@ -9,7 +9,9 @@ public class ForecastersTest {
private static final String FORECASTERS_PATH = "teead.forecast";
private static final String MEAN_FORECASTER_NAME = "MeanForecaster";
private static final String WEIGHTED_FORECASTER_NAME = "WeightedForecaster";
private static final String LINEAR_WEIGHTED_FORECASTER_NAME = "LinearWeightedForecaster";
private static final String LOGARITHMIC_WEIGHTED_FORECASTER_NAME = "LogarithmicWeightedForecaster";
private static final String EXPONENTIAL_WEIGHTED_FORECASTER_NAME = "ExponentialWeightedForecaster";
private static final String REGRESSION_FORECASTER_NAME = "RegressionForecaster";
@Test
......@@ -34,14 +36,24 @@ public class ForecastersTest {
}
@Test
public void testGetByFullClassNameWithWeigthedForecaster() {
String weigthMethodName = WeightedForecaster.WeightMethod.LINEAR.name();
ForecasterConfiguration configuration = new ForecasterConfiguration();
configuration.put(WeightedForecaster.WEIGHT_METHOD_CONFIGURATION_KEY, weigthMethodName);
public void testGetByFullClassNameWithLinearWeightedForecaster() {
Forecaster forecaster = Forecasters.getByFullClassName(FORECASTERS_PATH + '.' + LINEAR_WEIGHTED_FORECASTER_NAME, new ForecasterConfiguration());
Forecaster forecaster = Forecasters.getByFullClassName(FORECASTERS_PATH + '.' + WEIGHTED_FORECASTER_NAME, configuration);
assertThat(forecaster, instanceOf(LinearWeightedForecaster.class));
}
@Test
public void testGetByFullClassNameWithLogarithmicWeightedMeanForecaster() {
Forecaster forecaster = Forecasters.getByFullClassName(FORECASTERS_PATH + '.' + LOGARITHMIC_WEIGHTED_FORECASTER_NAME, new ForecasterConfiguration());
assertThat(forecaster, instanceOf(LogarithmicWeightedForecaster.class));
}
@Test
public void testGetByFullClassNameWithExponentialWeightedForecaster() {
Forecaster forecaster = Forecasters.getByFullClassName(FORECASTERS_PATH + '.' + EXPONENTIAL_WEIGHTED_FORECASTER_NAME, new ForecasterConfiguration());
assertThat(forecaster, instanceOf(WeightedForecaster.class));
assertThat(forecaster, instanceOf(ExponentialWeightedForecaster.class));
}
@Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment