avg
, avgs
, mavg
, wavg
¶
Averages
avg
¶
Arithmetic mean
avg x avg[x]
Where x
is a numeric or temporal list,
returns the arithmetic mean as a float.
The mean of an atom is its value as a float.
Null is returned if x
is empty, or contains both positive and negative infinity. Where x
is a vector null items are ignored.
q)avg 1 2 3
2f
q)avg 1 0n 2 3 / vector: null items are ignored
2f
q)avg (1 2;0N 4) / nested: null items are preserved
0n 3
q)avg 1.0 0w
0w
q)avg -0w 0w
0n
q)avg 101b
0.6666667
q)avg 1b
1f
q)\l trade.q
q)show select ap:avg price by sym from trade
sym| ap
---| -----
a | 10.75
avg
is an aggregate function, equivalent to {sum[x]%count x}
.
domain: b g x h i j e f c s p m d z n u v t
range: f . f f f f f f f . f f f f f f f f
avg
is a multithreaded primitive.
avgs
¶
Running averages
avgs x avgs[x]
Where x
is a numeric or temporal list,
returns the running averages, i.e. applies function avg
to successive prefixes of x
.
q)avgs 1 2 3 0n 4 -0w 0w
1 1.5 2 2 2.5 -0w 0n
avgs
is a uniform function, equivalent to (avg\)
.
domain: b g x h i j e f c s p m d z n u v t
range: f . f f f f f f . . f f f f f f f f
mavg
¶
Moving averages
x mavg y mavg[x;y]
Where
x
is a positive int atom (not infinite)y
is a numeric list
returns the x
-item simple moving averages of y
, with any nulls after the first item replaced by zero. The first x
items of the result are the averages of the terms so far, and thereafter the result is the moving average. The result is floating point.
q)2 mavg 1 2 3 5 7 10
1 1.5 2.5 4 6 8.5
q)5 mavg 1 2 3 5 7 10
1 1.5 2 2.75 3.6 5.4
q)5 mavg 0N 2 0N 5 7 0N / nulls after the first are replaced by 0
0n 2 2 3.5 4.666667 4.666667
q)0 mavg 2 3
0n 0n
mavg
is a uniform function.
Domain and range:
b g x h i j e f c s p m d z n u v t
----------------------------------------
b | f . f f f f f f . . f f f f f f f f
g | . . . . . . . . . . . . . . . . . .
x | f . f f f f f f . . f f f f f f f f
h | f . f f f f f f . . f f f f f f f f
i | f . f f f f f f . . f f f f f f f f
j | f . f f f f f f . . f f f f f f f f
e | . . . . . . . . . . . . . . . . . .
f | . . . . . . . . . . . . . . . . . .
c | . . . . . . . . . . . . . . . . . .
s | . . . . . . . . . . . . . . . . . .
p | . . . . . . . . . . . . . . . . . .
m | . . . . . . . . . . . . . . . . . .
d | . . . . . . . . . . . . . . . . . .
z | . . . . . . . . . . . . . . . . . .
n | . . . . . . . . . . . . . . . . . .
u | . . . . . . . . . . . . . . . . . .
v | . . . . . . . . . . . . . . . . . .
t | . . . . . . . . . . . . . . . . . .
Range: f
wavg
¶
Weighted average
x wavg y wavg[x;y]
Where
x
is a numeric listy
is a numeric list
returns the average of numeric list y
weighted by numeric list x
. The result is a float atom.
q)2 3 4 wavg 1 2 4
2.666667
q)2 0N 4 5 wavg 1 2 0N 8 / nulls in either argument ignored
6f
q)0 wavg 2 3
0n / since 4.1t 2021.09.03,4.0 2021.10.01, previously returned 2.5
q)0 wavg (1 2;3 4)
0n 0n / since 4.0/4.1 2024.07.08, previously returned 0n
Where x
and y
conform, the result has an atom for each sublist.
q)(1 2;3 4) wavg (500 400; 300 200)
350 266.6667
The financial analytic known as VWAP (volume-weighted average price) is a weighted average.
q)select size wavg price by sym from trade
sym| price
---| -----
a | 10.75
wavg
is an aggregate function, equivalent to {(sum x*y)%sum x}
.
Domain and range:
b g x h i j e f c s p m d z n u v t
----------------------------------------
b | f . f f f f f f f . f f f f f f f f
g | . . . . . . . . . . . . . . . . . .
x | f . f f f f f f f . f f f f f f f f
h | f . f f f f f f f . f f f f f f f f
i | f . f f f f f f f . f f f f f f f f
j | f . f f f f f f f . f f f f f f f f
e | f . f f f f f f f . f f f f f f f f
f | f . f f f f f f f . f f f f f f f f
c | f . f f f f f f f . f f f f f f f f
s | . . . . . . . . . . . . . . . . . .
p | f . f f f f f f f . f f f f f f f f
m | f . f f f f f f f . f f f f f f f f
d | f . f f f f f f f . f f f f f f f f
z | f . f f f f f f f . f f f f f f f f
n | f . f f f f f f f . f f f f f f f f
u | f . f f f f f f f . f f f f f f f f
v | f . f f f f f f f . f f f f f f f f
t | f . f f f f f f f . f f f f f f f f
Range: f
wavg
is a multithreaded primitive.
Implicit iteration¶
avg
, avgs
, and mavg
apply to dictionaries and tables.
wavg
applies to dictionaries.
q)k:`k xkey update k:`abc`def`ghi from t:flip d:`a`b!(10 21 3;4 5 6)
q)avg d
7 13 4.5
q)avg t
a| 11.33333
b| 5
q)avg k
a| 11.33333
b| 5
q)avgs t
a b
------------
10 4
15.5 4.5
11.33333 5
q)2 mavg k
k | a b
---| --------
abc| 10 4
def| 15.5 4.5
ghi| 12 5.5
q)1 2 wavg d
6 10.33333 5
Mathematics
Weighted average mean
Volume-weighted average price (VWAP)