Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
RandomVariable Struct Reference

#include <qle/math/randomvariable.hpp>

+ Collaboration diagram for RandomVariable:

Public Member Functions

 ~RandomVariable ()
 
 RandomVariable ()
 
 RandomVariable (const RandomVariable &r)
 
 RandomVariable (RandomVariable &&r)
 
RandomVariableoperator= (const RandomVariable &r)
 
RandomVariableoperator= (RandomVariable &&r)
 
 RandomVariable (const Size n, const Real value=0.0, const Real time=Null< Real >())
 
 RandomVariable (const Filter &f, const Real valueTrue=1.0, const Real valueFalse=0.0, const Real time=Null< Real >())
 
 RandomVariable (const Size n, const Real *const data, const Real time=Null< Real >())
 
 RandomVariable (const std::vector< double > &data, const Real time=Null< Real >())
 
 RandomVariable (const QuantLib::Array &data, const Real time=Null< Real >())
 
void copyToMatrixCol (QuantLib::Matrix &, const Size j) const
 
void copyToArray (QuantLib::Array &array) const
 
void clear ()
 
void set (const Size i, const Real v)
 
void setTime (const Real time)
 
void setAll (const Real v)
 
void resetSize (const Size n)
 
bool deterministic () const
 
void updateDeterministic ()
 
bool initialised () const
 
Size size () const
 
Real operator[] (const Size i) const
 
Real at (const Size i) const
 
Real time () const
 
RandomVariableoperator+= (const RandomVariable &)
 
RandomVariableoperator-= (const RandomVariable &)
 
RandomVariableoperator*= (const RandomVariable &)
 
RandomVariableoperator/= (const RandomVariable &)
 
void expand ()
 
double * data ()
 

Static Public Attributes

static std::function< void(RandomVariable &)> deleter
 

Private Member Functions

void checkTimeConsistencyAndUpdate (const Real t)
 

Private Attributes

Size n_
 
double constantData_
 
double * data_
 
bool deterministic_
 
Real time_
 

Friends

bool operator== (const RandomVariable &, const RandomVariable &)
 
RandomVariable operator+ (RandomVariable, const RandomVariable &)
 
RandomVariable operator- (RandomVariable, const RandomVariable &)
 
RandomVariable operator* (RandomVariable, const RandomVariable &)
 
RandomVariable operator/ (RandomVariable, const RandomVariable &)
 
RandomVariable max (RandomVariable, const RandomVariable &)
 
RandomVariable min (RandomVariable, const RandomVariable &)
 
RandomVariable pow (RandomVariable, const RandomVariable &)
 
RandomVariable operator- (RandomVariable)
 
RandomVariable abs (RandomVariable)
 
RandomVariable exp (RandomVariable)
 
RandomVariable log (RandomVariable)
 
RandomVariable sqrt (RandomVariable)
 
RandomVariable sin (RandomVariable)
 
RandomVariable cos (RandomVariable)
 
RandomVariable normalCdf (RandomVariable)
 
RandomVariable normalPdf (RandomVariable)
 
Filter close_enough (const RandomVariable &, const RandomVariable &)
 
Filter operator< (const RandomVariable &, const RandomVariable &)
 
Filter operator<= (const RandomVariable &, const RandomVariable &)
 
Filter operator> (const RandomVariable &, const RandomVariable &)
 
Filter operator>= (const RandomVariable &, const RandomVariable &)
 
bool close_enough_all (const RandomVariable &, const RandomVariable &)
 
RandomVariable applyFilter (RandomVariable, const Filter &)
 
RandomVariable applyInverseFilter (RandomVariable, const Filter &)
 
RandomVariable conditionalResult (const Filter &, RandomVariable, const RandomVariable &)
 
RandomVariable indicatorEq (RandomVariable, const RandomVariable &, const Real trueVal, const Real falseVal)
 
RandomVariable indicatorGt (RandomVariable, const RandomVariable &, const Real trueVal, const Real falseVal, const Real eps)
 
RandomVariable indicatorGeq (RandomVariable, const RandomVariable &, const Real trueVal, const Real falseVal, const Real eps)
 

Detailed Description

Definition at line 152 of file randomvariable.hpp.

Constructor & Destructor Documentation

◆ ~RandomVariable()

Definition at line 310 of file randomvariable.cpp.

+ Here is the call graph for this function:

◆ RandomVariable() [1/8]

◆ RandomVariable() [2/8]

Definition at line 315 of file randomvariable.cpp.

315 {
316 n_ = r.n_;
317 constantData_ = r.constantData_;
318 if (r.data_) {
319 resumeDataStats();
320 data_ = new double[n_];
321 // std::memcpy(data_, r.data_, n_ * sizeof(double));
322 std::copy(r.data_, r.data_ + n_, data_);
323 stopDataStats(n_);
324 } else {
325 data_ = nullptr;
326 }
327 deterministic_ = r.deterministic_;
328 time_ = r.time_;
329}

◆ RandomVariable() [3/8]

Definition at line 331 of file randomvariable.cpp.

