How to create a script in Matlab

2 views (last 30 days)
gjashta
gjashta on 11 May 2019
Edited: gjashta on 14 May 2019
I have the following codes: I want to bin the price data in 4 bins per month and to calculate the mean and std of demand that belong in each bin by month and year.
Can you help me to create a function that contain (the bin, month, year) as a script?? I am repeating the same code for each month, I need a structure that calculate
them very fast and save all the results in one table.
Price = Data1(:,1 );
Demand = Data1(:,2 );
tv = (datetime([2006,1,1]):datetime([2018,12,31])).';
[Ye,M,D] = ymd(tv);
DT = timetable(Price,Demand, Ye, M, D, 'RowTimes', tv );
MarchDataPD = DT(DT.M==3,{'Price','Demand'});
topEdge1=max(P.Price(1:31));
botEdge1=min(P.Price(1:31));
NumBins=4;
binEdges1=linspace(botEdge, topEdge, NumBins+1);
[h, WhichBin]= histc(P.Price(1:31),binEdges1);
for i = 1:NumBins
flagBinMembers=(WhichBin==i);
binMembers=d.Demand(flagBinMembers);
binMean1(i)=mean(binMembers);
binstd1(i)=std(binMembers);
end
topEdge2=max(P.Price(32:62));
botEdge2=min(P.Price(32:62));
NumBins=4;
binEdges2=linspace(botEdge, topEdge, NumBins+1);
[h, WhichBin]= histc(P.Price(32:62),binEdges2);
for s = 1:NumBins
flagBinMembers=(WhichBin==s);
binMembers=d.Demand(flagBinMembers);
binMean2(s)=mean(binMembers);
binstd2(s)=std(binMembers);
end

Accepted Answer

Guillaume
Guillaume on 11 May 2019
It's a bit puzzling that you are aware of timetables and yet resort to deprecated functions such as histc (which has no concept of dates) for the rest of your code, instead of using timetables functions which would make your task trivially easy.
Even the way you construct your timetable is more complicated than it should:
%Data1: a 2 column matrix of price and demand:
DT = array2timetable(Data1, 'VariableNames', {'Price', 'Demand'}, 'RowTimes', datetime(2006, 1, 1):dateime(2018, 12, 31));
There is no point in storing Ye, M, D separately. It's already stored as part of the time.
Once you have a timetable, you can easily resample it at regular or irregular intervals using retime. However retime doesn't do
%DT: a timetable
meanDT = retime(DT, 'regular', 'mean', 'TimeStep', caldays(14)); %discussion about timestep coming up
stdDT = retime(DT, 'regular', @std, 'TimeStep', caldays(14));
meanDT.Properties.VariableNames = {'MeanPrice', 'MeanDemand'};
stdDT.Properties.VariableNames = {'StdPrice', 'StdDemand'};
meanstdDT = [meanDT, stdDT]
Instead of using two retime, you could use groupsummary instead:
meanstdDT = groupsummary(DT, 'Time', caldays(14), {'mean', 'std'})
Now you say you want 4 bins per month. What does that mean exactly. 4 equal bins per month means that the edges fall at odd times during the days instead of falling at the end of days. That doesn't sound like something you'd want. In the above, I've used bins of 14 days but both retime and groupsummary support a bin vector instead. To construct a bin vector with 4 equal bins per month, the simplest is probably:
monthvector = DT.Time(1) : calmonths(1) : DT.Time(end) + calmonths(1);
binvector = fillmissing(reshape([monthvector; NaT(3, numel(monthvector))], [], 1), 'linear')
and then
meanstd = groupsummary(DT, 'Time', binvector, {'mean', 'std'})
  5 Comments
Guillaume
Guillaume on 14 May 2019
So, I need a better binning way to have reasonable observations that belong to 4 bins of the month
Well, you're free to use whichever binning method you want. As I've no idea why you're doing this, I've nothing to suggest. Perhaps you shoud bin according to demand instead of price but the binning method is for you to decide.
In the end, whichever binning method you'd still pass the bin edges to groupsummary.
gjashta
gjashta on 14 May 2019
Thank you Guillaume! And sorry because as a beginner in using Matlab, I don't express the question correctly.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!