Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- KX Community
- :
- Discussion Forums
- :
- Developer Tools
- :
- Re: how to avoid each?

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

how to avoid each?

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2018.04.07 09:29 PM

Is there any trick to avoid using each?

I found out this by implementing simple normal inverse function that each is fairly slow to use. It seems that one should avoid each as much as possible to take leverage of the vectorisation in Q.

However, for an example below, taking a Poisson distribution

(exp neg lambda) * prd (x#lambda) % 1 + til x

til itself is not vectorisable... i.e. If i put a vector after til, it will not work.

Any advice?

Thanks

5 REPLIES 5

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2018.04.08 08:23 AM

yes, this is an example where eaching is necessary.

you can hide this fact from the end user by allowing them to pass a vector of parameters and handling it within the function.

for example:

poisson:{[l;k]

if[0<max type each (l;k);:.z.s'[l;k]];

p:exp[neg l] * l xexp k;

p%:prd 1+til k;

p}

q)poisson[2.5] 1

0.2052125

q)poisson[2.5] til 5

0.082085 0.2052125 0.2565156 0.213763 0.1336019

q)poisson[til 5] 1

0 0.3678794 0.2706706 0.1493612 0.07326256

poisson:{[l;k]

if[0<max type each (l;k);:.z.s'[l;k]];

p:exp[neg l] * l xexp k;

p%:prd 1+til k;

p}

q)poisson[2.5] 1

0.2052125

q)poisson[2.5] til 5

0.082085 0.2052125 0.2565156 0.213763 0.1336019

q)poisson[til 5] 1

0 0.3678794 0.2706706 0.1493612 0.07326256

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2018.05.01 08:42 AM

For normal and its inverse, i used rational approximation so the code can be easily vectorised.

It seems there is no other way in this case. (unless there is a rational approximation for discrete pdf....)

Thanks anyway.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2018.05.01 12:38 PM

Hi,

For discrete distribution like Poisson, think you can vectorize the power/factorial bit, that should be much quicker.

p[1.5;1 2 3 4 5]

For large lambda (>50) you can use normal pdf with correct to approximate it anyway.

Regards,

Xi

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2018.05.09 11:02 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2018.05.27 03:09 PM

But I guess this is the most Q way of doing it.

Related Content

- What is a script? What is a library? How should I structure my project? in Community Blogs
- How long should a name be? in New kdb+/q users question forum
- Surveillance in the Cloud in Community Blogs
- KX v DBOps Benchmark Results by Ferenc Bodon in Community Blogs
- Finding primes with q in Community Blogs

Main Office Contacts

**EMEA**

Tel: +44 (0)28 3025 2242

**AMERICAS**

Tel: +1 (212) 447 6700

**APAC**

Tel: +61 (0)2 9236 5700

Useful Information

Resources

Popular Links

Follow Us

KX. All Rights Reserved.

KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.