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

4 次查看(过去 30 天)
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 个评论
Cris LaPierre
Cris LaPierre 2021-5-9
编辑:Cris LaPierre 2021-5-9
What do your Price dates represent? Day of year?
What is SimPriceDates supposed to be?
Tobias Poemsl
Tobias Poemsl 2021-5-9
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

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2021-5-10
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Price and Analyze Financial Instruments 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by