Modal

Modal

Return to topic list

Fri Jan 5 13:46:37 2018   by   wyrdR
Hi Torben,

Happy New Year!

Last year I created set of functions to return all Modal numbers [the most frequently appearing numbers in a set. While it appears to almost work, I am certain it is slow and unwieldy.

I wonder if you can:
a. Suggest any improvements for my functions;
b. Consider making a built-in operator similar to maximal & minimal that returns the modal numbers.

Thanks in advance,

wyrdR

------------------------------

\ Modal
\ wyrdR 2018.01.05
\ List the most frequent items in a collection (mode)

function freq(collection)=
\ List of frequencies
    foreach item in (different collection)
    do
        count (item = collection)

function withfreq(collection,freq) =
\ Find item in collection with a given frequency
    freq & 
    foreach item in (different collection)
    do
      ((count (item = collection)) keep freq)
      & item

function modal(collection)=
\ Get the list of mode(s) if any
  call withfreq(
    collection,
    max (call freq(collection))
  )

M:=
  5d6
;

[M, call modal(M)]

 
Fri Jan 5 16:06:31 2018   by   wyrdR
A built-in 'freq' and 'withfreq' type operator would be cool too.

Not sure how feasible it all is, but I have been inspired by maximal, minimal & median.
 
Fri Jan 5 16:07:15 2018   by   Torben

A slightly simpler implementation of modal could be:


function modal(collection) =
  call findModal(0, {}, collection)

function findModal(bestFreq, modals, collection) =
  if collection then
    x := min collection;
    freq := count (x = collection);
    call findModal(max {freq, bestFreq},
                  {(freq >= bestFreq) & x, (freq <= bestFreq) & modals},
                  collection drop x)
  else modals



Basically, it keeps a collection of the items with the highest frequency seen so far, resetting this if a higher frequency is found.

As for implementing modal as a standard operator, I'm inclined not to because

1. I don't think it is going to be used by very many people.
2. I don't think it will be much faster if implemented as a standard operator.
 
Fri Jan 5 16:11:33 2018   by   Torben
As for making freq a standard operator, I also doubt that it will be used much, and it can be done quite easily with count, as you do.

withfreq is a bit more difficult to implement, but it will probably be used even less than freq.
 

Return to topic list



New message:
Topic:
Posted by:

Type the values of the dice shown below:

Return to topic list