Skip to content
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

Add payoff scripting framework #16

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
ProgressBars = "49802e3a-d2f1-5c88-81d8-b72133a6f568"
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down
10 changes: 9 additions & 1 deletion src/DiffFusion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ module DiffFusion
using Distributions
using Interpolations
using LinearAlgebra
using Printf
using ProgressBars
using Random
using QuadGK
using Random
using Sobol
using SparseArrays

import Base.length
import Base.string

"""
A type alias for variables representing time.
Expand Down Expand Up @@ -44,6 +46,12 @@ include("paths/Context.jl")
include("paths/Path.jl")
include("paths/PathMethods.jl")

include("payoffs/Payoff.jl")
include("payoffs/Leafs.jl")
include("payoffs/UnaryNodes.jl")
include("payoffs/BinaryNodes.jl")
include("payoffs/RatesPayoffs.jl")

include("utils/Integrations.jl")
include("utils/InterpolationMethods.jl")

Expand Down
223 changes: 223 additions & 0 deletions src/payoffs/BinaryNodes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@

"""
struct Add <: BinaryNode
x::Payoff
y::Payoff
end

Addition of payoffs.
"""
struct Add <: BinaryNode
x::Payoff
y::Payoff
end

"""
struct Sub <: BinaryNode
x::Payoff
y::Payoff
end

Subtraction of payoffs.
"""
struct Sub <: BinaryNode
x::Payoff
y::Payoff
end

"""
struct Mul <: BinaryNode
x::Payoff
y::Payoff
end

Multiplication of payoffs.
"""
struct Mul <: BinaryNode
x::Payoff
y::Payoff
end

"""
struct Div <: BinaryNode
x::Payoff
y::Payoff
end

Division of payoffs.
"""
struct Div <: BinaryNode
x::Payoff
y::Payoff
end

"""
struct Max <: BinaryNode
x::Payoff
y::Payoff
end

Path-wise maximum
"""
struct Max <: BinaryNode
x::Payoff
y::Payoff
end

"""
struct Min <: BinaryNode
x::Payoff
y::Payoff
end

Path-wise minimum
"""
struct Min <: BinaryNode
x::Payoff
y::Payoff
end

"""
struct Logical <: BinaryNode
x::Payoff
y::Payoff
op::String
end

Logical operations
"""
struct Logical <: BinaryNode
x::Payoff
y::Payoff
op::String
end

"""
## Implementations
"""

"Addition."
at(p::Add, path::AbstractPath) = at(p.x, path) + at(p.y, path)

"Subtraction."
at(p::Sub, path::AbstractPath) = at(p.x, path) - at(p.y, path)

"Multiplication."
at(p::Mul, path::AbstractPath) = at(p.x, path) .* at(p.y, path)

"Division."
at(p::Div, path::AbstractPath) = at(p.x, path) ./ at(p.y, path)

"Maximum"
at(p::Max, path::AbstractPath) = max.(at(p.x, path), at(p.y, path))

"Minimum"
at(p::Min, path::AbstractPath) = min.(at(p.x, path), at(p.y, path))

"Logical"
function at(p::Logical, path::AbstractPath)
if p.op == "<"
return at(p.x, path) .< at(p.y, path)
elseif p.op == "<="
return at(p.x, path) .<= at(p.y, path)
elseif p.op == "=="
return at(p.x, path) .== at(p.y, path)
elseif p.op == "!="
return at(p.x, path) .!= at(p.y, path)
elseif p.op == ">="
return at(p.x, path) .>= at(p.y, path)
elseif p.op == ">"
return at(p.x, path) .> at(p.y, path)
end
error("Unknown logical operation.")
end


"""
## String output
"""

"Formatted addition."
string(p::Add) = @sprintf("(%s + %s)", string(p.x), string(p.y))

"Formatted subtraction."
string(p::Sub) = @sprintf("(%s - %s)", string(p.x), string(p.y))

"Formatted multiplication."
string(p::Mul) = @sprintf("%s * %s", string(p.x), string(p.y))

"Formatted division."
string(p::Div) = @sprintf("(%s / %s)", string(p.x), string(p.y))

"Formatted maximum."
string(p::Max) = @sprintf("Max(%s, %s)", string(p.x), string(p.y))

"Formatted minimum."
string(p::Min) = @sprintf("Min(%s, %s)", string(p.x), string(p.y))

"Formatted logical."
string(p::Logical) = @sprintf("(%s %s %s)", string(p.x), p.op, string(p.y))


"""
## Operator notation
"""

import Base.+
(+)(x::Payoff,y::Payoff) = Add(x,y)
(+)(x::Payoff,y) = Add(x,Fixed(y))
(+)(x,y::Payoff) = Add(Fixed(x),y)

import Base.-
(-)(x::Payoff,y::Payoff) = Sub(x,y)
(-)(x::Payoff,y) = Sub(x,Fixed(y))
(-)(x,y::Payoff) = Sub(Fixed(x),y)

import Base.*
(*)(x::Payoff,y::Payoff) = Mul(x,y)
(*)(x::Payoff,y) = Mul(x,Fixed(y))
(*)(x,y::Payoff) = Mul(Fixed(x),y)

import Base./
(/)(x::Payoff,y::Payoff) = Div(x,y)
(/)(x::Payoff,y) = Div(x,Fixed(y))
(/)(x,y::Payoff) = Div(Fixed(x),y)

#import Base.%
#(%)(x::Payoff,t) = Pay(x,t)

import Base.<
(<)(x::Payoff,y::Payoff) = Logical(x,y,"<")
(<)(x::Payoff,y) = Logical(x,Fixed(y),"<")
(<)(x,y::Payoff) = Logical(Fixed(x),y,"<")

import Base.<=
(<=)(x::Payoff,y::Payoff) = Logical(x,y,"<=")
(<=)(x::Payoff,y) = Logical(x,Fixed(y),"<=")
(<=)(x,y::Payoff) = Logical(Fixed(x),y,"<=")

import Base.==
(==)(x::Payoff,y::Payoff) = Logical(x,y,"==")
(==)(x::Payoff,y) = Logical(x,Fixed(y),"==")
(==)(x,y::Payoff) = Logical(Fixed(x),y,"==")

import Base.!=
(!=)(x::Payoff,y::Payoff) = Logical(x,y,"!=")
(!=)(x::Payoff,y) = Logical(x,Fixed(y),"!=")
(!=)(x,y::Payoff) = Logical(Fixed(x),y,"!=")

import Base.>=
(>=)(x::Payoff,y::Payoff) = Logical(x,y,">=")
(>=)(x::Payoff,y) = Logical(x,Fixed(y),">=")
(>=)(x,y::Payoff) = Logical(Fixed(x),y,">=")

import Base.>
(>)(x::Payoff,y::Payoff) = Logical(x,y,">")
(>)(x::Payoff,y) = Logical(x,Fixed(y),">")
(>)(x,y::Payoff) = Logical(Fixed(x),y,">")


Max(x::Payoff,y) = Max(x,Fixed(y))
Max(x,y::Payoff) = Max(Fixed(x),y)
Min(x::Payoff,y) = Min(x,Fixed(y))
Min(x,y::Payoff) = Min(Fixed(x),y)
Loading