|
| 1 | + |
| 2 | +package org.drip.measure.crng; |
| 3 | + |
| 4 | +import org.drip.numerical.linearalgebra.Matrix; |
| 5 | + |
| 6 | +/* |
| 7 | + * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 8 | + */ |
| 9 | + |
| 10 | +/*! |
| 11 | + * Copyright (C) 2022 Lakshmi Krishnamurthy |
| 12 | + * Copyright (C) 2021 Lakshmi Krishnamurthy |
| 13 | + * Copyright (C) 2020 Lakshmi Krishnamurthy |
| 14 | + * Copyright (C) 2019 Lakshmi Krishnamurthy |
| 15 | + * Copyright (C) 2018 Lakshmi Krishnamurthy |
| 16 | + * Copyright (C) 2017 Lakshmi Krishnamurthy |
| 17 | + * |
| 18 | + * This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics, |
| 19 | + * asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment |
| 20 | + * analytics, and portfolio construction analytics within and across fixed income, credit, commodity, |
| 21 | + * equity, FX, and structured products. It also includes auxiliary libraries for algorithm support, |
| 22 | + * numerical analysis, numerical optimization, spline builder, model validation, statistical learning, |
| 23 | + * graph builder/navigator, and computational support. |
| 24 | + * |
| 25 | + * https://lakshmidrip.github.io/DROP/ |
| 26 | + * |
| 27 | + * DROP is composed of three modules: |
| 28 | + * |
| 29 | + * - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/ |
| 30 | + * - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/ |
| 31 | + * - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/ |
| 32 | + * |
| 33 | + * DROP Product Core implements libraries for the following: |
| 34 | + * - Fixed Income Analytics |
| 35 | + * - Loan Analytics |
| 36 | + * - Transaction Cost Analytics |
| 37 | + * |
| 38 | + * DROP Portfolio Core implements libraries for the following: |
| 39 | + * - Asset Allocation Analytics |
| 40 | + * - Asset Liability Management Analytics |
| 41 | + * - Capital Estimation Analytics |
| 42 | + * - Exposure Analytics |
| 43 | + * - Margin Analytics |
| 44 | + * - XVA Analytics |
| 45 | + * |
| 46 | + * DROP Computational Core implements libraries for the following: |
| 47 | + * - Algorithm Support |
| 48 | + * - Computation Support |
| 49 | + * - Function Analysis |
| 50 | + * - Graph Algorithm |
| 51 | + * - Model Validation |
| 52 | + * - Numerical Analysis |
| 53 | + * - Numerical Optimizer |
| 54 | + * - Spline Builder |
| 55 | + * - Statistical Learning |
| 56 | + * |
| 57 | + * Documentation for DROP is Spread Over: |
| 58 | + * |
| 59 | + * - Main => https://lakshmidrip.github.io/DROP/ |
| 60 | + * - Wiki => https://github.com/lakshmiDRIP/DROP/wiki |
| 61 | + * - GitHub => https://github.com/lakshmiDRIP/DROP |
| 62 | + * - Repo Layout Taxonomy => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md |
| 63 | + * - Javadoc => https://lakshmidrip.github.io/DROP/Javadoc/index.html |
| 64 | + * - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal |
| 65 | + * - Release Versions => https://lakshmidrip.github.io/DROP/version.html |
| 66 | + * - Community Credits => https://lakshmidrip.github.io/DROP/credits.html |
| 67 | + * - Issues Catalog => https://github.com/lakshmiDRIP/DROP/issues |
| 68 | + * |
| 69 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 70 | + * you may not use this file except in compliance with the License. |
| 71 | + * |
| 72 | + * You may obtain a copy of the License at |
| 73 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 74 | + * |
| 75 | + * Unless required by applicable law or agreed to in writing, software |
| 76 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 77 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 78 | + * |
| 79 | + * See the License for the specific language governing permissions and |
| 80 | + * limitations under the License. |
| 81 | + */ |
| 82 | + |
| 83 | +/** |
| 84 | + * <i>RdRandomSequence</i> generates 1D and 2D random arrays. |
| 85 | + * |
| 86 | + * <br><br> |
| 87 | + * <ul> |
| 88 | + * <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li> |
| 89 | + * <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/NumericalAnalysisLibrary.md">Numerical Analysis Library</a></li> |
| 90 | + * <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> |
| 91 | + * <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> |
| 92 | + * </ul> |
| 93 | + * |
| 94 | + * @author Lakshmi Krishnamurthy |
| 95 | + */ |
| 96 | + |
| 97 | +public class RdRandomSequence |
| 98 | +{ |
| 99 | + |
| 100 | + /** |
| 101 | + * Construct a 1D Array of Random Elements up to the Maximum Value |
| 102 | + * |
| 103 | + * @param elementCount Number of Elements in the Array |
| 104 | + * @param maximumElement Maximum Element |
| 105 | + * @param isEntryInteger TRUE - Entry is an Integer |
| 106 | + * |
| 107 | + * @return 1D Array of Random Elements up to the Maximum Value |
| 108 | + */ |
| 109 | + |
| 110 | + public static final double[] OneD ( |
| 111 | + final int elementCount, |
| 112 | + final double maximumElement, |
| 113 | + final boolean isEntryInteger) |
| 114 | + { |
| 115 | + if (0 >= elementCount || 0 >= maximumElement) { |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + double[] oneDArray = new double[elementCount]; |
| 120 | + |
| 121 | + for (int i = 0; i < elementCount; ++i) { |
| 122 | + oneDArray[i] = Math.random() * maximumElement; |
| 123 | + |
| 124 | + if (isEntryInteger) { |
| 125 | + oneDArray[i] = (int) (oneDArray[i] + 0.5); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return oneDArray; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Construct a 2D Matrix of Random Elements up to the Maximum Value |
| 134 | + * |
| 135 | + * @param elementCount Number of Elements in the Array |
| 136 | + * @param maximumElement Maximum Element |
| 137 | + * @param isEntryInteger TRUE - Entry is an Integer |
| 138 | + * |
| 139 | + * @return 2D Matrix of Random Elements up to the Maximum Value |
| 140 | + */ |
| 141 | + |
| 142 | + public static final double[][] TwoD ( |
| 143 | + final int elementCount, |
| 144 | + final double maximumElement, |
| 145 | + final boolean isEntryInteger) |
| 146 | + { |
| 147 | + double[][] twoDArray = new double[elementCount][elementCount]; |
| 148 | + |
| 149 | + for (int i = 0; i < elementCount; ++i) { |
| 150 | + if (null == (twoDArray[i] = OneD (elementCount, maximumElement, isEntryInteger))) { |
| 151 | + return null; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return twoDArray; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Construct a Tridiagonal Matrix of Random Elements up to the Maximum Value |
| 160 | + * |
| 161 | + * @param elementCount Number of Elements in the Array |
| 162 | + * @param maximumElement Maximum Element |
| 163 | + * @param isEntryInteger TRUE - Entry is an Integer |
| 164 | + * |
| 165 | + * @return Tridiagonal Matrix of Random Elements up to the Maximum Value |
| 166 | + */ |
| 167 | + |
| 168 | + public static final double[][] Tridiagonal ( |
| 169 | + final int elementCount, |
| 170 | + final double maximumElement, |
| 171 | + final boolean isEntryInteger) |
| 172 | + { |
| 173 | + double[][] tridiagonalMatrix = TwoD (elementCount, maximumElement, isEntryInteger); |
| 174 | + |
| 175 | + if (null == tridiagonalMatrix) { |
| 176 | + return null; |
| 177 | + } |
| 178 | + |
| 179 | + for (int i = 0; i < tridiagonalMatrix.length; ++i) { |
| 180 | + for (int j = 0; j < tridiagonalMatrix.length; ++j) { |
| 181 | + if (j <= i - 2 || j >= i + 2) { |
| 182 | + tridiagonalMatrix[i][j] = 0.; |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return tridiagonalMatrix; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Construct a Periodic Tridiagonal Matrix of Random Elements up to the Maximum Value |
| 192 | + * |
| 193 | + * @param elementCount Number of Elements in the Array |
| 194 | + * @param maximumElement Maximum Element |
| 195 | + * @param isEntryInteger TRUE - Entry is an Integer |
| 196 | + * |
| 197 | + * @return Periodic Tridiagonal Matrix of Random Elements up to the Maximum Value |
| 198 | + */ |
| 199 | + |
| 200 | + public static final double[][] PeriodicTridiagonal ( |
| 201 | + final int elementCount, |
| 202 | + final double maximumElement, |
| 203 | + final boolean isEntryInteger) |
| 204 | + { |
| 205 | + double[][] tridiagonalMatrix = TwoD (elementCount, maximumElement, isEntryInteger); |
| 206 | + |
| 207 | + if (null == tridiagonalMatrix) { |
| 208 | + return null; |
| 209 | + } |
| 210 | + |
| 211 | + for (int i = 0; i < tridiagonalMatrix.length; ++i) { |
| 212 | + for (int j = 0; j < tridiagonalMatrix.length; ++j) { |
| 213 | + if (j <= i - 2 || j >= i + 2) { |
| 214 | + if (!Matrix.TopRight (i, j, elementCount) && !Matrix.BottomLeft (i, j, elementCount)) { |
| 215 | + tridiagonalMatrix[i][j] = 0.; |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + return tridiagonalMatrix; |
| 222 | + } |
| 223 | +} |
0 commit comments