function movavg dont work with matrix

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

function movavg dont work with matrix

Octave - General mailing list
Equity=magic(4);
AvgEquity=2;

 movAverage2=movavg(Equity,AvgEquity,AvgEquity,0);


Equity =

  16    2    3   13
    5   11   10    8
    9    7    6   12
    4   14   15    1

movAverage =
     OK          Not OK    Not OK  Not OK
   16.0000    2.0000    3.0000   13.0000
   10.5000   11.0000   10.0000    8.0000
    7.0000    7.0000    6.0000     12.0000
    6.5000   14.0000   15.0000    1.0000

the average of column 1 is correct ... while in the other columns they are
copied from the original maatrice



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: function movavg dont work with matrix

siko1056
On 8/28/19 12:19 AM, LucaLuca via Help list for GNU Octave wrote:

> Equity=magic(4);
> AvgEquity=2;
>
>  movAverage2=movavg(Equity,AvgEquity,AvgEquity,0);
>
>
> Equity =
>
>   16    2    3   13
>     5   11   10    8
>     9    7    6   12
>     4   14   15    1
>
> movAverage =
>      OK          Not OK    Not OK  Not OK
>    16.0000    2.0000    3.0000   13.0000
>    10.5000   11.0000   10.0000    8.0000
>     7.0000    7.0000    6.0000     12.0000
>     6.5000   14.0000   15.0000    1.0000
>
> the average of column 1 is correct ... while in the other columns they are
> copied from the original maatrice
>
>

The function "movavg" from the financial package is only capable of
treating vectors.  This I conclude from inspecting the current
implementation of about 60 lines of code

   pkg load financial
   edit movavg

Compared to the current Matlab version of "movavg" [1], the
implementation of the financial package is rather simple and might have
been compatible back in 2008 when it was written.

A workaround might be the code for "movAverage3" inspired by [2]:

  pkg load financial

  Equity=magic(4);
  AvgEquity=2;

  movAverage2=movavg(Equity,AvgEquity,AvgEquity,0)

  movAverage3=cell2mat ( ...
    arrayfun (@(i) movavg (Equity(:,i), AvgEquity, AvgEquity, 0), ...
              1:size(Equity,2), 'UniformOutput', false))

I added the package maintainer to this discussion and suggest that at
least the documentation should be updated, that only vectors can be treated.

It would be nice, if you can open a bug report about this [2].  You are
very welcome to improve that function with your knowledge, if you like.

Best regards,
Kai


[1] https://www.mathworks.com/help/finance/movavg.html
[2]
https://www.mathworks.com/matlabcentral/answers/417932-apply-function-to-each-column-of-matrix-for-all-columns-without-for-loop
[3] https://savannah.gnu.org/bugs/?func=additem&group=octave


Reply | Threaded
Open this post in threaded view
|

Re: function movavg dont work with matrix

Octave - General mailing list
hi,
i try diffucult open bug report.

I did the code for the solution (it's not the best but it's good)

   y = cumsum (x) ;
   n = rows (x) ;
   a=x(1:AvgEquity-1,:);
   b=y(AvgEquity,:)/AvgEquity;
   c= (y(AvgEquity+1:n,:) .- y(1:n-AvgEquity,:))
   movAvg=vertcat(a,b,c);

do you like it?



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: function movavg dont work with matrix

mmuetzel
In reply to this post by Octave - General mailing list
Maybe you could also use "movmean" that was added in Octave 5.1. Afaict, it
can operate on matrices.

hth
Markus




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: function movavg dont work with matrix

Octave - General mailing list
Reply | Threaded
Open this post in threaded view
|

Re: function movavg dont work with matrix

siko1056
In reply to this post by Octave - General mailing list
On 8/28/19 8:34 PM, LucaLuca via Help list for GNU Octave wrote:

> hi,
> i try diffucult open bug report.
>
> I did the code for the solution (it's not the best but it's good)
>
>    y = cumsum (x) ;
>    n = rows (x) ;
>    a=x(1:AvgEquity-1,:);
>    b=y(AvgEquity,:)/AvgEquity;
>    c= (y(AvgEquity+1:n,:) .- y(1:n-AvgEquity,:))
>    movAvg=vertcat(a,b,c);
>
> do you like it?
>

No problem, I open that report for you, see

   https://savannah.gnu.org/bugs/index.php?56829

I cannot run your without errors (x undefined) and do not care enough to
fix it.  If you really want to dive deeper into developing the financial
package, take a look at our contribution guidelines, especially the
Octave style guide

https://wiki.octave.org/Octave_style_guide

Otherwise I think it it best you stick to the advice of Markus and use
"movmean" instead.

Best,
Kai