Skip to content

Commit

Permalink
montecarlo advance
Browse files Browse the repository at this point in the history
  • Loading branch information
leliel12 committed Jan 11, 2016
1 parent 41eeeac commit 17489b0
Showing 1 changed file with 203 additions and 37 deletions.
240 changes: 203 additions & 37 deletions notebooks/montecarlo_mc.ipynb
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Análisis de artículo por MC "
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 78,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from skcriteria import topsis"
"%matplotlib inline\n",
"\n",
"from __future__ import print_function, unicode_literals\n",
"\n",
"import numpy as np\n",
"\n",
"from skcriteria import topsis\n",
"\n",
"from scipy.stats import norm, uniform, variation\n",
"\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -50,7 +67,28 @@
" [7, 5, 9, 5], #A28\n",
" [9, 7, 7, 7], #A29\n",
" [5, 9, 7, 9], #A30\n",
"]"
"]\n",
"\n",
"weights = [0.5579, 0.2633, 0.0569, 0.1219]\n",
"\n",
"criteria = [1, 1, 1, -1] # minimization is '-1'"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"expected_rank = [\n",
" 10, 30, 3, 16, 23, 2, 11, 13, 6, 26, 20, 29, 19, 22, 8, 7, 14, 21, 9, 27, 25, 12, 1, 4, 15, 28, 17, 18, 5, 24]\n",
"\n",
"expected_closeness = [\n",
" .6885, .0422, .8171, .5031, .3194, .8815, .6577, .5754, .7900, .2100, .3423, \n",
" .0812, .4113, .3210, .7552, .7625, .5540, .3388, .6900, .1843, .2448, .5899, .9578, .8068, \n",
" .5501, .1172, .4815, .4484, .7989, .3115]"
]
},
{
Expand All @@ -59,48 +97,174 @@
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"rank, closeness = topsis.topsis(mtx, criteria, weights)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"All ranks are equals: True\n",
"All closeness are equals (1e-02): True\n"
]
}
],
"source": [
"print(\"All ranks are equals:\", np.all(rank == expected_rank))\n",
"print(\"All closeness are equals (1e-02):\", np.allclose(closeness, expected_closeness, rtol=1e-02))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Simulación"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"iterations = 3\n",
"\n",
"def variate(mtx, err):\n",
" size, shape = np.size(mtx), np.shape(mtx)\n",
" rand = np.random.rand(size).reshape(shape)\n",
" var = uniform.ppf(rand, mtx, scale=err)\n",
" return var\n"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Errors Count: 50\n",
"Errors: 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3, 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4, 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5\n"
]
}
],
"source": [
"errors = np.arange(0.01, 0.51, 0.01)\n",
"print(\"Errors Count:\", len(errors))\n",
"print(\"Errors:\", \", \".join(str(e) for e in errors))"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for err in errors:\n",
" results = np.empty((iterations, np.shape(mtx)[0]), dtype=float)\n",
" for it in range(iterations):\n",
" var_mtx = variate(mtx, err)\n",
" vcloseness = topsis.topsis(var_mtx, criteria, weights)[1]\n",
" results[it] = vcloseness\n",
" avg = np.average(results, axis=0)\n",
" std = np.std(results, axis=0)\n",
" q25, median, q75 = np.percentile(results, q=(25, 50, 75), axis=0)\n",
" \n",
" \n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.0120403 , 0.26206107, 0.01478006, 0.05749514, 0.02675256,\n",
" 0.01615191, 0.00431874, 0.01265828, 0.02035407, 0.05256711,\n",
" 0.0352865 , 0.03836823, 0.0721683 , 0.02232573, 0.01159692,\n",
" 0.00688909, 0.03835897, 0.03831984, 0.0188888 , 0.09637118,\n",
" 0.03067471, 0.03699113, 0.01687682, 0.00653985, 0.03304554,\n",
" 0.03969488, 0.03378788, 0.05305784, 0.00928993, 0.03268327])"
]
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1=results[:, 0]\n",
"variation(results, axis=0)\n"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.71311171, 0.69261594, 0.70570808])"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[[9, 5, 7, 5],\n",
" [5, 5, 7, 9],\n",
" [9, 7, 7, 5],\n",
" [7, 7, 9, 7],\n",
" [5, 9, 5, 7],\n",
" [9, 9, 5, 7],\n",
" [9, 5, 5, 9],\n",
" [7, 9, 9, 7],\n",
" [9, 7, 5, 7],\n",
" [5, 7, 9, 7],\n",
" [5, 9, 9, 5],\n",
" [5, 5, 9, 9],\n",
" [7, 5, 7, 9],\n",
" [5, 9, 7, 7],\n",
" [9, 7, 5, 9],\n",
" [9, 7, 7, 9],\n",
" [7, 9, 7, 9],\n",
" [5, 9, 7, 5],\n",
" [9, 5, 9, 5],\n",
" [5, 7, 7, 9],\n",
" [5, 7, 9, 5],\n",
" [7, 9, 9, 5],\n",
" [9, 9, 7, 5],\n",
" [9, 7, 5, 5],\n",
" [7, 9, 5, 9],\n",
" [5, 5, 9, 7],\n",
" [7, 7, 7, 9],\n",
" [7, 5, 9, 5],\n",
" [9, 7, 7, 7],\n",
" [5, 9, 7, 9]]"
"0.012040300373212277"
]
},
"execution_count": 4,
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
"source": [
"np.std(a1) / np.average(a1) "
]
},
{
"cell_type": "code",
Expand All @@ -109,7 +273,9 @@
"collapsed": true
},
"outputs": [],
"source": []
"source": [
"np"
]
}
],
"metadata": {
Expand Down

0 comments on commit 17489b0

Please sign in to comment.