Serving the Quantitative Finance Community

 
User avatar
Darrell1977
Topic Author
Posts: 3
Joined: August 21st, 2002, 4:03 pm

easy mathematica question

October 2nd, 2003, 8:10 pm

Can somebody help me? This is probably easy but I couldn't find it out yet.how do I get mathematica to display the fitted parameters (using the function NonlinearFit) in the form a-> ..., b-> ..., etc.?the exact code looks like this (sorry it's a bit messy)Needs["Statistics`NonlinearFit`"]NonlinearFit[ {{0.5, .180253 }, {1, 0.191478}, {1.5, .186154}, {2, 0.177294}, \{2.5, .167887}, {3, .158123}, {3.5, 0.152688}, {4, 0.148709} , {4.5, 0.144703} , { 5, 0.141259}, {5.5, 0.137982} , {6, 0.134708}, {6.5, 0.131428}, {7, \0.128148}, {7.5, 0.1271} , {8, 0.126822}, {8.5, 0.126539}, {9, 0.126257}, {9.5, 0.12597}}, 1/(4*c^3)*(b^2 + 2*a*b*c + 2*(a^2)*(c^2) + \8*b*c*d + 8*a*(c^2)*d + 4*(c^3)*(d^2)*t) - Exp[-c*t]*(8* b*c*d + 8*a*(c^2)*d + 8*b*(c^2)*d*t) - Exp[-2*c*t]*((b^2) + 2*a* b*c + 2*(a^2)*(c^2) + 2*(b^2)*c*t + 4*a*b*(c^2)*t + 2*(b^2)*( c^2)*(t^2)) , t, {a, b, c, d}, MaxIterations -> 10000]Thanks!
 
User avatar
Liguo
Posts: 0
Joined: February 7th, 2003, 6:01 am

easy mathematica question

October 2nd, 2003, 8:32 pm

Why don't you use NonlinearRegress? It is much easier. Try the following:BestFitParameters /. NonlinearRegress[data, 1/(4*c^3)*(b^2 + 2*a*b*c + 2*(a^2)*(c^2) + 8*b*c*d + 8*a*(c^2)*d + 4*(c^3)*(d^2)*t) - Exp[-c*t]*(8*b*c*d + 8*a*(c^2)*d + 8*b*(c^2)*d*t) - Exp[-2*c*t]*((b^2) + 2*a*b*c + 2*(a^2)*(c^2) + 2*(b^2)*c*t + 4*a*b*(c^2)*t + 2*(b^2)*(c^2)*(t^2)), t, {a, b, c, d}, RegressionReport -> BestFitParameters, MaxIterations -> 10000]data is defined as your input data.If you cannot use NonlinearRegress, you can still use the element of the return result to arrange the form of the rule. For example, result = a Exp;result[[1]] (* is a *)result[[2, 2, 1]] (* is b *)This last method is really a dirty and messy way to go, though.Let me know if you have any other problem.
 
User avatar
Darrell1977
Topic Author
Posts: 3
Joined: August 21st, 2002, 4:03 pm

easy mathematica question

October 3rd, 2003, 2:36 pm

Thank you for the fast reply. I'm not sure if I made myself clear.I seem to be using the function NonlinearFit alright. I get a result but I get it in a way where it is hard to extract what the 4 parameters a,b,c,d are. Therefore I would like to know how to obtain the same result but by the parameter substitution a-> ..., b->...,c->I also made a little mistake with the brackets. The real code goes like thisNeeds["Statistics`NonlinearFit`"]NonlinearFit[ {{0.5, .180253 }, {1, 0.191478}, {1.5, .186154}, {2, 0.177294}, \{2.5, .167887}, {3, .158123}, {3.5, 0.152688}, {4, 0.148709} , {4.5, 0.144703} , { 5, 0.141259}, {5.5, 0.137982} , {6, 0.134708}, {6.5, 0.131428}, {7, \0.128148}, {7.5, 0.1271} , {8, 0.126822}, {8.5, 0.126539}, {9, 0.126257}, {9.5, 0.12597}}, 1/(4*c^3)*(((b^2) + 2*a*b*c + 2*( a^2)*(c^2) + 8*b*c*d + 8*a*(c^2)*d + 4*(c^3)*(d^2)*t) - Exp[-c*t]*( 8*b*c*d + 8*a*(c^2)*d + 8*b*(c^2)*d*t) - Exp[-2*c*t]*((b^2) + 2* a*b*c + 2*(a^2)*(c^2) + 2*(b^2)*c*t + 4*a*b*(c^2)*t + 2*(b^2)*(c^2)*( t^2)) ), t, {a, b, c, d}, MaxIterations -> 10000]I hope this is readable at all.Thanks!
 
User avatar
Liguo
Posts: 0
Joined: February 7th, 2003, 6:01 am

easy mathematica question

October 3rd, 2003, 3:01 pm

Did you tried the NonlinearRegress method? It did exactly what you wanted.Here is the result:{a -> 1.197071784342699`, b -> -1.606134993286988`, c -> 3.2666722896250926`, d -> 0.0008110958925133949`}
 
User avatar
Darrell1977
Topic Author
Posts: 3
Joined: August 21st, 2002, 4:03 pm

easy mathematica question

October 3rd, 2003, 3:55 pm

