Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Matlab by Roberto Estrada ( 16 years ago )
%% Suavizado Exponencial <Exponential Smoothing> 
% AUTHOR Roberto Estrada
% DATE Jun 06 2010
% Copyright (c) 2010, Roberto Estrada
% All rights reserved.
%
% Redistribution and use in source and binary forms, with
% or without modification, are permitted provided that
% the following conditions are met:
%
%  * Redistributions of source code must retain the above
%    copyright notice, this list of conditions and the 
%    following disclaimer.
%  * Redistributions in binary form must reproduce the
%    above copyright notice, this list of conditions and
%    the following disclaimer in the documentation and/or
%    other materials provided with the distribution.
%  * Neither the name of the copyright holders nor
%    the names of its contributors may be used to endorse
%    or promote products derived from this software without
%    specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
% CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
% PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
% OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
% (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
% GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
% BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
% OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
% -------------------------------------------------------------------
% -------------------------------------------------------------------
% -------------------------------------------------------------------
% Version 1.0
% License BSD
% Contact [email protected]
% Twitter @Paridin
%
%    DATA TO EXAMPLE
%       x = [10,12,9,8,11,13,9,12,11,10]
%       alpha = [ 0.5, 0.8 ]
%       whereAreYouSeeAPrognostic = 11 
%
%% Función Principal  
    function [table,prognostic,ECM]=smoothing( x, alpha , whereAreYouSeeAPrognostic )
        nameAlpha=['[alpha = ', num2str(alpha(1)) , ']'];
        solution={ nameAlpha };
        ft=[];
        table=[];
        global AVERAGE ALPHA_TO_SEARCH
        AVERAGE = [];
        ALPHA_TO_SEARCH = [];
        if( length( x ) < 1 ) 
            disp ( 'You have an error, with you serie in X' );
      
        else if ( length( x ) == 1)
            prognostic = x( 1 );
            ECM = 0;
        else
            ft = x( 1 );
                for i=1 : length( alpha )
                    for j=1 : whereAreYouSeeAPrognostic-1
                        counter = j + 1;
                        if( counter <= length( x ) )
                            ft( j + 1 ) = ( alpha( i ) * x( j + 1 ) ) + ( ( 1 - alpha( i ) ) * ft( j ) );
                            solution = [solution;ft(j)];
                        else
                            x =[x,ft(j)];
                            ft( j + 1 ) = ( alpha( i ) * x( j + 1 ) ) + ( ( 1 - alpha( i ) ) * ft( j ) );
                            solution = [solution;ft( j )];
                        end
                    end
                            table = [table,solution]; 
                        if ( i < length(alpha) )
                            solution = ecm(x,ft,alpha,i,whereAreYouSeeAPrognostic); % Sacamos el ECM de la primera ronda.
                            table = [table,solution];  % Aqui ya tenemos el ECM de alpha en i
                            nameAlpha=['[alpha = ', num2str(alpha(i+1)) , ']'];
                            solution = { nameAlpha };
                        end
                end  
                solution = ecm(x,ft,alpha,i,whereAreYouSeeAPrognostic); % Sacamos el último ECM de la primera ronda.
                table = [table,solution];  % Aqui ya tenemos el ECM de alpha en i
        end
        end
        [ECM,prognostic]=toPrognostic(table);
       
        disp('The ECM is');
        disp(ECM);
        disp('The prognostic is');
        disp(prognostic);
    end 
    
%%  ECM Error Cuadrado Medio  
function [solution]=ecm(x,ft,alpha,i,whereAreYouSeeAPrognostic)
    global AVERAGE ALPHA_TO_SEARCH
    nameECM=['[ECM = ', num2str(alpha(i)) , ']'];
    solution = { nameECM };
    toAverage =[];
    for j=1 : whereAreYouSeeAPrognostic-2
          
            ecm = ( x( j + 1 ) - ft( j ) ) ^ 2;
            solution = [solution;ecm];
            toAverage = [toAverage ; ecm ]; 
    end
      
        solution = [solution ; average(toAverage)];
        AVERAGE = [AVERAGE ; average(toAverage) ];
        ALPHA_TO_SEARCH = [ALPHA_TO_SEARCH ; alpha(i)];
end

%% Promedio 
function [res]=average(x)
    res = sum( x ) / length( x );
end

%% Búsqueda 

function [find]=search(whereSearch,stringToSearch)  
    [row,col] = size(whereSearch) ;
    stringToSearch={stringToSearch};
        for j=1 : col
            if( strcmp(whereSearch (1,j) , stringToSearch) == 1 )
                sizeString = length(whereSearch);
                find = whereSearch(sizeString,j); %Encontre el dato donde alpha es el menor :)
            end
        end
end

%% Obtengo Posiciones del menor ECM y su pronóstico
function [ECM,prognostic]=toPrognostic(table)
    global AVERAGE ALPHA_TO_SEARCH
    
    for i=1 : length(AVERAGE)-1
        if( AVERAGE(i) < AVERAGE(i+1) )
            ECM = AVERAGE(i);
            stringToSearch = ['[alpha = ', num2str(ALPHA_TO_SEARCH(i)) , ']'];
            prognostic= search(table,stringToSearch);
        end
    end
end

 

Revise this Paste

Your Name: Code Language: