Category Archives: Q

msum using sliding window in kdb+

I implemented the msum verb in kdb+ using the sliding window code. First step is to find the sliding window function in q using scan operator : q)sw:{{1 _ x, y}\[x#0;y]} q)y 1 6 21 56 The first argument of … Continue reading

Posted in Q | Tagged , | 1 Comment

Generate all subsets of a string in q/KDB+

Input : “abc” Here, the elements “a”,”b”,”c” represent the elements of the set. Output : “abc”, “ab”,”ca”,”bc”,”a”,”b”,”c”,””. The output represents all subsets of the set containing the elements “abc”. Q Code : subsets:{[s1;s2] $[(count s2)=0;show s1;[.z.s[string `$s1,(first s2);1_s2];.z.s[s1;1_s2]]]} Run it … Continue reading

Posted in Q | Leave a comment

Cumulative Normal Distribution in Q/KDB+

I have implemented the numerical approximation for the normal cumulative distribution function(cdf) here. Normal cdf is very important in financial mathematics because of its applications, one of them being in Black Scholes pricing. Mathematically, can be expressed as : Q … Continue reading

Posted in Pricing, Q, Regression | Tagged , | 2 Comments

N Queens : all possible solutions in q/kdb+

I have written the code to generate all possible solutions for the N Queens problem. I have used backtracking approach, which allows me to generate all the solutions. Whenever, a solution is found I print it and then backtrack to … Continue reading

Posted in KDB, Q | Tagged , | Leave a comment

Equality of Strings in q/KDB+

I came across an error and in the process of solving it discovered more about regular expressions in q. I will list the problem first. I created a fictitious table named t for the same. q)t sym price volume ——————— … Continue reading

Posted in Q, Regular Expressions | Tagged | Leave a comment

Shorter Code to Generate all Permutations of a string in q/KDB+

My new Solution : perm:{[s] $[(count s)=1;s;raze {(first x),/:perm 1_x}each (rotate[1]\)s]} My old Solution : perm:{[s] $[(count s)=1;s;[p:(rotate[1]\)s;raze((string first each p),/:’perm each 1_/:p)]]} Variant of my old Solution : perm:{[s] $[(count s)=1;s;[p:(rotate[1]\)s;raze((string first each p),/:’.z.s each 1_/:p)]]} The .z.s function … Continue reading

Posted in KDB, Q | Tagged | Leave a comment

Generate all permutations in q/KDB+

My solution : perm:{[s] $[(count s)=1;s;[p:(rotate[1]\)s;raze((string first each p),/:’perm each 1_/:p)]]} Results : perm “a” “a” perm “ab” “ab” “ba” perm “abc” “abc” “acb” “bca” “bac” “cab” “cba” The kdb+ personal developers blog contains a thread on generating all the … Continue reading

Posted in Q, Regular Expressions | Tagged , | 2 Comments

Exponential Smoothing in q/KDB+

The formula for exponential smoothing is : S(1) = Y(0) S(t) = Alpha * Y(t) + (1-Alpha) S(t-1) 1) The corresponding solution in Q is : es:{{(x*z)+(1-x)*y}[x]\[y]} where, x is the smoothing factor(Alpha) and y is the list containing the … Continue reading

Posted in Pricing, Q, Regression, Time Series | Tagged , , | Leave a comment

Publication on KDB

Its a lazy sunday morning and the first time I felt that high temperatures are coming back to the city. Winter is over. Perfect time, not to go out of home, cozily sit in your room and do some random … Continue reading

Posted in KDB, Q | Tagged | Leave a comment

Bond Vauation – Present Value Approach in Q/KDB+

Let, c denote the cash flow, t denote the time frame, d denote the discount rate, pv denote the present value of the cash flows, M the face value then PV = (c/d + c/d^2 + … + c/d^n) + … Continue reading

Posted in KDB, Pricing, Q | Tagged | Leave a comment