Python for Finance: Analyze Big Financial Data

(Elle) #1

Convex Optimization


In finance and economics, convex optimization plays an important role. Examples are the


calibration of option pricing models to market data or the optimization of an agent’s


utility. As an example function that we want to minimize, we take fm, as defined in the


following:


In  [ 54 ]: def fm((x,  y)):
return (np.sin(x) + 0.05 * x ** 2
+ np.sin(y) + 0.05 * y ** 2 )
In [ 55 ]: x = np.linspace(- 10 , 10 , 50 )
y = np.linspace(- 10 , 10 , 50 )
X, Y = np.meshgrid(x, y)
Z = fm((X, Y))

Figure 9-14 shows the function graphically for the defined intervals for x and y. Visual


inspection already reveals that this function has multiple local minima. The existence of a


global minimum cannot really be confirmed by this particular graphical representation:


In  [ 56 ]: fig =   plt.figure(figsize=( 9 ,     6 ))
ax = fig.gca(projection=‘3d’)
surf = ax.plot_surface(X, Y, Z, rstride= 2 , cstride= 2 ,
cmap=mpl.cm.coolwarm,
linewidth=0.5, antialiased=True)
ax.set_xlabel(‘x’)
ax.set_ylabel(‘y’)
ax.set_zlabel(‘f(x, y)’)
fig.colorbar(surf, shrink=0.5, aspect= 5 )

Figure 9-14. Function to minimize with two parameters

In what follows, we want to implement both a global minimization approach and a local


one. The functions brute and fmin that we want to use can be found in the sublibrary


scipy.optimize:


In  [ 57 ]: import scipy.optimize as spo

Global Optimization


To have a closer look behind the scenes when we initiate the minimization procedures, we


amend the original function by an option to output current parameter values as well as the


function value:

Free download pdf