i have a file, which is the energy consumption of a house.
every 10 minute one value (watt):
10:00 123
10:10 125
10:20 0
...
It means each day have 144 value (Rows).
i want to forecast the energy of the next day with ARX an ARMAX programm. i did write ARX code in Matlab. but i can't forecast the next day. My code take the last 5 consumption and forecast the 6th one. How can i forecast the nex 144 value ( = the day after)
% ARX Process----------------------------
L=length(u_in)
u_in_ID=u_in;% Input data used for Identification
u_in_vfy=u_in;% Input data used for verification
y_out_ID=y_out;% Output data used for Identification
y_out_vfy=y_out;%Output data used for verification
m=5; %Parameter to be used to generate order of delay for Input, Output and Error
n=length(y_out_ID)-m;
I=eye(n,1)+1;
I(1)=I(1)-1;
A=I; % Initialize Matrix A
Y=y_out_ID((m+1):end); % Defining Y vector
length(Y)
na=1;
% Put output delay 1 to m-na in A matrix
for k=1:1:m-na
A=[A y_out_ID((m-k+1):(end-k))];
end
% Put "Current Input -- mth delayed Input" to Matrix A
for p=1:1:m
k=p-1;
A=[A u_in_ID((m-k+1):(end-k))];
end
A(:,1)=[]; % Delete 1st column of Matrix A, which was used to Initialize it
parsol=inv(A'*A)*A'*Y;
BB=A*parsol;
% Generate Identified Output vector based on previous
% outputs, current and previous Inputs and Parameters solved by Least
% square method
n=length(y_out_vfy)-m;
I=eye(n,1)+1;
I(1)=I(1)-1;
A=I;
for k=1:1:m-na
A=[A y_out_vfy((m-k+1):(end-k))];
end
for p=1:1:m
k=p-1;
A=[A u_in_vfy((m-k+1):(end-k))];
end
A(:,1)=[]; % Delete 1st column of Matrix A, which was used to Initialize it
A;
y_out_sysID=A*parsol;
can anyone help me?
Related
syms omega j theta sigma
k=[90 94 98 102 106 110]; % Strike prices
putprices=[.1606 .2806 .9285 2.8801 6.1523 10.0147]; % Put prices at each strike
mustar = log(100)-sigma.^2/2-omega.*(exp(-theta)-1); % risk-neutral mean
Es1j=exp(mustar-theta.*j+sigma.^2./2); % Expected value of stock price at state j 1 period from now
x1=(log(k)-(mustar-theta.*j))./sigma; % the first normcdf value
x2=(log(k)-(mustar-theta.*j)-sigma.^2)./sigma; % the second normcdf value
qpkandj = k.*normcdf(x1)-Es1j.*normcdf(x2); % combination of normcdfs and strike prices and expected value of stock price 1 peroid from now
qpk=exp(-omega).*omega.^j./factorial(j).*qpkandj; % Poisson formula
eqns = symsum(qpk,j,0,inf)== putprices % Continuation of Poisson formula
assume(omega > 0)
assume(theta, 'real')
assume(sigma, 'real')
S = solve(eqns, [omega theta sigma], 'Real', true, 'ReturnConditions', true)
S.omega
S.theta
S.sigma
Trying to work on a Poisson mixture model to find the values of the three parameters (omega, theta, and sigma) that match the Poisson equation's outputs to the putprices in the third line. Whenever I run this, I get a very, very long conditions list and I tried to put it all into the assume condition and it made no difference. Anyone have any ideas what's going on or how I could manage to make this work?
Matlab will print the condition list because you don't add a semicolon. To avoid print the condition list, just add semicolon like this:
eqns = symsum(qpk,j,0,inf) == putprices; % Continuation of Poisson formula
A subquestion of my assignment requires to compute the implied volatility σ via the Black and Scholes option valuation formula which is:
More specifically, it requires to solve the equation numerically via rootsolving for σ when all parameters have given values.
I am trying to use the fzero function of MATLAB in order to estimate σ. I created two scripts.
The fist script includes:
S_0 = 1403; % Stock Price
K = 1350 ; % Strike Price
rf = 0.0534; % Risk Free Rate
div=0.0118; % Divident Rate
T=0.1028; % Maturity Period
C=81;% Call option value
fixed_input=[S_0,K,rf,div,T,C];% Construct vector input values
save ('fixed_input.mat','fixed_input');
imp_vol_ini=0.1; % Initial Implied Volatility Value
BlackScholes = #BSF;
[imp_vol,y]=fzero(BlackScholes,imp_vol_ini)
While the second script includes the code:
function y=BSF(imp_vol)
load fixed_input
S_0=fixed_input(1);
K=fixed_input(2);
rf=fixed_input(3);
div=fixed_input(4);
T=fixed_input(5);
C=fixed_input(6);
d1_nominator=log(S_0/K)+(rf-div+(imp_vol^2)/2)*T;
d1_denominator=imp_vol*sqrt(T);
d1=d1_nominator/d1_denominator;
d2=d1-imp_vol*sqrt(T);
y=C-(exp(-div*T))*S_0*normcdf(d1)+K*(exp(-rf/T))*normcdf(d2);
end
The code works but the numbers are not reasonable. Normally, after the solving, y should be close to zero while σ should lie between the interval [0.1 , 0.3] but this is not the case. The numbers that I retrieve are y=81 while σ=-2.7018e-16.
I sense that this has something to do with the constraints and the options of the fzero. Can you please help me?
The error is in the last line of BSF function which is:
y=C-(exp(-div*T))*S_0*normcdf(d1)+K*(exp(-rf/T))*normcdf(d2);
% ↑
You wrote a / instead of *. It should've been this:
y=C-(exp(-div*T))*S_0*normcdf(d1)+K*(exp(-rf*T))*normcdf(d2);
% ↑
I am new to neural network and trying to write a wavelet neural network without matlab toolbox.
For function approximation the sigmoid active function output is between 0 and 1 and for calculate the error we use sum square, this is what i know.
But after some iteration the result shows NaN(not a number) in Matlab. Perhaps the change in parameter of weights in the NN(neural network) and doesn't show the NaN , at the end for testing the NN for any input it shows specific constant value.
For more information for the NN:
3 layers==> 1 input in inputLayer,20 units in hidden layer, 1 output in outputLayer
This is a part of the code:
--------------------------------------------------------
x=0:0.1*pi:pi;
y=sin(x);
HiddenLayer=20;%Number of Units in HiddenLayer
M=size(x,1);%Number of row in InputData
N=size(x,2);%Number of col in InputData
% % -------InitialValues------
w=zeros(1,HiddenLayer);
w(1,:)=1;%Initial
a=zeros(M,HiddenLayer);
a(:,:)=0.4;%Initial
b=zeros(M,HiddenLayer);
b(:,:)=0.5;
miu=0.2;%Number
beta=0.05;%Number
ErrorGoal=0.0001;%Number
%---------------Main--------------------
Iteration=1;
while Iteration<1000
ErrorSave=zeros(1,N);
z=zeros(1,HiddenLayer);
p=zeros(1,HiddenLayer);
multiwp=zeros(1,HiddenLayer);
for u=1:N % for more than one input if needed
% keyboard;
for i=1:HiddenLayer
z(i)=((x(u)-b(i))/a(i));
end
for j=1:HiddenLayer
p(j)=cos(1.75*z(j))*exp((-z(j)^2)/2);
end
for k=1:HiddenLayer
multiwp(k)=w(k)*p(k);
end
sumwp=sum(multiwp);
gTotal=sigmf(sumwp,[1 0]);
Error=0.5*(y(u)-gTotal).^2;
% keyboard;
ErrorSave(u)=Error;
% keyboard;
gradEW=-(y(u)-gTotal).*gTotal.*(1-gTotal)*cos(1.75*z);
gradEB=zeros(M,HiddenLayer);
for m=1:HiddenLayer
gradEB(m)=-(y(u)-gTotal).*gTotal.*(1-gTotal).*(w(m)./a(m)).*exp(-(z(m).^2)/2).*...
((1.75*sin(1.75*z(m)))+(z(m)*cos(1.75*z(m))));
end
gradEA=zeros(M,HiddenLayer);
for o=1:HiddenLayer
gradEA(o)=-(y(u)-gTotal).*gTotal.*(1-gTotal)*z(o).*(w(o)./a(o))*exp(-(z(o).^2)/2).*...
((1.75*sin(1.75*z(o)))+(z(o)*cos(1.75*z(o))));
end
% keyboard;
%------------Updating Parameter ---------------
w=w-(miu*gradEW)+beta*w;
b=b+(miu*gradEB)+beta*b;
a=a+(miu*gradEA)+beta*a;
end
----------------------------------------------------------------------
If anyone can help and point out my mistake in concept or coding.
Best wishes :)
I found this speech recognition code that I downloaded from a blog. It works fine, it asks to record sounds to create a dataset and then you have to call a function to train the system using neural networks.
I want to use this code to train using my dataset of 20 words that I want to recognise.
Problem:
I have a dataset of 800 files for twenty words i.e. 40 recordings from different people for each word. I used Windows sound recorder to collect the files.
The problem is that in the code is that the size of the input file is set to ALWAYS be 8000, my dataset on the other hand is not constant, some files are 2 seconds long, some are 3 that means there'll be different number of samples in each file.
If the samples per input signal variate it'll probably generate errors.
I want to use my files to train the system.
How do I do that?
Code:
clc;clear all;
load('voicetrainfinal.mat');
Fs=8000;
for l=1:20
clear y1 y2 y3;
display('record voice');
pause();
x=wavrecord(Fs,Fs); % wavrecord(n,Fs) records n samples at a sampling rate of Fs
maxval = max(x);
if maxval<0.04
display('Threshold value is too large!');
end
t=0.04;
j=1;
for i=1:8000
if(abs(x(i))>t)
y1(j)=x(i);
j=j+1;
end
end
y2=y1/(max(abs(y1)));
y3=[y2,zeros(1,3120-length(y2))];
y=filter([1 -0.9],1,y3');%high pass filter to boost the high frequency components
%%frame blocking
blocklen=240;%30ms block
overlap=80;
block(1,:)=y(1:240);
for i=1:18
block(i+1,:)=y(i*160:(i*160+blocklen-1));
end
w=hamming(blocklen);
for i=1:19
a=xcorr((block(i,:).*w'),12);%finding auto correlation from lag -12 to 12
for j=1:12
auto(j,:)=fliplr(a(j+1:j+12));%forming autocorrelation matrix from lag 0 to 11
end
z=fliplr(a(1:12));%forming a column matrix of autocorrelations for lags 1 to 12
alpha=pinv(auto)*z';
lpc(:,i)=alpha;
end
wavplay(x,Fs);
X1=reshape(lpc,1,228);
a1=sigmoid(Theta1*[1;X1']);
h=sigmoid(Theta2*[1;a1]);
m=max(h);
p1=find(h==m);
if(p1==10)
P=0
else
P=p1
end
end
In your code you have:
Fs=8000;
wavrecord(n,Fs) % records n samples at a sampling rate Fs
for i=1:8000
if(abs(x(i))>t)
y1(j)=x(i);
j=j+1;
end
end
It seems that instead of recording you are going to import your sound file (here for a .wave file):
[y, Fs] = wavread(filename);
Instead of hardcoding the 8000value you can read the length of your file:
n = length(y);
and then just use that n variable in the for loop:
for i=1:n
if(abs(x(i))>t)
y1(j)=x(i);
j=j+1;
end
end
The rest of the code seems to be independent of that 8000 value.
If you are worried that having non-constant file length. Compute n_max, the maximum length of all the audio recordings you have. And for recording shorter than n_max samples pad them with zeros so as to make them all n_max long.
n_max = 0;
for file = ["file1" "file2" ... "filen"]
[y, Fs] = wavread(filename);
n_max = max(n_max,length(y));
end
Then each time you process a sound vector you can pad it with 0 (harmless for you, because 0 means no sound) like so:
y = [y, zeros(1, n_max - length(y))];
n=noOfFiles
for k=1:n
M(k,1:length(filedata{k})) = filedata{k}
end
:P
I am receiving the following error message:
Attempted to access sym(67); index out of bounds because numel(sym)=2.
I have been working on this for three days. I looked for similar error, but it didn't help. My code is below:
filename='DriveCyclesCP.xlsx';
V=xlsread('DriveCyclesCP.xlsx',2,'C9:C774'); % Get the velocity values, they are in an array V.
N=length(V); % Find out how many readings
mass = 1700 ; % Vehicle mass+ two 70 kg passengers.
area_Cd = 0.75; % Frontal area in square metres
Crr=0.009; %rolling resistance
g=9.8; % gravity acceleration
T=774; %UDDS cycle time duration
V_ave = 21.5; % UDDS avearage speed im m/s
rd=0.3; % Effective tire radius
Qhv =12.22; % E85 low Heating value in kWh/kg
Vd = 2.189; % engine size in L
md=0.801; % mass density of Ethanol
mf =Vd*md; % mf is the fuel mass consumed per cycle
Per = zeros(1,N); % engine power for each point of the drive cycle
a = zeros(1,N); % acceleration
SFC = zeros(1,N); % specific fuel consumption
Wc = zeros (1,N); % mass flow rate
nf = zeros (1,N); %fuel efficiency
Pm = zeros (1,N); % motor power
Pt = zeros (1,N);
Te =zeros (1,N); % Engine Troque
Tt = zeros (1,N);
Tm =zeros (1,N);
we =zeros (1,N); % Engine rot speed
wt = zeros (1,N);
wm =zeros (1,N);
S =zeros (1,8);
int (sym ('C'));
for C=1:N
a(C)=V(C+1)-V(C);
Pt(C)= V(C)*(mass*g*Crr + (0.5*area_Cd*1.202*(V(C))^2) + mass*a(C))/1000;
Per(C)=(mass*g*Crr +0.5*area_Cd*1.202*(V(C))^2 +mass*g*0.03)/1000*0.85;% e
syms Te(C) Tt(C) Tm(C) wt(C) we(C) wm(C) k1 k2
S = solve( Pm(C)==Pt(C) - Per(C), Tt(C)*wt(C)== Pt(C), Tt(C)*wt(C)== Te(C)*we(C) + Tm(C)*wm(C), wt(C)==we(C)/k1, wt(C)==wm(C)/k2, Pm(C)==wm(C) *Tm(C), Per(C)==we(C) *Te(C), Tt == k1*Te + k2*Tm );
end
The problem is on the line
int (sym ('C'));
You have defined sym to be a matrix with 2 entries somewhere (either earlier in the code or in a previous mfile), thus it treats sym as a matrix instead of a function. Thus when Matlab gets to the statement sym('C') it first converts the character 'C' to its ASCII integer representation (this just happens to be the number 67), then it tries to calculate sym(67) which is impossible as sym only has 2 elements.
Thus you have to stop sym from being a matrix (variable) and let it be a function again. There are two ways to solve this, either you can start you file with the statement clear;, this will remove all variables in memory, which might not be what you want; or you can use a function instead of script, as this hides all variables that have been defined previously and prevents this sort of error.
Note the line numel(X) is a way to measure how many elements are in X. Thus numel(sym)=2 means that sym has 2 elements.
P.S. There is an error in the lines (notice that I only taken some of the lines of you code)
N=length(V); % Find out how many readings
for C=1:N
a(C)=V(C+1)-V(C);
end
When C becomes equal to N, then V(C+1) will generate an error.