-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUnaryNodes.jl
161 lines (123 loc) · 3.06 KB
/
UnaryNodes.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
struct Pay <: UnaryNode
x::Payoff
obs_time::ModelTime
test_times::Bool
end
A Pay payoff allows the user to modify the observation time of
a given payoff. This is relevant for discounting.
Typically, we use Pay to specify the pay time for a payoff.
"""
struct Pay <: UnaryNode
x::Payoff
obs_time::ModelTime
test_times::Bool
end
"""
Pay(x::Payoff, pay_time::ModelTime)
Create a Pay object and check for consistency.
"""
function Pay(x::Payoff, pay_time::ModelTime)
obs_time_x = obs_time(x)
if pay_time < obs_time_x
@warn "Pay time is before observation time." pay_time obs_time_x
end
return Pay(x, pay_time, true)
end
"""
at(p::Pay, path::AbstractPath)
Derive payoff of the child payoff.
"""
at(p::Pay, path::AbstractPath) = at(p.x, path)
"""
obs_time(p::Pay)
Return decorating observation time.
"""
obs_time(p::Pay) = p.obs_time
"""
obs_times(p::Pay)
Return all observation times of the linked payoff.
"""
obs_times(p::Pay) = union(obs_times(p.x), p.obs_time)
"""
string(p::Pay)
Formatted (and shortened) output for Pay payoff.
"""
string(p::Pay) = @sprintf("(%s @ %.2f)", string(p.x), p.obs_time)
"""
mutable struct Cache <: UnaryNode
x::Payoff
path::Union{AbstractPath, Nothing}
value::Union{AbstractVector, Nothing}
end
A Cache payoff aims at avoiding repeated calculations of the same payoff.
If a Payoff object is referenced by several parent Payoff objects then
each call of *at()* of the parent object triggers a call of *at()* of
the child object that all return the same value(s).
A Cache payoff checks whether the payoff was already evaluated and if
yes then returns a cached value.
"""
mutable struct Cache <: UnaryNode
x::Payoff
path::Union{AbstractPath, Nothing}
value::Union{AbstractVector, Nothing}
end
"""
Cache(x::Payoff)
Create a Cache payoff from a given payoff.
"""
Cache(x::Payoff) = Cache(x, nothing, nothing)
"""
at(p::Cache, path::AbstractPath)
Derive payoff of the child payoff only if not yet calculated.
"""
function at(p::Cache, path::AbstractPath)
if !(p.path === path)
p.value = at(p.x, path)
p.path = path
end
return p.value
end
"""
string(p::Cache)
Formatted (and shortened) output for Cache payoff.
"""
string(p::Cache) = @sprintf("{%s}", string(p.x))
"""
struct Exp <: UnaryNode
x::Payoff
end
Function exp() applied to payoff.
"""
struct Exp <: UnaryNode
x::Payoff
end
"""
at(p::Exp, path::AbstractPath)
Evaluate exp-function.
"""
at(p::Exp, path::AbstractPath) = exp.(at(p.x, path))
"""
string(p::Exp)
Formatted output for Exp payoff.
"""
string(p::Exp) = @sprintf("Exp(%s)", string(p.x))
"""
struct Log <: UnaryNode
x::Payoff
end
Function log() applied to payoff.
"""
struct Log <: UnaryNode
x::Payoff
end
"""
at(p::Log, path::AbstractPath)
Evaluate log-function.
"""
at(p::Log, path::AbstractPath) = log.(at(p.x, path))
"""
string(p::Log)
Formatted output for Log payoff.
"""
string(p::Log) = @sprintf("Log(%s)", string(p.x))