How to set input in a dynamic way, by means of indirect references to an object or other variable

5 views (last 30 days)
Hello to everybody,
I created a curve object "CurveName" with IRDataCurve and then I derived a rate structure from it by using toRateSpec function. Since I have several curves to choose among depending on the type of instrument to calculate the present value for, I need to set the name of the curve in a dynamic way in order to assign different curves to the DiscFactorsCurveName variable and maintain constant the script.
CurveName is the curve object (discount type)
Dates is the vector of input dates
DiscFactorsCurveName=toRateSpec(CurveName,Dates);
I need to assign a different curve object to the CurveName, for example Curve1, Curve2, Curve3, etc...
Have you any suggestions please?
Thank you very much in advance.
Angelo
  5 Comments
Steven Lord
Steven Lord on 19 Mar 2024
maintain constant the script.
If you're writing this code as a script file, I'd consider converting it into a function file. That way, whatever name you use inside the function will be completely independent of what name(s) you use outside.
% part 1 of example
x1 = [1 2 3 4];
p = myprod(x1)
p = 24
x2 = 5:8;
p = myprod(x2)
p = 1680
x3 = [9 10; 11 12];
p = myprod(x3)
p = 11880
% Part 2 of example
X = randperm(12)
X = 1×12
12 4 11 9 1 6 3 7 10 2 5 8
for k = 1:4:12
fprintf("Taking the product of X(%d:%d) = %s\n", k, k+3, mat2str(X(k:k+3)))
p = myprod(X(k:k+3))
end
Taking the product of X(1:4) = [12 4 11 9]
p = 4752
Taking the product of X(5:8) = [1 6 3 7]
p = 126
Taking the product of X(9:12) = [10 2 5 8]
p = 800
function theOutput = myprod(theInput)
theOutput = prod(theInput(:));
end
Note that I did use numbered variable names x1, x2, and x3 in the first part of the example. I did that for illustration purposes only. When I wanted to "automate" this, in the second, I created "unnamed" temporary variables using indexing to pass into myprod. Note that nowhere outside myprod did I create any variables named theInput or theOutput, but MATLAB was able to handle passing the data into and out from the function just fine.
Angelo Amoroso
Angelo Amoroso on 28 Mar 2024
Hi Stephen and Steven,
I'm sorry not to have reply you earlier, thank you for your suggestion. I'm aware my question was a bit high-level as you noticed. I'm not so expert to solve this kind of issues, even if them may appear simple to you.
I wrote a script to create curve object for each curve I may use, then a schedule of dates for a financial instrument. The forward step is to associate to the dates discount factors and forward rates from the curve objects created.
How can I handle an indexation or/and a "cycle for" solution to chose one of the n curves for every operation to calculate? I imagine to start from an excel input file like the one attached.
Is it possible to calculate as a matrix composed of several operation for which to calculate cash flows profiles of different lenghts or I have to use a cycle for to calculate one by one?
Please let me know if I didn't manage to be clear and many thanks to you all for your support.
Kind regards
Angelo

Sign in to comment.

Answers (1)

Shivam
Shivam on 17 Mar 2024
Hello Angelo,
Based on your shared information, it seems you are interested in dynamically allocating various IRDataCurve objects and their corresponding RateSpec objects to distinct variables.
A systematic way to handle this would be to use MATLAB's structures to store and retrieve these objects. This method lets you dynamically assign names and efficiently organize multiple curves with their respective RateSpec objects.
You can have a look at the example code that demonstrates how IRDataCurve and RateSpec objects are stored in structures under dynamically generated names:
curve = struct();
rateSpec = struct();
for i = 1:2
curveName = sprintf('Curve%d', i); % Curve will be created as Curve1, Curve2, Curve2, etc.
% Creating dummy data
CurveSettle = datetime(2016,3,2);
Data = [2.09 2.47 2.71 3.12 3.43 3.85 4.57 4.58]/100;
Dates = datemnth(CurveSettle,12*[1 2 3 5 7 10 20 30]);
% Create IRDataCurve object dynamically
curve.(curveName) = IRDataCurve('Zero',CurveSettle,Dates,Data,'Compounding',4,'Basis',4);
% Create rateSpec object dynamically
rateSpecName = sprintf('RateSpec%d', i);
rateSpec.(rateSpecName) = toRateSpec(curve.(curveName), Dates);
end
Also, you can access a specific object as follows:
selectedCurve = curve.Curve2;
selectedRateSpec = rateSpec.RateSpec2;
You can refer to the documentation provided to know more about 'IRDataCurve' and 'toRateSpec':
I hope it helps.
  3 Comments
Angelo Amoroso
Angelo Amoroso on 18 Mar 2024
Edited: Angelo Amoroso on 18 Mar 2024
Hi Shivam,
thank you for your suggestion, it seems to me very intersting. Do you think this solution may apply in case I mean to take the input from an excel file where for every deal I indicate a curve name to use?I'll try to apply and then let you know.
Thank you very much
Shivam
Shivam on 19 Mar 2024
Hi Stephen,
In the case where you provide a specific curve name rather than using the generic Curve1, Curve2, and so forth, the described approach remains effective. Rather than dynamically generating the name, you have the option to store the curve objects under the names you provide. If this doesn't solve the problem, please do let me know.

Sign in to comment.

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!