- Introduction to Python
- Getting started with Python and the IPython notebook
- Functions are first class objects
- Data science is OSEMN
- Working with text
- Preprocessing text data
- Working with structured data
- Using SQLite3
- Using HDF5
- Using numpy
- Using Pandas
- Computational problems in statistics
- Computer numbers and mathematics
- Algorithmic complexity
- Linear Algebra and Linear Systems
- Linear Algebra and Matrix Decompositions
- Change of Basis
- Optimization and Non-linear Methods
- Practical Optimizatio Routines
- Finding roots
- Optimization Primer
- Using scipy.optimize
- Gradient deescent
- Newton’s method and variants
- Constrained optimization
- Curve fitting
- Finding paraemeters for ODE models
- Optimization of graph node placement
- Optimization of standard statistical models
- Fitting ODEs with the Levenberg–Marquardt algorithm
- 1D example
- 2D example
- Algorithms for Optimization and Root Finding for Multivariate Problems
- Expectation Maximizatio (EM) Algorithm
- Monte Carlo Methods
- Resampling methods
- Resampling
- Simulations
- Setting the random seed
- Sampling with and without replacement
- Calculation of Cook’s distance
- Permutation resampling
- Design of simulation experiments
- Example: Simulations to estimate power
- Check with R
- Estimating the CDF
- Estimating the PDF
- Kernel density estimation
- Multivariate kerndel density estimation
- Markov Chain Monte Carlo (MCMC)
- Using PyMC2
- Using PyMC3
- Using PyStan
- C Crash Course
- Code Optimization
- Using C code in Python
- Using functions from various compiled languages in Python
- Julia and Python
- Converting Python Code to C for speed
- Optimization bake-off
- Writing Parallel Code
- Massively parallel programming with GPUs
- Writing CUDA in C
- Distributed computing for Big Data
- Hadoop MapReduce on AWS EMR with mrjob
- Spark on a local mahcine using 4 nodes
- Modules and Packaging
- Tour of the Jupyter (IPython3) notebook
- Polyglot programming
- What you should know and learn more about
- Wrapping R libraries with Rpy
Newton-Rhapson Method
We want to find the value \(\theta\) so that some (differentiable) function \(g(\theta)=0\). Idea: start with a guess, \(\theta_0\). Let \(\tilde{\theta}\) denote the value of \(\theta\) for which \(g(\theta) = 0\) and define \(h = \tilde{\theta} - \theta_0\). Then:
This implies that
\[h\approx \frac{g(\theta_0)}{g'(\theta_0)}\]
So that
\[\tilde{\theta}\approx \theta_0 - \frac{g(\theta_0)}{g'(\theta_0)}\]
Thus, we set our next approximation:
\[\theta_1 = \theta_0 - \frac{g(\theta_0)}{g'(\theta_0)}\]
and we have developed an interative procedure with:
\[\theta_n = \theta_{n-1} - \frac{g(\theta_{n-1})}{g'(\theta_{n-1})}\]
Example:
Let
\[g(x) = \frac{x^3-2x+7}{x^4+2}\]
The graph of this function is:
x = np.arange(-5,5, 0.1); y = (x**3-2*x+7)/(x**4+2) p1=plt.plot(x, y) plt.xlim(-4, 4) plt.ylim(-.5, 4) plt.xlabel('x') plt.axhline(0) plt.title('Example Function') plt.show()
x = np.arange(-5,5, 0.1); y = (x**3-2*x+7)/(x**4+2) p1=plt.plot(x, y) plt.xlim(-4, 4) plt.ylim(-.5, 4) plt.xlabel('x') plt.axhline(0) plt.title('Good Guess') t = np.arange(-5, 5., 0.1) x0=-1.5 xvals = [] xvals.append(x0) notconverge = 1 count = 0 cols=['r--','b--','g--','y--','c--','m--','k--','w--'] while (notconverge==1 and count < 6): funval=(xvals[count]**3-2*xvals[count]+7)/(xvals[count]**4+2) slope=-((4*xvals[count]**3 *(7 - 2 *xvals[count] + xvals[count]**3))/(2 + xvals[count]**4)**2) + (-2 + 3 *xvals[count]**2)/(2 + xvals[count]**4) intercept=-slope*xvals[count]+(xvals[count]**3-2*xvals[count]+7)/(xvals[count]**4+2) plt.plot(t, slope*t + intercept, cols[count]) nextval = -intercept/slope if abs(funval) < 0.01: notconverge=0 else: xvals.append(nextval) count = count+1 plt.show()
From the graph, we see the zero is near -2. We make an initial guess of
\[x=-1.5\]
We have made an excellent choice for our first guess, and we can see rapid convergence!
funval
0.007591996330867034
In fact, the Newton-Rhapson method converges quadratically. However, NR (and the secant method) have a fatal flaw:
x = np.arange(-5,5, 0.1); y = (x**3-2*x+7)/(x**4+2) p1=plt.plot(x, y) plt.xlim(-4, 4) plt.ylim(-.5, 4) plt.xlabel('x') plt.axhline(0) plt.title('Bad Guess') t = np.arange(-5, 5., 0.1) x0=-0.5 xvals = [] xvals.append(x0) notconverge = 1 count = 0 cols=['r--','b--','g--','y--','c--','m--','k--','w--'] while (notconverge==1 and count < 6): funval=(xvals[count]**3-2*xvals[count]+7)/(xvals[count]**4+2) slope=-((4*xvals[count]**3 *(7 - 2 *xvals[count] + xvals[count]**3))/(2 + xvals[count]**4)**2) + (-2 + 3 *xvals[count]**2)/(2 + xvals[count]**4) intercept=-slope*xvals[count]+(xvals[count]**3-2*xvals[count]+7)/(xvals[count]**4+2) plt.plot(t, slope*t + intercept, cols[count]) nextval = -intercept/slope if abs(funval) < 0.01: notconverge = 0 else: xvals.append(nextval) count = count+1 plt.show()
We have stumbled on the horizontal asymptote. The algorithm fails to converge.
Basins of Attraction Can Be ‘Close’
def f(x): return x**3 - 2*x**2 - 11*x +12 def s(x): return 3*x**2 - 4*x - 11 x = np.arange(-5,5, 0.1); p1=plt.plot(x, f(x)) plt.xlim(-4, 5) plt.ylim(-20, 22) plt.xlabel('x') plt.axhline(0) plt.title('Basin of Attraction') t = np.arange(-5, 5., 0.1) x0=2.43 xvals = [] xvals.append(x0) notconverge = 1 count = 0 cols=['r--','b--','g--','y--','c--','m--','k--','w--'] while (notconverge==1 and count < 6): funval = f(xvals[count]) slope = s(xvals[count]) intercept=-slope*xvals[count]+funval plt.plot(t, slope*t + intercept, cols[count]) nextval = -intercept/slope if abs(funval) < 0.01: notconverge = 0 else: xvals.append(nextval) count = count+1 plt.show() xvals[count-1]
-3.1713324128480282
p1=plt.plot(x, f(x)) plt.xlim(-4, 5) plt.ylim(-20, 22) plt.xlabel('x') plt.axhline(0) plt.title('Basin of Attraction') t = np.arange(-5, 5., 0.1) x0=2.349 xvals = [] xvals.append(x0) notconverge = 1 count = 0 cols=['r--','b--','g--','y--','c--','m--','k--','w--'] while (notconverge==1 and count < 6): funval = f(xvals[count]) slope = s(xvals[count]) intercept=-slope*xvals[count]+funval plt.plot(t, slope*t + intercept, cols[count]) nextval = -intercept/slope if abs(funval) < 0.01: notconverge = 0 else: xvals.append(nextval) count = count+1 plt.show() xvals[count-1]
0.9991912395651094
Convergence Rate
The following is a derivation of the convergence rate of the NR method:
Suppose \(x_k \; \rightarrow \; x^*\) and \(g'(x^*) \neq 0\). Then we may write:
\[x_k = x^* + \epsilon_k\]
.
Now expand \(g\) at \(x^*\):
\[g(x_k) = g(x^*) + g'(x^*)\epsilon_k + \frac12 g''(x^*)\epsilon_k^2 + ...\] \[g'(x_k)=g'(x^*) + g''(x^*)\epsilon_k\]
We have that
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论