Idiomatic Q

Computer scientist Arthur Whitney invented the programing language Q and the time series database KDB+. Which has now become the most widely used timeseries database among Quants all over the world. Major banks, hedge funds widely use this technology, mainly due to its efficiency.

Q is a product of radical novelty. With a language grammar that challenges norms set out by preceding languages such as Java, C.

Only way to embrace the radical novelty is approaching it in the idiomatic way. Best way to learn Q is to forget about what you know about your core competence language, which for me is Java.

Like this wise monk said, empty your cup.

empty-your-cpu

Q-uirky 101

I always tell this joke that Q stands for quirky. This post is not about comprehensive guide for all data structures utility methods Q has to offer. But to illustrate the Q-uirkiness, the simplicity behind this sophisticated language.

quirky

Variable assignment

= is the test operator

q)2=1
0b
q)

: is the assignment operator

q)q:1
q)q
1

Evaluating expressions/statements

Expressions and Statements are evaluated right-to-left. Or more cheeky way of saying this is left-of-right.

q)show b:1+a:42
43
q)

This statement is interpreted as

  1. a is assigned to 42
  2. add 1 to a
  3. Assign the result to b
  4. show value of b

Temporal types - Date & Timespan

Date are represented using integers. 2000.01.01 is the magic day which has the integer value 0.

q)2000.01.01=0
1b
q)
q)2000.01.01+4
2000.01.05
q)2000.01.01-365
1999.01.01
q)

Similar to date for timespan 00:00:00 is the integer value 0.

q)00:00:00=0
1b
q)

Data structures

Only base data structure Q has is list. All Other data structures are derived using list.

q)1 2 3
1 2 3

For an example dictionary data structure can be viewed as two list with same length.

q)`a`b`c!1 2 3
a| 1
b| 2
c| 3
q)

Functions

Definitions & Invocation

Defining functions has similar anatomy to list.

Let’s say you are task with writing function to get sum of square of two value.

q)pyth:{[x;y] a:x*x;b:y*y;a+b}
q)pyth[3;4]
25

Function signature for arguments is a list

[x;y]

Expressions and statements are also a list

a:x*x;b:y*y;a+b

And invoking function with arguments, the arguments are passed as a list

[3;4]

Over

In q / is called the over, where function is applied and accumulated to a list. {[x;y] x+y} is the function, and applied to list (1; 2; 3) and accumulated with 0 as the base value.

q)0 {[x;y] x+y}/ 1 2 3
6

over without the base value.

q)({[x;y] x+y}/) 1 2 3
6

Overload of over to apply function specified amount of times

Here the {[x] x, sum -2#x} is applied on 10 times on list 1 1

q)10 {[x] x, sum -2#x}/ 1 1 
1 1 2 3 5 8 13 21 34 55 89 144

Scan

Scan is sibling of the over, where list with intermediate accumulation value thus far is created from the original list

Scan returns a list of smallest value thus far found in the original list

q)list1:1 2 -9 4 5 10 0 2 3
q)(&\) list1 
1 1 -9 -9 -9 -9 -9 -9 -9

Table - A Flipped dictionary

Dictionary is composed of two list with same length.

Imaging dictionary with two list first list each item is a symbol. And each item in the second list is a list as well. All these list have the same length as well. When you flip such dictionary you get a table.

Example:

Dictionary c1c2!(10 20 30; 1.1 1.2 1.3) is two list of same length c1c2 and (10 20 30; 1.1 1.2 1.3) Items in the second list is lists as well (10 20 30) and (1.1 1.2 1.3)

q)flip `c1`c2!(10 20 30; 1.1 1.2 1.3)
c1 c2
------
10 1.1
20 1.2
30 1.3
q)dict1:`c1`c2!(1 2 3; `aeron`artio`sbe)
q)
q)flip dict1
c1 c2   
--------
1  aeron
2  artio
3  sbe  
q)
q)stack_table[`c1]
1 2 3
q)stack_table[0]
c1| 1
c2| `aeron
q)

26 May 2024 - Isuru