331 {
332 n_ = r.n_;
333 constantData_ = r.constantData_;
334 data_ = r.data_;
335 r.data_ = nullptr;
336 deterministic_ = r.deterministic_;
337 time_ = r.time_;
338}

◆ RandomVariable() [4/8]

RandomVariable ( const Size  n,
const Real  value = 0.0,
const Real  time = Null<Real>() 
)
explicit

Definition at line 385 of file randomvariable.cpp.

386 : n_(n), constantData_(value), data_(nullptr), deterministic_(n != 0), time_(time) {}

◆ RandomVariable() [5/8]

RandomVariable ( const Filter f,
const Real  valueTrue = 1.0,
const Real  valueFalse = 0.0,
const Real  time = Null<Real>() 
)
explicit

Definition at line 388 of file randomvariable.cpp.

388 {
389 if (!f.initialised()) {
390 data_ = nullptr;
391 clear();
392 return;
393 }
394 n_ = f.size();
395 if (f.deterministic()) {
396 data_ = nullptr;
397 setAll(f.at(0) ? valueTrue : valueFalse);
398 } else {
399 resumeDataStats();
400 constantData_ = 0.0;
401 deterministic_ = false;
402 data_ = new double[n_];
403 for (Size i = 0; i < n_; ++i)
404 set(i, f[i] ? valueTrue : valueFalse);
405 stopDataStats(n_);
406 }
407 time_ = time;
408}
void set(const Size i, const Real v)
void setAll(const Real v)
+ Here is the call graph for this function:

◆ RandomVariable() [6/8]

RandomVariable ( const Size  n,
const Real *const  data,
const Real  time = Null<Real>() 
)
explicit

Definition at line 410 of file randomvariable.cpp.

410 {
411 n_ = n;
412 deterministic_ = false;
413 time_ = time;
414 if (n_ != 0) {
415 resumeDataStats();
416 data_ = new double[n_];
417 // std::memcpy(data_, array.begin(), n_ * sizeof(double));
418 std::copy(data, data + n_, data_);
419 stopDataStats(n_);
420 } else {
421 data_ = nullptr;
422 }
423 constantData_ = 0.0;
424}
+ Here is the call graph for this function:

◆ RandomVariable() [7/8]

RandomVariable ( const std::vector< double > &  data,
const Real  time = Null< Real >() 
)
explicit
+ Here is the call graph for this function:

◆ RandomVariable() [8/8]

RandomVariable ( const QuantLib::Array &  data,
const Real  time = Null<Real>() 
)
explicit

Definition at line 429 of file randomvariable.cpp.

430 : RandomVariable(data.size(), data.begin(), time) {}

Member Function Documentation

◆ operator=() [1/2]

RandomVariable & operator= ( const RandomVariable r)

Definition at line 340 of file randomvariable.cpp.

340 {
341 if (r.deterministic_) {
342 deterministic_ = true;
343 if (data_) {
344 delete[] data_;
345 data_ = nullptr;
346 }
347 } else {
348 deterministic_ = false;
349 if (r.n_ != 0) {
350 resumeDataStats();
351 if (n_ != r.n_) {
352 if (data_)
353 delete[] data_;
354 data_ = new double[r.n_];
355 }
356 // std::memcpy(data_, r.data_, r.n_ * sizeof(double));
357 std::copy(r.data_, r.data_ + r.n_, data_);
358 stopDataStats(r.n_);
359 } else {
360 if (data_) {
361 delete[] data_;
362 data_ = nullptr;
363 }
364 }
365 }
366 n_ = r.n_;
367 constantData_ = r.constantData_;
368 time_ = r.time_;
369 return *this;
370}

◆ operator=() [2/2]

RandomVariable & operator= ( RandomVariable &&  r)

Definition at line 372 of file randomvariable.cpp.

372 {
373 n_ = r.n_;
374 constantData_ = r.constantData_;
375 if (data_) {
376 delete[] data_;
377 }
378 data_ = r.data_;
379 r.data_ = nullptr;
380 deterministic_ = r.deterministic_;
381 time_ = r.time_;
382 return *this;
383}

◆ copyToMatrixCol()

void copyToMatrixCol ( QuantLib::Matrix &  m,
const Size  j 
) const

Definition at line 432 of file randomvariable.cpp.

432 {
433 if (deterministic_)
434 std::fill(m.column_begin(j), std::next(m.column_end(j), n_), constantData_);
435 else if (n_ != 0) {
436 resumeDataStats();
437 std::copy(data_, data_ + n_, m.column_begin(j));
438 stopDataStats(n_);
439 }
440}
+ Here is the caller graph for this function:

◆ copyToArray()

void copyToArray ( QuantLib::Array &  array) const

Definition at line 442 of file randomvariable.cpp.

442 {
443 if (deterministic_)
444 std::fill(array.begin(), array.end(), constantData_);
445 else if (n_ != 0) {
446 resumeDataStats();
447 // std::memcpy(array.begin(), data_, n_ * sizeof(double));
448 std::copy(data_, data_ + n_, array.begin());
449 stopDataStats(n_);
450 }
451}
+ Here is the caller graph for this function:

◆ clear()

void clear ( )

Definition at line 453 of file randomvariable.cpp.

