{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import openturns as ot\n", "import otmorris\n", "import time\n", "import numpy as np\n", "\n", "ot.RandomGenerator.SetSeed(1)\n", "b0 = ot.DistFunc.rNormal()\n", "alpha = ot.DistFunc.rNormal(10)\n", "beta = ot.DistFunc.rNormal(6*14)\n", "gamma = ot.DistFunc.rNormal(20*14)\n", "g = ot.Function(otmorris.MorrisFunction(alpha, beta, gamma, b0))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.05263158, 0.10526316, 0.15789474, 0.21052632,\n", " 0.26315789, 0.31578947, 0.36842105, 0.42105263, 0.47368421,\n", " 0.52631579, 0.57894737, 0.63157895, 0.68421053, 0.73684211,\n", " 0.78947368, 0.84210526, 0.89473684, 0.94736842, 1. ])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.linspace(0.0, 1.0, 20)\n", "x" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "

[-47.1449]

" ], "text/plain": [ "class=Point name=Unnamed dimension=1 values=[-47.1449]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g(x)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "distributionList = [ot.Uniform(0,1)]*20\n", "myDistribution = ot.ComposedDistribution(distributionList)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "n_train = 1000" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Elapsed= 8.0120210647583\n" ] } ], "source": [ "start = time.time()\n", "\n", "inputTrain = myDistribution.getSample(n_train)\n", "outputTrain = g(inputTrain)\n", "\n", "elapsed = (time.time() - start)\n", "print(\"Elapsed=\", elapsed)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Version rapide en Numpy" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def w(x):\n", " x=x.reshape(-1,1)\n", " y= (2*x-1)*np.ones((20,1))\n", " y[[2,4,6]]=2*(1.1*x[[2,4,6]]/(x[[2,4,6]]+0.1)-1/float(2))\n", " return y" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def Morris_function(x, b0, alpha, beta):\n", " delta=np.zeros((20,20,20))\n", " delta[:5,:5,:5].fill(-10)\n", " gamma=np.zeros((20,20,20,20))\n", " gamma[:4,:4,:4,:4].fill(5)\n", " W=np.sum(alpha.reshape(-1,1)*w(x))\n", " W1=np.sum(beta*(w(x).reshape(1,-1)*w(x))*(np.arange(20).reshape(-1,1)[-0.130249]

" ], "text/plain": [ "class=Point name=Unnamed dimension=1 values=[-0.130249]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g(x)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Elapsed= 2.741170883178711\n" ] } ], "source": [ "start = time.time()\n", "\n", "inputTrain = myDistribution.getSample(n_train)\n", "outputTrain = g(inputTrain)\n", "\n", "elapsed = (time.time() - start)\n", "print(\"Elapsed=\", elapsed)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ce n'est pas beaucoup plus rapide en fait." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }