Need help understanding function passing in Python
<p>I am trying to teach myself Python by working through some problems I came up with, and I need some help understanding how to pass functions.</p>

<p>Let's say I am trying to predict tomorrow's temperature based on today's and yesterday's temperature, and I have written the following function:</p>

<pre><code>def predict_temp(temp_today, temp_yest, k1, k2):
    return k1*temp_today + k2*temp_yest
</code></pre>

<p>And I have also written an error function to compare a list of predicted temperatures with actual temperatures and return the mean absolute error:</p>

<pre><code>def mean_abs_error(predictions, expected):
    return sum([abs(x - y) for (x,y) in zip(predictions,expected)]) / float(len(predictions))
</code></pre>

<p>Now if I have a list of daily temperatures for some interval in the past, I can see how my prediction function would have done <strong>with specific k1 and k2 parameters</strong> like this:</p>

<pre><code>&gt;&gt;&gt; past_temps = [41, 35, 37, 42, 48, 30, 39, 42, 33]
&gt;&gt;&gt; pred_temps = [predict_temp(past_temps[i-1],past_temps[i-2],0.5,0.5) for i in xrange(2,len(past_temps))]
&gt;&gt;&gt; print pred_temps
[38.0, 36.0, 39.5, 45.0, 39.0, 34.5, 40.5]
&gt;&gt;&gt; print mean_abs_error(pred_temps, past_temps[2:])
6.5
</code></pre>

<p><strong>But how do I design a function to minimize my parameters k1 and k2 of my predict_temp function given an error function and my past_temps data?</strong></p>

<p>Specifically I would like to write a function minimize(args*) that takes a prediction function, an error function, some training data, and that uses some search/optimization method (gradient descent for example) to estimate and return the values of k1 and k2 that minimize my error given the data?</p>

<p>I am not asking how to implement the optimization method. Assume I can do that. Rather, I would just like to know <strong>how to pass my predict and error functions</strong> (and my data) to my minimize function, and <strong>how to tell my minimize function that it should optimize the parameters k1 and k2</strong>, so that my minimize function can automatically search a bunch of different settings of k1 and k2, applying my prediction function with those parameters each time to the data and computing error (like I did manually for k1=0.5 and k2=0.5 above) and then return the best results.</p>

<p>I would like to be able to pass these functions so I can easily swap in different prediction and error functions (differing by more than just parameter settings that is). Each prediction function might have a different number of free parameters.</p>

<p>My minimize function should look something like this, but I don't know how to proceed:</p>

<pre><code>def minimize(prediction_function, which_args_to_optimize, error_function, data):
    # 1: guess initial parameters
    # 2: apply prediction function with current parameters to data to compute predictions
    # 3: use error function to compute error between predictions and data
    # 4: if stopping criterion is met, return parameters
    # 5: update parameters
    # 6: GOTO 2
</code></pre>

<p>Edit: It's that easy?? This is no fun. I am going back to Java. </p>

<p>On a more serious note, I think I was also getting hung up on how to use different prediction functions with different numbers of parameters to tune. If I just take all the free parameters in as one tuple I can keep the form of the function the same so it easy to pass and use.</p>