453 {
454 n_ = 0;
455 constantData_ = 0.0;
456 if (data_) {
457 delete[] data_;
458 data_ = nullptr;
459 }
460 deterministic_ = false;
461 time_ = Null<Real>();
462}
+ Here is the caller graph for this function:

◆ set()

void set ( const Size  i,
const Real  v 
)

Definition at line 346 of file randomvariable.hpp.

346 {
347 QL_REQUIRE(i < n_, "RandomVariable::set(" << i << "): out of bounds, size is " << n_);
348 if (deterministic_) {
349 if (!QuantLib::close_enough(v, constantData_)) {
350 expand();
351 } else {
352 return;
353 }
354 }
355 data_[i] = v;
356}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setTime()

void setTime ( const Real  time)

Definition at line 174 of file randomvariable.hpp.

174{ time_ = std::max(time, 0.0); }
+ Here is the call graph for this function:

◆ setAll()

void setAll ( const Real  v)

Definition at line 478 of file randomvariable.cpp.

478 {
479 QL_REQUIRE(n_ > 0, "RandomVariable::setAll(): dimension is zero");
480 if (data_) {
481 delete[] data_;
482 data_ = nullptr;
483 }
484 constantData_ = v;
485 deterministic_ = true;
486}
+ Here is the caller graph for this function:

◆ resetSize()

void resetSize ( const Size  n)

Definition at line 488 of file randomvariable.cpp.

488 {
489 QL_REQUIRE(deterministic_, "RandomVariable::resetSize(): only possible for deterministic variables.");
490 n_ = n;
491}

◆ deterministic()

bool deterministic ( ) const

Definition at line 180 of file randomvariable.hpp.

180{ return deterministic_; }
+ Here is the caller graph for this function:

◆ updateDeterministic()

void updateDeterministic ( )

Definition at line 464 of file randomvariable.cpp.

464 {
465 if (deterministic_ || !initialised())
466 return;
467 for (Size i = 0; i < n_; ++i) {
468 resumeCalcStats();
469 if (!QuantLib::close_enough(data_[i], constantData_)) {
470 stopCalcStats(i);
471 return;
472 }
473 }
475 stopCalcStats(n_);
476}
+ Here is the call graph for this function:

◆ initialised()

bool initialised ( ) const

Definition at line 182 of file randomvariable.hpp.

182{ return n_ != 0; }
+ Here is the caller graph for this function:

◆ size()

Size size ( ) const

Definition at line 183 of file randomvariable.hpp.

183{ return n_; }
+ Here is the caller graph for this function:

◆ operator[]()

Real operator[] ( const Size  i) const

Definition at line 358 of file randomvariable.hpp.

358 {
359 if (deterministic_)
360 return constantData_;
361 else
362 return data_[i];
363}
+ Here is the caller graph for this function:

◆ at()

Real at ( const Size  i) const

Definition at line 365 of file randomvariable.hpp.

365 {
366 QL_REQUIRE(n_ > 0, "RandomVariable::at(" << i << "): dimension is zero");
367 if (deterministic_)
368 return constantData_;
369 QL_REQUIRE(i < n_, "RandomVariable::at(" << i << "): out of bounds, size is " << n_);
370 return operator[](i);
371}
Real operator[](const Size i) const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ time()

Real time ( ) const

Definition at line 186 of file randomvariable.hpp.

186{ return time_; }
+ Here is the caller graph for this function:

◆ operator+=()

RandomVariable & operator+= ( const RandomVariable y)

Definition at line 533 of file randomvariable.cpp.

533 {
534 if (!y.initialised())
535 clear();
536 if (!initialised())
537 return *this;
538 QL_REQUIRE(size() == y.size(),
539 "RandomVariable: x += y: x size (" << size() << ") must be equal to y size (" << y.size() << ")");
541 if (!y.deterministic_)
542 expand();
543 else if (QuantLib::close_enough(y.constantData_, 0.0))
544 return *this;
545 if (deterministic_)
546 constantData_ += y.constantData_;
547 else {
548 resumeCalcStats();
549 for (Size i = 0; i < n_; ++i) {
550 data_[i] += y[i];
551 }
552 stopCalcStats(n_);
553 }
554 return *this;
555}
void checkTimeConsistencyAndUpdate(const Real t)
+ Here is the call graph for this function:

◆ operator-=()

RandomVariable & operator-= ( const RandomVariable y)

Definition at line 557 of file randomvariable.cpp.

557 {
558 if (!y.initialised())
559 clear();
560 if (!initialised())
561 return *this;
562 QL_REQUIRE(size() == y.size(),
563 "RandomVariable: x -= y: x size (" << size() << ") must be equal to y size (" << y.size() << ")");
565 if (!y.deterministic_)
566 expand();
567 else if (QuantLib::close_enough(y.constantData_, 0.0))
568 return *this;
569 if (deterministic_)
570 constantData_ -= y.constantData_;
571 else {
572 resumeCalcStats();
573 for (Size i = 0; i < n_; ++i) {
574 data_[i] -= y[i];
575 }
576 stopCalcStats(n_);
577 }
578 return *this;
579}
+ Here is the call graph for this function:

