I currently am going through some examples of J, and am trying to do an exponential moving average.
For the simple moving average I have done as follows:
sma =: +/%[
With the following given:
5 sma 1 2 3 4 5
1.2 1.4 1.6 1.8 2
After some more digging I found an example of the exponential moving average in q.
.q.ema:{first[y]("f"$1-x)\x*y}
I have tried porting this to J with the following code:
ema =: ({. y (1 - x)/x*y)
However this results in the following error:
domain error
| ema=:({.y(1-x) /x*y)
This is with x = 20
, and y
an array of 20 random numbers.
A couple of things that I notice that might help you out.
1) When you declare a verb explicitly you need to use the :
Explicit conjunction and in this case since you have a dyadic verb the correct form would be 4 : 'x contents of verb y'
Your first definition of sma =: +/%[
is tacit, since no x
or y
variables are shown.
ema =: 4 : '({. y (1 - x)/x*y)'
2) I don't know q, but in J it looks as if you are trying to Insert /
a noun 1 - x
into a list of integers x * y
. I am guessing that you really want to Divides %
. You can use a gerunds as arguments to Insert but they are special nouns representing verbs and 1 - x
does not represent a verb.
ema =: 4 : '({. y (1 - x)%x*y)'
3) The next issue is that you would have created a list of numbers with (1 - x) % x * y
, but at that point y
is a number adjacent to a list of numbers with no verb between which will be an error. Perhaps you meant to use y * (1 - x)%x*y
?
At this point I am not sure what you want exponential moving average to do and hope the clues I have provided will give you the boost that you need.