Skip to content

Commit

Permalink
Add example framework
Browse files Browse the repository at this point in the history
  • Loading branch information
FrameConsult authored and sschlenkrich committed Sep 23, 2023
1 parent b9773c5 commit 8529a80
Show file tree
Hide file tree
Showing 10 changed files with 872 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/DiffFusion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ module Examples
using DiffFusion:ModelValue
using OrderedCollections
using Random
using YAML
#
include("examples/Examples.jl")
include("examples/Models.jl")
include("examples/Products.jl")
end # module

Expand Down
46 changes: 46 additions & 0 deletions src/examples/Examples.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

"A list of example models and instrument specifications."
examples = [
"g3_1factor_flat",
]

"""
load(name::String)
Return a list of dictionaries representing a DiffFusion example.
Example details can be modified by changing the dictionary entries.
"""
function load(name::String)
file_name = joinpath(@__DIR__, "yamls", name * ".yaml")
dict_list = YAML.load_file(file_name; dicttype=OrderedDict{String,Any})
return dict_list
end


"""
build(dict_list::Vector{OrderedDict{String, Any}})
Return a dictionary of objects and configurations representing a
DiffFusion example.
The resulting dictionary is supposed to be queried and amended by
methods operating on examples.
"""
function build(dict_list::Vector{OrderedDict{String, Any}})
example = DiffFusion.deserialise_from_list(dict_list)
return example
end

"""
Return the first object of a given type from an example dictionary.
"""
function get_object(example::OrderedDict{String,Any}, obj_type)
for value in values(example)
if typeof(value) == obj_type
return value
end
end
error("Example does not contain " * string(obj_type) * ".")
end

126 changes: 126 additions & 0 deletions src/examples/Models.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@

"""
model(example::OrderedDict{String,Any})
Return the hybrid model of an example.
"""
model(example::OrderedDict{String,Any}) = get_object(example, DiffFusion.SimpleModel)

"""
correlation_holder(example::OrderedDict{String,Any})
Return the correlation holder of an example.
"""
correlation_holder(example::OrderedDict{String,Any}) = get_object(example, DiffFusion.CorrelationHolder)

"""
context(example::OrderedDict{String,Any})
Return the context of a given example.
"""
context(example::OrderedDict{String,Any}) = get_object(example, DiffFusion.Context)

"""
term_structures(example::OrderedDict{String,Any})
Return a dictionary of term structures for an example.
"""
function term_structures(example::OrderedDict{String,Any})
ts_dict = Dict{String,DiffFusion.Termstructure}()
for (key, value) in example
if isa(value, DiffFusion.Termstructure)
ts_dict[key] = value
end
end
return ts_dict
end

"""
simulation!(example::OrderedDict{String,Any})
Return a Monte Carlo simulation for a given example.
If no simulation exists it is created.
"""
function simulation!(example::OrderedDict{String,Any})
for value in values(example)
if typeof(value) == DiffFusion.Simulation
return value
end
end
@assert haskey(example, "config/simulation")
config = example["config/simulation"]
#
model_ = model(example)
ch_ = correlation_holder(example)
#
@assert haskey(config, "simulation_times")
times = config["simulation_times"]
if isa(times, AbstractDict)
times = Vector(times["start"]:times["step"]:times["stop"])
end
@assert isa(times, Vector)
#
@assert haskey(config, "n_paths")
n_paths = config["n_paths"]
@assert typeof(n_paths) == Int
#
if haskey(config, "with_progress_bar")
with_progress_bar = config["with_progress_bar"]
else
with_progress_bar = true
end
@assert typeof(with_progress_bar) == Bool
#
if haskey(config, "seed")
brownian_increments(n_states::Int, n_paths::Int, n_times::Int) =
DiffFusion.pseudo_brownian_increments(n_states, n_paths, n_times, config["seed"])
else
brownian_increments = DiffFusion.sobol_brownian_increments
end
sim = DiffFusion.simple_simulation(
model_,
ch_,
times,
n_paths,
with_progress_bar = with_progress_bar,
brownian_increments = brownian_increments,
)
example[DiffFusion.alias(model_) * "/simulation"] = sim
return sim
end

"""
path!(example::OrderedDict{String,Any})
Return a Monte Carlo path for a given example.
"""
function path!(example::OrderedDict{String,Any})
for value in values(example)
if typeof(value) == DiffFusion.Path
return value
end
end
sim = simulation!(example)
ts_dict = term_structures(example)
#
@assert haskey(example, "config/simulation")
config = example["config/simulation"]
#
ctx = context(example)
if haskey(config, "path_interpolation")
path_interpolation = config["path_interpolation"]
@assert typeof(path_interpolation) == Bool
if path_interpolation
path_interpolation = DiffFusion.LinearPathInterpolation
else
path_interpolation = DiffFusion.NoPathInterpolation
end
else
path_interpolation = DiffFusion.NoPathInterpolation
end
#
path_ = DiffFusion.path(sim, ts_dict, ctx, path_interpolation)
example[DiffFusion.alias(sim.model) * "/path"] = path_
return path_
end
32 changes: 32 additions & 0 deletions src/examples/Products.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,35 @@ function display_portfolio(example::OrderedDict{String,Any})
end
end

"""
scenarios!(example::OrderedDict{String,Any})
Create the exposure scenarios for the portfolio.
"""
function scenarios!(example::OrderedDict{String,Any})
if haskey(example, "scenarios")
return example["scenarios"]
end
config = example["config/instruments"]
@assert haskey(config, "obs_times")
obs_times = config["obs_times"]
if isa(obs_times, AbstractDict)
obs_times = Vector(obs_times["start"]:obs_times["step"]:obs_times["stop"])
end
@assert isa(obs_times, Vector)
#
if haskey(config, "with_progress_bar")
with_progress_bar = config["with_progress_bar"]
else
with_progress_bar = true
end
@assert typeof(with_progress_bar) == Bool
#
discount_curve_key = config["discount_curve_key"]
#
legs = vcat(portfolio!(example)...)
path_ = path!(example)
scens = DiffFusion.scenarios(legs, obs_times, path_, discount_curve_key, with_progress_bar=with_progress_bar)
example["scenarios"] = scens
return scens
end
Loading

0 comments on commit 8529a80

Please sign in to comment.