◆ operator*=()

RandomVariable & operator*= ( const RandomVariable y)

Definition at line 581 of file randomvariable.cpp.

581 {
582 if (!y.initialised())
583 clear();
584 if (!initialised())
585 return *this;
586 QL_REQUIRE(size() == y.size(),
587 "RandomVariable: x *= y: x size (" << size() << ") must be equal to y size (" << y.size() << ")");
589 if (!y.deterministic_)
590 expand();
591 else if (QuantLib::close_enough(y.constantData_, 1.0))
592 return *this;
593 if (deterministic_)
594 constantData_ *= y.constantData_;
595 else {
596 resumeCalcStats();
597 for (Size i = 0; i < n_; ++i) {
598 data_[i] *= y[i];
599 }
600 stopCalcStats(n_);
601 }
602 return *this;
603}
+ Here is the call graph for this function:

◆ operator/=()

RandomVariable & operator/= ( const RandomVariable y)

Definition at line 605 of file randomvariable.cpp.

605 {
606 if (!y.initialised())
607 clear();
608 if (!initialised())
609 return *this;
610 QL_REQUIRE(size() == y.size(),
611 "RandomVariable: x /= y: x size (" << size() << ") must be equal to y size (" << y.size() << ")");
613 if (!y.deterministic_)
614 expand();
615 else if (QuantLib::close_enough(y.constantData_, 1.0))
616 return *this;
617 if (deterministic_)
618 constantData_ /= y.constantData_;
619 else {
620 resumeCalcStats();
621 for (Size i = 0; i < n_; ++i) {
622 data_[i] /= y[i];
623 }
624 stopCalcStats(n_);
625 }
626 return *this;
627}
+ Here is the call graph for this function:

◆ expand()

void expand ( )

Definition at line 493 of file randomvariable.cpp.

493 {
494 if (!deterministic_)
495 return;
496 deterministic_ = false;
497 resumeDataStats();
498 data_ = new double[n_];
499 std::fill(data_, data_ + n_, constantData_);
500 stopDataStats(n_);
501}
+ Here is the caller graph for this function:

◆ data()

double * data ( )

Definition at line 373 of file randomvariable.hpp.

373{ return data_; }
+ Here is the caller graph for this function:

◆ checkTimeConsistencyAndUpdate()

void checkTimeConsistencyAndUpdate ( const Real  t)
private

Definition at line 503 of file randomvariable.cpp.

503 {
504 QL_REQUIRE((time_ == Null<Real>() || t == Null<Real>()) || QuantLib::close_enough(time_, t),
505 "RandomVariable: inconsistent times " << time_ << " and " << t);
506 if (time_ == Null<Real>())
507 time_ = t;
508}
+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator==

bool operator== ( const RandomVariable ,
const RandomVariable  
)
friend

Definition at line 515 of file randomvariable.cpp.

515 {
516 if (a.size() != b.size())
517 return false;
518 if (a.deterministic_ && b.deterministic_) {
519 return a.constantData_ == b.constantData_;
520 } else {
521 resumeCalcStats();
522 for (Size j = 0; j < a.size(); ++j)
523 if (a[j] != b[j]) {
524 stopCalcStats(j);
525 return false;
526 }
527 }
528 return QuantLib::close_enough(a.time(), b.time());
529}

◆ operator+

RandomVariable operator+ ( RandomVariable  ,
const RandomVariable  
)
friend

Definition at line 629 of file randomvariable.cpp.

629 {
630 if (!x.initialised() || !y.initialised())
631 return RandomVariable();
632 x += y;
633 return x;
634}

◆ operator- [1/2]

RandomVariable operator- ( RandomVariable  ,
const RandomVariable  
)
friend

Definition at line 636 of file randomvariable.cpp.

636 {
637 if (!x.initialised() || !y.initialised())
638 return RandomVariable();
639 x -= y;
640 return x;
641}

◆ operator*

RandomVariable operator* ( RandomVariable  ,
const RandomVariable  
)
friend

Definition at line 643 of file randomvariable.cpp.

643 {
644 if (!x.initialised() || !y.initialised())
645 return RandomVariable();
646 x *= y;
647 return x;
648}

◆ operator/

RandomVariable operator/ ( RandomVariable  ,
const RandomVariable  
)
friend

Definition at line 650 of file randomvariable.cpp.

650 {
651 if (!x.initialised() || !y.initialised())
652 return RandomVariable();
653 x /= y;
654 return x;
655}

◆ max

RandomVariable max ( RandomVariable  ,
const RandomVariable  
)
friend

Definition at line 657 of file randomvariable.cpp.

657 {
658 if (!x.initialised() || !y.initialised())
659 return RandomVariable();
660 QL_REQUIRE(x.size() == y.size(),
661 "RandomVariable: max(x,y): x size (" << x.size() << ") must be equal to y size (" << y.size() << ")");
662 x.checkTimeConsistencyAndUpdate(y.time());
663 if (!y.deterministic_)
664 x.expand();
665 if (x.deterministic())
666 x.constantData_ = std::max(x.constantData_, y.constantData_);
667 else {
668 resumeCalcStats();
669 for (Size i = 0; i < x.size(); ++i) {
670 x.data_[i] = std::max(x.data_[i], y[i]);
671 }
672 stopCalcStats(x.size());
673 }
674 return x;
675}

