Skip to content

Commit 5bdb6f4

Browse files
author
sebastien.bouvard
committed
QPR-13698 Add new funciton to scriptedtrade ops/grad + doc
1 parent 13d79af commit 5bdb6f4

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

Docs/ScriptedTrade/docs/language.tex

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
\verb+max+ & \\
4242
\verb+min+ & \\
4343
\verb+pow+ & \\
44+
\verb+frac+ & \\
45+
\verb+round+ & \\
4446
\verb+black+ & \\
4547
\verb+dcf+ & \\
4648
\verb+days+ & \\ \hline
@@ -398,21 +400,21 @@
398400
\item {\tt REQUIRE Strike >= 0;}
399401
\end{itemize}
400402

401-
% ====================================================
402-
\stsubsection{Functions {\tt min}, {\tt max}, {\tt pow}}
403-
% ====================================================
403+
% =========================================================================
404+
\stsubsection{Functions {\tt min}, {\tt max}, {\tt pow}}, {\tt round(x,y)}
405+
% =========================================================================
404406

405-
Binary functions {\tt min(x,y)}, {\tt max(x,y)}, {\tt pow(x,y)}, applicable to numbers only.
407+
Binary functions {\tt min(x,y)}, {\tt max(x,y)}, {\tt pow(x,y)}, {\tt round(x,y)}, applicable to numbers only.
406408

407-
% ====================================================
408-
\stsubsection{Functions {\tt -}, abs, exp, ln, sqrt}
409-
% ====================================================
409+
% ========================================================
410+
\stsubsection{Functions {\tt -}, abs, exp, ln, sqrt, frac}
411+
% ========================================================
410412

411-
Unary functions {\tt -x}, {\tt abs(x)}, {\tt exp(x)}, {\tt ln(x)}, {\tt sqrt(x)}, applicable to numbers only.
413+
Unary functions {\tt -x}, {\tt abs(x)}, {\tt exp(x)}, {\tt ln(x)}, {\tt sqrt(x)}, {\tt frac(x)}, applicable to numbers only.
412414

413-
% ====================================================
415+
% =======================================================
414416
\stsubsection{Functions {\tt normalPdf}, {\tt normalCdf}}
415-
% ====================================================
417+
% =======================================================
416418

417419
Returns the standard normal pdf $\phi(x)$ resp. cdf $\Phi(x)$, applicable to numbers only.
418420

OREData/ored/scripting/astprinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ class ASTPrinter : public AcyclicVisitor,
9494
void visit(FunctionNormalPdfNode& n) override { print("FunctionNormalPdf", n); }
9595
void visit(FunctionMinNode& n) override { print("FunctionMin", n); }
9696
void visit(FunctionMaxNode& n) override { print("FunctionMax", n); }
97-
void visit(FunctionFractionNode& n) override { print("FunctionFractionNode", n); }
98-
void visit(FunctionRoundNode& n) override { print("FunctionRoundNode", n); }
97+
void visit(FunctionFractionNode& n) override { print("FunctionFraction", n); }
98+
void visit(FunctionRoundNode& n) override { print("FunctionRound", n); }
9999
void visit(FunctionPowNode& n) override { print("FunctionPow", n); }
100100
void visit(FunctionBlackNode& n) override { print("FunctionBlack", n); }
101101
void visit(FunctionDcfNode& n) override { print("FunctionDcf", n); }

QuantExt/qle/math/randomvariable_ops.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ getRandomVariableOps(const Size size, const Size regressionOrder, QuantLib::LsmB
188188
// NormalPdf = 18
189189
ops.push_back(
190190
[](const std::vector<const RandomVariable*>& args, const Size node) { return QuantExt::normalPdf(*args[0]); });
191+
192+
// Frac = 19
193+
ops.push_back(
194+
[](const std::vector<const RandomVariable*>& args, const Size node) { return QuantExt::frac(*args[0]); });
195+
196+
// Round = 29
197+
ops.push_back(
198+
[](const std::vector<const RandomVariable*>& args, const Size node) { return QuantExt::round(*args[0], *args[1]); });
191199

192200
return ops;
193201
}
@@ -300,11 +308,25 @@ std::vector<RandomVariableGrad> getRandomVariableGradients(const Size size, cons
300308
grads.push_back([](const std::vector<const RandomVariable*>& args, const RandomVariable* v,
301309
const Size node) -> std::vector<RandomVariable> { return {-(*args[0]) * *v}; });
302310

311+
// Frac = 19
312+
grads.push_back([](const std::vector<const RandomVariable*>& args, const RandomVariable* v,
313+
const Size node) -> std::vector<RandomVariable> { return {QuantExt::frac(*args[0])}; });
314+
315+
// Round = 20
316+
grads.push_back([](const std::vector<const RandomVariable*>& args, const RandomVariable* v,
317+
const Size node) -> std::vector<RandomVariable> { return {QuantExt::round(*args[0], *args[1])}; });
318+
303319
return grads;
304320
}
305321

306322
std::vector<RandomVariableOpNodeRequirements> getRandomVariableOpNodeRequirements() {
307323
std::vector<RandomVariableOpNodeRequirements> res;
324+
/*
325+
Each op is a function f(x1,...,xn) args. If the value is needed, we set the value to true.
326+
The vector represents x1,...,xn and the second element of the pair f(vector<xi>)
327+
Note: It is used for optimization, i.e. values that are not needed are deleted early. If everything is set to true, that will increase the memory footprint unnecessarily.
328+
And if something is set to false that is actually required later on to calculate a gradient, a run time error about an uninitilized randomvariable
329+
*/
308330

309331
// None = 0
310332
res.push_back([](const std::size_t nArgs) { return std::make_pair(std::vector<bool>(nArgs, false), false); });
@@ -360,9 +382,15 @@ std::vector<RandomVariableOpNodeRequirements> getRandomVariableOpNodeRequirement
360382
// NormalCdf = 17
361383
res.push_back([](const std::size_t nArgs) { return std::make_pair(std::vector<bool>(nArgs, true), false); });
362384

363-
// NormalCdf = 18
385+
// NormalPdf = 18
364386
res.push_back([](const std::size_t nArgs) { return std::make_pair(std::vector<bool>(nArgs, true), true); });
365387

388+
// Frac = 19
389+
res.push_back([](const std::size_t nArgs) { return std::make_pair(std::vector<bool>(nArgs, true), false); });
390+
391+
// Round = 20
392+
res.push_back([](const std::size_t nArgs) { return std::make_pair(std::vector<bool>(nArgs, true), false); });
393+
366394
return res;
367395
}
368396

0 commit comments

Comments
 (0)