Skip to content

Commit

Permalink
updating airline example in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Saavedra committed Jul 27, 2019
1 parent 3224976 commit e91d91c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 31 deletions.
Binary file added docs/src/assets/logap_components.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/src/assets/logap_forecast.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/src/assets/seasonalairpassengers.png
Binary file not shown.
Binary file removed docs/src/assets/simulationlogofairpassengers.png
Binary file not shown.
Binary file removed docs/src/assets/trendairpassengers.png
Binary file not shown.
52 changes: 21 additions & 31 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,41 @@ p1 = plot(AP[:Date], logAP, label = "AirPassengers timeseries", size = (1000, 50

![Log of Air Passengers time series](./assets/logofairpassengers.png)

Estimating a `StateSpaceModel` gives us the trend and seasonal components of the time series.
First we need to specify a state-space model. In this case, we'll utilize the basic structural model.

```julia
# Define its seasonality
s = 12
# Create structural model with seasonality of 12 months
model = structural(logAP, 12)
```

# Estimate a StateSpace Structure
ss = statespace(logAP, s)
Estimating the model gives us the trend and seasonal components of the time series.

# Analyze its decomposition in seasonal and trend
p2 = plot(AP[:Date], ss.state.seasonal, label = "AirPassengers seasonal", size = (1000, 500))
p3 = plot(AP[:Date], ss.state.trend, label = "AirPassengers trend", size = (1000, 500))
```julia
# Estimate a StateSpace structure
ss = statespace(model)

# Analyze its decomposition in trend and seasonal
p2 = plot(AP[:Date], [ss.smoother.alpha[:, 1] ss.smoother.alpha[:, 3]], layout = (2, 1),
label = ["Trend component" "Seasonal component"], legend = :topleft)
```

![Log of Air Passengers trend component](./assets/trendairpassengers.png)
![Log of Air Passengers seasonal component](./assets/seasonalairpassengers.png)
![Trend and seasonal components for log of Air Passengers](./assets/logap_components.png)

We can also generate future scenarios for this time series through Monte Carlo simulation. In this example, we simulate 100 scenarios for up to five years (60 time periods) ahead.
We can also forecast this time series. In this example, we will forecast 24 months ahead.

```julia
# Simulate 100 scenarios, 60 steps ahead
num_scenarios = 100
num_steps_ahead = 60
simulation = simulate(ss, num_steps_ahead, num_scenarios)
# Forecast 24 months ahead
N = 24
pred, dist = forecast(ss, N)

# Define simulation dates
# Define forecasting dates
firstdate = AP[:Date][end] + Month(1)
newdates = collect(firstdate:Month(1):firstdate + Month(num_steps_ahead - 1))

# Evaluating the mean of the forecast and its quantiles
simulation_mean = mean(simulation, dims = 3)[1, :, :]

n = length(logAP)
nmonths = length(simulation[1, :, 1])
simulation_q05 = zeros(nmonths)
simulation_q95 = zeros(nmonths)
for t = 1:nmonths
simulation_q05[t] = quantile(simulation[1, t, :], 0.05)
simulation_q95[t] = quantile(simulation[1, t, :], 0.95)
end
newdates = collect(firstdate:Month(1):firstdate + Month(N - 1))

plot!(p1, newdates, [simulation_q05, simulation_mean, simulation_q95], labels = ["5% quantile", "mean", "95% quantile"])
p3 = plot!(p1, newdates, pred, label = "Forecast")
```

![Log of Air Passengers simulation](./assets/simulationlogofairpassengers.png)
![Forecast for log of Air Passengers](./assets/logap_forecast.png)


## Vehicle tracking
Expand Down

0 comments on commit e91d91c

Please sign in to comment.