-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathscriptingDomainProc.h
557 lines (473 loc) · 12.7 KB
/
scriptingDomainProc.h
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
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 "functDomain.h"
#include "scriptingNodes.h"
//#define DUMP
#ifdef DUMP
#include <fstream>
#endif
using namespace std;
/*
Domain processor
Variable indexer and if processor must have been run prior
Determines the domains of all variables and expressions. Note that the goal is to identify singletons from continuous intervals,
not necessarily to compute all intervals accurately. For example, is x's domain is {0} and y's domain is (-inf, inf) then
xy's domain is {0}, but if x and y have domain (-inf, inf), then xy's domain is (-inf, inf), even if it turns out that y=x.
This processor sets the "always true" and "always false" flags
on the if, equal, superior, not, and and or nodes according to the condition's domain
In addition, when fuzzy processing is requested, the processor sets the continuous/discrete flag on the equal and superior nodes
and if discrete, the left and right interpolation bounds
*/
class DomainProcessor : public Visitor<DomainProcessor>
{
// Fuzzy?
const bool myFuzzy;
// Domains for all variables
vector<Domain> myVarDomains;
// Stack of domains for expressions
staticStack<Domain> myDomStack;
// Stack of always true/false properties for conditions
enum CondProp
{
alwaysTrue,
alwaysFalse,
trueOrFalse
};
staticStack<CondProp> myCondStack;
// LHS variable being visited?
bool myLhsVar;
size_t myLhsVarIdx;
public:
using Visitor<DomainProcessor>::visit;
// Domains start with the singleton 0
DomainProcessor( const size_t nVar, const bool fuzzy) :
myFuzzy( fuzzy), myVarDomains( nVar, 0.0), myLhsVar( false) {}
// Visitors
// Expressions
// Binaries
void visit( NodeAdd& node)
{
visitArguments( node);
Domain res = myDomStack[1] + myDomStack[0];
myDomStack.pop( 2);
myDomStack.push( move( res));
}
void visit( NodeSub& node)
{
visitArguments( node);
Domain res = myDomStack[1] - myDomStack[0];
myDomStack.pop( 2);
myDomStack.push( move( res));
}
void visit( NodeMult& node)
{
visitArguments( node);
Domain res = myDomStack[1] * myDomStack[0];
myDomStack.pop( 2);
myDomStack.push( move( res));
}
void visit( NodeDiv& node)
{
visitArguments( node);
Domain res = myDomStack[1] / myDomStack[0];
myDomStack.pop( 2);
myDomStack.push( move( res));
}
void visit( NodePow& node)
{
visitArguments( node);
Domain res = myDomStack[1].applyFunc2<double(*)(const double, const double)>(
pow,
myDomStack[0],
Interval( Bound::minusInfinity, Bound::plusInfinity));
myDomStack.pop( 2);
myDomStack.push( move( res));
}
// Unaries
void visit( NodeUplus& node)
{
visitArguments( node);
}
void visit( NodeUminus& node)
{
visitArguments( node);
myDomStack.top() = -myDomStack.top();
}
// Functions
void visit( NodeLog& node)
{
visitArguments( node);
Domain res = myDomStack.top().applyFunc<double(*)(const double)>(
log,
Interval( Bound::minusInfinity, Bound::plusInfinity));
myDomStack.pop();
myDomStack.push( move( res));
}
void visit( NodeSqrt& node)
{
visitArguments( node);
Domain res = myDomStack.top().applyFunc<double(*)(const double)>(
sqrt,
Interval( 0.0, Bound::plusInfinity));
myDomStack.pop();
myDomStack.push( move( res));
}
void visit( NodeMax& node)
{
visitArguments( node);
Domain res = myDomStack.top();
myDomStack.pop();
for(size_t i=1; i<node.arguments.size(); ++i)
{
res = res.dmax( myDomStack.top());
myDomStack.pop();
}
myDomStack.push( move( res));
}
void visit( NodeMin& node)
{
visitArguments( node);
Domain res = myDomStack.top();
myDomStack.pop();
for(size_t i=1; i<node.arguments.size(); ++i)
{
res = res.dmin( myDomStack.top());
myDomStack.pop();
}
myDomStack.push( move( res));
}
void visit( NodeSmooth& node)
{
visitArguments( node);
// Pop eps
myDomStack.pop();
// Makes no sense with non-continuous x
if (myDomStack[2].discrete())
{
throw runtime_error("Smooth called with discrete x");
}
// Get min and max val if neg and if pos
Bound minIfNeg = myDomStack[0].minBound();
Bound maxIfNeg = myDomStack[0].maxBound();
Bound minIfPos = myDomStack[1].minBound();
Bound maxIfPos = myDomStack[1].maxBound();
Bound minB = min( minIfNeg, minIfPos), maxB = max( maxIfNeg, maxIfPos);
// Pop
myDomStack.pop( 3);
// Result
myDomStack.push( Interval( minB, maxB));
}
// Conditions
void visit( NodeEqual& node)
{
visitArguments( node);
Domain& dom = myDomStack.top();
// Always true / false?
if( !dom.canBeZero())
{
node.alwaysTrue = false;
node.alwaysFalse = true;
myCondStack.push( alwaysFalse);
}
else if( !dom.canBeNonZero())
{
node.alwaysTrue = true;
node.alwaysFalse = false;
myCondStack.push( alwaysTrue);
}
else
{
node.alwaysTrue = node.alwaysFalse = false;
myCondStack.push( trueOrFalse);
if( myFuzzy)
{
// Continuous or discrete?
node.discrete = dom.zeroIsDiscrete();
// Discrete
if( node.discrete)
{
bool subDomRightOfZero = dom.smallestPosLb( node.rb, true);
if( !subDomRightOfZero) node.rb = 0.5;
bool subDomLeftOfZero = dom.biggestNegRb( node.lb, true);
if( !subDomLeftOfZero) node.lb = -0.5;
}
}
}
// Dump domain info to file, comment when not using
#ifdef DUMP
static int iii = 0;
++iii;
{
ofstream ofs( string( "c:\\temp\\equal") + to_string( iii) + ".txt");
ofs << "Equality " << iii << endl;
ofs << "Domain = " << dom << endl;
ofs << "Node discrete = " << node.discrete << endl;
if( node.discrete) ofs << "Node lB, rB = " << node.lb << "," << node.rb << endl;
}
#endif
// End of dump
myDomStack.pop();
}
void visit( NodeNot& node)
{
visitArguments( node);
CondProp cp = myCondStack.top();
myCondStack.pop();
if( cp == alwaysTrue)
{
node.alwaysTrue = false;
node.alwaysFalse = true;
myCondStack.push( alwaysFalse);
}
else if( cp == alwaysFalse)
{
node.alwaysTrue = true;
node.alwaysFalse = false;
myCondStack.push( alwaysTrue);
}
else
{
node.alwaysTrue = node.alwaysFalse = false;
myCondStack.push( trueOrFalse);
}
}
// For visiting superior and supEqual
template<bool strict, class NodeSup>
inline void visitSupT( NodeSup& node)
{
visitArguments( node);
Domain& dom = myDomStack.top();
// Always true / false?
if( !dom.canBePositive( strict))
{
node.alwaysTrue = false;
node.alwaysFalse = true;
myCondStack.push( alwaysFalse);
}
else if( !dom.canBeNegative( !strict))
{
node.alwaysTrue = true;
node.alwaysFalse = false;
myCondStack.push( alwaysTrue);
}
// Can be true or false
else
{
node.alwaysTrue = node.alwaysFalse = false;
myCondStack.push( trueOrFalse);
if( myFuzzy)
{
// Continuous or discrete?
node.discrete = !dom.canBeZero() || dom.zeroIsDiscrete();
// Fuzzy logic processing
if( node.discrete)
{
// Case 1: expr cannot be zero
if( !dom.canBeZero())
{
// we know we have subdomains on the left and on the right of 0
dom.smallestPosLb( node.rb, true);
dom.biggestNegRb( node.lb, true);
}
// Case 2: {0} is a singleton
else
{
if( strict)
{
node.lb = 0.0;
dom.smallestPosLb( node.rb, true);
}
else
{
node.rb = 0.0;
dom.biggestNegRb( node.lb, true);
}
}
}
}
}
// Dump domain info to file, comment when not using
#ifdef DUMP
static int iii = 0;
++iii;
{
ofstream ofs( string( "c:\\temp\\sup") + (strict? "": "equal") + to_string( iii) + ".txt");
ofs << "Inequality " << iii << endl;
ofs << "Domain = " << dom << endl;
ofs << "Node discrete = " << node.discrete << endl;
if( node.discrete) ofs << "Node lB, rB = " << node.lb << "," << node.rb << endl;
}
#endif
// End of dump
myDomStack.pop();
}
void visit( NodeSup& node)
{
visitSupT<true>( node);
}
void visit( NodeSupEqual& node)
{
visitSupT<false>( node);
}
void visit( NodeAnd& node)
{
visitArguments( node);
CondProp cp1 = myCondStack.top();
myCondStack.pop();
CondProp cp2 = myCondStack.top();
myCondStack.pop();
if( cp1 == alwaysTrue && cp2 == alwaysTrue)
{
node.alwaysTrue = true;
node.alwaysFalse = false;
myCondStack.push( alwaysTrue);
}
else if( cp1 == alwaysFalse || cp2 == alwaysFalse)
{
node.alwaysTrue = false;
node.alwaysFalse = true;
myCondStack.push( alwaysFalse);
}
else
{
node.alwaysTrue = node.alwaysFalse = false;
myCondStack.push( trueOrFalse);
}
}
void visit( NodeOr& node)
{
visitArguments( node);
CondProp cp1 = myCondStack.top();
myCondStack.pop();
CondProp cp2 = myCondStack.top();
myCondStack.pop();
if( cp1 == alwaysTrue || cp2 == alwaysTrue)
{
node.alwaysTrue = true;
node.alwaysFalse = false;
myCondStack.push( alwaysTrue);
}
else if( cp1 == alwaysFalse && cp2 == alwaysFalse)
{
node.alwaysTrue = false;
node.alwaysFalse = true;
myCondStack.push( alwaysFalse);
}
else
{
node.alwaysTrue = node.alwaysFalse = false;
myCondStack.push( trueOrFalse);
}
}
// Instructions
void visit( NodeIf& node)
{
// Last "if true" statement index
size_t lastTrueStat = node.firstElse == -1? node.arguments.size()-1: node.firstElse-1;
// Visit condition
node.arguments[0]->accept( *this);
// Always true/false?
CondProp cp = myCondStack.top();
myCondStack.pop();
if( cp == alwaysTrue)
{
node.alwaysTrue = true;
node.alwaysFalse = false;
// Visit "if true" statements
for(size_t i=1; i<=lastTrueStat; ++i) node.arguments[i]->accept( *this);
}
else if( cp == alwaysFalse)
{
node.alwaysTrue = false;
node.alwaysFalse = true;
// Visit "if false" statements, if any
if( node.firstElse != -1)
for(size_t i=node.firstElse; i<node.arguments.size(); ++i) node.arguments[i]->accept( *this);
}
else
{
node.alwaysTrue = node.alwaysFalse = false;
// Record variable domain before if statements are executed
vector<Domain> domStore0( node.affectedVars.size());
for(size_t i=0; i<node.affectedVars.size(); ++i) domStore0[i] = myVarDomains[node.affectedVars[i]];
// Execute if statements
for(size_t i=1; i<=lastTrueStat; ++i) node.arguments[i]->accept( *this);
// Record variable domain after if statements are executed
vector<Domain> domStore1( node.affectedVars.size());
for(size_t i=0; i<node.affectedVars.size(); ++i) domStore1[i] = move( myVarDomains[node.affectedVars[i]]);
// Reset variable domains
for(size_t i=0; i<node.affectedVars.size(); ++i) myVarDomains[node.affectedVars[i]] = move( domStore0[i]);
// Execute else statements if any
if( node.firstElse != -1)
for(size_t i=node.firstElse; i<node.arguments.size(); ++i) node.arguments[i]->accept( *this);
// Merge domains
for(size_t i=0; i<node.affectedVars.size(); ++i) myVarDomains[node.affectedVars[i]].addDomain( domStore1[i]);
}
}
void visit( NodeAssign& node)
{
// Visit the LHS variable
myLhsVar = true;
node.arguments[0]->accept( *this);
myLhsVar = false;
// Visit the RHS expression
node.arguments[1]->accept( *this);
// Write RHS domain into variable
myVarDomains[myLhsVarIdx] = myDomStack.top();
// Pop
myDomStack.pop();
}
void visit( NodePays& node)
{
// Visit the LHS variable
myLhsVar = true;
node.arguments[0]->accept( *this);
myLhsVar = false;
// Visit the RHS expression
node.arguments[1]->accept( *this);
// Write RHS domain into variable
// Numeraire domain = (0,+inf)
static const Domain numDomain( Interval( 0.0, Bound::plusInfinity));
// Payment domain
Domain payDomain = myDomStack.top() / numDomain;
// Write
myVarDomains[myLhsVarIdx] = myVarDomains[myLhsVarIdx] + payDomain;
// Pop
myDomStack.pop();
}
// Variables and constants
void visit( NodeVar& node)
{
// LHS?
if( myLhsVar) // Write
{
// Record address in myLhsVarAdr
myLhsVarIdx = node.index;
}
else // Read
{
// Push domain onto the stack
myDomStack.push( myVarDomains[node.index]);
}
}
void visit( NodeConst& node)
{
myDomStack.push( node.constVal);
}
// Scenario related
void visit( NodeSpot& node)
{
static const Domain realDom(
Interval( Bound::minusInfinity, Bound::plusInfinity));
myDomStack.push( realDom);
}
};