Time series forecast with scarce data - prediction

Lets say i want to get a weekly forecast of y (e.g. consumption) for 1 year ahead, but i only got training data of y for the last year, seeing a clear pattern during the year.
I know that y depends on x (population), which was linear monotonic increasing in my data.
My first naive approach is:
forecast x with linear regression to get predicted x values for the future
forecast y with $$y_{t1}=(y_{t0} / x_{t0})*x_{t1}$$
What would be a more "machine learning" approach to this problem?
Is this even feasible without any additional data (more than one cycle of the timeframe to forecast)?

Related

Integrate a function using trapz with datetime - Calculate Energy with a current vector

i have a problem with my calculation tool. I have a vector of current values and a time vector in datetime format. Now i need to get the integral for overall Power consumption and im struggling.
I tried to integrate about the vector with trapz but the outcome isn't realistic for this vector. I tested different vectors and signal but same result.
My timevector wasn't used in this example.
%% Power Calculate
Energy = Ch1L1*230; %P=U*I scale vector with 230V
EnergySum = trapz(Energy)/1000 %Output: Power in kW
Does anyone of you know a solution?
I think i misunderstand the time in this outcome. Normally the integral should be the overall Power consumption for the time i logged the data.

How to predict temperature for the 4th day, given temperatures for previous days, using a linear perceptron?

I have four sets of data (3 for training, 1 for testing) that include the hour of the day and temperatures in this format:
Time | Temperature
5, 60
6, 63
7,70
8,73
9,78
10,81.5
11,85.1
12,87
13,90
I need to train and test a perceptron and then predict what the temperatures will be on the next day at the same hours.
I am trying to use Matlab to do this and I know I am supposed to normalize the data and use time-series prediction. However I can't figure out how to start.
I don't understand what the inputs and outputs are, and what activations function to use to make the output linearly from -infinity to +infinity.
I'm pretty sure you won't have to use a perceptron for this task as you want to perform regression and not classification. (Perceptron is a binary classifier see Matlab documentation.)
To start with normalization: You need to adjust your data such that the mean is zero and the standard deviation equals 1. For example:
data = rand(1,100);
data = (data - mean(data))/sqrt(var(data));
You can interpret your input and output as follows:
You have an underlying function which maps your time-values to the temperature values (f:time->temperature). Time is the independent variable and temperature the dependent variable (see for example Wikipedia). And you want to find an approximation for f based on your input data.
For time series regression you will find a detailed example here. If you
are required to use a feedforward network you can also take a look at this.

interpolation of fortnightly annual temperature data into hourly measurements in matlab

I have a dataset of annual temperature measurements recorded at fortnightly intervals. The data looks similar to the following:
t = 1:14:365;
% GENERATE DATA
y = 1 + (30-1).*rand(1,length(t));
y1 = 20*sin(2*pi*t/max(t)); % Annual variation °C
y1(y1<0) = [];
tt = 365/14;
time = 1:tt:365;
plot(time,y1,'-o');
where it clearly follows a annual temperature cycle.
From this I am wondering if it is possible to add a sine function (which would represent a diurnal temperature range) onto the data? For example, from the fortnightly data, if we were to interpolate the series to have 8760 measurements i.e. hourly measurements, for the series to be believable it would need to be characterized by a diurnal temperature cycle in addition to the annual temperature cycle. Furthermore, the diurnal temperature cycle would need to be a function of the temperature measurements at that time i.e. would be greater in the summer than in winter. So maybe it would be better to firstly use linear interpolation to get the data to represents hourly intervals and then add the sine function. Is there a method for writing this into a script? or does anyone have an opinion on how to accurately achieve this?
You could first interpolate your data (down to 1 hours) using something like
x = 1:inv(24):365;
T_interp = interp1(t,y1,x,'spline');
Check out Matlab documentation for interp1 (example 2)
and then add a sine onto it. The following a sine of period 1 (24 hours) with amplitude A, with a minimum at 3am.
T_diurn = -A*sin(2*pi*x+(3/24)*2*pi);
Then
T_total = T_diurn + T_interp;
First: you know that good-looking plots are the most misleading things in existence? Interpolating data gathered every 14 days so that it will look like data collected every hour is considered at least bad practice most circles...
Having said that, I would use splines to do the interpolation -- they are a lot more flexible when it comes to changing from fortnightly and hourly to some arbitrary other combination, plus the annual temperature variation will be a lot smoother.
Here's how:
% Create spline through data
pp = spline(time, y1);
% define diurnal variation (this one is minimal at 4 AM)
T_diurn = #(t) -A*cos(2*pi*(t-(4/24)));
% plot example
t = 150 : 1/24 : 250;
plot( t, ppval(pp,t)+T_diurn(t) , 'b')

analyse time series at a specific frequency

I have a long data set of water temperature:
t = 1/24:1/24:365;
y = 1 + (30-1).*rand(1,length(t));
plot(t,y)
The series extends for one year and the number of measurements per day is 24 (i.e. hourly). I expect the water temperature to follow a diurnal pattern (i.e. have a period of 24 hours), therefore I would like to evaluate how the 24 hour cycle varies throughout the year. Is there a method for only looking at specific frequencies when analyzing a signal? If so, I would like to draw a plot showing how the 24 hour periodicity in the data varies through the year (showing for example if it is greater in the summer and less in the winter). How could I do this?
You could use reshape to transform your data to a 24x365 matrix. In the new matrix every column is a day and every row a time of day.
temperature=reshape(y,24,365);
time=(1:size(temperature,1))-1;
day=(1:size(temperature,2))-1;
[day,time]=meshgrid(day,time);
surf(time,day,temperature)
My first thought would be fourier transformation. This will give you a frequency spectrum.
At high frequencies (> 1/d) you would have the pattern for a day, at low frequencies the patter over longer times. (see lowpass and highpass filter)
Also you could go for a frequency/time visualization that will show how the frequencies change over a year.
A bit more work - but you could write a simple model and create a Kalman filter for it.

generate signal with seasonal and diurnal component

This is a rather vague question but here we go - I would like to generate a time series for hourly measurements of one year, so for 2011 ti would 8760 values within the series. To make it easier to understand what I am trying to do I will use a real world example:
If we had a time series of hourly air temperature measurements an then plotted the entire series it would look similar to a bell shaped curve i.e.
a = 0; b = 30;
x = a + (b-a) * rand(1, 8760);
m = (a + b)/2;
s = 12;
p1 = -.5 * ((x - m)/s) .^ 2;
p2 = (s * sqrt(2*pi));
f = exp(p1) ./ p2;
plot(x,f,'.')
with the maximum values occurring in mid summer and lowest values during the winter. However, by zooming in on specific days we would see that the temperature also fluctuates between the day and the evening where maximum temperatures would occur at approximately 15:00 and minimum temperature at approximately 06:00.
So, my question is how would I generate this series, i.e. a time series which had a maximum value of say 30 degrees in mid summer i.e. value (8760/2) and also had the daily pattern mentioned above incorporated into the overall pattern?
The obvious way to do this would be to add together 2 sine waves, one for the diurnal variations and one for the annual variations.
Whether or not a sine wave is close enough to a bell-shaped curve for your liking I don't know, but I could make a vague argument that since the variation in annual and diurnal temperatures is (in part) a product of (approximately) circular motions you should be using sine waves anyway.
If you need help generating the sine waves update your question.
If I understand the question correctly, you'd like to have a superposition of the two series of known shape, right? If so, you just have to add them up. The important part is to shift the daily temperature fluctuation signal so that its mean is 0, provided the "year" curve expresses the average temperature.