Skip to content

Commit

Permalink
Merge pull request #149 from ffCrack/HW8_branch
Browse files Browse the repository at this point in the history
Fangfang Wang HW8
  • Loading branch information
jakevdp committed Dec 2, 2014
2 parents ea19263 + 98035d5 commit fc66ad1
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions ffCrack/HW8_Wang_Fangfang.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"metadata": {
"name": "",
"signature": "sha256:4ec35056e921c7f616f14cc2382d88b15da4734866df9b138c681e301ccd11f8"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"HW8 Fangfang Wang ID 1224541"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# start the inline backend for plotting\n",
"%matplotlib inline\n",
"import numpy as np\n",
"from itertools import product\n",
"from scipy import optimize"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"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",
"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 ##delete the ,\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",
" print(\"Benching 1D root-finder optimizers from scipy.optimize:\")\n",
" for optimizer in optimizers: ##change to optimizers\n",
" random_a = -1.3 + np.random.random(size=100)\n",
" random_b = .3 + np.random.random(size=100)\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",
"if __name__ == '__main__':\n",
" compare_optimizers(OPTIMIZERS)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Benching 1D root-finder optimizers from scipy.optimize:\n",
"brenth: 602348 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"brentq: 593959 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"ridder: 777648 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"bisect: 2146910 total function calls"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"My debugging approach:\n",
"There are 3 functions defined in the code, I checked the functions one by one, and validated by plugging in trial parameters. \n",
"Firstly, I checked the syntax errors.\n",
"At the apply_optimizer() function, I deleted the \",\" following \"function_calls\". \n",
"Currently, I didn't notice any runtime error. Then I checked the semantic errors. \n",
"In the code of compare_optimizers(optimizers), I noticed that in the for loop, \"for optimizer in OPTIMIZERS:\", since the parameter taken is \"optimizers\", I changed it to \"for optimizer in optimizers:\"\n",
"After correcting the above error, I noticed that the total calls of brentq, ridder, bisect algorithms were all 0, only the first algorithm's number of calls output is around 602000. Actually, I'm still not quite confident in explaining this. But this could possibly be because the 2nd to 4th algorithms took the optimizer calculated from the first algorithm as the initial value, so it reached the desired value at the 1st iteration. I also tested this assumption by running the two algorithms consecutively using some test numbers. This won't compare the efficiency of the 4 algorithms, so I move the code of creating random arrays random_a and random_b into the loop, so this will let each algorithm's initial value to be different of the optimizer.\n",
"In addition,I notice that the above code will throw valueError excepetion when the interval [a,b] does not contain 0."
]
}
],
"metadata": {}
}
]
}

0 comments on commit fc66ad1

Please sign in to comment.