Here is a test of another parameterization, a simple piecewise function.


In [1]:
# Routine

# This line configures matplotlib to show figures embedded in the notebook, 
# instead of opening a new window for each figure. More about that later. 
# If you are using an old version of IPython, try using '%pylab inline' instead.
%matplotlib inline
%load_ext snakeviz

import numpy as np
from scipy.optimize import minimize
from scipy.optimize import rosen, differential_evolution
from scipy.special import expit
import matplotlib.pyplot as plt
import scipy

from matplotlib.lines import Line2D

import timeit

import pandas as pd

import plotly.plotly as py
from plotly.graph_objs import *
import plotly.tools as tls

In [2]:
# For test use. This is some random structure.

array8 = np.asarray(np.split(np.random.rand(1,40)[0],8))
array8zero = np.asarray(np.split(np.zeros(40),8))

In [20]:
# Density matrix in the forms that I wrote down on my Neutrino Physics notebook
# x is a real array of 8 lenghth mmax arrays. x[i] is for A_m\sin(m x) while x[i+4] is for B_m \cos(m x)

init = np.array([1.0,0.0,0.0,0.0])

def rho(x,ti):
    
    elem = np.zeros(4)
    
    for i in np.linspace(0,3,4):
        elem[i] = np.sum( x[i]*np.sin(x[i]*ti)  + x[i+4]*np.cos(x[i+4]*ti)  )

    return elem

In [4]:
rho(array8,0.3)


Out[4]:
array([ 2.20952767,  3.05834199,  2.74872491,  3.47114563])

In [39]:
def rhop(x,ti,length):
    rhoprime = np.zeros(4)
#    mlen = len(x[0])
    mlen = length
    marray = np.linspace(0,mlen-1,mlen)
    
    for i in np.linspace(0,3,4):
        rhoprime[i] = np.sum(  ( x[i]*np.cos(x[i]*ti) - x[i+4]*np.sin(x[i+4]*ti) )*marray )
        
    return rhoprime

In [40]:
rhop(array8,0.3,5)


Out[40]:
array([ 2.77173221,  3.03459901,  4.24083388,  3.28080818])

In [17]:
tri = np.array([np.cos(2.0),np.sin(2.0)])

In [63]:
def costi0(x,ti,initialCondition):
    
    costTemp = np.zeros(4)
    
    for i in np.linspace(0,3,4):
        costTemp[i] = (np.sum( x[i+4] ) - initialCondition[i])**2
    
    return np.sum(costTemp)


def costi(x,ti,initialCondition,length):
    
    rhoi = rho(x,ti)
    rhopi = rhop(x,ti,length)
    
    costTemp = np.zeros(4)
    
    costTemp[0] = ( rhopi[0] - 2.0*rhoi[3]*tri[1] )**2
    costTemp[1] = ( rhopi[1] + 2.0*rhoi[3]*tri[1] )**2
    costTemp[2] = ( rhopi[2] - 2.0*rhoi[3]*tri[0] )**2
    costTemp[3] = ( rhopi[3] + 2.0*rhoi[2]*tri[0] - tri[1] * (rhoi[1] - rhoi[0] ) )**2
    
    return np.sum(costTemp) + costi0(x,ti,initialCondition)

In [64]:
costi(array8,0,init,5)


Out[64]:
174.12007225567433

In [65]:
def cost(x,t,initialCondition,length):
    
    costTotal = map(lambda t: costi(x,t,initialCondition,length),t)
    
    return 0.5*np.sum(costTotal)

In [66]:
cost(array8,np.array([0,1,2]),init,5)


Out[66]:
237.2836413889969

Numpy OPTIMIZATION


In [67]:
# initial guess

initGuess = np.array(np.split(np.random.rand(1,40)[0],8))

endpoint = 2
tlin = np.linspace(0,endpoint,11)
lengthx = len(initGuess[0])

costF = lambda x: cost(x,tlin,init,lengthx)

