Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:08:30+00:00 2026-06-18T07:08:30+00:00

Intro: I’m using MATLAB’s Neural Network Toolbox in an attempt to forecast time series

  • 0

Intro: I’m using MATLAB’s Neural Network Toolbox in an attempt to forecast time series one step into the future. Currently I’m just trying to forecast a simple sinusoidal function, but hopefully I will be able to move on to something a bit more complex after I obtain satisfactory results.

Problem: Everything seems to work fine, however the predicted forecast tends to be lagged by one period. Neural network forecasting isn’t much use if it just outputs the series delayed by one unit of time, right?

Code:

t = -50:0.2:100;
noise = rand(1,length(t));
y = sin(t)+1/2*sin(t+pi/3);
split = floor(0.9*length(t));
forperiod = length(t)-split;
numinputs = 5;
forecasted = [];
msg = '';
for j = 1:forperiod
    fprintf(repmat('\b',1,numel(msg)));
    msg = sprintf('forecasting iteration %g/%g...\n',j,forperiod);
    fprintf('%s',msg);

    estdata = y(1:split+j-1);
    estdatalen = size(estdata,2);

    signal = estdata;
    last = signal(end);

    [signal,low,high] = preprocess(signal'); % pre-process
    signal = signal';

    inputs = signal(rowshiftmat(length(signal),numinputs));
    targets = signal(numinputs+1:end);

    %% NARNET METHOD
    feedbackDelays = 1:4;
    hiddenLayerSize = 10;
    net = narnet(feedbackDelays,[hiddenLayerSize hiddenLayerSize]);
    net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
    signalcells = mat2cell(signal,[1],ones(1,length(signal)));
    [inputs,inputStates,layerStates,targets] = preparets(net,{},{},signalcells);
    net.trainParam.showWindow = false;
    net.trainparam.showCommandLine = false;
    net.trainFcn = 'trainlm';  % Levenberg-Marquardt
    net.performFcn = 'mse';  % Mean squared error
    [net,tr] = train(net,inputs,targets,inputStates,layerStates);
    next = net(inputs(end),inputStates,layerStates);


    next = postprocess(next{1}, low, high); % post-process
    next = (next+1)*last;

    forecasted = [forecasted next];
end

figure(1);
plot(1:forperiod, forecasted, 'b', 1:forperiod, y(end-forperiod+1:end), 'r');
grid on;

Note:
The function ‘preprocess’ simply converts the data into logged % differences and ‘postprocess’ converts the logged % differences back for plotting. (Check EDIT for preprocess and postprocess code)

Results:

A screenshot of the forecasting results using MATLAB.

BLUE: Forecasted Values

RED: Actual Values

Can anyone tell me what I’m doing wrong here? Or perhaps recommend another method to achieve the desired results (lagless prediction of sinusoidal function, and eventually more chaotic timeseries)? Your help is very much appreciated.

EDIT:
It’s been a few days now and I hope everyone has enjoyed their weekend. Since no solutions have emerged I’ve decided to post the code for the helper functions ‘postprocess.m’, ‘preprocess.m’, and their helper function ‘normalize.m’. Maybe this will help get the ball rollin.

postprocess.m:

function data = postprocess(x, low, high)

% denormalize
logdata = (x+1)/2*(high-low)+low;

% inverse log data
sign = logdata./abs(logdata);
data = sign.*(exp(abs(logdata))-1);

end

preprocess.m:

function [y, low, high] = preprocess(x)

% differencing
diffs = diff(x);
% calc % changes
chngs = diffs./x(1:end-1,:);
% log data
sign = chngs./abs(chngs);
logdata = sign.*log(abs(chngs)+1);
% normalize logrets
high = max(max(logdata));
low = min(min(logdata));
y=[];
for i = 1:size(logdata,2)
    y = [y normalize(logdata(:,i), -1, 1)];
end

end

normalize.m:

function Y = normalize(X,low,high)
%NORMALIZE Linear normalization of X between low and high values.

if length(X) <= 1
    error('Length of X input vector must be greater than 1.');
end

mi = min(X);
ma = max(X);
Y = (X-mi)/(ma-mi)*(high-low)+low;

end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:08:32+00:00Added an answer on June 18, 2026 at 7:08 am

    I didn’t check you code, but made a similar test to predict sin() with NN. The result seems reasonable, without a lag. I think, your bug is somewhere in synchronization of predicted values with actual values.
    Here is the code:

    %% init & params
    t = (-50 : 0.2 : 100)';
    y = sin(t) + 0.5 * sin(t + pi / 3);
    sigma = 0.2;
    n_lags = 12;
    hidden_layer_size = 15;
    %% create net
    net = fitnet(hidden_layer_size);
    %% train
    noise = sigma * randn(size(t));
    y_train = y + noise;
    out = circshift(y_train, -1);
    out(end) = nan;
    in = lagged_input(y_train, n_lags);
    net = train(net, in', out');
    %% test
    noise = sigma * randn(size(t)); % new noise
    y_test = y + noise;
    in_test = lagged_input(y_test, n_lags);
    out_test = net(in_test')';
    y_test_predicted = circshift(out_test, 1); % sync with actual value
    y_test_predicted(1) = nan;
    %% plot
    figure, 
    plot(t, [y, y_test, y_test_predicted], 'linewidth', 2); 
    grid minor; legend('orig', 'noised', 'predicted')
    

    and the lagged_input() function:

    function in = lagged_input(in, n_lags)
        for k = 2 : n_lags
            in = cat(2, in, circshift(in(:, end), 1));
            in(1, k) = nan;
        end
    end
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Content type Events has intro, body and an event date field (using Date module).
link Im having trouble converting the html entites into html characters, (&# 8217;) i
INTRO: I created a java application using JFrame. I have a JMenuBar at the
I am taking an intro to programming online. However, I am stuck on one
Intro One of the coolest things about Java is that the JRE has a
Intro: I am using the MVP as a design pattern for a windows form
Intro: I am using PySide and the Qt Framework to build a GUI app
Intro I am developing a web application using ASP.NET MVC 3, C#, targeting IIS
Intro - I am working on a iOS game using Flash CS5.5. In my
Intro I've been given a messy excel dump straight into a table. Now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.