cjm2671
cjm2671

Reputation: 19476

What's the easiest way of making a histogram in KDB?

If I've got a list of values x, what's the easiest way to make a histogram with bin-size b?

It seems like I could hack a complicated solution, but I feel like there must be a built-in function for this somewhere that I don't know about.

Upvotes: 2

Views: 1223

Answers (2)

nikeros
nikeros

Reputation: 3379

Assuming you have a list of x values (let's assume x = 1000):

v:1000?1.0;

You can achieve what you need as follows:

b:0.1;
hist:(count') group xbar[b;v];

There are two points:

  1. the keys in hist are not sorted
  2. For the bucket, do you prefer to output the left or the right delimiter?

To solve for 1), you simply do:

hist:(asc key hist)#hist;

To solve for 2) - I mean, if you want to have the right delimiter:

hist:(+[b;key hist])!value hist;

Upvotes: 1

Anton Dovzhenko
Anton Dovzhenko

Reputation: 2569

I haven't heard about built-in histogram so far. But I would approach this task like below.

For fixed bucket size:

a: 0.39 0.51 0.51 0.4 0.17 0.3 0.78 0.53 0.71 0.41;
b: 0.1;
{count each group x xbar y}[b;a]
// returns 0.3 0.5 0.4 0.1 0.7!2 3 2 1 2j

For "floating" buckets:

a: 0.39 0.51 0.51 0.4 0.17 0.3 0.78 0.53 0.71 0.41;
b: -1 0.5 0.7 1;
{count each group x@x bin y}[b;a]
// returns -1 0.5 0.7!5 3 2j

Above functions return dictionary with bucket starts as keys and number of bucket occurrences as values.

Upvotes: 3

Related Questions