{ "cells": [ { "cell_type": "markdown", "id": "4de7a818-629f-44fa-863a-55531fa9e626", "metadata": {}, "source": [ "# Using RandomParameters" ] }, { "cell_type": "markdown", "id": "a0c2463e-8671-4991-822d-c31627df488d", "metadata": {}, "source": [ "To generate random samples of parameters, one can use the `RandomParameters` class as demonstrated in this example." ] }, { "cell_type": "code", "execution_count": 1, "id": "d21fc3f9-a94f-4e15-bc54-a775d04f8228", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------\n", "Keys Value Fixed? Lower bound Upper bound \n", "------------------------------------------------------------------\n", "L0 1.00 No 1.00e-04 1.00e+03 \n", "A0 1.00 No 1.00e-04 1.00e+03 \n", "mu_0 1.00 No 1.00e-04 1.00e+02 \n", "------------------------------------------------------------------\n", "\n" ] } ], "source": [ "import pymecht as pmt\n", "from matplotlib import pyplot as plt\n", "import numpy as np\n", "\n", "mat = pmt.MatModel('nh')\n", "sample = pmt.UniaxialExtension(mat)\n", "sample_params = sample.parameters\n", "print(sample_params)" ] }, { "cell_type": "markdown", "id": "b0bbc315-2319-483d-a4a1-65444f373270", "metadata": {}, "source": [ "Once we have the parameters, we can use it to create a `RandomParameter` instance as follows" ] }, { "cell_type": "code", "execution_count": 2, "id": "099a4556-a0cd-47ee-872c-8300c165a313", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Keys Type Lower/mean Upper/std \n", "------------------------------------------------------------------------\n", "L0 uniform 1.00e-04 1.00e+03 \n", "A0 uniform 1.00e-04 1.00e+03 \n", "mu_0 uniform 1.00e-04 1.00e+02 \n", "------------------------------------------------------------------------\n", "\n" ] } ], "source": [ "Theta = pmt.RandomParameters(sample_params)\n", "print(Theta)" ] }, { "cell_type": "markdown", "id": "206e47c6-006c-4fc3-92f9-325ade6104c1", "metadata": {}, "source": [ "By default, each parameter will have a uniform probability density function (PDF) between the lower and upper bounds. From the random parameter `Theta`, we can create a required number of samples." ] }, { "cell_type": "code", "execution_count": 3, "id": "ffbd7159-e37d-4deb-a828-0bdbf541accd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m \u001b[0mTheta\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msample\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mN\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msample_type\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Returns a list of N samples of the parameters\n", "\n", "Parameters\n", "----------\n", "N: int\n", " Number of samples to be generated\n", "\n", "sample_type: str\n", " Type of sampling to be performed. \n", " Options are \n", "\n", " * None (default)\n", " * 'lhcube' for Latin-Hypercube sampling\n", " * 'sobol' for Sobol sequence sampling\n", "\n", " If None, random sampling is performed.\n", "\n", "Returns\n", "-------\n", "list\n", " A list of N dictionaries with random values of the parameters\n", "\u001b[0;31mFile:\u001b[0m /usr/local/lib/python3.9/site-packages/pymecht/RandomParameters.py\n", "\u001b[0;31mType:\u001b[0m method\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "Theta.sample?" ] }, { "cell_type": "markdown", "id": "393b543b-a5a3-44eb-876d-59ac2eb32956", "metadata": {}, "source": [ "There are a few options available for sampling the random parameters: default, Latin hypercube, and Sobol. The latter two only work for uniformly distributed parameters. The `sample` method returns a list of (normal) dictionaries, that we can use to run simulations." ] }, { "cell_type": "code", "execution_count": 4, "id": "2a69f5da-d177-4ee5-ab23-1146d25640d5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "[{'L0': 891.507336479485, 'A0': 335.299648762739, 'mu_0': 95.4678573610962}, {'L0': 341.0010091911493, 'A0': 439.0918199716474, 'mu_0': 79.67639341557204}, {'L0': 137.405730071511, 'A0': 83.37527510409612, 'mu_0': 60.82007599086426}, {'L0': 929.2802499555028, 'A0': 238.28812347567, 'mu_0': 7.499850667149027}, {'L0': 718.0661804372189, 'A0': 608.0690988826448, 'mu_0': 20.728033597900517}, {'L0': 72.9417514464427, 'A0': 573.7396959544901, 'mu_0': 19.835028404995008}, {'L0': 568.2921881249891, 'A0': 167.03698843301927, 'mu_0': 31.550781679043173}, {'L0': 405.91357475801743, 'A0': 735.1233178703956, 'mu_0': 42.07752038879198}, {'L0': 243.7715469093828, 'A0': 815.2039715505399, 'mu_0': 53.67664849319548}, {'L0': 699.8607748041638, 'A0': 921.7720680553437, 'mu_0': 85.55720663091368}]\n" ] } ], "source": [ "theta_samples1 = Theta.sample(10)\n", "theta_samples2 = Theta.sample(10,'lhcube')\n", "theta_samples3 = Theta.sample(10,'sobol')\n", "print(type(theta_samples1))\n", "print(theta_samples2)" ] }, { "cell_type": "code", "execution_count": 5, "id": "715f6114-09e0-48ce-b69d-9de987bc5a5d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'L0': 337.6500076794028, 'A0': 348.5217149853289, 'mu_0': 47.428845286279916} [4521.838187728782]\n", "{'L0': 879.7981263199628, 'A0': 879.9794436312377, 'mu_0': 91.39257934145331} [22000.17244712658]\n", "{'L0': 632.7859530498386, 'A0': 168.79554954348802, 'mu_0': 19.473174274998904} [899.167012956701]\n", "{'L0': 88.68471705517099, 'A0': 575.3297038363576, 'mu_0': 62.99445157353282} [9914.292317621073]\n", "{'L0': 200.1005018954754, 'A0': 112.81597545432747, 'mu_0': 85.67158820576071} [2643.930558019085]\n", "{'L0': 517.3883324675619, 'A0': 643.0570840150655, 'mu_0': 29.608790932279824} [5208.501861932982]\n", "{'L0': 768.4285872287988, 'A0': 433.3359569731116, 'mu_0': 57.76784470908046} [6847.838589365981]\n", "{'L0': 453.0938814613998, 'A0': 838.1649417587638, 'mu_0': 1.3662562773393467} [313.25952516027945]\n", "{'L0': 398.56916719875335, 'A0': 202.77536437041314, 'mu_0': 51.08966354013086} [2833.9413399263003]\n", "{'L0': 817.4261633341372, 'A0': 545.287594966942, 'mu_0': 7.165533751574158} [1068.8500631922789]\n" ] } ], "source": [ "for theta_i in theta_samples3:\n", " result = sample.disp_controlled([1.1],theta_i)\n", " print(theta_i, result)" ] }, { "cell_type": "markdown", "id": "4f004f6a-95d5-4b29-a1c5-33cd898e0aa5", "metadata": {}, "source": [ "## Normal and log-normal distributed parameters" ] }, { "cell_type": "markdown", "id": "55cb5ded-ae63-4378-be16-1cc8c346c022", "metadata": {}, "source": [ "The PDF of individual parameters can be changed to normal or log-normal, as follows:" ] }, { "cell_type": "code", "execution_count": 6, "id": "b1327a2c-df56-437a-b18f-7772d375cb79", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Keys Type Lower/mean Upper/std \n", "------------------------------------------------------------------------\n", "L0 normal 5.00e+02 2.50e+02 \n", "A0 uniform 1.00e-04 1.00e+03 \n", "mu_0 uniform 1.00e-04 1.00e+02 \n", "------------------------------------------------------------------------\n", "\n" ] } ], "source": [ "Theta.make_normal('L0')\n", "print(Theta)" ] }, { "cell_type": "code", "execution_count": 7, "id": "a432f31c-b6c2-490c-9e92-ecc0a6ba0860", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Keys Type Lower/mean Upper/std \n", "------------------------------------------------------------------------\n", "L0 normal 5.00e+02 2.50e+02 \n", "A0 lognormal 0.32 56.23 \n", "mu_0 uniform 1.00e-04 1.00e+02 \n", "------------------------------------------------------------------------\n", "\n" ] } ], "source": [ "Theta.make_lognormal('A0')\n", "print(Theta)" ] }, { "cell_type": "markdown", "id": "754e4b51-56fa-48bb-b60a-d5436a04a234", "metadata": {}, "source": [ "Note that when making a variable normal, by default, its mean is choosen to be as the mean of lower/upper bounds, and the standard deviation to be the 1/4th of the range (range being the upper-lower bound). Similarly, for log-normal, the same is done on the logarithm of the bounds. " ] }, { "cell_type": "markdown", "id": "e8a632cd-77f1-49b5-8bdf-8b2c45bfc91b", "metadata": {}, "source": [ "Note that when the `RandomParameter` instance contains any normal or log-normal variable, only the default sampling is available." ] }, { "cell_type": "code", "execution_count": 8, "id": "a38abc1b-5a4f-4e61-ba0d-2e1728bcc4cc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Error raised For Latin Hypercube sampling, only uniform distributions are currently implemented\n", "Error raised For Sobol sequence sampling, only uniform distributions are currently implemented\n" ] } ], "source": [ "theta_samples1 = Theta.sample(10)\n", "try:\n", " theta_samples2 = Theta.sample(10,'lhcube')\n", "except ValueError as e:\n", " print(\"Error raised\", e)\n", "\n", "try:\n", " theta_samples3 = Theta.sample(10,'sobol')\n", "except ValueError as e:\n", " print(\"Error raised\", e)" ] }, { "cell_type": "markdown", "id": "9bd2e336-42b2-451c-922a-e10b59ae2908", "metadata": {}, "source": [ "## Fixing parameters" ] }, { "cell_type": "markdown", "id": "2c567c0b-611a-4692-a00f-36e3eb35f41e", "metadata": {}, "source": [ "We can also fix certain parameters either before or after creating the `RandomParameter` instance." ] }, { "cell_type": "code", "execution_count": 9, "id": "e68c628f-1866-4b67-80ff-eb6e00b058f8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Keys Type Lower/mean Upper/std \n", "------------------------------------------------------------------------\n", "L0 uniform 1.00e-04 1.00e+03 \n", "A0 fixed 1.00 1.00 \n", "mu_0 uniform 1.00e-04 1.00e+02 \n", "------------------------------------------------------------------------\n", "\n", "Keys Type Lower/mean Upper/std \n", "------------------------------------------------------------------------\n", "L0 normal 5.00e+02 2.50e+02 \n", "A0 fixed 1.00 1.00 \n", "mu_0 uniform 1.00e-04 1.00e+02 \n", "------------------------------------------------------------------------\n", "\n" ] } ], "source": [ "#fixing before\n", "sample_params.fix('A0')\n", "print(pmt.RandomParameters(sample_params))\n", "\n", "#fixing after\n", "Theta.fix('A0')\n", "print(Theta)" ] }, { "cell_type": "markdown", "id": "4159a682-643b-4f8a-9db5-9897c205f550", "metadata": {}, "source": [ "## Evaluating PDF" ] }, { "cell_type": "markdown", "id": "1c9f8132-988c-4cd5-9cda-c5e8189e6caf", "metadata": {}, "source": [ "Given a sample, one can also evaulate the PDF at that." ] }, { "cell_type": "code", "execution_count": 10, "id": "121fedd0-6acc-433c-a006-b4bd9b524492", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.158572574771021e-06" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "theta_samples = Theta.sample()\n", "Theta.prob(theta_samples[0])" ] }, { "cell_type": "markdown", "id": "10a2af44-d768-4601-8a93-9a984962d133", "metadata": {}, "source": [ "## Add parameter" ] }, { "cell_type": "markdown", "id": "570af1a6-f8a7-4d44-8ab4-491cc00f6df6", "metadata": {}, "source": [ "If we want to add another parameter, we can do it either before or after creating the `RandomParameter` instance. " ] }, { "cell_type": "code", "execution_count": 11, "id": "d71508c1-bb91-417a-a387-b2fbf4f8bae3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Keys Type Lower/mean Upper/std \n", "------------------------------------------------------------------------\n", "L0 uniform 1.00e-04 1.00e+03 \n", "A0 fixed 1.00 1.00 \n", "mu_0 uniform 1.00e-04 1.00e+02 \n", "phi uniform 0.00 90.00 \n", "------------------------------------------------------------------------\n", "\n", "Before adding:\n", " Keys Type Lower/mean Upper/std \n", "------------------------------------------------------------------------\n", "L0 uniform 1.00e-04 1.00e+03 \n", "A0 fixed 1.00 1.00 \n", "mu_0 uniform 1.00e-04 1.00e+02 \n", "------------------------------------------------------------------------\n", "\n", "After adding:\n", " Keys Type Lower/mean Upper/std \n", "------------------------------------------------------------------------\n", "L0 uniform 1.00e-04 1.00e+03 \n", "A0 fixed 1.00 1.00 \n", "mu_0 uniform 1.00e-04 1.00e+02 \n", "phi uniform 0.00 90.00 \n", "------------------------------------------------------------------------\n", "\n" ] } ], "source": [ "#Adding before creating the RandomParameter instance\n", "sample_params['phi'] = pmt.Param(10,0,90)\n", "print(pmt.RandomParameters(sample_params))\n", "\n", "#First create a RandomParameter instance\n", "Theta = pmt.RandomParameters(sample.parameters)\n", "print('Before adding:\\n', Theta)\n", "#then add a variable to it, with a current value of 10, lower limit of 0, upper limit of 90, and a uniform distribution\n", "Theta.add('phi',10,0,90,'uniform')\n", "print('After adding:\\n', Theta)" ] }, { "cell_type": "markdown", "id": "ef35727e-8bed-44c5-84e6-1047108f8927", "metadata": {}, "source": [ "Note that the current capability is limited to non-correlated parameters only. " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }