0

First let me explain the problem I'm trying to solve. I'm integrating my code with 3rd party library which does quite complicated financial predictions. For the purposes of this question let's just say I have a blackbox which returns y when I pass in x. Now, what I need to do is find input (x) for a given output (y). Since I know lowest and highest possible input values I wrote the following algorithm:

  1. define starting input range (minimum input value to maximum input value)
  2. divide the range into two equal parts and find output for a middle value
  3. find which half output falls into
  4. repeat steps 2 and 3 until range is too small to divide any further

This algorithm does the job nicely, I don't see any problems with it. However, is there a faster way to solve this problem?

3
  • 1
    You aren't telling much of the mathematical properties of that complicated financial predicstions algorithm. Hence it's hard to tell. Commented Mar 27, 2013 at 14:48
  • @Vytas It sounds like x and y are strongly correlated (i.e. as x increases, so does y), as otherwise your divide and conquer algorithm wouldn't work. Can you confirm this is the case? Commented Mar 27, 2013 at 14:50
  • Ondrej, RB - the problem is that I don't know anything about inner workings of this prediction algorithm. The only thing I know there is a strong correlation between input and output, i.e. as x increases so does y. However, relationship between x and y is not linear. Commented Mar 27, 2013 at 14:58

1 Answer 1

1

It sounds like x and y are strongly correlated (i.e. as x increases, so does y), as otherwise your divide and conquer algorithm wouldn't work.

Assumuing this is the case, and you could work out a correlation factor, then you might be able to multiply the midpoint by the correlation factor to potentially hone in the expected value quicker.

Please note that I've not tested this idea at all, but it's something to think about. Possible improvements would be to make the correlationFactor a moving average, or precompute it based on, say, the deciles between xLow and xHigh.

Also, this assumes that calling f(x) is relatively inexpensive. If it is expensive, then the increased number of calls to f(x) would dwarf any savings. In fact - I'm starting to think this is a stupid idea...

Hopefully the following pseudo-code illustrates what I mean:

DivideAndConquer(xLow, xHigh, correlationFactor, expectedValue)
    xMid = (xHigh - xLow) * correlationFactor
    // Add some range checks to make sure that xMid is within xLow and xHigh!!
    y = f(xMid)
    if (y == expectedValue)
        return expectedValue
    elseif (y < expectedValue) 
        correlationFactor = (xMid - xLow) / (f(xMid) - f(xLow))
        return DivideAndConquer(xLow, xMid, correlationFactor, expectedValue)
    else
        correlationFactor = (xHigh - xMid) / (f(xHigh) - f(xMid))
        return DivideAndConquer(xMid, xHigh, correlationFactor, expectedValue)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.