forked from asavine/Scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptingConstProcessor.h
More file actions
252 lines (204 loc) · 6.42 KB
/
scriptingConstProcessor.h
File metadata and controls
252 lines (204 loc) · 6.42 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#pragma once
/*
Written by Antoine Savine in 2018
This code is the strict IP of Antoine Savine
License to use and alter this code for personal and commercial applications
is freely granted to any person or company who purchased a copy of the book
Modern Computational Finance: Scripting for Derivatives and XVA
Jesper Andreasen & Antoine Savine
Wiley, 2018
As long as this comment is preserved at the top of the file
*/
#pragma once
#include <iostream>
#include "scriptingNodes.h"
#include "scriptingScenarios.h"
#include <vector>
#include "quickStack.h"
class ConstProcessor : public Visitor<ConstProcessor>
{
protected:
// State
// Const status of variables
vector<char> myVarConst;
vector<double> myVarConstVal;
// Inside an if?
bool myInConditional;
// Is this node a constant?
// Note the argument must be of exprNode type
static bool constArg(const ExprTree& node)
{
return downcast<const exprNode>(node)->isConst;
}
// Are all the arguments to this node constant?
// Note the arguments must be of exprNode type
static bool constArgs(const Node& node, const size_t first = 0)
{
for (size_t i = first; i < node.arguments.size(); ++i)
{
if (!constArg(node.arguments[i])) return false;
}
return true;
}
public:
using Visitor<ConstProcessor>::visit;
// Constructor, nVar = number of variables, from Product after parsing and variable indexation
// All variables start as constants with value 0
ConstProcessor(const size_t nVar) :
myVarConst(nVar, true),
myVarConstVal(nVar, 0.0),
myInConditional(false)
{}
// Visitors
// Expressions
// Binaries
template <class OP>
void visitBinary(exprNode& node, const OP op)
{
visitArguments(node);
if (constArgs(node))
{
node.isConst = true;
const double lhs = downcast<exprNode>(node.arguments[0])->constVal;
const double rhs = downcast<exprNode>(node.arguments[1])->constVal;
node.constVal = op(lhs, rhs);
}
}
void visit(NodeAdd& node)
{
visitBinary(node, [](const double& x, const double& y) {return x + y; });
}
void visit(NodeSub& node)
{
visitBinary(node, [](const double& x, const double& y) {return x - y; });
}
void visit(NodeMult& node)
{
visitBinary(node, [](const double& x, const double& y) {return x * y; });
}
void visit(NodeDiv& node)
{
visitBinary(node, [](const double& x, const double& y) {return x / y; });
}
void visit(NodePow& node)
{
visitBinary(node, [](const double& x, const double& y) {return pow(x, y); });
}
void visit(NodeMax& node)
{
visitBinary(node, [](const double& x, const double& y) {return max(x, y); });
}
void visit(NodeMin& node)
{
visitBinary(node, [](const double& x, const double& y) {return min(x, y); });
}
// Unaries
template <class OP>
void visitUnary(exprNode& node, const OP op)
{
visitArguments(node);
if (constArgs(node))
{
node.isConst = true;
const double arg = downcast<exprNode>(node.arguments[0])->constVal;
node.constVal = op(arg);
}
}
void visit(NodeUplus& node)
{
visitUnary(node, [](const double& x) {return x; });
}
void visit(NodeUminus& node)
{
visitUnary(node, [](const double& x) {return -x; });
}
// Functions
void visit(NodeLog& node)
{
visitUnary(node, [](const double& x) {return log(x); });
}
void visit(NodeSqrt& node)
{
visitUnary(node, [](const double& x) {return sqrt(x); });
}
// Multies
void visit(NodeSmooth& node)
{
visitArguments(node);
if (constArgs(node))
{
node.isConst = true;
const double x = reinterpret_cast<exprNode*>(node.arguments[0].get())->constVal;
const double vPos = reinterpret_cast<exprNode*>(node.arguments[1].get())->constVal;
const double vNeg = reinterpret_cast<exprNode*>(node.arguments[2].get())->constVal;
const double halfEps = 0.5 * reinterpret_cast<exprNode*>(node.arguments[3].get())->constVal;
if (x < -halfEps) node.constVal = vNeg;
else if (x > halfEps) node.constVal = vPos;
else
{
node.constVal = vNeg + 0.5 * (vPos - vNeg) / halfEps * (x + halfEps);
}
}
}
// If
void visit(NodeIf& node)
{
// Mark conditional
// Identify nested
bool nested = myInConditional;
// Mark
if (!nested) myInConditional = true;
// Visit arguments
visitArguments(node);
// Reset (unless nested)
if (!nested) myInConditional = false;
}
void visit(NodeAssign& node)
{
// Get index from LHS
const size_t varIndex = downcast<const NodeVar>(node.arguments[0])->index;
// Visit RHS
node.arguments[1]->accept(*this);
// All conditional assignments result in non const vars
if (!myInConditional)
{
// RHS constant?
if (constArg(node.arguments[1]))
{
myVarConst[varIndex] = true;
myVarConstVal[varIndex] = downcast<const exprNode>(node.arguments[1])->constVal;
}
else
{
myVarConst[varIndex] = false;
}
}
else
{
myVarConst[varIndex] = false;
}
}
void visit(NodePays& node)
{
// A payment is always non constant because it is normalized by a possibly stochastic numeraire
const size_t varIndex = downcast<const NodeVar>(node.arguments[0])->index;
myVarConst[varIndex] = false;
// Visit RHS
node.arguments[1]->accept(*this);
}
// Variables, RHS only, we don't visit LHS vars
void visit(NodeVar& node)
{
if (myVarConst[node.index])
{
node.isConst = true;
node.constVal = myVarConstVal[node.index];
}
else
{
node.isConst = false;
}
}
// We don't visit boolean nodes, that is best left to fuzzy logic
// We don't visit constants (which are always const) or spots (which are never const)
};