Its my lengthy collection  moving average

 m.shimura ([email protected])

 


NB. ---------cut here ------------------------
NB. ===============================
NB. chapter 4
NB. ============================
NB. calculate mav mav12
NB. ============================
mean=: +/%#
dev=: -(+/%#)
dev2=: -"1 (+/%#)
mav=: +/\ % [
NB. Season adjustment using 12 month
mav12=: [: 2&mav 12&mav
mavm=: +/\("1) % ("1) [ NB. mav for matrix : usagge: 12 mavm data(matrix yoko)
mav_c=:2&m...@mav ` mav @. (2&|@[)
NB. =================================
NB. apply for vertical matrix
vmav=: +/\ % "1 [
vmav12=: [: 2&vmav 12&vmav
NB. ===================================
mav_w3=: 3 : 0
wt=: 1 2 3 2 1 % 9
M0=:> +/ L:0(5<\ y ) * L:0 wt 
)


NB. complement lack of w_mav3
NB. y  is yel mav data
cpl_mavw3=: 3 : 0
Y=: mav_w3 y 
wt_0=: 3 7 10 7 % 27
wt_1=: 2 3 4 % 9
HEAD0=: +/ (|. wt_1)* 3 {. y 
HEAD1=: +/(|. wt_0) *4 {. y 
TAIL99=:+/ wt_1 * _3{. y 
TAIL98=:+/ wt_0 * _4{. y 
TMP=: (HEAD0,HEAD1),Y,(TAIL98,TAIL99)
)

NB. ====================================
NB. plot combination data and mav12
NB. Usage: plotcomb  y 
NB. ====================================
plot_mav12=: 3 : 0
NB. plot mav 12 and actual data
NB. Ussage: plotcomb_12 y 
tmp0=:(7#6),7 + i. # tmp1=.mav12 y  
tmp0=.tmp0 ,7 #({: tmp0) NB. x axis
tmp2=:( 7# {. tmp1), tmp1, 7 # {: tmp1 NB. y axis  
tmp3=.tmp0;tmp2
pd 'reset'
pd y 
pd 'color red'
pd tmp3
pd 'show'
)


mav12cpl=: 3 : 0
NB. mav12 and hokan by centered simple mav
NB. amari sinnrai sinai houga yoi
NB. Usage: mav12cpl y 
BAI=: 0.5,(11#1),0.5 NB. 0.5 1 1 ..1 0.5
BAI2=: 6}. }: |. L:0 <\ BAI  NB. /. .1 1 0.5
TMP0=:12 %~ L:0 +/ L:0 BAI * (L:0)   13<\y 
FST=: 12{. y  NB. early times
FST1=:(0.5 -~ L:0 # L:0 FST0)%~ L:0 +/ L:0 BAI2 * L:0 FST0=: 6}. 12{. <\FST
NEAR=: |. _12{.y  NB. recent times
FST3=:(0.5 -~ L:0 # L:0 FST2)%~ L:0 +/ L:0  (|. L:0 BAI2) * L:0 FST2=: 6}. 12{
. <\ NEAR
(>FST1),(>TMP0),|. > FST3
)

mav_w13=: 3 : 0
NB. WEG=: _11 0 9 16 21 24 25 24 21 16 9 0 -11
WEG=: _11 0 9 16 21 24 25 24 21 16 9 0 _11
TMP0=:  143 %~ L:0  +/ L:0  WEG * (L:0)  13<\y  
WEGN=: |. (L:0)_6{. }: <\  WEG
WEGF=: _6{.}: <\ WEG
NB.  5}. |. L:0  L:0 <\  WEG
FST=: 12{. y  NB. early times
FST1=:(+/ L:0 WEGF)%~ L:0 +/ L:0 WEGF * L:0 FST0=: 6{. 6}. <\FST
NEAR=: |. _12{.y  NB. recent times
FST3=:(+/ L:0 WEGN)%~ L:0 +/ L:0 WEGN * L:0 FST2=: 6{. 6}. <\ NEAR
(>FST1),(>TMP0),|. > FST3
)

plot_w13=: 3 : 0
NB. compare mav12cpl /fine!!
pd 'reset'
pd y 
pd 'color green '
pd mav12cpl y 
pd 'color red'
pd mav_w13 y 
pd 'show'
)
plot_mav12cpl=: 3 : 0
pd 'reset'
pd y 
pd 'color red'
pd mav12cpl y 
pd 'show'
)

NB. =================================
NB. mav any dimension
NB. ussage: x  plotcomb y   (x   dimension y  data)
NB. ==================================
p_sub0=: 4 : 0
NB. make mav anytime odd or even
mav=. +/\ % [
mav_even=.[: 2&mav mav
select. tmp0 =.2 | x 
case. 0 = tmp0 do. goto_odd.
case. 1 = tmp0 do. goto_even.
end.
label_odd.
tmp10=.x  mav y 
tmp2=.<. -: x 
return.
label_even.
tmp10=. x  mav_even y 
tmp2=.>. -: x 
return.
)
p_sub1=: 4 : 0
NB. make plot data by mav
x  p_sub0 y 
tmp30=:(tmp2 # tmp2) ,  tmp2 +i. # tmp10 
tmp3=.tmp30,tmp2 #({: tmp30)  NB. x axis
tmp4=:( tmp2 # {. tmp10), tmp10,tmp20=.tmp2 # {: tmp10 NB. y axis   
tmp5=.tmp3;tmp4
)

plotcomb=: 4 : 0
NB. usage: x  plotcomb y 
NB. x  jisuu
NB. y  data
pd 'reset'
pd x  p_sub1 y 
pd y 
pd 'color red'
pd  tmp5
pd 'show'
)


plot_w3=: 3 : 0
tmp=. mav_w3 y 
NB. tmp1=:(2# (2{tmp1))(0 1)} tmp1=: i. # y  
NB. tmp1=: tmp1 ,2# {: tmp1=:  _2}. tmp1 NB. compaund X
NB. tmp2=: ((2# {. tmp),tmp,2#{:tmp)     NB. expand Y
pd 'reset'
pd 'color red'
pd  y 
pd 'color blue'
pd  cpl_mavw3 y  NB. tmp1;tmp2
pd 'color green'
NB. pd spencer y  NB. may display spencer
pd 'show'
)

plot_ws=: 3 : 0
NB. plot w3 and spencer
tmp=. mav_w3 y 
NB. tmp1=:(2# (2{tmp1))(0 1)} tmp1=: i. # y  
NB. tmp1=: tmp1 ,2# {: tmp1=:  _2}. tmp1 NB. compaund X
NB. tmp2=: ((2# {. tmp),tmp,2#{:tmp)     NB. expand Y
pd 'reset'
pd 'color red'
pd  y 
pd 'color blue'
pd  cpl_mavw3 y  NB. tmp1;tmp2
pd 'color green'
pd spencer y  NB. may display spencer
pd 'show'
)
  

NB. ---spencer----------------------------------
spencer2=: 3 : 0
NB. Spencer 15 weighted mav
WEIGHT=. _3 _6 _5 3 21 46 67 74 67 46 21 3 _5 _6 _3 % 320
 y,. ; WEIGHT +/ . * (L:0)  15<\ ( (7 # {.),] ,7 #{:) y
)

NB. C.Reiter Fractals Visuarization and J (2nd Edition 2000)
wts=: (|.,}.) 74 67 46 21 3 _5 _6 _3%320
locspen=: (+/ . *)&wts
spencer=: 15"_ locspen\ (7: # {.),] ,7:#{:

plot_spencer=: 3 : 0
NB. Usage: plot_spencer y  
NB. plot y  & spencer y 
pd 'reset' 
pd 'type line'
pd 'itemcolor 255 0 0' NB. Blue
pd  y  NB. walk
pd 'type line'
pd 'itemcolor 0 0 255' NB. Akane
pd spencer  y  NB. spencer walk
pd 'show'
)

NB. ==========================
NB. henderson moving average
NB. this Script is written by J.Takeuchi
NB.=========================== 
hnwgt=: 4 : 0
m=: -: x  +3 NB. (n+3)/2
nm=: 315*(((m-1)^2)-*:y )*((m^2)-*:y )*(((m+1)^2)-*:y )*(((3**:m)-16)-11**:y )
hi=:nm%8*m*((m^2)-1)*((4**:m)-1)*((4**:m)-9)*((4**:m)-25)
)

henderson=: 4 : 0
NB. use x  under  odd/not even
NB. Usage: i.e. 15&henderson y  //or 7 henderson y 
wt=: x  hnwgt i:-:<:x 
x  (+/ . *)&wt\((<....@-:x )#{.y ),y ,(<....@-:x )#{:y 
)

NB. ===================
NB. written by M. Shimura 2003/Dec-2004/Mar
plot_henderson=: 3 : 0
pd 'reset'
pd 'color green'
pd y 
pd 'color blue'
NB. pd 'key red=henderson 31'
NB. pd 'key blue =Spencer 15'
pd spencer y 
pd 'color red'
pd 15&henderson y 
pd 'show'
)


   plot_henderson2=:4 : 0
pd 'reset'
NB. pd 'pensize 3'
pd 'title Henderson 31 and Spencer 15'
pd 'color green'
pd y 
pd 'color blue'
pd spencer y 
pd 'color red'
pd  x  henderson y 
NB. pd 'xlabel 4 5 6 7 8 9 0 1 2 3 '
pd 'keystyle 0'
pd 'key y spencer henderson'
pd 'show'
)
   

   plot_comb_all=: 4 : 0
NB. henderson spencer mav12cpl
NB. x  is jisuu(henderson)/must kisuu
NB. like 31 33 /well as mav12
pd 'reset'
pd 'color green'
pd y 
pd 'color blue'
pd spencer y 
pd 'color red'
pd  x  henderson y 
pd 'color purple'
pd mav12cpl y 
pd 'show'
)

NB. -----------------cut here ------------------------

----- Original Message -----
> From: [email protected]
> Subject: Programming Digest, Vol 50, Issue 57
> To: [email protected]
> Date: Mon, 30 Nov 2009 12:00:01 +0800
> 
> 
> Send Programming mailing list submissions to
>       [email protected]
> 
> To subscribe or unsubscribe via the World Wide Web, visit
>       http://jsoftware.com/cgi-bin/mailman/listinfo/programming
> or, via email, send a message with subject or body 'help' to
>       [email protected]
> 
> You can reach the person managing the list at
>       [email protected]
> 
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Programming digest..."
> 
> 
> Today's Topics:
> 
>    1. Re: Moving averages of data (Matthew Brand)
>    2. Re: Moving averages of data (PackRat)
>    3. Re: Moving averages of data (Sherlock, Ric)
> 
> 
> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Sun, 29 Nov 2009 09:54:23 +0000
> From: Matthew Brand <[email protected]>
> Subject: Re: [Jprogramming] Moving averages of data
> To: Programming forum <[email protected]>
> Message-ID:
>       <[email protected]>
> Content-Type: text/plain; charset=UTF-8
> 
> Might be a good idea to have MA, SMA, WMA and EWMA  (+ anymore !?) in
> a library file somewhere so that people can just say:
> 
> require 'movingAverages' NB. Or whatever the J team want to call it.
> 
> I suspect that a lot of J users are re-inventing the wheel on this
> one. And in my case at least, probably doing a bad job of it!
> 
> 2009/11/29 Fraser Jackson <[email protected]>:
> > In J we often have both arguments arrays
> >
> > When the shape of one is a prefix of the shape of the other, the following
> > gives a weighted mean and two functions with consistent monadic and dyadic
> > use
> > NObs ? ?=: # : (+/ @([+((0&*)@])))
> > ?Mean ? ?=: (+/%#) : ((+/@([*]))%([NObs]))
> >
> > In the general case there is often a kernel function to generate the
> > weights. ?In that case
> > constructing a conjunction to specify the changing weights for each point 
is
> > worth the effort. ? It is then easy to explore the effect of different
> > weighting functions adn to change the points at which weights are evaluate
d.
> >
> > ----- Original Message -----
> > From: "Brian Schott" <[email protected]>
> > To: "Programming forum" <[email protected]>
> > Sent: Sunday, November 29, 2009 11:41 AM
> > Subject: Re: [Jprogramming] Moving averages of data
> >
> >
> >> Here are some ideas for weighted average (wa) and weighted moving average
> >> (wma)
> >>
> >> ? wa
> >> * %&(+/) [
> >> ? wma
> >> [ wa"1 #...@[ ]\ ]
> >> ? 1 2 wa 10 20
> >> 16.6667
> >> ? 1 1 wa 10 20
> >> 15
> >> ? 1 2 1 wa 10 20 30
> >> 20
> >> ? 1 2 1 wma 10 20 30 40 50
> >> 20 30 40
> >>
> >> A ?recent thread discussed EWMA's
> >> http://www.jsoftware.com/pipermail/programming/2009-November/016898.html
> >> ----------------------------------------------------------------------
> >> For information about J forums see http://www.jsoftware.com/forums.htm
> >
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> 
> 
> ------------------------------
> 
> Message: 2
> Date: Sun, 29 Nov 2009 12:12:28 -0600
> From: "PackRat" <[email protected]>
> Subject: Re: [Jprogramming] Moving averages of data
> To: Programming forum <[email protected]>
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset=US-ASCII
> 
> > Might be a good idea to have MA, SMA, WMA and EWMA  (+ anymore !?)
> > in a library file somewhere so that people can just say: 
> > require 'movingAverages' NB. Or whatever the J team want to call it.
> > I suspect that a lot of J users are re-inventing the wheel on this
> > one. And in my case at least, probably doing a bad job of it! 
> 
> This would be MOST helpful!  Trying to avoid reinventing the wheel is 
> why I originally asked the question.  The EMA (or EWMA) is used quite a 
> bit with stock market data and, according to several leading technical 
> analysts, is supposed to be the best MA for such analysis.  However, my 
> main reason for asking is that, as a Type 2 diabetic, I also wanted to 
> track and analyze my daily glucose levels (which I've been maintaining 
> in data files for several years) in terms of the equivalent hemoglobin 
> A1c figures (which doctors use), which would be very similar to using 
> an EMA on the data.
> 
> Harvey
> 
> 
> 
> ------------------------------
> 
> Message: 3
> Date: Mon, 30 Nov 2009 08:58:57 +1300
> From: "Sherlock, Ric" <[email protected]>
> Subject: Re: [Jprogramming] Moving averages of data
> To: Programming forum <[email protected]>
> Message-ID:
>       <f43d47796ad0844785a03f363452208468694d0...@tur-exchmbx.massey.ac.nz>
> Content-Type: text/plain; charset="utf-8"
> 
> If you gather the definitions and put them in a script, it could be added to
 the 'stats/base' addon or the 'math/misc' addon.
> 
> > From: Matthew Brand
> > 
> > Might be a good idea to have MA, SMA, WMA and EWMA  (+ anymore !?) in
> > a library file somewhere so that people can just say:
> > 
> > require 'movingAverages' NB. Or whatever the J team want to call it.
> > 
> > I suspect that a lot of J users are re-inventing the wheel on this
> > one. And in my case at least, probably doing a bad job of it!
> > 
> > 2009/11/29 Fraser Jackson <[email protected]>:
> > > In J we often have both arguments arrays
> > >
> > > When the shape of one is a prefix of the shape of the other, the
> > following
> > > gives a weighted mean and two functions with consistent monadic and
> > dyadic
> > > use
> > > NObs ? ?=: # : (+/ @([+((0&*)@])))
> > > ?Mean ? ?=: (+/%#) : ((+/@([*]))%([NObs]))
> > >
> > > In the general case there is often a kernel function to generate the
> > > weights. ?In that case
> > > constructing a conjunction to specify the changing weights for each
> > point is
> > > worth the effort. ? It is then easy to explore the effect of
> > different
> > > weighting functions adn to change the points at which weights are
> > evaluated.
> > >
> > > ----- Original Message -----
> > > From: "Brian Schott" <[email protected]>
> > > To: "Programming forum" <[email protected]>
> > > Sent: Sunday, November 29, 2009 11:41 AM
> > > Subject: Re: [Jprogramming] Moving averages of data
> > >
> > >
> > >> Here are some ideas for weighted average (wa) and weighted moving
> > average
> > >> (wma)
> > >>
> > >> ? wa
> > >> * %&(+/) [
> > >> ? wma
> > >> [ wa"1 #...@[ ]\ ]
> > >> ? 1 2 wa 10 20
> > >> 16.6667
> > >> ? 1 1 wa 10 20
> > >> 15
> > >> ? 1 2 1 wa 10 20 30
> > >> 20
> > >> ? 1 2 1 wma 10 20 30 40 50
> > >> 20 30 40
> > >>
> > >> A ?recent thread discussed EWMA's
> > >> http://www.jsoftware.com/pipermail/programming/2009-
> > November/016898.html
> > >> --------------------------------------------------------------------
> > --
> > >> For information about J forums see
> > http://www.jsoftware.com/forums.htm
> > >
> > >
> > > ---------------------------------------------------------------------
> > -
> > > For information about J forums see
> > http://www.jsoftware.com/forums.htm
> > >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> 
> ------------------------------
> 
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
> 
> End of Programming Digest, Vol 50, Issue 57
> *******************************************

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to