Skip to content

Commit

Permalink
Merge pull request #137 from ssusie/HW8_branch
Browse files Browse the repository at this point in the history
hw8
  • Loading branch information
jakevdp committed Dec 2, 2014
2 parents a5c79de + deb4457 commit e63d827
Showing 1 changed file with 164 additions and 0 deletions.
164 changes: 164 additions & 0 deletions ssusie/HW8.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
{
"metadata": {
"name": "",
"signature": "sha256:0ffec203c49688b38a40bb8159741e4c4dcf239b4375ca03de0483da0ab7155d"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"\"\"\"\n",
"A script to compare different root-finding algorithms.\n",
"\n",
"This version of the script is buggy and does not execute. It is your task\n",
"to find an fix these bugs.\n",
"\n",
"The output of the script sould look like:\n",
"\n",
" Benching 1D root-finder optimizers from scipy.optimize:\n",
" brenth: 604678 total function calls\n",
" brentq: 594454 total function calls\n",
" ridder: 778394 total function calls\n",
" bisect: 2148380 total function calls\n",
"\"\"\"\n",
"from itertools import product\n",
"\n",
"import numpy as np\n",
"from scipy import optimize\n",
"\n",
"FUNCTIONS = (np.tan, # Dilating map\n",
" np.tanh, # Contracting map\n",
" lambda x: x**3 + 1e-4*x, # Almost null gradient at the root\n",
" lambda x: x+np.sin(2*x), # Non monotonous function\n",
" lambda x: 1.1*x+np.sin(4*x), # Fonction with several local maxima\n",
" )\n",
"\n",
"OPTIMIZERS = (optimize.brenth, optimize.brentq,\n",
" optimize.ridder, optimize.bisect)\n",
"\n",
"\n",
"def apply_optimizer(optimizer, func, a, b):\n",
" \"\"\" Return the number of function calls given an root-finding optimizer, \n",
" a function and upper and lower bounds.\n",
" \"\"\"\n",
" X=optimizer(func, a, b, full_output=True)\n",
" return X[1].function_calls\n",
"\n",
"\n",
"def bench_optimizer(optimizer, param_grid):\n",
" \"\"\" Find roots for all the functions, and upper and lower bounds\n",
" given and return the total number of function calls.\n",
" \"\"\"\n",
" S=0\n",
"# print(optimizer)\n",
" for func, a, b in param_grid:\n",
" S=S+apply_optimizer(optimizer, func, a, b)\n",
"# print(param_grid)\n",
" return S\n",
"\n",
"\n",
"def compare_optimizers(optimizers):\n",
" \"\"\" Compare all the optimizers given on a grid of a few different\n",
" functions all admitting a signle root in zero and a upper and\n",
" lower bounds.\n",
" \"\"\"\n",
"# import pdb; \n",
" \n",
" random_a = -1.3 + np.random.random(size=100)\n",
" random_b = .3 + np.random.random(size=100)\n",
" \n",
" print(\"Benching 1D root-finder optimizers from scipy.optimize:\")\n",
"# pdb.set_trace()\n",
" for optimizer in OPTIMIZERS:\n",
" param_grid = product(FUNCTIONS, random_a, random_b)\n",
"# print(optimizer)\n",
" ncalls = bench_optimizer(optimizer, param_grid)\n",
" print('{name}: {ncalls} total function calls'.format(\n",
" name=optimizer.__name__, ncalls=ncalls))\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" compare_optimizers(OPTIMIZERS)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Benching 1D root-finder optimizers from scipy.optimize:\n",
"brenth: 604637 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"brentq: 594152 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"ridder: 774866 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"bisect: 2148090 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 31
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First I fixed the for loop in bench_optimizer by moving the for statement to the line above. Then it gave an error sayng that we add integer to tuple. By using pdb it was clear that function_calls returns intiger. We had to erase the comma to make it of the right type. Then our program worked, however the result was correct only for the first element of the OPTIMIZER variable(I changed the orders to make sure it works for other functions too). Then using pdb step function it was possible to go into the function inside the for loop and see how that works. For the second loop the <br>\n",
"&emsp;for func, a, b in param_grid: <br>\n",
"&emsp; &emsp; S=S+apply_optimizer(optimizer, func, a, b) <br>\n",
"didn't work so we assumed that it is being erased. We moved definition of param_grid inside the loop and it worked! "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 31
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}

0 comments on commit e63d827

Please sign in to comment.