I have a nice amount of data from a trading strategy I am working on, where I have two different liquidity parameters as x, y variables.

Before entering a trade I am taking the moving average of Volume*SharePrice & if > than my "liquidity requirement" I enter, along with other unmentioned rules. The length of the moving average is my x variable and my liquidity requirement is my y variable. My data includes several outputs over these x and y variables such as P/L, ROI, $Won, etc.

My question is: how can I use Mathematica to interpolate an approximate function for any output so that I can optimize x, y? For instance say I want to approximate ROI=f(x,y) or P/L=f(x,y)...I dont expect something that describes my data perfectly, but something that approximates it would greatly help.

  • 5
    $\begingroup$ type ?*Interpolation* browse the corresponding documentation (which can be evaluated), write a similar code, improve it, and if it works I ll only take 30 % of the net profit for this great advice ;-) $\endgroup$
    – chris
    Nov 30, 2012 at 8:32
  • $\begingroup$ Yeah I saw that documentation and I was doing something wrong before, but now I seem to have it working. Can I use the Interpolating function as a regular function? For instance say I have f1=Interpolation[DollarWon] and f2=Interpolation[DollarLost] can I now do Maximize[f1-f2,{x,y}]? $\endgroup$
    – Brandon
    Nov 30, 2012 at 16:41
  • $\begingroup$ you can do dat = Table[{x, Cos[x]}, {x, 0, 2 Pi, Pi/8.}]; f1 = Interpolation[dat]; NMinimize[{f1[x], x > 0, x < 2 Pi}, {x, 0, 2 Pi}] $\endgroup$
    – chris
    Nov 30, 2012 at 16:54
  • $\begingroup$ I really appreciate the help, but I am still getting hung up on the NMaximize.. I have PL = data[[All, {1, 2, 3}]]; where data is a csv holding all my backtest results. I am able to successfully get the Interpolation with f1 = Interpolation[PL]; But trying NMaximize[{f1[x, y], x >= 30, x <= 90, y >= 1000000, y <= 10000000}, {x, y}] yields NMinimize::nnum: The function value InterpolatingFunction[{{0.,6.28319}},<<3>>,{Automatic}][89.9186,1.83176*10^6] is not a number at {x,y} = {89.9186,1.83176*10^6}. >> $\endgroup$
    – Brandon
    Nov 30, 2012 at 18:10
  • $\begingroup$ I am confused by this bit {x, 0, 2 Pi} in your NMinimize.. isnt this supposed to be the choice variables mathematica searches for? I am having trouble finding documentation on this function regarding multivariate functions... I hope I am not driving you crazy, I really appreciate the help! $\endgroup$
    – Brandon
    Nov 30, 2012 at 18:16

1 Answer 1


Let's just consider a test case:

dat = Table[{x, y, -Sin[x]*Sin[y]}, {x, 0, Pi, Pi/8.}, {y, 0, Pi, Pi/8}];

f = Interpolation[Flatten[dat, 1]];

as a check let us plot sections through the interpolation.

Plot[Table[f[x, y], {x, 0, Pi, Pi/8}]//Evaluate, {y, 0, Pi}];

Mathematica graphics

Now let's minimize that interpolated function:

NMinimize[{f[x, y], x > 0, x <= Pi, y >= 0, y <= Pi}, {{x, 0, Pi}, {y, 0, Pi}}]

(* ==> {-1., {x -> 1.5708, y -> 1.5708}} *)
  • $\begingroup$ Awesome! This got me where I needed to be! I really appreciate the write up! $\endgroup$
    – Brandon
    Dec 2, 2012 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.