◆ min

RandomVariable min ( RandomVariable  ,
const RandomVariable  
)
friend

Definition at line 677 of file randomvariable.cpp.

677 {
678 if (!x.initialised() || !y.initialised())
679 return RandomVariable();
680 QL_REQUIRE(x.size() == y.size(),
681 "RandomVariable: min(x,y): x size (" << x.size() << ") must be equal to y size (" << y.size() << ")");
682 x.checkTimeConsistencyAndUpdate(y.time());
683 if (!y.deterministic_)
684 x.expand();
685 if (x.deterministic())
686 x.constantData_ = std::min(x.constantData_, y.constantData_);
687 else {
688 resumeCalcStats();
689 for (Size i = 0; i < x.size(); ++i) {
690 x.data_[i] = std::min(x.data_[i], y[i]);
691 }
692 stopCalcStats(x.size());
693 }
694 return x;
695}

◆ pow

RandomVariable pow ( RandomVariable  ,
const RandomVariable  
)
friend

Definition at line 697 of file randomvariable.cpp.

697 {
698 if (!x.initialised() || !y.initialised())
699 return RandomVariable();
700 QL_REQUIRE(x.size() == y.size(),
701 "RandomVariable: pow(x,y): x size (" << x.size() << ") must be equal to y size (" << y.size() << ")");
702 x.checkTimeConsistencyAndUpdate(y.time());
703 if (!y.deterministic_)
704 x.expand();
705 else if (QuantLib::close_enough(y.constantData_, 1.0))
706 return x;
707 if (x.deterministic())
708 x.constantData_ = std::pow(x.constantData_, y.constantData_);
709 else {
710 resumeCalcStats();
711 for (Size i = 0; i < x.size(); ++i) {
712 x.data_[i] = std::pow(x.data_[i], y[i]);
713 }
714 stopCalcStats(x.size());
715 }
716 return x;
717}

◆ operator- [2/2]

RandomVariable operator- ( RandomVariable  )
friend

Definition at line 719 of file randomvariable.cpp.

719 {
720 if (x.deterministic_)
721 x.constantData_ = -x.constantData_;
722 else {
723 resumeCalcStats();
724 for (Size i = 0; i < x.n_; ++i) {
725 x.data_[i] = -x.data_[i];
726 }
727 stopCalcStats(x.n_);
728 }
729 return x;
730}

◆ abs

RandomVariable abs ( RandomVariable  )
friend

Definition at line 732 of file randomvariable.cpp.

732 {
733 if (x.deterministic_)
734 x.constantData_ = std::abs(x.constantData_);
735 else {
736 resumeCalcStats();
737 for (Size i = 0; i < x.n_; ++i) {
738 x.data_[i] = std::abs(x.data_[i]);
739 }
740 stopCalcStats(x.n_);
741 }
742 return x;
743}

◆ exp

RandomVariable exp ( RandomVariable  )
friend

Definition at line 745 of file randomvariable.cpp.

745 {
746 if (x.deterministic_)
747 x.constantData_ = std::exp(x.constantData_);
748 else {
749 resumeCalcStats();
750 for (Size i = 0; i < x.n_; ++i) {
751 x.data_[i] = std::exp(x.data_[i]);
752 }
753 stopCalcStats(x.n_);
754 }
755 return x;
756}

◆ log

RandomVariable log ( RandomVariable  )
friend

Definition at line 758 of file randomvariable.cpp.

758 {
759 if (x.deterministic_)
760 x.constantData_ = std::log(x.constantData_);
761 else {
762 resumeCalcStats();
763 for (Size i = 0; i < x.n_; ++i) {
764 x.data_[i] = std::log(x.data_[i]);
765 }
766 stopCalcStats(x.n_);
767 }
768 return x;
769}

◆ sqrt

RandomVariable sqrt ( RandomVariable  )
friend

Definition at line 771 of file randomvariable.cpp.

771 {
772 if (x.deterministic_)
773 x.constantData_ = std::sqrt(x.constantData_);
774 else {
775 resumeCalcStats();
776 for (Size i = 0; i < x.n_; ++i) {
777 x.data_[i] = std::sqrt(x.data_[i]);
778 }
779 stopCalcStats(x.n_);
780 }
781 return x;
782}

◆ sin

RandomVariable sin ( RandomVariable  )
friend

Definition at line 784 of file randomvariable.cpp.

784 {
785 if (x.deterministic_)
786 x.constantData_ = std::sin(x.constantData_);
787 else {
788 resumeCalcStats();
789 for (Size i = 0; i < x.n_; ++i) {
790 x.data_[i] = std::sin(x.data_[i]);
791 }
792 stopCalcStats(x.n_);
793 }
794 return x;
795}

◆ cos

RandomVariable cos ( RandomVariable  )
friend

Definition at line 797 of file randomvariable.cpp.

