problem with curvefit on matlab

  • Thread starter Thread starter mouna
  • Start date Start date
Joined
11/3/08
Messages
18
Points
11
i have the following function:
R(T) = beta - ((lamda * sigma)/ alpha) + (r0 - (beta - ((lamda * sigma)/ alpha)) *( 1- exp (- beta*T))/ (beta *T) + ( (sigma^2)*(1- exp (- beta*T))^2)/ (4*(beta^3)*T)

where lamda is the only unknown because alpha, beta et sigma are already estimated using GMM. the result are (Alpha = 5.1356%, beta = 0.1374, sigma = 0.0118)
the only parameter lamda will be estimated using the fitting of the function to the data.

where the real data of R (T) is the vector y = [0.079 0.0782 0.0791 0.0793 0.0791 0.0802 0.0798]
and T is the maturity present in the vector x = [0.5 1 2 3 5 7 10]
i have make a curvefitting with the curve fitting toolbox but the result was so bad: as follow: where a is lamda

General model:
f(x) = (0.1374-(a*0.0118/0.051356)-((0.0118^2)/(2*(0.051356^2))))+(0.05-
(0.1374-(a*0.0118/0.051356)-((0.0118^2)/(2*(0.051356^2)
))))*((1-exp(-0.1374*x))/(0.1374*x))+(((0.0118^2)/(4*(0.1374^3)
*x))*(1-exp(-0.1375*x))^2)
Coefficients (with 95% confidence bounds):
a = -0.1382 (-0.4263, 0.1499)

Goodness of fit:
SSE: 0.002049
R-square: -846.7
Adjusted R-square: -846.7
RMSE: 0.01848

Warning: A negative R-square is possible if the model
does not contain a constant term and the fit
is poor (worse than just fitting the mean).
Try changing the model or using a different StartPoint.

I have read in the help of matlab that i can use the function lsqcurvefit to compute exactly what i need. but the help wasn't so clear about how to use it.
please can you help me . i rely so much on your help. My knowledge about programming is so poor.
 
You have missing parenthesis in the formula for R(T), so it is not possible to try fitting the data you provided; you also need to write down the value of r0 parameter.
 
You have missing parenthesis in the formula for R(T), so it is not possible to try fitting the data you provided; you also need to write down the value of r0 parameter.
thank you very much for your relply.
you have raison, i have missed a part of the formula of R(T):-ssand also the value of r0

R(T) = (beta - ((lamda * sigma)/ alpha)- ((sigma^2)/ (2*(alpha^2))) + (r0 - (beta - ((lamda * sigma)/ alpha)- ((sigma^2)/ (2*(alpha^2))) *( 1- exp (- beta*T))/ (beta *T) + ( (sigma^2)*(1- exp (- beta*T))^2)/ (4*(beta^3)*T)

where lamda is the only unknown because alpha, beta et sigma are already estimated using GMM. the result are (Alpha = 5.1356%, beta = 0.1374, sigma = 0.0118)
the only parameter lamda will be estimated using the fitting of the function to the data.

where the real data of R (T) is the vector y = [0.079 0.0782 0.0791 0.0793 0.0791 0.0802 0.0798]
and T is the maturity present in the vector x = [0.5 1 2 3 5 7 10]
r0 = 0.05.
 
Now you have not one, but three parentheses missing in your formula for R(T)...

In any case - I don't have Matlab copy available, but I do have Mathematica installed on my machine, so I thought running your data through Mathematica regression routine. So if I try with R(T) formula you provided above, and with three parentheses just added at the end of formula, then here is the sequence of Mathematica commands that would generate model, and plot the model against input data:
Code:
alpha = 5.1356*1/100; beta = 0.1374; sigma = 0.0118; r0 = 0.05;
data = Transpose[{{0.5, 1, 2, 3, 5, 7, 10}, {0.079, 0.0782, 0.0791, 
     0.0793, 0.0791, 0.0802, 0.0798}}];
model = NonlinearModelFit[
   data, (beta - ((lamda*sigma)/
       alpha) - ((sigma^2)/(2*(alpha^2))) + (r0 - (beta - ((lamda*
             sigma)/alpha) - ((sigma^2)/(2*(alpha^2)))*(1 - 
             Exp[-beta*T])/(beta*
             T) + ((sigma^2)*(1 - Exp[-beta*T])^2)/(4*(beta^3)*
            T)))), {lamda}, T];
Show[Plot[model[x], {x, 0, 10}, AxesOrigin -> {0, 0}, 
  PlotRange -> {0, 0.1}], ListPlot[data]]
The result is as in attached picture, which doesn't seem good indeed; but Mathematica complains during the NonlinarModelFit[] calculation, so I guess if you could start with some kind of constraints upon "lamda" value, that could be helpful too...
 

Attachments

  • fit.png
    fit.png
    5.6 KB · Views: 13
thank you very very much for your reply. i don't have mathematica but i have matlab. i will try to covert this program to matlab language
 
I did lots of furve-fitting in my Engineering PhD study, and the nonlinearity of function is very high. Sometimes the function may includes integration or differentiation. I seldom used curve-fitting function in Matlab. I always used either FMINCON or FMINSEARCH to do fitting via optimization. You have more controls in optimization options.
 
Now you have not one, but three parentheses missing in your formula for R(T)...

In any case - I don't have Matlab copy available, but I do have Mathematica installed on my machine, so I thought running your data through Mathematica regression routine. So if I try with R(T) formula you provided above, and with three parentheses just added at the end of formula, then here is the sequence of Mathematica commands that would generate model, and plot the model against input data:
Code:
alpha = 5.1356*1/100; beta = 0.1374; sigma = 0.0118; r0 = 0.05;
data = Transpose[{{0.5, 1, 2, 3, 5, 7, 10}, {0.079, 0.0782, 0.0791, 
     0.0793, 0.0791, 0.0802, 0.0798}}];
model = NonlinearModelFit[
   data, (beta - ((lamda*sigma)/
       alpha) - ((sigma^2)/(2*(alpha^2))) + (r0 - (beta - ((lamda*
             sigma)/alpha) - ((sigma^2)/(2*(alpha^2)))*(1 - 
             Exp[-beta*T])/(beta*
             T) + ((sigma^2)*(1 - Exp[-beta*T])^2)/(4*(beta^3)*
            T)))), {lamda}, T];
Show[Plot[model[x], {x, 0, 10}, AxesOrigin -> {0, 0}, 
  PlotRange -> {0, 0.1}], ListPlot[data]]
The result is as in attached picture, which doesn't seem good indeed; but Mathematica complains during the NonlinarModelFit[] calculation, so I guess if you could start with some kind of constraints upon "lamda" value, that could be helpful too...

inspired from the program you have make on mathematica. i have try to make the same for an other model. please verify if it is true or false.
the model has the form:

R(T) = (B(T) / T) * r0-[(( theta + gamma) / 2* beta * apha)* (In (A(T)) / T]

with
gamma = beta - lamda * simga
theta = ((gamma^2) + 2* (simga^2))^0.5

A(T) =[ ( 2* theta*exp((gamma+theta)/(T/2))) / ((gamma + theta)* (exp(theta*T)-1) + (2 * theta))]^[(theta + gamma)* ((2 * beta * alpha)/ (gamma + theta))/(simga^2)]
B(T) = (2 * (exp(theta*T) -1)) / [ ((gamma+theta) * (exp( theta* T)- 1))+ (2* theta)]
and the parameters alpha = 5.1329%, beta = 0.1669, sigma = 0.0507. and r0 = 0.012
then the program will be on the form?

alpha = 5.1329*1/100; beta = 0.1669; sigma = 0.0507; r0 = 0.012;
gamma = beta - lamda * simga;
theta = ((gamma^2) + 2* (simga^2))^0.5;

A(T) =[ ( 2* theta*exp((gamma+theta)/(T/2))) / ((gamma + theta)* (exp(theta*T)-1) + (2 * theta))]^[(theta + gamma)* ((2 * beta * alpha)/ (gamma + theta))/(simga^2)];

B(T) = (2 * (exp(theta*T) -1)) / [ ((gamma+theta) * (exp( theta* T)- 1))+ (2* theta)];

data = Transpose[{{0.5, 1, 2, 3, 5, 7, 10}, {0.079, 0.0782, 0.0791,
0.0793, 0.0791, 0.0802, 0.0798}}];
model = NonlinearModelFit[
data, (B(T) / T) * r0-[(( theta + gamma) / 2* beta * apha)* (In (A(T)) / T];
Show[Plot[model[x], {x, 0, 10}, AxesOrigin -> {0, 0},
PlotRange -> {0, 0.1}], ListPlot[data]]


 
Sorry, but I'm afraid I don't understand your intention here... The sequence of statements your wrote above is not valid Mathematica code - Mathematica has rather strict syntax, for example all types of parentheses have complete distinct purpose etc. So if you don't know Mathematica beforehand, then it's probably better to stick with Matlab, especially as capabilities of both packages in the domain of fitting are similar. I just thought, initially, I can run your data through Mathematica routine, as a quick check and with some hope that good fit may be generated that you could eventually use to come to something alike in Matlab. However, as mentioned by featips above, the type of function to fit to is rather complex, and CAS packages are sometimes simply not able to handle that kind of functions, and other times it may take some time and experimentation to find the right function to use. So I'd strongly suggest you follow his advice and try the other approach he suggested (with FMINCON or FMINSEARCH).
 
Back
Top Bottom