Hello,
I am currently trying to plot some kind of term structure of Value at Risk estimates and I am struggling to fit the xticks correctly. My code correctly displays 500 days with the approriate spaces between the "days", but I don't get how to match my datapoints (6 in total) to the right period (i. e. 10 days estimate to 10 days point, 30 days estimate to 30 days point and so on). I tried a different approach with datetime, but then it is impossible to just display days and not specific dates.
TermStructure = [VaR1D; VaR10D; VaR30D; VaR125D; VaR1Y; VaR2Y]
x = linspace(0,500,length(TermStructure))
plot(x, TermStructure)
xticks([1 10 30 125 250 500])
Thanks a lot in advance for any suggestions.
Leo

 Respuesta aceptada

dpb
dpb el 7 de Feb. de 2021
Editada: dpb el 7 de Feb. de 2021

0 votos

x=[1 10 30 125 250 500];
plot(x,sort(randi(40,[6,1])),'*-')
xticks(0:100:500)
will duplicate the above with the addition of a marker at the datapoints instead of just the line segments. Salt to suit...

Más respuestas (1)

dpb
dpb el 7 de Feb. de 2021
Editada: dpb el 7 de Feb. de 2021

0 votos

"with datetime, but then it is impossible to just display days and not specific dates."
Au contraire, good buddy!
tt=array2timetable(rand(100,1),'RowTimes',datetime(date)+days(linspace(0,500)).'); % make up something
>> head(tt)
ans =
8×1 timetable
Time Var1
____________________ _______
07-Feb-2021 00:00:00 0.16931
12-Feb-2021 01:12:43 0.78235
17-Feb-2021 02:25:27 0.34228
22-Feb-2021 03:38:10 0.54597
27-Feb-2021 04:50:54 0.55591
04-Mar-2021 06:03:38 0.20465
09-Mar-2021 07:16:21 0.59334
14-Mar-2021 08:29:05 0.93012
>> figure; plot(tt.Time,tt.Var1) % and plot it
>> hAx=gca; % get axes handle
>> hAx.XAxis.TickLabelFormat % display default time axes format
ans =
'MMM u'
>> hAx.XAxis.TickLabelFormat='dd'; % set to days, two digits
yields
This time scale happens to have default tick spacing at beginnings of months so all are day one, but it shows only the day.
You may be wanting a duration here, not a date if is elapsed days since some point.
Then again, it isn't clear to me what "match my datapoints (6 in total) to the right period" really means, either; if it is that you have a set of calculations (6?) to some calculated time frame over which those were calculated, you may not want a time axes at all but simply an ordinal plot with labels to match the period lengths.
TermStructure = randi(300,[6,1]);
x=categorical({'1Day'; '10Days'; '30Days; '125Days'; '1Year'; '2Years'}, ...
{'1Day'; '10Days'; '30Days'; '125Days'; '1Year'; '2Years'},'ordinal',1));
plot(x, TermStructure)

5 comentarios

Leonard Meyer
Leonard Meyer el 7 de Feb. de 2021
This might be what I'm looking for, but can you tell me what exactly you are doing in the line with "make something up"? I get the error, that you cannot plot timetables or something. I always struggle with anything related to datetime...
Thanks a lot so far!
Adam Danz
Adam Danz el 7 de Feb. de 2021
It's not clear to me what the goal is or the type of data you're working with.
Do you want durations along the x axis? What determines those durations?
Leonard Meyer
Leonard Meyer el 7 de Feb. de 2021
I definitely don't want a simple ordinal plot, since that defies the correct time proportions.
"You may be wanting a duration here, not a date if is elapsed days since some point". Elapsed days since day 0 would be it, I think.
I attached an excel solution below, but I would prefer it to be done in Matlab for future projects as well!
Leonard Meyer
Leonard Meyer el 7 de Feb. de 2021
I'd like to plot 6 values for the Value at risk, that have been calculated via monte carlo simulation and refer to a specific time horizon each (10 days, 30 days...). So the x-axis should just have those 500 days with the correct proportions for the respective VaR estimates. I hope that makes it clearer, thanks again!
dpb
dpb el 7 de Feb. de 2021
"what exactly you are doing in the line with "make something up"
Just created a dummy timetable with one variable over a time period of 100 entries spread out over 500 days. It was so had something to use in plot command below.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2020b

Preguntada:

el 7 de Feb. de 2021

Editada:

dpb
el 7 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by