Skip to content

Commit

Permalink
Merge pull request #160 from meagana/meagana
Browse files Browse the repository at this point in the history
HW 8
  • Loading branch information
jakevdp committed Dec 8, 2014
2 parents 2b28dd9 + b4145a6 commit 8798405
Showing 1 changed file with 299 additions and 0 deletions.
299 changes: 299 additions & 0 deletions meagana/HW8.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "raw",
"metadata": {},
"source": [
"Summary: \n",
"\n",
"I began by checking for any syntax errors. There ended up being only one syntax error; A line of the code had an extra comma in the following return statement \"return optimizer(func, a, b, full_output=True)[1].function_calls\" After fixing this, the code appeared to only be outputting the count for optimizer.breth. I first checked the variables random_a and random_b to see if they parameters met the requirements listed in the documentation for each optimizer. This appeared to be fine. Next, I checked the OPTIMIZER list by removing optimizer.brenth and running the code on the other three optimizers. This produced the same error as before-- the program was counting the first optimizer in the list but returning zero function calls for the others. \n",
"\n",
"My next line of attack was printing the results of the functions. Printing the apply_optimizer function didn't give any useful results, but printing the return value in the bench_optimizer showed again that only one of the optimizers was being counted. Since the apply_optimizer function seemed to be working, I moved on to testing the param_grid object. Upon printing, I got the following output :\n",
" <itertools.product object at 0x105ac07d0>\n",
"This didn't give any useful information. After investigating the \"product\" documentation, I realized that it would make more sense to print this object in a loop. The results of this indicated that param_grid what it was supposed to, but only for the first optimizer. After moving it inside the loop, the bench_optimizer function was printing out the correct number of function calls for all of the optimizers, however, the final output was\n",
"\n",
"Benching 1D root-finder optimizers from scipy.optimize:\n",
"599821\n",
"brenth: 0 total function calls\n",
"588991\n",
"brentq: 0 total function calls\n",
"770432\n",
"ridder: 0 total function calls\n",
"2145735\n",
"bisect: 0 total function calls.\n",
"\n",
"This ended up having something to with the print statement I put in bench_optimizers. After commenting the print statement out, the compare_ optimizers printed out the correct output.\n",
" "
]
},
{
"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 should 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), # Function with several local maxima\n",
" )\n",
"\n",
"OPTIMIZERS = (optimize.brenth, optimize.brentq,\n",
" optimize.ridder, optimize.bisect)\n",
"#OPTIMIZERS= (optimize.brentq,optimize.ridder, optimize.bisect) # Hrmm. It seems to work for the first element of the array.\n",
" # But it is not being recognized in the compare_optizers func\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",
" # print optimizer(func, a, b, full_output=True)[1].function_calls\n",
" return optimizer(func, a, b, full_output=True)[1].function_calls # Syntax Error: Comma at end of line. Removed.\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",
" #print sum(apply_optimizer(optimizer, func, a, b) \n",
" # for func, a, b in param_grid) #only works for first element in optimizer\n",
" #after moving param_grid into the forloop, this is working, but the call to it in compare_optimizers isn't working.\n",
" #apparently printing the sum screws with something and causes it not to print when it's calls??\n",
" return sum(apply_optimizer(optimizer, func, a, b)\n",
" for func, a, b in param_grid)\n",
"\n",
"\n",
"def compare_optimizers(optimizers): #maybe putting the function in there will work??\n",
" \"\"\" Compare all the optimizers given on a grid of a few different\n",
" functions all admitting a single root in zero and a upper and\n",
" lower bounds.\n",
" \"\"\"\n",
" random_a = -1.3 + np.random.random(size=100)\n",
" random_b = .3 + np.random.random(size=100)\n",
" #what are these numbers?\n",
" #print random_a\n",
" #print random_b\n",
" # They appear to contain the right signs...\n",
" \n",
" # param_grid = product(FUNCTIONS, random_a, random_b) #product gives a Cartesian product of input iterables.\n",
" # print param_grid # not giving anything\n",
" #What if I put this in a forloop?\n",
" #for (x,y,z) in param_grid : print (x,y,z), param_grid #seems; to work \n",
" print(\"Benching 1D root-finder optimizers from scipy.optimize:\")\n",
" for optimizer in OPTIMIZERS:\n",
" param_grid = product(FUNCTIONS, random_a, random_b) #Moving this into the for loop seems to work, but the counts\n",
" #still aren't being recognized when I call the\n",
" #the bench_optimizer function. Hmmm.\n",
" ncalls = bench_optimizer(optimizer, param_grid)\n",
" # print ncalls # using the optimizer array that has three elements, it is not counting any elements.\n",
" #after moving param_grid inside, this is still printing the sums as 0. Hrmm.\n",
" print('{name}: {ncalls} total function calls'.format(\n",
" name=optimizer.__name__, ncalls=ncalls))\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" compare_optimizers(OPTIMIZERS) \n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Benching 1D root-finder optimizers from scipy.optimize:\n",
"brenth: 607945 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"brentq: 597041 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"ridder: 776904 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"bisect: 2152130 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 63
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"The next cell contains the corrected program."
]
},
{
"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",
" return optimizer(func, a, b, full_output=True)[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",
" return sum(apply_optimizer(optimizer, func, a, b)\n",
" for func, a, b in param_grid)\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",
" 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",
" for optimizer in OPTIMIZERS:\n",
" param_grid = product(FUNCTIONS, random_a, random_b)\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: 599291 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"brentq: 588597 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"ridder: 768560 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"bisect: 2144465 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 64
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}

0 comments on commit 8798405

Please sign in to comment.