# Estimating parameters of a self-defined function

I have a self-defined function in Mathematica consisting of four input variables. Two variables are known and I want to use Mathematica to find estimates of the other two input variables.

My function has the signature

CalculatePrice[v1_, v2_, v3_, v4_]


and calculates the price of a financial derivative, which is a real number.

Furthermore, I have a variable actualPrice = 100.15. I know two of the input variables. For the other two input variables, I have guesstimates. I want to find the best value of the two unknown variables, so that the output value of CalculatePrice approaches actualPrice as closely as possible, i.e. I want to minimize the error between actualPriceand the output of CalculatePrice.

Can someone help me with the most efficient way to do so, as the calculation of an output value by CalculatePrice takes quite some time.

• Have you tried NonlinearModelFit ? (or any of the other regression functions?) Also, what is a "double" ? – JimB Aug 11 '17 at 18:33
• Minimize(error(X, MyFunc[v1, v2, v3, v4, v5, v6, v7_est, v8_est, v9_est, v10_est])) is nonsense in Mathematica, both from the syntax and the semantic point of view. Please edit your question to explain what you were trying to say with that expression. Further explain how x relates to MyFunc – m_goldberg Aug 11 '17 at 23:51
• Why don't you make up a very simple version of the problem, say two variables, where we can help get the code correct, then later expand to your 10 parameter model. – bill s Aug 12 '17 at 0:30
• Thank you for your comments. I have edited my question. Hopefully, my question is more clear now – Peter Lawrence Aug 13 '17 at 12:16

It's hard to give any advice without knowing anything about your model. If you give even a toy model, and some toy data, representing some of the salient features, you'll get much better answers.

Whenever one has data and a model there's some fun analysis to be done. While on a statistical analysis site, it might be reasonable to discuss what type of analysis to do in the abstract, on a $Mathematica$ focused site people are available to help you solve an actual problem with actual code. This is why right now you're sitting on a bunch of comments from people asking for anything they can sink their teeth into.

To get the ball rolling, I'll make up my own example of model and data and begin poking at it in data rich and data poor environments. I'll use it as an example to introduce the use of FindFit and FindMinimum. There are almost certainly better tools to help you with your model, but in the absence of signal, and given the comments already made, I'm not sure how else to prompt you into providing the type of information that can eventually be used to help.

## Data Rich

To get the conversation started, let's pretend you're accurately sitting on a polynomial:

CalculatePrice[v1_, v2_, v3_, v4_] := v1 + v2^v4 + v3


Let's pretend you have some large set of mildly noisy data so you know how actual price depends on $v_1$ and $v_2$, and for the sake of having a target lets say the data is drawn from a world where $v_3=3$ and $v_4$=5.

actualPrice =
Table[{v1, v2, 3 + v1 + v2^5 + RandomReal[{-1/10, 1/10}]}, {v1, 1,
10, .01}, {v2, 1, 10, .01}] // Flatten[#, 1] &;


How can we find $v_3$ and $v_4$? One way is to try FindFit ala

FindFit[actualPrice, v3 + v1 + v2^v4, {{v3, 2.5}, {v4, 6.2}}, {v1, v2}]


which yields:

{v3 -> 3.00002, v4 -> 5.}


So in this magical contrived example, where we were data rich, $Mathematica$ landed on the signal hidden in the data.

Depending on your model and the type of data FindFit may not get you anywhere close. This is really just an example that happens to work out ok.

How much confidence should you have in your fit? What's the best way to test it? How should you revise your model? All great questions that can be addressed in the context of Mathematica when you offer some notion of the type of game you want to play.

## Data poor

Let's say you only had one data point, where $v_1=3$ and $v_2=5$.

oneDataPoint =
Select[actualPrice, #[[1]] == 3 && #[[2]] == 5 &] // First


(yielding: {3., 5., 3131.02} )

Are you out of luck? Not necessarily. You could always try minimizing something like:

FindMinimum[(CalculatePrice[3, 5, v3, v4] -
oneDataPoint[[-1]])^2, {{v3, 3.5}, {v4, 6.2}}]


yielding:

{8.27181*10^-25, {v3 -> 4.5504, v4 -> 4.97105}}


The relative success is due to the nature of the model introduced and the type of noise involved with that one data point.

Again there are lots of approaches for data poor environments, and people can help you out if you're a little clearer about the nature of your model and your expectations as to the nature of the noise in your data.