797 {
798 if (x.deterministic_)
799 x.constantData_ = std::cos(x.constantData_);
800 else {
801 resumeCalcStats();
802 for (Size i = 0; i < x.n_; ++i) {
803 x.data_[i] = std::cos(x.data_[i]);
804 }
805 stopCalcStats(x.n_);
806 }
807 return x;
808}

◆ normalCdf

RandomVariable normalCdf ( RandomVariable  )
friend

Definition at line 810 of file randomvariable.cpp.

810 {
811 static const boost::math::normal_distribution<double> n;
812 if (x.deterministic_)
813 x.constantData_ = boost::math::cdf(n, x.constantData_);
814 else {
815 resumeCalcStats();
816 for (Size i = 0; i < x.n_; ++i) {
817 x.data_[i] = boost::math::cdf(n, x.data_[i]);
818 }
819 stopCalcStats(x.n_);
820 }
821 return x;
822}

◆ normalPdf

RandomVariable normalPdf ( RandomVariable  )
friend

Definition at line 824 of file randomvariable.cpp.

824 {
825 static const boost::math::normal_distribution<double> n;
826 if (x.deterministic_)
827 x.constantData_ = boost::math::pdf(n, x.constantData_);
828 else {
829 resumeCalcStats();
830 for (Size i = 0; i < x.n_; ++i) {
831 x.data_[i] = boost::math::pdf(n, x.data_[i]);
832 }
833 stopCalcStats(x.n_);
834 }
835 return x;
836}

◆ close_enough

Filter close_enough ( const RandomVariable ,
const RandomVariable  
)
friend

Definition at line 838 of file randomvariable.cpp.

838 {
839 if (!x.initialised() || !y.initialised())
840 return Filter();
841 QL_REQUIRE(x.size() == y.size(), "RandomVariable: close_enough(x,y): x size ("
842 << x.size() << ") must be equal to y size (" << y.size() << ")");
844 if (x.deterministic_ && y.deterministic_) {
845 return Filter(x.size(), QuantLib::close_enough(x.constantData_, y.constantData_));
846 }
847 resumeCalcStats();
848 Filter result(x.size(), false);
849 for (Size i = 0; i < x.size(); ++i) {
850 result.set(i, QuantLib::close_enough(x[i], y[i]));
851 }
852 stopCalcStats(x.size());
853 return result;
854}
void checkTimeConsistency(const RandomVariable &x, const RandomVariable &y)

◆ operator<

Filter operator< ( const RandomVariable x,
const RandomVariable y 
)
friend

Definition at line 988 of file randomvariable.cpp.

988 {
989 if (!x.initialised() || !y.initialised())
990 return Filter();
991 QL_REQUIRE(x.size() == y.size(),
992 "RandomVariable: x < y: x size (" << x.size() << ") must be equal to y size (" << y.size() << ")");
994 if (x.deterministic_ && y.deterministic_) {
995 return Filter(x.size(),
996 x.constantData_ < y.constantData_ && !QuantLib::close_enough(x.constantData_, y.constantData_));
997 }
998 resumeCalcStats();
999 Filter result(x.size(), false);
1000 for (Size i = 0; i < x.size(); ++i) {
1001 result.set(i, x[i] < y[i] && !QuantLib::close_enough(x[i], y[i]));
1002 }
1003 stopCalcStats(x.size());
1004 return result;
1005}

◆ operator<=

Filter operator<= ( const RandomVariable x,
const RandomVariable y 
)
friend

Definition at line 1007 of file randomvariable.cpp.

1007 {
1008 if (!x.initialised() || !y.initialised())
1009 return Filter();
1010 QL_REQUIRE(x.size() == y.size(),
1011 "RandomVariable: x <= y: x size (" << x.size() << ") must be equal to y size (" << y.size() << ")");
1013 if (x.deterministic_ && y.deterministic_) {
1014 return Filter(x.size(),
1015 x.constantData_ < y.constantData_ || QuantLib::close_enough(x.constantData_, y.constantData_));
1016 }
1017 resumeCalcStats();
1018 Filter result(x.size(), false);
1019 for (Size i = 0; i < x.size(); ++i) {
1020 result.set(i, x[i] < y[i] || QuantLib::close_enough(x[i], y[i]));
1021 }
1022 stopCalcStats(x.size());
1023 return result;
1024}

◆ operator>

Filter operator> ( const RandomVariable x,
const RandomVariable y 
)
friend

Definition at line 1026 of file randomvariable.cpp.

1026 {
1027 if (!x.initialised() || !y.initialised())
1028 return Filter();
1029 QL_REQUIRE(x.size() == y.size(),
1030 "RandomVariable: x > y: x size (" << x.size() << ") must be equal to y size (" << y.size() << ")");
1032 if (x.deterministic_ && y.deterministic_) {
1033 return Filter(x.size(),
1034 x.constantData_ > y.constantData_ && !QuantLib::close_enough(x.constantData_, y.constantData_));
1035 }
1036 Filter result(x.size(), false);
1037 for (Size i = 0; i < x.size(); ++i) {
1038 result.set(i, x[i] > y[i] && !QuantLib::close_enough(x[i], y[i]));
1039 }
1040 return result;
1041}

