KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ how to understand the below code

  • how to understand the below code

    Posted by chan_chenyanj on February 28, 2022 at 12:00 am

    hi All,

    I am new to q and have some issue in understanding the below code

     

    .quantQ.stats.kendallTauRank:{[xS;yS]
    
        //xS,yS -- arrays of values to compare the rank
    
       // aggregate concordance statictics
    
      stats: sum raze {.quantQ.stats.concordanceRoutine/:[y;(1+x?y)_x]}[t] each t: flip(xS,yS);
    
     // return kendall's tau rank
    
    :(stats[0]-stats[1])%0.5*count[xS]*count[xS]-1;
    
    }

     

    .quantQ.stats.concordanceRoutine return a tuple like 100b, 010b

     

    my understanding is t is like (num1,num2) if so, what is the each right operation doing? in particular the x,y should be numeric value then how should i read the staff inside [y;(1+x?y)_x] based on the drop, it seems like x,y are still vector

     

    chan_chenyanj replied 9 months, 1 week ago 2 Members · 1 Reply
  • 1 Reply
  • Laura

    Administrator
    March 1, 2022 at 12:00 am

    Lets see what we can infer.

    .quantQ.stats.kendallTauRank:{[xS;yS]   //xS,yS -- arrays of values to compare the rank   // aggregate concordance statictics   stats: sum raze {.quantQ.stats.concordanceRoutine/:[y;(1+x?y)_x]}[t] each t: flip(xS,yS);   // return kendall's tau rank   :(stats[0]-stats[1])%0.5*count[xS]*count[xS]-1;   }

    flip(xS,yS) tells us that xS and yS must be same-width matrices otherwise there would be nothing to flip.

    The binary lambda {.quantQ.stats.concordanceRoutine/:[y;(1+x?y)_x]} is projected on t, which thus becomes the y in the lambda.

    The lambda could equally be written {y .quantQ.stats.concordanceRoutine/:(1+x?y)_x} from which it might be clearer how, with x set to t, the binary concordance routine is being iterated: each row of t against the rows that follow it.

    Two ways to express the same iteration:

    raze{.quantQ.stats.concordanceRoutine/:[y;(1+x?y)_x]}[t] each t t .quantQ.stats.concordanceRoutine'(1+til count t)_: t 

    Line 6 defines the result. Omitting the explicit Return : and the trailing semicolon would be better style. stats would be three integers; the difference could also be written -/[2#stats].

Log in to reply.