def example(): xs = numpy.sort( numpy.hstack(( numpy.array([0., 100.]), numpy.random.random(48) * 100, ))) ynoises = numpy.random.random(xs.shape[0]) ys = numpy.round(ynoises + xs / 200 - 0.5, 1) f = interpolate.UnivariateSpline(xs, ys) g = interpolate.interp1d(xs, ys) c = xa.spline1dbuildakima(list(xs), list(ys)) xnews = numpy.arange(1., 99., .1) print([i.shape for i in ( xs, ys, xnews, )]) cys = numpy.array([xa.spline1dcalc(c, x) for x in xnews]) plt.plot(xs, ys, 'ro') plt.plot(xnews, cys, label='akima') plt.plot(xnews, f(xnews), label='Univariate') plt.plot(xnews, g(xnews), label='interp1d') plt.legend(loc='upper left') plt.show()
def redistribute(S): #equal redistribute all beads on the string n = len(S) #calculate DS as an reference for the distribution of beads ds = list(range(n)) ds[0] = 0 for i in range(1, n): ds[i] = ((S[i][0] - S[i - 1][0])**2 + (S[i][1] - S[i - 1][1])**2)**0.5 Sum = 0 tol = sum(ds) DS = list(range(n)) for i in range(n): Sum += ds[i] DS[i] = Sum / tol #create an arithmetic progression standing for uniformly distributed beads. h = [x / (n - 1) for x in range(n)] #split the old string x, y = [], [] for i in range(n): x.append(S[i][0]) y.append(S[i][1]) #interpolation interx = xalglib.spline1dbuildcubic(DS, x) intery = xalglib.spline1dbuildcubic(DS, y) S_new = [] for i in range(n): x_new = xalglib.spline1dcalc(interx, h[i]) y_new = xalglib.spline1dcalc(intery, h[i]) S_new.append([x_new, y_new]) return S_new
def Redistri(S): ds = range(len(S)) ds[0] = 0 for i in range(1,len(S)): ds[i] = ((S[i][0]-S[i-1][0])**2+(S[i][1]-S[i-1][1])**2)**0.5 Sum = 0 ds_cum=range(len(S)) for i in range(len(ds)): Sum = Sum + ds[i] ds_cum[i] = Sum DS = range(len(S)) for i in range(len(ds_cum)): DS[i] = ds_cum[i]/Sum h = range(len(DS)) for i in range(len(h)): h[i] = h[i]/float(len(h)-1) x = range(len(S)) y = range(len(S)) for i in range(len(S)): x[i] = S[i][0] y[i] = S[i][1] interX = xalglib.spline1dbuildcubic(DS,x) interY = xalglib.spline1dbuildcubic(DS,y) S_new = range(len(S)) for i in range(len(h)): X_new = xalglib.spline1dcalc(interX,h[i]) Y_new = xalglib.spline1dcalc(interY,h[i]) S_new[i] = [X_new, Y_new] return S_new
def example(): xs = numpy.sort( numpy.hstack( (numpy.array([0.,100.]),numpy.random.random(48)*100,) ) ) ynoises = numpy.random.random(xs.shape[0]) ys = numpy.round( ynoises + xs/200 - 0.5, 1) f = interpolate.UnivariateSpline(xs,ys) g = interpolate.interp1d(xs,ys) c = xa.spline1dbuildakima(list(xs),list(ys)) xnews = numpy.arange(1.,99.,.1) print( [i.shape for i in (xs,ys,xnews,)] ) cys = numpy.array( [xa.spline1dcalc(c,x) for x in xnews] ) plt.plot(xs,ys,'ro') plt.plot(xnews,cys,label='akima') plt.plot(xnews,f(xnews),label='Univariate') plt.plot(xnews,g(xnews),label='interp1d') plt.legend(loc='upper left') plt.show()
def calc_strike_vols( self, strikes, underlying_price, time_to_exp, atm_strike, atm_vol, wide_skew, tight_skew, put_curve, call_curve, put_first_diff, put_second_diff, put_wing_diff, put_first_x, put_second_x, put_wing_x, call_first_diff, call_second_diff, call_wing_diff, call_first_x, call_second_x, call_wing_x, put_wing_slope_diff, call_wing_slope_diff, ): if time_to_exp <= 0.0: log.warn("time_to_exp<=0, returning atm_vol for all strike vols: atm_vol=%s", atm_vol) strike_vols = np.empty_like(strikes) strike_vols.fill(atm_vol) return strike_vols calc_inputs = self._get_calc_inputs_from_curve_inputs( time_to_exp, atm_strike, atm_vol, wide_skew, tight_skew, put_curve, call_curve, put_first_diff, put_second_diff, put_wing_diff, put_first_x, put_second_x, put_wing_x, call_first_diff, call_second_diff, call_wing_diff, call_first_x, call_second_x, call_wing_x, put_wing_slope_diff, call_wing_slope_diff, ) try: interpolant = self._build_interpolant(time_to_exp, **calc_inputs) # calculate boundary conditions std_dev = atm_strike * atm_vol * math.sqrt(time_to_exp) put_wing_strike = put_wing_x * std_dev + atm_strike put_wing_vol = calc_inputs["put_wing"] + atm_vol put_wing_slope = calc_inputs["put_wing_slope"] call_wing_strike = call_wing_x * std_dev + atm_strike call_wing_vol = calc_inputs["call_wing"] + atm_vol call_wing_slope = calc_inputs["call_wing_slope"] strike_vols = np.empty_like(strikes, dtype=float) for i, strike in enumerate(strikes): if strike < put_wing_strike: strike_vol = put_wing_vol + (strike - put_wing_strike) * put_wing_slope elif strike > call_wing_strike: strike_vol = call_wing_vol + (strike - call_wing_strike) * call_wing_slope else: strike_vol = xalglib.spline1dcalc(interpolant, strike) strike_vols[i] = strike_vol except ValueError: log.error("unable to build cubic spline, return atm_vol for all vols: atm_vol=%s", atm_vol) strike_vols = np.empty_like(strikes) strike_vols.fill(atm_vol) return strike_vols
def _get_calc_inputs_from_curve_inputs( self, time_to_exp, atm_strike, atm_vol, wide_skew, tight_skew, put_curve, call_curve, put_first_diff, put_second_diff, put_wing_diff, put_first_x, put_second_x, put_wing_x, call_first_diff, call_second_diff, call_wing_diff, call_first_x, call_second_x, call_wing_x, put_wing_slope_diff, call_wing_slope_diff, ): put_first = put_first_x * (wide_skew + tight_skew) + put_first_x ** 2 * put_curve + put_first_diff put_second = put_second_x * wide_skew + put_second_x ** 2 * put_curve + put_second_diff put_wing = put_wing_x * wide_skew + put_wing_x ** 2 * put_curve + put_wing_diff call_first = call_first_x * (wide_skew + tight_skew) + call_first_x ** 2 * call_curve + call_first_diff call_second = call_second_x * wide_skew + call_second_x ** 2 * call_curve + call_second_diff call_wing = call_wing_x * wide_skew + call_wing_x ** 2 * call_curve + call_wing_diff log.debug("time_to_exp: %s", time_to_exp) log.debug("atm_strike: %s", atm_strike) log.debug("atm_vol: %s", atm_vol) log.debug("put_first: %s", put_first) log.debug("put_second: %s", put_second) log.debug("put_wing: %s", put_wing) log.debug("call_first: %s", call_first) log.debug("call_second: %s", call_second) log.debug("call_wing: %s", call_wing) try: interpolant = self._build_interpolant( time_to_exp, atm_strike, atm_vol, put_first, put_second, put_wing, put_first_x, put_second_x, put_wing_x, call_first, call_second, call_wing, call_first_x, call_second_x, call_wing_x, put_wing_slope=None, call_wing_slope=None, ) std_dev = atm_strike * atm_vol * math.sqrt(time_to_exp) put_wing_strike = std_dev * put_wing_x + atm_strike put_wing_vol = put_wing + atm_vol call_wing_strike = std_dev * call_wing_x + atm_strike call_wing_vol = call_wing + atm_vol incremented_put_strike_vol = xalglib.spline1dcalc(interpolant, put_wing_strike + 1.0) incremented_call_strike_vol = xalglib.spline1dcalc(interpolant, call_wing_strike - 1.0) log.debug("std_dev: %s", std_dev) log.debug("put_wing_strike: %s", put_wing_strike) log.debug("put_wing_vol: %s", put_wing_vol) log.debug("call_wing_strike: %s", call_wing_strike) log.debug("call_wing_vol: %s", call_wing_vol) log.debug("incremented_put_strike_vol: %s", incremented_put_strike_vol) log.debug("incremented_call_strike_vol: %s", incremented_call_strike_vol) put_wing_slope = incremented_put_strike_vol - put_wing_vol + put_wing_slope_diff call_wing_slope = call_wing_vol - incremented_call_strike_vol + call_wing_slope_diff except ValueError: log.error("error when calculating implied slopes") put_wing_slope = 0 + put_wing_slope_diff call_wing_slope = 0 + call_wing_slope_diff log.debug("put_wing_slope: %s", put_wing_slope) log.debug("call_wing_slope: %s", call_wing_slope) calc_inputs = { "atm_strike": atm_strike, "atm_vol": atm_vol, "put_first": put_first, "put_second": put_second, "put_wing": put_wing, "put_first_x": put_first_x, "put_second_x": put_second_x, "put_wing_x": put_wing_x, "call_first": call_first, "call_second": call_second, "call_wing": call_wing, "call_first_x": call_first_x, "call_second_x": call_second_x, "call_wing_x": call_wing_x, "put_wing_slope": put_wing_slope, "call_wing_slope": call_wing_slope, } return calc_inputs