Derivative not working plot
1 view (last 30 days)
Show older comments
Hello. My %Local Risk Aversion Plot is not working. I am trying to plot the risk aversison (y) over monthly expense (x).
Risk aversion = second derivative of curve y / derivative of curve y
clear;
clc;
close all;
% Curve Fit
x = [6500 6350 6000 5400 5200 4500];
y = [0 0.25 0.5 0.75 0.875 1.0];
theFit=fit(x',y','exp2')
plot(theFit,x,y)
% Monthly Cost
cost = [6500 6350 6000 5400 4500];
costUtility = [0 0.25 0.5 0.75 1.0];
% Plot Utility Points
figure;
plot(cost,costUtility,'o','MarkerFaceColor','b');
xlim([4500 6500]);ylim([0 1.2]);
yticks([costUtility 1.2]);
grid on;
xlabel('Monthly Expense ($)');
ylabel('Utility');
% Add utility curve fit
a = -2.914e-07;
b = 0.002246;
c = 2.751;
d = -0.0002216;
curveX = linspace(4500,6500);
curveY = a*exp(b*curveX) + c*exp(d*curveX);
hold on;
plot(curveX,curveY,'Color','b');
% Consistency Check
% New Lottery
costCheck = 5100;
costUtilityCheck = 0.875;
plot(costCheck,costUtilityCheck,'o','Color','r','MarkerFaceColor','r');
legend('Utility Lottery','Utility Curve Fitting','Consistency Check');
% Local Risk Aversion
x_q = 45000:1:6500;
q = (a*b^2*exp(b.*x_q) + c*d^2*exp(d.*x_q)) ./ (a*b*exp(b.*x_q) + c*d*exp(d.*x_q));
figure;
plot(x_q,q);
grid on;
xlabel('Monthly Expense ($)');
ylabel('Risk Aversion (q(x))');
0 Comments
Accepted Answer
Dyuman Joshi
on 18 Nov 2023
Edited: Dyuman Joshi
on 18 Nov 2023
Because x_q is empty, thus q is also empty. And plotting empty arrays leads to empty figures.
a = -2.914e-07;
b = 0.002246;
c = 2.751;
d = -0.0002216;
% Local Risk Aversion
x_q = 45000:1:6500
q = (a*b^2*exp(b.*x_q) + c*d^2*exp(d.*x_q)) ./ (a*b*exp(b.*x_q) + c*d*exp(d.*x_q))
Given the values used above in the code, xq should be -
x_q = 4500:1:6500
More Answers (2)
Torsten
on 18 Nov 2023
Edited: Torsten
on 18 Nov 2023
clear;
clc;
close all;
% Curve Fit
x = [6500 6350 6000 5400 5200 4500];
y = [0 0.25 0.5 0.75 0.875 1.0];
theFit=fit(x',y','exp2')
plot(theFit,x,y)
% Monthly Cost
cost = [6500 6350 6000 5400 4500];
costUtility = [0 0.25 0.5 0.75 1.0];
% Plot Utility Points
figure;
plot(cost,costUtility,'o','MarkerFaceColor','b');
xlim([4500 6500]);ylim([0 1.2]);
yticks([costUtility 1.2]);
grid on;
xlabel('Monthly Expense ($)');
ylabel('Utility');
% Add utility curve fit
a = theFit.a;
b = theFit.b;
c = theFit.c;
d = theFit.d;
curveX = linspace(4500,6500);
curveY = a*exp(b*curveX) + c*exp(d*curveX);
hold on;
plot(curveX,curveY,'Color','b');
% Consistency Check
% New Lottery
costCheck = 5100;
costUtilityCheck = 0.875;
plot(costCheck,costUtilityCheck,'o','Color','r','MarkerFaceColor','r');
legend('Utility Lottery','Utility Curve Fitting','Consistency Check');
% Local Risk Aversion
x_q = 4500:1:6500;
q = (a*b^2*exp(b.*x_q) + c*d^2*exp(d.*x_q)) ./ (a*b*exp(b.*x_q) + c*d*exp(d.*x_q));
figure;
plot(x_q,q);
grid on;
xlabel('Monthly Expense ($)');
ylabel('Risk Aversion (q(x))');
0 Comments
Alex Sha
on 19 Nov 2023
Unfortunately, the "exp2" fitting result below given by Matlab is not correctly.
theFit =
General model Exp2:
theFit(x) = a*exp(b*x) + c*exp(d*x)
Coefficients (with 95% confidence bounds):
a = -2.914e-07 (-8.891e-06, 8.309e-06)
b = 0.002246 (-0.002157, 0.006648)
c = 2.751 (-4.377, 9.878)
d = -0.0002216 (-0.0007844, 0.0003413)
The right solution should be:
Sum Squared Error (SSE): 0.00221359211819696
Root of Mean Square Error (RMSE): 0.0210408750682901
Correlation Coef. (R): 0.998229714367421
R-Square: 0.996462562646063
Parameter Best Estimate Std. Deviation Confidence Bounds[95%]
--------- ------------- -------------- --------------------------------
a -1.22298139733268E-22 3.10616723815539E-21 [-3.9589895012534E-20, 3.93452987330675E-20]
b 0.00759142454398694 0.00389884818712735 [-0.0419481387569168, 0.0571309878448906]
c 11.4185970882907 4.74945964529719 [-48.9290095510551, 71.7662037276365]
d -0.000545792208874116 8.65259412243978E-5 [-0.00164520853306153, 0.0005536241153133]
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!