-
Notifications
You must be signed in to change notification settings - Fork 13
Feature/kucherenko indices #276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sitoryu
wants to merge
16
commits into
master
Choose a base branch
from
feature/kucherenko_indices
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6093d7c
sample based estimator for kucherenko indices
sitoryu ceb175d
Linear example from Kucherenko et al. (2012)
sitoryu 62486f3
removed unneccessary UQ namespaces
sitoryu 5f1041d
included DOI
sitoryu 654a9b9
pushed test for single variable up so that first order indices do not…
sitoryu 5e3d545
added functionality to generate conditional samples based on gaussian…
sitoryu 7467270
Added conditional sampling based Kucherenko indices - old method now …
sitoryu 03c82bd
kucherenkoindices_bin pass calculated samples as output
sitoryu 2e111ab
added quantile bins and sperated 1D and MultiD
sitoryu 1bd0f23
changed Demo example to Portfolio model
sitoryu e651f37
added tests for kucherenko inidces and conditional sampling
sitoryu ce5fb08
Merge master into feature/kucherenko_indices - resolved conflicts in …
sitoryu ae5e0d8
Refactor demo/kucherenkoindices.jl for local package usage
sitoryu 9ec08e9
changed jointdistribution calls to the new constructor/changed sample…
sitoryu 6929477
Added LinearAlgebra to test/Project.toml
sitoryu 6734b30
slightly increased tolerance on 2 dimensional binning test due to low…
sitoryu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| using UncertaintyQuantification | ||
| using LinearAlgebra | ||
|
|
||
| # Kucherenko et al. (2012) (DOI: 10.1016/j.cpc.2011.12.020) --- Test Case 2: Portfolio model with analytical indices --- | ||
|
|
||
| μ = [0.0, 0.0, 250.0, 400.0] | ||
|
|
||
| σ1, σ2, σ3, σ4 = sqrt(16.0), sqrt(4.0), sqrt(4e4), sqrt(9e4) | ||
| σ12, σ21 = 2.4, 2.4 | ||
| σ34, σ43 = -1.8e4, -1.8e4 | ||
|
|
||
| Σ = [ | ||
| 16.0 2.4 0.0 0.0; | ||
| 2.4 4.0 0.0 0.0; | ||
| 0.0 0.0 4e4 -1.8e4; | ||
| 0.0 0.0 -1.8e4 9e4 | ||
| ] | ||
|
|
||
| # Marginals | ||
| marginals = RandomVariable[ | ||
| RandomVariable(Normal(μ[1], σ1), :x1), | ||
| RandomVariable(Normal(μ[2], σ2), :x2), | ||
| RandomVariable(Normal(μ[3], σ3), :x3), | ||
| RandomVariable(Normal(μ[4], σ4), :x4) | ||
| ] | ||
|
|
||
| # Correlation matrix for Gaussian copula | ||
| Dvec = sqrt.(diag(Σ)) | ||
| R = Σ ./ (Dvec * Dvec') | ||
|
|
||
| inputs = [ | ||
| JointDistribution(GaussianCopula(R), marginals) | ||
| ] | ||
|
|
||
| model = Model(df -> df.x1 .* df.x3 .+ df.x2 .* df.x4, :y) | ||
| sim = MonteCarlo(200000) | ||
|
|
||
|
|
||
| # Analytical values | ||
| μ3, μ4 = μ[3], μ[4] | ||
| σ2_1, σ2_2, σ2_3, σ2_4 = 16.0, 4.0, 4e4, 9e4 | ||
| σ12, σ34 = 2.4, -1.8e4 | ||
| ρ12 = σ12 / (σ1 * σ2) | ||
| ρ34 = σ34 / (σ3 * σ4) | ||
|
|
||
| D = σ2_1 * (σ2_3 + μ3^2) + σ2_2 * (σ2_4 + μ4^2) + 2 * σ12 * (σ34 + μ3 * μ4) | ||
|
|
||
| S1_analytical = σ2_1 * (μ3 + μ4 * ρ12 * σ2 / σ1)^2 / D | ||
| ST1_analytical = σ2_1 * (1 - ρ12^2) * (σ2_3 + μ3^2) / D | ||
|
|
||
| S2_analytical = σ2_2 * (μ4 + μ3 * ρ12 * σ1 / σ2)^2 / D | ||
| ST2_analytical = σ2_2 * (1 - ρ12^2) * (σ2_4 + μ4^2) / D | ||
|
|
||
| S3_analytical = 0.0 | ||
| ST3_analytical = σ2_1 * σ2_3 * (1 - ρ34^2) / D | ||
|
|
||
| S4_analytical = 0.0 | ||
| ST4_analytical = σ2_2 * σ2_4 * (1 - ρ34^2) / D | ||
|
|
||
|
|
||
|
|
||
| try | ||
| indices, bin_samples = kucherenkoindices_bin([model], inputs, [:y], sim; min_bin_sample_multi_dims=5) | ||
| println("Sample-based Kucherenko Indices calculation using bins:") | ||
| println(indices) | ||
|
|
||
| tol = 0.01 | ||
| @assert abs(indices.FirstOrder[1] - S1_analytical) < tol "S1 error too large" | ||
| @assert abs(indices.FirstOrder[2] - S2_analytical) < tol "S2 error too large" | ||
| @assert abs(indices.FirstOrder[3] - S3_analytical) < tol "S3 error too large" | ||
| @assert abs(indices.FirstOrder[4] - S4_analytical) < tol "S4 error too large" | ||
| @assert abs(indices.TotalEffect[1] - ST1_analytical) < tol "ST1 error too large" | ||
| @assert abs(indices.TotalEffect[2] - ST2_analytical) < tol "ST2 error too large" | ||
| @assert abs(indices.TotalEffect[3] - ST3_analytical) < tol "ST3 error too large" | ||
| @assert abs(indices.TotalEffect[4] - ST4_analytical) < tol "ST4 error too large" | ||
|
|
||
| println("Portfolio model Kucherenko indices validation passed - all values within tolerance.") | ||
|
|
||
| catch e | ||
| println("Error computing Portfolio model Kucherenko indices: $e") | ||
| rethrow(e) | ||
| end | ||
|
|
||
| try | ||
| indices = indices = kucherenkoindices([model], inputs, [:y], sim) | ||
| println("Standard Kucherenko Indices calculation:") | ||
| println(indices) | ||
|
|
||
| tol = 0.01 | ||
| @assert abs(indices.FirstOrder[1] - S1_analytical) < tol "S1 error too large" | ||
| @assert abs(indices.FirstOrder[2] - S2_analytical) < tol "S2 error too large" | ||
| @assert abs(indices.FirstOrder[3] - S3_analytical) < tol "S3 error too large" | ||
| @assert abs(indices.FirstOrder[4] - S4_analytical) < tol "S4 error too large" | ||
| @assert abs(indices.TotalEffect[1] - ST1_analytical) < tol "ST1 error too large" | ||
| @assert abs(indices.TotalEffect[2] - ST2_analytical) < tol "ST2 error too large" | ||
| @assert abs(indices.TotalEffect[3] - ST3_analytical) < tol "ST3 error too large" | ||
| @assert abs(indices.TotalEffect[4] - ST4_analytical) < tol "ST4 error too large" | ||
|
|
||
| println("Portfolio model Kucherenko indices validation passed - all values within tolerance.") | ||
| catch e | ||
| println("Error computing Portfolio model Kucherenko indices: $e") | ||
| rethrow(e) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to have a function that takes a
JointDistributionand aDataFramewith less columns than the joint distribution requires and returns the data frame with the remaining columns samples conditionally based on the existing samples.