◆ operator>=

Filter operator>= ( const RandomVariable x,
const RandomVariable y 
)
friend

Definition at line 1043 of file randomvariable.cpp.

1043 {
1044 if (!x.initialised() || !y.initialised())
1045 return Filter();
1046 QL_REQUIRE(x.size() == y.size(),
1047 "RandomVariable: x >= y: x size (" << x.size() << ") must be equal to y size (" << y.size() << ")");
1049 if (x.deterministic_ && y.deterministic_) {
1050 return Filter(x.size(),
1051 x.constantData_ > y.constantData_ || QuantLib::close_enough(x.constantData_, y.constantData_));
1052 }
1053 resumeCalcStats();
1054 Filter result(x.size(), false);
1055 for (Size i = 0; i < x.size(); ++i) {
1056 result.set(i, x[i] > y[i] || QuantLib::close_enough(x[i], y[i]));
1057 }
1058 stopCalcStats(x.size());
1059 return result;
1060}

◆ close_enough_all

bool close_enough_all ( const RandomVariable ,
const RandomVariable  
)
friend

Definition at line 856 of file randomvariable.cpp.

856 {
857 QL_REQUIRE(x.size() == y.size(), "RandomVariable: close_enough_all(x,y): x size ("
858 << x.size() << ") must be equal to y size (" << y.size() << ")");
860 if (x.deterministic_ && y.deterministic_) {
861 return QuantLib::close_enough(x.constantData_, y.constantData_);
862 }
863 resumeCalcStats();
864 for (Size i = 0; i < x.size(); ++i) {
865 if (!QuantLib::close_enough(x[i], y[i]))
866 return false;
867 }
868 stopCalcStats(x.size());
869 return true;
870}

◆ applyFilter

RandomVariable applyFilter ( RandomVariable  ,
const Filter  
)
friend

Definition at line 1062 of file randomvariable.cpp.

1062 {
1063 if (!x.initialised())
1064 return x;
1065 QL_REQUIRE(!f.initialised() || f.size() == x.size(), "RandomVariable: applyFitler(x,f): filter size ("
1066 << f.size() << ") must be equal to x size (" << x.size()
1067 << ")");
1068 if (!f.initialised())
1069 return x;
1070 if (f.deterministic()) {
1071 if (!f[0])
1072 return RandomVariable(x.size(), 0.0, x.time());
1073 else
1074 return x;
1075 }
1076 if (x.deterministic_ && QuantLib::close_enough(x.constantData_, 0.0))
1077 return x;
1078 resumeCalcStats();
1079 for (Size i = 0; i < x.size(); ++i) {
1080 if (!f[i])
1081 x.set(i, 0.0);
1082 }
1083 stopCalcStats(x.size());
1084 return x;
1085}

◆ applyInverseFilter

RandomVariable applyInverseFilter ( RandomVariable  ,
const Filter  
)
friend

Definition at line 1087 of file randomvariable.cpp.

1087 {
1088 if (!x.initialised())
1089 return x;
1090 QL_REQUIRE(!f.initialised() || f.size() == x.size(), "RandomVariable: applyFitler(x,f): filter size ("
1091 << f.size() << ") must be equal to x size (" << x.size()
1092 << ")");
1093 if (!f.initialised())
1094 return x;
1095 if (f.deterministic()) {
1096 if (f[0])
1097 return RandomVariable(x.size(), 0.0, x.time());
1098 else
1099 return x;
1100 }
1101 if (x.deterministic_ && QuantLib::close_enough(x.constantData_, 0.0))
1102 return x;
1103 resumeCalcStats();
1104 for (Size i = 0; i < x.size(); ++i) {
1105 if (f[i])
1106 x.set(i, 0.0);
1107 }
1108 stopCalcStats(x.size());
1109 return x;
1110}

◆ conditionalResult

RandomVariable conditionalResult ( const Filter ,
RandomVariable  ,
const RandomVariable  
)
friend

Definition at line 872 of file randomvariable.cpp.

872 {
873 if (!f.initialised() || !x.initialised() || !y.initialised())
874 return RandomVariable();
875 QL_REQUIRE(f.size() == x.size(),
876 "conditionalResult(f,x,y): f size (" << f.size() << ") must match x size (" << x.size() << ")");
877 QL_REQUIRE(f.size() == y.size(),
878 "conditionalResult(f,x,y): f size (" << f.size() << ") must match y size (" << y.size() << ")");
879 x.checkTimeConsistencyAndUpdate(y.time());
880 if (f.deterministic())
881 return f.at(0) ? x : y;
882 resumeCalcStats();
883 x.expand();
884 for (Size i = 0; i < f.size(); ++i) {
885 if (!f[i])
886 x.set(i, y[i]);
887 }
888 stopCalcStats(f.size());
889 return x;
890}

◆ indicatorEq

RandomVariable indicatorEq ( RandomVariable  ,
const RandomVariable ,
const Real  trueVal,
const Real  falseVal 
)
friend

Definition at line 892 of file randomvariable.cpp.

