LinearCongruentialGenerator.java

  1. package org.drip.measure.crng;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  *
  11.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  12.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  13.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  14.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  15.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  16.  *      and computational support.
  17.  *  
  18.  *      https://lakshmidrip.github.io/DROP/
  19.  *  
  20.  *  DROP is composed of three modules:
  21.  *  
  22.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  23.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  24.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  25.  *
  26.  *  DROP Product Core implements libraries for the following:
  27.  *  - Fixed Income Analytics
  28.  *  - Loan Analytics
  29.  *  - Transaction Cost Analytics
  30.  *
  31.  *  DROP Portfolio Core implements libraries for the following:
  32.  *  - Asset Allocation Analytics
  33.  *  - Asset Liability Management Analytics
  34.  *  - Capital Estimation Analytics
  35.  *  - Exposure Analytics
  36.  *  - Margin Analytics
  37.  *  - XVA Analytics
  38.  *
  39.  *  DROP Computational Core implements libraries for the following:
  40.  *  - Algorithm Support
  41.  *  - Computation Support
  42.  *  - Function Analysis
  43.  *  - Model Validation
  44.  *  - Numerical Analysis
  45.  *  - Numerical Optimizer
  46.  *  - Spline Builder
  47.  *  - Statistical Learning
  48.  *
  49.  *  Documentation for DROP is Spread Over:
  50.  *
  51.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  52.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  53.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  54.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  55.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  56.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  57.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  58.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  59.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  60.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  61.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  62.  *
  63.  *  Licensed under the Apache License, Version 2.0 (the "License");
  64.  *      you may not use this file except in compliance with the License.
  65.  *  
  66.  *  You may obtain a copy of the License at
  67.  *      http://www.apache.org/licenses/LICENSE-2.0
  68.  *  
  69.  *  Unless required by applicable law or agreed to in writing, software
  70.  *      distributed under the License is distributed on an "AS IS" BASIS,
  71.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  72.  *  
  73.  *  See the License for the specific language governing permissions and
  74.  *      limitations under the License.
  75.  */

  76. /**
  77.  * <i>LinearCongruentialGenerator</i> implements a RNG based on Recurrence Based on Modular Integer
  78.  * Arithmetic.
  79.  *
  80.  *  <br><br>
  81.  *  <ul>
  82.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  83.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li>
  84.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/README.md">R<sup>d</sup> Continuous/Discrete Probability Measures</a></li>
  85.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/measure/crng/README.md">Continuous Random Number Stream Generator</a></li>
  86.  *  </ul>
  87.  *
  88.  * @author Lakshmi Krishnamurthy
  89.  */

  90. public class LinearCongruentialGenerator extends org.drip.measure.crng.RandomNumberGenerator {
  91.     private long _lA = java.lang.Long.MIN_VALUE;
  92.     private long _lB = java.lang.Long.MIN_VALUE;
  93.     private long _lM = java.lang.Long.MIN_VALUE;
  94.     private org.drip.measure.crng.RecursiveGenerator _rg = null;

  95.     /**
  96.      * Construct an Instance of LinearCongruentialGenerator with the MRG of Type MRG32k3a
  97.      *
  98.      * @param lA A
  99.      * @param lB B
  100.      * @param lM M
  101.      *
  102.      * @return Instance of LinearCongruentialGenerator with the MRG of Type MRG32k3a
  103.      */

  104.     public static final LinearCongruentialGenerator MRG32k3a (
  105.         final long lA,
  106.         final long lB,
  107.         final long lM)
  108.     {
  109.         try {
  110.             return new LinearCongruentialGenerator (lA, lB, lM,
  111.                 org.drip.measure.crng.MultipleRecursiveGeneratorLEcuyer.MRG32k3a());
  112.         } catch (java.lang.Exception e) {
  113.             e.printStackTrace();
  114.         }

  115.         return null;
  116.     }

  117.     /**
  118.      * Construct a NumericalRecipes Version of LinearCongruentialGenerator
  119.      *
  120.      * @param rg The Recursive Generator Instance
  121.      *
  122.      * @return NumericalRecipes Version of LinearCongruentialGenerator
  123.      */

  124.     public static final LinearCongruentialGenerator NumericalRecipes (
  125.         final org.drip.measure.crng.RecursiveGenerator rg)
  126.     {
  127.         long l2Power32 = 1;

  128.         for (int i = 0; i < 32; ++i)
  129.             l2Power32 *= 2;

  130.         try {
  131.             return new LinearCongruentialGenerator (1664525L, 1013904223L, l2Power32, rg);
  132.         } catch (java.lang.Exception e) {
  133.             e.printStackTrace();
  134.         }

  135.         return null;
  136.     }

  137.     /**
  138.      * LinearCongruentialGenerator Contructor
  139.      *
  140.      * @param lA A
  141.      * @param lB B
  142.      * @param lM M
  143.      * @param rg The MultipleRecursiveGenerator Instance
  144.      *
  145.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  146.      */

  147.     public LinearCongruentialGenerator (
  148.         final long lA,
  149.         final long lB,
  150.         final long lM,
  151.         final org.drip.measure.crng.RecursiveGenerator rg)
  152.         throws java.lang.Exception
  153.     {
  154.         if (!org.drip.numerical.common.NumberUtil.IsValid (_lA = lA) || !org.drip.numerical.common.NumberUtil.IsValid
  155.             (_lB = lB) || !org.drip.numerical.common.NumberUtil.IsValid (_lM = lM) || null == (_rg = rg))
  156.             throw new java.lang.Exception ("LinearCongruentialGenerator Constructor => Invalid Inputs");
  157.     }

  158.     /**
  159.      * Retrieve A
  160.      *
  161.      * @return A
  162.      */

  163.     public long a()
  164.     {
  165.         return _lA;
  166.     }

  167.     /**
  168.      * Retrieve B
  169.      *
  170.      * @return B
  171.      */

  172.     public long b()
  173.     {
  174.         return _lB;
  175.     }

  176.     /**
  177.      * Retrieve M
  178.      *
  179.      * @return M
  180.      */

  181.     public long m()
  182.     {
  183.         return _lM;
  184.     }

  185.     /**
  186.      * Retrieve the Recursive Generator Instance
  187.      *
  188.      * @return The Recursive Generator Instance
  189.      */

  190.     public org.drip.measure.crng.RecursiveGenerator recursiveGenerator()
  191.     {
  192.         return _rg;
  193.     }

  194.     /**
  195.      * Retrieve the Next Pseudo-random Long
  196.      *
  197.      * @return The Next Pseudo-random Long
  198.      */

  199.     public long nextLong()
  200.     {
  201.         return (_lA * _rg.next() + _lB) % _lM;
  202.     }

  203.     /**
  204.      * Retrieve a Random Number between -1 and 1
  205.      *
  206.      * @return Random Number between -1 and 1
  207.      */

  208.     public double nextDouble()
  209.     {
  210.         return ((double) nextLong()) / ((double) _lM);
  211.     }

  212.     @Override public double nextDouble01()
  213.     {
  214.         return 0.5 * (1. + nextDouble());
  215.     }
  216. }