Skip to content

Commit

Permalink
Merge pull request #7 from LAMPSPUC/remove_interpolation_warning
Browse files Browse the repository at this point in the history
fix interpolation warning and change docs
  • Loading branch information
andreramosfdc committed Aug 28, 2024
2 parents b5b9d4e + 6fb200a commit 981f5da
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 58 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/TagBot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: TagBot
on:
issue_comment: # THIS BIT IS NEW
types:
- created
workflow_dispatch:
jobs:
TagBot:
# THIS 'if' LINE IS NEW
if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot'
# NOTHING BELOW HAS CHANGED
runs-on: ubuntu-latest
steps:
- uses: JuliaRegistries/TagBot@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
Manifest.toml
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018-2021: Raphael Saavedra, Guilherme Bodin, Mario Souto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 7 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NonParametricNORTA"
uuid = "e97a57fc-2266-4ea9-80d8-a1f12fb1471b"
authors = ["andreramosfc <[email protected]>"]
version = "0.1.1"
version = "0.2.0"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand All @@ -10,3 +10,9 @@ KernelDensity = "5ab0869b-81aa-558d-bb23-cbf5423bbe9b"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
julia = "1"
Distributions = "0.16, 0.17, 0.18, 0.19, 0.20, 0.21, 0.22, 0.23, 0.24, 0.25"
Interpolations = "0.14, 0.15"
KernelDensity = "0.5, 0.6"
81 changes: 41 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using Plots
using Distributions

y = rand(1000, 3)*rand(3).*15 #generate y as a regression
y_norta, non_parametric_distribution = NonParametricNORTA.convertData(y)
y_norta, non_parametric_distribution = NonParametricNORTA.convert_data(y)
```

### Transformation visualization
Expand All @@ -26,7 +26,7 @@ This transformation involves obtaining the non-parametric distribution's cumulat
## Data reverse transformation

```julia
sc = NonParametricNORTA.reverseData(rand(Normal(0, 1), 100), non_parametric_distribution)
sc = NonParametricNORTA.reverse_data(rand(Normal(0, 1), 100), non_parametric_distribution)
```
### Reverse transformation visualization

Expand All @@ -36,54 +36,55 @@ Similar to the initial transformation but in reverse, this process involves reve

In time series simulation processes within stationary contexts, maintaining scenarios that respect historical value bounds becomes crucial. Ensuring scenarios do not violate the maximum and minimum values can be achieved by modeling the transformed NORTA series and then performing the reverse transformation process.

### Regression + residual bootstraping example
### Simulation of Water Inflows

```julia
T = 1000
train_idx = 1:900
test_idx = 901:1000
N_scenarios = 100

X = rand(T, 10)
X_train = X[train_idx, :]
X_test = X[test_idx, :]

y = X*(rand(10).*10) + rand(T)
y_train = y[train_idx]
y_test = y[test_idx]
In this example we want to generate scenario paths for a water inflow time series. We will make two simulations using an Auto ARIMA (estimated vi StateSpaceModels.jl package). The first one will be the simple output of the model and the second one will be utilizing the NORTA transformation.

y_train_norta, np = NonParametricNORTA.convertData(y_train)
```julia
using StateSpaceModels, CSV, DataFrames, Plots
df = CSV.read("datasets/inflows.csv")

β = X_train\y_train_norta #Model the NORTA transformed data
residuals = y_train_norta - X_train*β
y = df[!, 2]
dates = df[!, 1]
forecast_dates = collect(Date(2015, 1, 1): Month(1): Date(2016, 12, 1))
steps_ahead = 24

NORTA_scenarios = zeros(length(y_test), N_scenarios)
for i in 1:N_scenarios
NORTA_scenarios[:, i] = X[T_train+1:T, :]*β + rand(residuals, length(y_test))
end
```
#### Transformed data results
#### Simulating Original Time Series
```julia
plot(y_train_norta, w=2, color = "black", lab = "NORTA transformed historic", legend=:outerbottom)
for i in 1:N_scenarios
plot!(vcat(ones(T_train).*NaN, NORTA_scenarios[:, i]), color = "red", alpha = 0.2, lab = "")
end
plot!([], color="red", lab = "NORTA transformed scenarios")
model = auto_arima(y; seasonal = 12)
fit!(model)
scenarios = simulate_scenarios(model, 24, 100)[:, 1, :]
expected_value = [i[1] for i in forecast(model, 24).expected_value]

plt = plot(dates, y, label = "Historical Values", color = :black, w=2, legend=:outertop)
plot!(plt, forecast_dates, scenarios, label = "", color = "grey", width = 0.2)
plot!(forecast_dates, expected_value.*NaN, color = "grey", label="Scenarios")
plot!(forecast_dates, expected_value, lab = "Expected Value", color = :red)

```
![norta_simulation](./docs/figures/norta_sim.PNG)
![simulation](./docs/figures/inflow_simulation.PNG)

The modeled simulation, when visualized in the transformed scale, does not adhere to historical bounds. This is evident as the maximum and minimum of the simulation exceed historical boundaries.
The modeled simulation does not adhere to historical bounds. This is evident as, in this case, the minimum of the simulation is below historical boundaries.

#### Original scale data results
#### Simulating utilizing NORTA
```julia
scenarios = NonParametricNORTA.reverseData(NORTA_scenarios, np)
transformed_y, non_parametric_distribution = NonParametricNORTA.convert_data(y)

model = auto_arima(transformed_y; seasonal = 12)
fit!(model)
scenarios = simulate_scenarios(model, 24, 100)[:, 1, :]
expected_value = [i[1] for i in forecast(model, 24).expected_value]

scenarios = NonParametricNORTA.reverse_data(scenarios, non_parametric_distribution)
expected_value = NonParametricNORTA.reverse_data(expected_value, non_parametric_distribution)

plt = plot(dates, y, label = "Historical Values", color = :black, w=2, legend=:outertop)
plot!(plt, forecast_dates, scenarios, label = "", color = "grey", width = 0.2)
plot!(forecast_dates, expected_value.*NaN, color = "grey", label="Scenarios")
plot!(forecast_dates, expected_value, lab = "Expected Value", color = :red)

plot(y_train, w=2, color = "black", lab = "Original Historic", legend=:outerbottom)
for i in 1:N_scenarios
plot!(vcat(ones(T_train).*NaN, scenarios[:, i]), color = "red", alpha = 0.2, lab = "")
end
plot!([], color="red", lab = "Scenarios")
```
![simulation](./docs/figures/simulation.PNG)
![norta_simulation](./docs/figures/inflow_norta_simulation.PNG)

However, upon reverse transforming the scenarios, we observe that the simulation respects the historical boundaries. This demonstrates the utility of the reverse transformation process in maintaining data integrity within the historical context.
We can see that using the NORTA transformation the simulation respects the historical boundaries.
Loading

0 comments on commit 981f5da

Please sign in to comment.