Thanks again. You convinced me to look at NonlinearRegress. I slightly changed the code otherwise it's the same. I'm getting an error message on the NonlinearRegress function version:1) VolModel = Integrate[((a + b*(T - t))*Exp[-c*(T - t)] + d)*((a + b*(T - t))* Exp[-c*(T - t)] + d), {t, 0, T}]2) VolData = {{0.5, .180253}, {1, 0.191478}, {1.5, .186154}, {2, 0.177294}, { 2.5, .167887}, {3, .158123}, {3.5, 0.152688}, {4, 0.148709}, {4.5, 0.144703}, {5, 0.141259}, {5.5, 0.137982}, {6, 0.134708}, {6.5, 0.131428}, {7, 0.128148}, {7.5, 0.1271}, {8, 0.126822}, {8.5, 0.126539}, {9, 0.126257}, {9.5, 0.12597}}3) BestFitParameters /. NonlinearRegress[VolData, VolModel, T, {a, b, c, d}, RegressionReport -> BestFitParameters, MaxIterations -> 10000]If I substitute 3) Needs["Statistics`NonlinearFit`"]NonlinearFit[VolData, VolModel, T, {a, b, c, d}, RegressionReport -> \BestFitParameters, MaxIterations -> 10000]instead of 3) I get a result but no replacement rules. I looked through the help files but cannot find the error.Sorry if I'm a bit slow...
 
User avatar
Liguo
Posts: 0
Joined: February 7th, 2003, 6:01 am

easy mathematica question

October 3rd, 2003, 8:32 pm

Try to do Needs["Statistics`NonlinearFit`"]before you run the NonlinearRegress. Just like NonlinearFit, NonelinearRegress also needs Statistics`NonlinearFit`.If this doesn't solve your problem, check the version of your Mathematica. It works in Mathemtica 4.2.1.0.
 
User avatar
Darrell1977
Topic Author
Posts: 3
Joined: August 21st, 2002, 4:03 pm

easy mathematica question

October 4th, 2003, 7:46 am

Unfortunately even then I get this error message:I just wrote << Statistics`NonlinearFit`before the BestFitParameters part.ReplaceAll::"reps": "{<< 1 >>} is neither a list of replacement rules nor a valid dispatch table, and so cannotbe used for replacing."My version is 4.2.0.0.
 
User avatar
Liguo
Posts: 0
Joined: February 7th, 2003, 6:01 am

easy mathematica question

October 4th, 2003, 3:50 pm

Please see the warning at the end!!!!!Sorry. I cannot reproduce your error in the following versions of Mathematica: 4.0 Linux, 4.2.0 Linux, 4.2.1 Linux, and 5.0.0 Win32.The error message you get is due to the failure of NonlinearRegress. I accidentally found out this when I failed to define VolDataBut. After I redefine the VolData, it works and gives me the same results as before. Please restart the math kernel, and copy and paste the following codes.<< Statistics`NonlinearFit`;VolModel = Integrate[((a + b*(T - t))*Exp[-c*(T - t)] + d)*((a + b*(T - t))* Exp[-c*(T - t)] + d), {t, 0, T}];VolData = {{0.5, .180253}, {1, 0.191478}, {1.5, .186154}, {2, 0.177294}, { 2.5, .167887}, { 3, .158123}, {3.5, 0.152688}, {4, 0.148709}, {4.5, 0.144703}, {5, \0.141259}, {5.5, 0.137982}, {6, 0.134708}, {6.5, 0.131428}, {7, 0.128148}, { 7.5, 0.1271}, {8, 0.126822}, {8.5, 0.126539}, {9, 0.126257}, { 9.5, 0.12597}};Param=BestFitParameters /. NonlinearRegress[VolData, VolModel, T, {a, b, c, d}, \RegressionReport -> BestFitParameters, MaxIterations -> 10000]WARNING: Even the above codes work, but the result it generate is far from satisfying. Try ListPlot[VolData] and Plot[VolModel/.Param, {T,0,10}] and you will see. Two suggestions: 1. Use a better data fitting tools (or MatLab); 2. Choose better starting values for your parameters. This can be done by replacing {a,b,c,d} with {{a,1}, {b,2}, {c,3}, {d,4}}. See the help of NonlinearRegress for details.Overall, curvefitting is no easy task. I am so glad that I only waste one year of my life for it.
 
User avatar
Darrell1977
Topic Author
Posts: 3
Joined: August 21st, 2002, 4:03 pm

easy mathematica question

October 5th, 2003, 10:19 am

Thank you very much for your answers and your patience. After pasting the code I still get the error message. I will have to rethink the whole procedure. Also I wasn't aware that Matlab is better than Mathematica for curve fitting. Maybe I will use Matlab then. Apparently an upgrade of the version of Mathematica might also help. Anyways I have a couple of things to work on ... :-)Thanks again.
 
User avatar
jfuqua
Posts: 6
Joined: July 26th, 2002, 11:41 am

easy mathematica question

March 4th, 2006, 2:40 pm

GREEKS in Mathematica or other packages. Can Mathematica, Maple or others do something as simple as get the derivatives of Black-Scholes [e.g. Delta] in the very simple form that they can come out as, e.g. functions of Normal(d2). Instead they return long formulas with functions of ERF and other unresolved elements where as by hand the formulas come out very simple functions. I played with all kinds of 'hints' for the packages but they kept returning much longer derivations than were necessary. I finally gave up on trying to use the packages for that---even if accurate the formulas they give would still not be effective code. Last I spoke to Mathematica and Maple staff [several years ago], they did not know or seem to be interested in this type of problem.
 
User avatar
MCarreira
Posts: 64
Joined: January 1st, 1970, 12:00 am

easy mathematica question

August 8th, 2006, 7:16 pm

When working with Mathematica I had the same problem jfuqua described, one has to replace formulas manually to end with "nice" expressions. Auxiliary functions are needed to simplify the exponentials, and checking whether one expression is the same as another is not as simple as it seems. I'm not strong at pattern matching and functional programming though, so maybe there's some way of recognizing patterns like Erf[k1*x] and replacing it with a Normal[k2*x], I haven't tried that hard enough yet.