tags:

views:

340

answers:

9

I have values returned by unknown function like for example

# this is an easy case - parabolic function
# but in my case function is realy unknown as it is connected to process execution time
[0, 1, 4, 9]

is there a way to predict next value?

+1  A: 

You can apply statistical methods to try and guess the next answer, but that might not work very well if the function is like this one (c):

int evil(void){
  static int e = 0;
  if(50 == e++){
    e = e * 100;
  }
  return e;
}

This function will return nice simple increasing numbers then ... BAM.

tomjen
+4  A: 

Not necessarily. Your "parabolic function" might be implemented like this:

def mindscrew
  @nums ||= [0, 1, 4, 9, "cat", "dog", "cheese"]
  @nums.pop
end

You can take a guess, but to predict with certainty is impossible.

Chuck
I know that there will be numbers
tig
Chuck's point is that the function might be arbitrary, say, [0, 1, 4, 9, 42, 1, 4251, 23, 17, ...]. But actually it's not a serious point, because you can pick a notion of the "simplest" function that fits your given values (eg. x^2 is the lowest-degree polynomial taking values 0,1,4,9; you could also look for the polynomial with the fewest coefficients, a function with the smallest support in your favourite vectorspace of functions, one with the smallest Kolmogorov complexity etc. All these notions formalise the intuitive notion of simplest => most likely; also look up [information criteria].
ShreevatsaR
Yes, you can find *a* function that fits the numbers and have a decent likelihood of being right. My point was just that you can't be certain without imposing constraints on the function. Also, I'm not sure that returning a result from a stack of previous calculations is all that much more complex than x^2. On a low level it is, but on the code level one is simple multiplication and the other is simple array access.
Chuck
You misunderstand what the questioner wants. He/she has only some values, and wants to infer a good function that fits; the question is not about computing a **known** function.
ShreevatsaR
@ShreevatsaR: Perhaps you're right. That certainly wasn't what I'd understood. From the question, it sounds like he has values returned from an unknown function and wishes to predict the next output of that function.
Chuck
+2  A: 

Use the Wolfram Alpha API :)

Dan Diplo
A: 

That's a hard problem.

You should check out the recurrence relation equation for special cases where it could be possible such a task.

Nick D
+4  A: 

If you just want data points

Extrapolation of data outside of known points can be estimated, but you need to accept the potential differences are much larger than with interpolation of data between known points. Strictly, both can be arbitrarily inaccurate, as the function could do anything crazy between the known points, even if it is a well-behaved continuous function. And if it isn't well-behaved, all bets are already off ;-p

There are a number of mathematical approaches to this (that have direct application to computer science) - anything from simple linear algebra to things like cubic splines; and everything in between.

If you want the function

Getting esoteric; another interesting model here is genetic programming; by evolving an expression over the known data points it is possible to find a suitably-close approximation. Sometimes it works; sometimes it doesn't. Not the language you were looking for, but Jason Bock shows some C# code that does this in .NET 3.5, here: Evolving LINQ Expressions.

I happen to have his code "to hand" (I've used it in some presentations); with something like a => a * a it will find it almost instantly, but it should (in theory) be able to find virtually any method - but without any defined maximum run length ;-p It is also possible to get into a dead end (evolutionary speaking) where you simply never recover...

Marc Gravell
I would be inclined to say it is perhaps a bit of overkill compared to more traditional function fitting techniques (least-square stuff?), but cool nonetheless!
martijn_himself
It depends on the scenario; if we assume an unknown function of arbitrary complexity...
Marc Gravell
+2  A: 

Yes. Maybe.

If you have some input and output values, i.e. in your case [0,1,2,3] and [0,1,4,9], you could use response surfaces (basicly function fitting i believe) to 'guess' the actual function (in your case f(x)=x^2). If you let your guessing function be f(x)=c1*x+c2*x^2+c3 there are algorithms that will determine that c1=0, c2=1 and c3=0 given your input and output and given the resulting function you can predict the next value.

Note that most other answers to this question are valid as well. I am just assuming that you want to fit some function to data. In other words, I find your question quite vague, please try to pose your questions as complete as possible!

martijn_himself
+4  A: 

You can try using neural networks approach. There are pretty many articles you can find by Google query "neural network function approximation". Many books are also available, e.g. this one.

Rorick
+2  A: 

In general, no... unless you know it's a function of a particular form (e.g. polynomial of some degree N) and there is enough information to constrain the function.

e.g. for a more "ordinary" counterexample (see Chuck's answer) for why you can't necessarily assume n^2 w/o knowing it's a quadratic equation, you could have f(n) = n4 - 6n3 + 12n2 - 6n, which has for n=0,1,2,3,4,5 f(n) = 0,1,4,9,40,145.

If you do know it's a particular form, there are some options... if the form is a linear addition of basis functions (e.g. f(x) = a + b*cos(x) + c*sqrt(x)) then using least-squares can get you the unknown coefficients for the best fit using those basis functions.

Jason S
+1  A: 

See also this question.

lhf