start = timeit.default_timer()
costvFResult = minimize(costF,initGuess,method="SLSQP",tol=1e-10)
stop = timeit.default_timer()

print stop - start

print costvFResult


6.31389713287
  status: 0
 success: True
    njev: 36
    nfev: 1537
     fun: 3.8352251361253469
       x: array([  4.68854744e-02,   3.25283243e-04,   1.49396412e-04,
        -1.91467039e-02,   2.19321333e-01,   1.20106659e-03,
         1.09584008e-03,  -2.14742587e-03,   7.45231406e-01,
         8.33897339e-01,   4.52807397e-01,   9.70563195e-01,
         5.17681010e-01,   4.66866668e-01,   1.49499579e-01,
         4.79070508e-02,   2.90993450e-01,   2.99525159e-01,
         7.32258458e-01,   3.23154186e-01,   9.22672325e-01,
         6.51670535e-01,   9.17555199e-02,   6.63286851e-01,
         5.66183721e-02,   5.94078359e-01,   1.47172472e-02,
         1.47523048e-01,   8.00476620e-01,   4.82298357e-01,
         9.74052238e-01,   4.23114706e-01,   6.94935985e-01,
         5.00782811e-01,   4.99306733e-01,   5.94940666e-01,
         1.36722491e-01,   2.56936175e-01,   9.80723234e-01,
         4.32729219e-01])
 message: 'Optimization terminated successfully.'
     jac: array([  9.47713852e-06,  -1.15633011e-05,  -1.50799751e-05,
         1.95205212e-05,   8.04662704e-07,   1.26361847e-05,
         7.77840614e-06,  -2.89082527e-05,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   0.00000000e+00])
     nit: 36

In [68]:
xmid = costvFResult.x

start = timeit.default_timer()
#costvFResult = minimize(costF,xmid,method="SLSQP",tol=1e-30,options={"ftol":1e-30,"maxiter":100000})
costvFResult = minimize(costF,xmid,method='Nelder-Mead',tol=1e-15,options={"ftol":1e-15, "maxfev": 1000000,"maxiter":1000000})
stop = timeit.default_timer()

print stop - start

print costvFResult


15.707957983
  status: 0
    nfev: 3875
 success: True
     fun: 3.8352251360888792
       x: array([  4.68858500e-02,   3.24922536e-04,   1.49244334e-04,
        -1.91468980e-02,   2.19321336e-01,   1.19985585e-03,
         1.09508306e-03,  -2.14536934e-03,   1.04004220e+00,
         3.14208998e-01,   5.55516345e-01,   1.07122139e+00,
         5.96309864e-01,   6.34823086e-01,   1.74143095e-01,
         7.42541014e-02,   4.01039087e-01,   3.57898140e-01,
         7.81564845e-01,   3.26117324e-01,   6.34567956e-01,
         9.59764144e-01,   1.31324992e-01,   6.94483350e-01,
         5.50681517e-02,   2.15670965e-01,   1.27432770e-02,
        -2.14318652e-02,   8.68347535e-01,   9.09060849e-01,
         2.97815292e-01,   4.82566933e-01,   5.24124894e-01,
         5.00776810e-01,   3.01350709e-01,   5.81698086e-01,
         5.48566256e-02,   3.73526516e-01,   8.00867102e-01,
         4.80785382e-01])
 message: 'Optimization terminated successfully.'
     nit: 1632

In [69]:
plttlin=np.linspace(0,endpoint,100)
xresult = costvFResult.x

pltdata11 = np.array([])
for i in plttlin:
    pltdata11 = np.append(pltdata11,rho(xresult,i)[0] )

In [70]:
plt.figure(figsize = (16,9.36))
plt.plot(np.linspace(0,2,10),1-(np.sin(2.0)**2)*(np.sin(0.5*np.linspace(0,2,10)) )**2,"r-")
plt.plot(plttlin,pltdata11)
plt.show()



In [ ]: