Who can help me to run this code in matlab?

  • Context: MATLAB 
  • Thread starter Thread starter xuej1112
  • Start date Start date
  • Tags Tags
    Code Matlab
Click For Summary

Discussion Overview

The discussion revolves around running a MATLAB code related to the Vasicek model for bond pricing, as presented in a specific textbook. Participants are addressing discrepancies between the expected results from the book and the actual output from the code.

Discussion Character

  • Technical explanation, Debate/contested, Homework-related

Main Points Raised

  • One participant reports that their output is a decreasing curve, contrary to the increasing curve shown in the book.
  • Another participant suggests changing the sign of the exponential to achieve an increasing result, indicating a potential issue with the code.
  • A third participant notes that the code runs properly on their end and speculates that there may be typos in the original code provided by the first poster.
  • The first poster clarifies that the code is based on the Vasicek model as described in a specific textbook, providing context for the problem.
  • There is a request for more precise information about the problem to facilitate assistance.

Areas of Agreement / Disagreement

Participants express differing views on the cause of the discrepancy in results, with no consensus reached on the correct approach to resolve the issue. Some believe it may be a coding error, while others suggest a misunderstanding of the model.

Contextual Notes

Unresolved issues include potential typos in the code, the specific definitions and parameters used in the Vasicek model, and the assumptions underlying the PDE being solved.

Who May Find This Useful

Readers interested in MATLAB programming, financial modeling, or the Vasicek model for bond pricing may find this discussion relevant.

xuej1112
Messages
17
Reaction score
0
who can help me to run this code in matlab? I run it in my computer but the result is totally different from the book! In the book,the image is a increase curve,but my result is a decrease curve.So, is there something wrong of my MATLAB?

code:

syms r P A B t;
P = exp(A+B*r);
disp('first derivative of P with respect to r')
diff(P,r)
disp('second derivative of P with respect to r')
diff(diff(P,r))

syms f B A s k m p r t DA DB
f1=0.5*B^2*s^2+B*(k*(m-r)-p*s)-DA-DB*r-r
collect(f1,r)

dsolve('-B*k-DB-1=0','B(0)=0')
B = -1/k+exp(-k*t)/k;
f2=1/2*B^2*s^2+B*(k*m-p*s)-DA
dsolve('1/2*(-1/k+exp(-k*t)/k)^2*s^2+(-1/k+exp(-k*t)/k)*(k*m-p*s)-DA=0','A(0)=0')

tau=(1:1:60)';
nTau=length(tau);
p=0.20;s=0.25;k=0.30;theta=5;r=6;
% Calculate A, B and the yield curve;
B=(exp(-k.*tau)-1)./k;
A=B.*(((p*s)/k)-theta)-theta.*tau+(p.*s.*tau)./k+(s^2/(4*k)).*(-(B.^2)+(2*exp(-k.*tau)-2)./(k^2)+(2.*tau)./k);
y=-(1./tau).*(A+B.*r);
figure;
plot(tau,y)
 
Physics news on Phys.org
I would say change the sign of the exponential, it increases. There is no way that anybody can help you on this unless you state more precisely what the problem is.
 
thank you! the problem is about a example of Vasicek model.it is in the book"Strategic Asset Allocation in Fixed Income Markets A Matlab based user's guide",page 77.It wants to solve a PDE of Vasicek model for bond pricing.
 
Well, the code is running properly, and I don't have the book so my guess goes with some typos in the code... Try to comment some of the portions of the code and check by hand
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
0
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K