How to solve duration/double Error in yearfrac when forecasting prices?

3 views (last 30 days)
When I want to add back the seasonality as shown in the code snippet to the simulated prices I get the following error message below
I attached the format that is used for the Price dates.
Unable to perform assignment because value of type 'duration' is not convertible to 'double'.
Error in yearfrac (line 96)
YearFraction(Ind) = daysact(Date1(Ind), Date2(Ind)) ./...
Error in Test (line 106)
SimPriceTimes = yearfrac(PriceDates(1), SimPriceDates);
Caused by:
Error using duration/double (line 1075)
Undefined function 'double' for input arguments of type 'duration'. To convert from durations to numeric, use
the SECONDS, MINUTES, HOURS, DAYS, or YEARS functions.
% Add back seasonality.
SimPriceDates = PriceDates(end) + days(0:(nPeriods-1))';
SimPriceTimes = yearfrac(PriceDates(1), SimPriceDates);
CSim = seasonMatrix(SimPriceTimes);
logSimPrices = SimPrices + repmat(CSim*seasonParam,1,nTrials);
  2 Comments
Cris LaPierre
Cris LaPierre on 9 May 2021
Edited: Cris LaPierre on 9 May 2021
What do your Price dates represent? Day of year?
What is SimPriceDates supposed to be?
Tobias Poemsl
Tobias Poemsl on 9 May 2021
Yes, Price dates represent the day of year. Sim Price Dates is the timespan the simulation is conducted for (one year) and represents the future simulated paths the price forecast should be projected on

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 10 May 2021
My suggestion is to turn your PriceDates into datetimes. The yearfrac function can work with those.
Do you know what year the data is from? Here's an example assuming 2019.
PriceDates = readmatrix("Timeseries_Electricity.xls");
PriceDates = PriceDates + datetime(2019,1,0)
PriceDates = 365×1 datetime array
01-Jan-2019 02-Jan-2019 03-Jan-2019 04-Jan-2019 05-Jan-2019 06-Jan-2019 07-Jan-2019 08-Jan-2019 09-Jan-2019 10-Jan-2019 11-Jan-2019 12-Jan-2019 13-Jan-2019 14-Jan-2019 15-Jan-2019 16-Jan-2019 17-Jan-2019 18-Jan-2019 19-Jan-2019 20-Jan-2019 21-Jan-2019 22-Jan-2019 23-Jan-2019 24-Jan-2019 25-Jan-2019 26-Jan-2019 27-Jan-2019 28-Jan-2019 29-Jan-2019 30-Jan-2019
nPeriods = 4;
% Add back seasonality.
SimPriceDates = PriceDates(end) + days(0:(nPeriods-1))'
SimPriceDates = 4×1 datetime array
31-Dec-2019 01-Jan-2020 02-Jan-2020 03-Jan-2020
SimPriceTimes = yearfrac(PriceDates(1), SimPriceDates)
SimPriceTimes = 4×1
0.9973 1.0000 1.0027 1.0055

More Answers (0)

Categories

Find more on Price and Analyze Financial Instruments in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!