python - Optimize leastsq execution order -


i found weird thing problem. put whole file here replacing previous one.

import pandas pd   import numpy np   option_pricer import bs_call, r_input   scipy.optimize import leastsq    ## function returns error vector  def res_bs(p):        sigma = p       err = np.zeros(len(k))       in range(len(k)):           err[i] = (calleu[i]-bs_call(sigma, r[i], t[i]/360, s[i], k[i] ))/sqrt(len(k))       return err  ## function returns root mean squared error  def rmse_bs(p):      return sqrt(sum(res_bs(p)**2))    ## function converts raw data variables used       def params_in(options):      k = options.strk_prc       k = pd.np.array(k)       t = options.tmy       t = pd.np.array(t)       s = options.undl_prc       s = pd.np.array(s)       calleu = options.eur_call_prc       calleu = pd.np.array(calleu)       callam = options.ame_call_prc       callam = pd.np.array(callam)       r1 = unique(options.r1)       r1 = pd.np.array(r1)           r2 = unique(options.r2)       r2 = pd.np.array(r2)           r3 = unique(options.r3)       r3 = pd.np.array(r3)           r4 = unique(options.r4)       r4 = pd.np.array(r4)           r5 = unique(options.r5)       r5 = pd.np.array(r5)       r = r_input(t, r1, r2, r3, r4, r5)       return [k, t, s, calleu, callam, r]    ## calibration routine def calib_bs(data):      tddays = sorted(set(data.d_td))       n = len(tddays)       sigma_implied = np.zeros(n)       rmsebs = np.zeros(n)       in range(n):           options = data[data['d_td']==tddays[i]]           global k, t, s, calleu, callam, r           [k, t, s, calleu, callam, r] = params_in(options)           p0 = 0.05             sigma_implied[i] = leastsq(res_bs, p0)[0]           rmsebs[i] = rmse_bs(sigma_implied[i])       return [sigma_implied, rmsebs]    data = pd.read_csv("mydata.csv")  [sigma, rmsebs] = calib_bs(data) 

if execute above in spyder directly (pressing "f5"), result p0! but, if execute interactively - is, load data, define functions, call calibration function - right answer!

my intuition tells me problem relates way or order python executes commands. not have knowledge that.

can tell me how modify code allow direct execution rather interactively?

appendix: tried following test , did not find problem. is, result correct , not depend on how execute code, e.g., execute directly ("f5") or interactively. "data.csv" file can anything, made matrix np.arrange(1,41).reshape(20,2) , save data.csv

import numpy np
scipy.optimize import leastsq
import pandas pd
testfuncs import f ## f f(x, p) return x*p, linear function

def ssr(p):      beta = p       err = np.zeros(len(x))       in range(len(x)):           err[i] = ( y[i]-f(x[i],beta) )       return err    def calibration(data):      l = data.count()[0]     = data.a     = pd.np.array(a)     b = data.b     b = pd.np.array(b)     par = np.zeros(l/5)     in range(l/5):         global x, y         x = a[i*5:(i+1)*5]         y = b[i*5:(i+1)*5]         p0 = 1         par[i] = leastsq(ssr, p0)[0]     return par  result = calibration(data) 


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -