add first method
This commit is contained in:
parent
e5f2e0a3f3
commit
62f3ffcf5c
@ -3,10 +3,7 @@ package ru.ulstu.controllers;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import ru.ulstu.configurations.ApiConfiguration;
|
import ru.ulstu.configurations.ApiConfiguration;
|
||||||
import ru.ulstu.models.TimeSeries;
|
import ru.ulstu.models.TimeSeries;
|
||||||
import ru.ulstu.services.TimeSeriesService;
|
import ru.ulstu.services.TimeSeriesService;
|
||||||
@ -31,4 +28,9 @@ public class TimeSeriesController {
|
|||||||
public ResponseEntity<TimeSeries> getRandomTimeSeries(@RequestParam("length") int length) {
|
public ResponseEntity<TimeSeries> getRandomTimeSeries(@RequestParam("length") int length) {
|
||||||
return new ResponseEntity<>(timeSeriesService.getRandomTimeSeries(length), HttpStatus.OK);
|
return new ResponseEntity<>(timeSeriesService.getRandomTimeSeries(length), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "ts.get_forecast", method = RequestMethod.POST)
|
||||||
|
public ResponseEntity<TimeSeries> getForecastTimeSeries(@RequestBody TimeSeries timeSeries) {
|
||||||
|
return new ResponseEntity<>(timeSeriesService.getForecast(timeSeries), HttpStatus.OK);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package ru.ulstu.models;
|
package ru.ulstu.models;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -11,6 +14,16 @@ public class TimeSeries {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonCreator
|
||||||
|
public TimeSeries(@JsonProperty(value = "values") List<TimeSeriesValue> values, @JsonProperty(value = "name") String name) {
|
||||||
|
this.values = values;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TimeSeries() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public List<TimeSeriesValue> getValues() {
|
public List<TimeSeriesValue> getValues() {
|
||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
@ -26,4 +39,20 @@ public class TimeSeries {
|
|||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return values.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addValue(TimeSeriesValue timeSeriesValue) {
|
||||||
|
values.add(timeSeriesValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addValue(TimeSeriesValue basedOnValue, Double value) {
|
||||||
|
values.add(new TimeSeriesValue(basedOnValue.getDate().plusDays(1), value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public TimeSeriesValue getLastValue() {
|
||||||
|
return values.get(values.size()-1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
package ru.ulstu.models;
|
package ru.ulstu.models;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
public class TimeSeriesValue {
|
public class TimeSeriesValue {
|
||||||
private LocalDate date;
|
private LocalDate date;
|
||||||
private Double value;
|
private Double value;
|
||||||
|
|
||||||
public TimeSeriesValue(LocalDate date, Double value) {
|
@JsonCreator
|
||||||
|
public TimeSeriesValue(@JsonProperty(value="date") LocalDate date, @JsonProperty(value = "value") Double value) {
|
||||||
this.date = date;
|
this.date = date;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package ru.ulstu.services;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ru.ulstu.models.TimeSeries;
|
import ru.ulstu.models.TimeSeries;
|
||||||
import ru.ulstu.models.TimeSeriesValue;
|
import ru.ulstu.models.TimeSeriesValue;
|
||||||
|
import ru.ulstu.tsMethods.exponential.NoTrendNoSeason;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
@ -18,4 +19,11 @@ public class TimeSeriesService {
|
|||||||
}
|
}
|
||||||
return ts;
|
return ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TimeSeries getForecast(TimeSeries timeSeries) {
|
||||||
|
NoTrendNoSeason nn = new NoTrendNoSeason(timeSeries, 5);
|
||||||
|
nn.setAlpa(0.1);
|
||||||
|
nn.createModel();
|
||||||
|
return nn.getForecastTimeSeries();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
5
src/main/java/ru/ulstu/tsMethods/Param.java
Normal file
5
src/main/java/ru/ulstu/tsMethods/Param.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package ru.ulstu.tsMethods;
|
||||||
|
|
||||||
|
public enum Param {
|
||||||
|
ALPHA
|
||||||
|
}
|
37
src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java
Normal file
37
src/main/java/ru/ulstu/tsMethods/TimeSeriesMethod.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package ru.ulstu.tsMethods;
|
||||||
|
|
||||||
|
import ru.ulstu.models.TimeSeries;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public abstract class TimeSeriesMethod {
|
||||||
|
protected TimeSeries originalTimeSeries;
|
||||||
|
protected TimeSeries modelTimeSeries;
|
||||||
|
protected TimeSeries forecastTimeSeries;
|
||||||
|
protected int countForecast;
|
||||||
|
protected Map<Param, Double> parameters = new HashMap<>();
|
||||||
|
|
||||||
|
protected void init() {
|
||||||
|
modelTimeSeries = new TimeSeries("Model time series of '" + originalTimeSeries.getName() + "'");
|
||||||
|
forecastTimeSeries = new TimeSeries("Forecast time series of '" + originalTimeSeries.getName() + "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
public TimeSeries getOriginalTimeSeries() {
|
||||||
|
return originalTimeSeries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TimeSeries getModelTimeSeries() {
|
||||||
|
return modelTimeSeries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCountForecast() {
|
||||||
|
return countForecast;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TimeSeries getForecastTimeSeries() {
|
||||||
|
return forecastTimeSeries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void createModel();
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package ru.ulstu.tsMethods.exponential;
|
||||||
|
|
||||||
|
import ru.ulstu.models.TimeSeries;
|
||||||
|
import ru.ulstu.tsMethods.Param;
|
||||||
|
import ru.ulstu.tsMethods.TimeSeriesMethod;
|
||||||
|
|
||||||
|
public class NoTrendNoSeason extends TimeSeriesMethod {
|
||||||
|
public NoTrendNoSeason(TimeSeries originalTimeSeries, int countForecast) {
|
||||||
|
this.originalTimeSeries = originalTimeSeries;
|
||||||
|
this.countForecast = countForecast;
|
||||||
|
if (originalTimeSeries.isEmpty()) {
|
||||||
|
throw new RuntimeException("Time series must not empty");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void init() {
|
||||||
|
super.init();
|
||||||
|
modelTimeSeries.addValue(originalTimeSeries.getValues().get(0));
|
||||||
|
forecastTimeSeries.addValue(originalTimeSeries.getValues().get(0));
|
||||||
|
parameters.put(Param.ALPHA, 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAlpa(double value) {
|
||||||
|
parameters.put(Param.ALPHA, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void createModel() {
|
||||||
|
init();
|
||||||
|
double e;
|
||||||
|
|
||||||
|
//выполняется проход модели по сглаживанию и прогнозированию countPointForecast точек
|
||||||
|
for (int t = 0; t < originalTimeSeries.getValues().size() - 1 + countForecast; t++) {
|
||||||
|
// пока не дошли до конца ряда - сглаживаем, иначе строим прогноз
|
||||||
|
if (t < originalTimeSeries.getValues().size()) {
|
||||||
|
e = originalTimeSeries.getValues().get(t).getValue() - forecastTimeSeries.getValues().get(t).getValue();
|
||||||
|
} else {
|
||||||
|
e = 0;
|
||||||
|
}
|
||||||
|
modelTimeSeries.addValue(modelTimeSeries.getLastValue(), modelTimeSeries.getValues().get(t).getValue() + parameters.get(Param.ALPHA) * e); // уровень
|
||||||
|
forecastTimeSeries.addValue(modelTimeSeries.getLastValue()); // прогноз
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user