Qlik Sense Set Analysis (A or B) and (C or D) - qliksense

Trying to write a function to pull counts for Items sold in (Region A or Region B) AND (Region C or Region D). An intersection between these 2 sets
count({<Item={'Region A', 'Region B'} * {'Region C', 'Region D'}>} Item)
This is not working.
Example:
[O,R,S]
[O1,A,10]
[O2,B,20]
[O2,D,20]
[O1,C,10]
[O3,A,25]
[O4,B,35]
Expected result:
Only count: O1 since it was sold in region A and C and O2 since it was sold in region B and D. Do not count O3, O4
Final count would be 2

You could try:
=count({<Order={'Region A', 'Region B'}>} Orders)
+
count({<Order={'Region C', 'Region D'}>} Orders)
That way you are just adding results of both.

Asked the same question on the Qlik community site and got a correct answer:
sum(aggr(count({< Region = {'A', 'B'}>} Region) * count({< Region = {'C', 'D'}>} Region), Item))

Related

Plotting ATR as hline

i want two horizontal lines on chart with marks day highest and lowest according to ATR.
Let’s say ATR (14) is 1$ and market opens at 10$.
So, one line at day highest 11$ (market open + ATR -> 10$ + 1$)
Other line at day lowest 9$ (market open – ATR -> 10$ - 1$)
If I use plot(l_atr') it works, but if I use hline I get an error: line 13: Cannot call 'hline' with argument 'price'='l_atr'. An argument of 'series float' type was used but a 'input float' is expected.
I’m not sure what it means with “input float”.
How can I fix this?
//#version=5
indicator("Arego ATR", overlay=true)
o = request.security("", "D", open, lookahead=barmerge.lookahead_on)
atr = request.security("", "D", ta.atr(14))
l = request.security("", "D", low, lookahead=barmerge.lookahead_on)
h = request.security("", "D", high, lookahead=barmerge.lookahead_on)
l_atr = o + (h - o) - atr
h_atr = o + (l - o) + atr
hline(l_atr)
//plot(h_atr)
//plot(l_atr)

How does wavg work in depth using a vectorised approach?

The objective of the snippet below is to evaluate weighted mid for n levels of an order book. The code snippet is from the book Machine Learning and Big Data with kdb+/q (2020 Wiley).
n:10;
quote: ([] sym: n?`A`B; time: asc n?0t; bid1: n?10f; bidSize1: n?100 );
update bid2: 0 | bid1 - .1 * n ? 10, bidSize2: n?100, ask1: bid1 + .2 * n ? 10, askSize1: n?100 from quote;
update ask2: ask1 + .1 * n ? 10, askSize2: n?100 from `quote;
select sym,time, wmid: ( bidSize1; bidSize2; askSize1; askSize2 ) wavg (bid1; bid2; ask1; ask2) from quote
I would like to understand a generic rule for how the wavg method works in-depth for lists of vectors. Could you please help me? Appreciate your help.
There are docs here on wavg https://code.kx.com/q/ref/avg/#wavg.
From these docs we can see that calling wavg is the equivalent to the function {(sum x*y)%sum x}
Using your example:
q)res1:select sym,time, wmid: ( bidSize1; bidSize2; askSize1; askSize2 ) wavg (bid1; bid2; ask1; ask2) from quote;
q)res2:select sym,time, wmid:{(sum x*y)%sum x} [( bidSize1; bidSize2; askSize1; askSize2 );(bid1; bid2; ask1; ask2)] from quote;
q)res1 ~ res2
1b
So in the case of your example we multiply bidSize1bid, bidSize2bid2, etc. , sum this result, then divide by the sums of our sizes e.g. (bidSize1[0]+ bidSize2[0] + askSize1[0] + askSize2[0];(bidSize1[1]+ bidSize2[1] + askSize1[1] + askSize2[1]; etc...)
I'm not sure if there is a more a general way of describing this but the above may help understand the nuts and bolts of what's going on

Turing Machine to Accept String from a 3 character alphabet

I need to create a turing machine that accepts the language a^1 b^j c^k where i >= j >= k, but I am not even really sure how to start. Turing machines in this context are a hard concept for me to grasp for some reason.
Turing machines can read and write to the tape and move back and forth over it. If you had a line of three colors of marbles how would you see if they arranged like the strings in your language? You could verify they're in order and then separately count each color and make sure the relationships hold. "greater than or equal to" is a binary relation so you'd probably check both pairs separately. This is really easy to envision using three extra tapes:
scan from left to right to make sure a comes first, then b, then c, then go back to the beginning
scan right counting the a's, writing one a to extra tape #1 for each a you read on the input tape
continue scanning using extra tape #2 to count b
continue scanning using extra tape #3 to count c
reset all tape heads
scan right to make sure extra tape #1 has more stuff in it than extra tape #2
reset all tape heads
scan right to make sure extra tape #2 has more stuff in it than extra tape #3
If we don't want to use extra tapes, how can we proceed? Well, we can go ahead and make sure the symbols are in the right order first... makes the rest neater. Then, we can "cross off" pairs of a and b until all b are exhausted (if we exhaust all a first, then halt_reject); then, un-cross the b and cross off pairs of b and c until you run out of c (if you run out of b first, halt_reject). Something like...
q t q' t' d
q0 # q1 # right //
q1 a q1 a right //
q1 b q2 b right //
q1 # q4 # left //
q2 b q2 b right // verify subset of
q2 c q3 c right // a*b*c*
q2 # q4 # left //
q3 c q3 c right //
q3 # q4 # left //
q4 a q4 a left //
q4 b q4 b left // reset input
q4 c q4 c left // tape to start
q4 # q5 # right //
q5 a q5 a right //
q5 A q5 A right // change susbtring a^j b^j
q5 b q6 B left // into substring A^j b^j
q5 B q5 B right // if run out of a, crash
q5 c q7 C left // if run out of b and no c, accept
q5 # h_a # left // if run out of b and c, continue
q6 a q5 A right //
q6 A q6 A left //
q6 B q6 B left //
q7 B q8 D right //
q7 C q7 C left // change substring B^k c^k
q7 D q7 D left // to substring D^k c^k
q8 D q8 D right // if run out of B, crash
q8 C q8 C right // if run out of c, accept
q8 c q7 C left //
q8 # h_a # left //
Example 1: aaabbc
(q0, [#]aaabbc#) -> (q1, #[a]aabbc#) -> (q1, #a[a]abbc#) //
-> (q1, #aa[a]bbc#) -> (q1, #aaa[b]bc#) -> (q2, #aaab[b]c#) // a*b*c*
-> (q2, #aaabb[c]#) -> (q3, #aaabbc[#]) -> (q4, #aaabb[c]#) //
-> (q4, #aaab[b]c#) -> (q4, #aaa[b]bc#) -> (q4, #aa[a]bbc#) //
-> (q4, #a[a]abbc#) -> (q4, #[a]aabbc#) -> (q4, [#]aaabbc#) // reset
-> (q5, #[a]aabbc#) //
-> (q5, #a[a]abbc#) -> (q5, #aa[a]bbc#) -> (q5, #aaa[b]bc#) //
-> (q6, #aa[a]Bbc#) -> (q5, #aaA[B]bc#) -> (q5, #aaAB[b]c#) // a^j b^j
-> (q6, #aaA[B]Bc#) -> (q6, #aa[A]BBc#) -> (q6, #a[a]ABBc#) // A^j B^j
-> (q5, #aA[A]BBc#) -> (q5, #aAA[B]Bc#) -> (q5, #aAAB[B]c#) //
-> (q5, #aAABB[c]#) -> (q7, #aAAB[B]C#) //
-> (q8, #aAABD[C]#) -> (q8, #aAABDC[#]) -> (ha, #aAABD[C]#) // B^k c^k
// D^k C^k

q/KDB - nprev function to get all the previous n elements

I am struggling to write a nprev function in KDB; xprev function returns the nth element but I need all the prev n elements relative to the current element.
q)t:([] i:1+til 26; s:.Q.a)
q)update xp:xprev[3;]s,p:prev s from t
Any help is greatly appreciated.
You can achieve the desired result by applying prev repeatedly and flipping the result
q)n:3
q)select flip 1_prev\[n;s] from t
s
-----
" "
"a "
"ba "
"cba"
"dcb"
"edc"
..
If n is much smaller than the rows count, this will be faster than some of the more straightforward solutions.
The xprev function basically looks like this :
xprev1:{y til[count y]-x} //readable xprev
We can tweak it to get all n elements
nprev:{y til[count y]-\:1+til x}
using nprev in the query
q)update np: nprev[3;s] , xp1:xprev1[3;s] , xp: xprev[3;s], p:prev[s] from t
i s np xp1 xp p
-------------------
1 a " "
2 b "a " a
3 c "ba " b
4 d "cba" a a c
5 e "dcb" b b d
6 f "edc" c c e
k equivalent of nprev
k)nprev:{$[0h>#y;'`rank;y(!#y)-\:1+!x]}
and similarly nnext would look like
k)nnext:{$[0h>#y;'`rank;y(!#y)+\:1+!x]}

Crystal Reports Cross-tab with mix of Sum, Percentages and Computed values

Being new to crystal, I am unable to figure out how to compute rows 3 and 4 below.
Rows 1 and 2 are simple percentages of the sum of the data.
Row 3 is a computed value (see below.)
Row 4 is a sum of the data points (NOT a percentage as in row 1 and row 2)
Can someone give me some pointers on how to generate the display as below.
My data:
2010/01/01 A 10
2010/01/01 B 20
2010/01/01 C 30
2010/02/01 A 40
2010/02/01 B 50
2010/02/01 C 60
2010/03/01 A 70
2010/03/01 B 80
2010/03/01 C 90
I want to display
2010/01/01 2010/02/01 2010/03/01
========== ========== ==========
[ B/(A + B + C) ] 20/60 50/150 80/240 <=== percentage of sum
[ C/(A + B + C) ] 30/60 60/150 90/240 <=== percentage of sum
[ 1 - A/(A + B + C) ] 1 - 10/60 1 - 40/150 1 - 70/240 <=== computed
[ (A + B + C) ] 60 150 250 <=== sum
Assuming you are using a SQL data source, I suggest deriving each of the output rows' values (ie. [B/(A + B + C)], [C/(A + B + C)], [1 - A/(A + B + C)] and [(A + B + C)]) per date in the SQL query, then using Crystal's crosstab feature to pivot them into the output format desired.
Crystal's crosstabs aren't particularly suited to deriving different calculations on different rows of output.