892 {
893 if (!x.initialised() || !y.initialised())
894 return RandomVariable();
895 QL_REQUIRE(x.size() == y.size(), "RandomVariable: indicatorEq(x,y): x size ("
896 << x.size() << ") must be equal to y size (" << y.size() << ")");
897 x.checkTimeConsistencyAndUpdate(y.time());
898 if (!y.deterministic_)
899 x.expand();
900 if (x.deterministic()) {
901 x.constantData_ = QuantLib::close_enough(x.constantData_, y.constantData_) ? trueVal : falseVal;
902 } else {
903 resumeCalcStats();
904 for (Size i = 0; i < x.n_; ++i) {
905 x.data_[i] = QuantLib::close_enough(x.data_[i], y[i]) ? trueVal : falseVal;
906 }
907 stopCalcStats(x.n_);
908 }
909 return x;
910}

◆ indicatorGt

RandomVariable indicatorGt ( RandomVariable  ,
const RandomVariable ,
const Real  trueVal,
const Real  falseVal,
const Real  eps 
)
friend

Definition at line 912 of file randomvariable.cpp.

913 {
914 if (!x.initialised() || !y.initialised())
915 return RandomVariable();
916 QL_REQUIRE(x.size() == y.size(), "RandomVariable: indicatorEq(x,y): x size ("
917 << x.size() << ") must be equal to y size (" << y.size() << ")");
918 x.checkTimeConsistencyAndUpdate(y.time());
919 if (!y.deterministic_)
920 x.expand();
921 if (eps != 0.0) {
922 Real delta = getDelta(x - y, eps);
923 if (!QuantLib::close_enough(delta, 0.0)) {
924 // logistic function
925 x.expand();
926 Real delta = getDelta(x - y, eps);
927 resumeCalcStats();
928 for (Size i = 0; i < x.n_; ++i) {
929 x.data_[i] = falseVal + (trueVal - falseVal) * 1.0 / (1.0 + std::exp(-x.data_[i] / delta));
930 }
931 stopCalcStats(x.n_);
932 return x;
933 }
934 }
935 // eps == 0.0 or delta == 0.0
936 if (x.deterministic()) {
937 x.constantData_ =
938 (x.constantData_ > y.constantData_ && !QuantLib::close_enough(x.constantData_, y.constantData_)) ? trueVal
939 : falseVal;
940 } else {
941 resumeCalcStats();
942 for (Size i = 0; i < x.n_; ++i) {
943 x.data_[i] = (x.data_[i] > y[i] && !QuantLib::close_enough(x.data_[i], y[i])) ? trueVal : falseVal;
944 }
945 stopCalcStats(x.n_);
946 }
947 return x;
948}

◆ indicatorGeq

RandomVariable indicatorGeq ( RandomVariable  ,
const RandomVariable ,
const Real  trueVal,
const Real  falseVal,
const Real  eps 
)
friend

Definition at line 950 of file randomvariable.cpp.

951 {
952 if (!x.initialised() || !y.initialised())
953 return RandomVariable();
954 QL_REQUIRE(x.size() == y.size(), "RandomVariable: indicatorEq(x,y): x size ("
955 << x.size() << ") must be equal to y size (" << y.size() << ")");
956 x.checkTimeConsistencyAndUpdate(y.time());
957 if (!y.deterministic_)
958 x.expand();
959 if (eps != 0.0) {
960 Real delta = getDelta(x - y, eps);
961 if (!QuantLib::close_enough(delta, 0.0)) {
962 // logistic function
963 x.expand();
964 Real delta = getDelta(x - y, eps);
965 resumeCalcStats();
966 for (Size i = 0; i < x.n_; ++i) {
967 x.data_[i] = falseVal + (trueVal - falseVal) * 1.0 / (1.0 + std::exp(-x.data_[i] / delta));
968 }
969 stopCalcStats(x.n_);
970 return x;
971 }
972 }
973 // eps == 0.0 or delta == 0.0
974 if (x.deterministic()) {
975 x.constantData_ =
976 (x.constantData_ > y.constantData_ || QuantLib::close_enough(x.constantData_, y.constantData_)) ? trueVal
977 : falseVal;
978 } else {
979 resumeCalcStats();
980 for (Size i = 0; i < x.n_; ++i) {
981 x.data_[i] = (x.data_[i] > y[i] || QuantLib::close_enough(x.data_[i], y[i])) ? trueVal : falseVal;
982 }
983 stopCalcStats(x.n_);
984 }
985 return x;
986}

Member Data Documentation

◆ deleter

std::function< void(RandomVariable &)> deleter
static
Initial value:
=
std::function<void(RandomVariable&)>([](RandomVariable& x) { x.clear(); })

Definition at line 227 of file randomvariable.hpp.

◆ n_

Size n_
private

Definition at line 246 of file randomvariable.hpp.

◆ constantData_

double constantData_
private

Definition at line 247 of file randomvariable.hpp.

◆ data_

double* data_
private

Definition at line 248 of file randomvariable.hpp.

◆ deterministic_

bool deterministic_
private

Definition at line 249 of file randomvariable.hpp.

◆ time_

Real time_
private

Definition at line 250 of file randomvariable.hpp.