Author Topic: Topic: -={ Challenge }=- Nth Percentile  (Read 3366 times)

0 Members and 1 Guest are viewing this topic.

pkohut

  • Guest
Topic: -={ Challenge }=- Nth Percentile
« on: July 17, 2010, 04:00:26 PM »
Here's a simple challenge. Write a function to get the Nth percentile for a given list of numbers.

So for the 80 percentile of
(1 2 3 4 5 6 7 8 9) would be like.
Code: [Select]
(nth 80 '(1 2 3 4 5 6 7 8 9))would be 8,

and the 75 percentile of
Code: [Select]
(nth 75 '(2 5 6 3 1 9 7 10 14 3 12 8 4 11 13 5))??




« Last Edit: July 17, 2010, 07:27:28 PM by pkohut »

LE3

  • Guest
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #1 on: July 17, 2010, 06:19:41 PM »
:-o ... I get for the first: 7.4 and from the next: -33.0 --- :)  :-(
Code: [Select]
(defun floor  (num)
  (if (>= (abs (- num (fix num))) 0.5)
    (if (minusp num)
      (1- (fix num))
      (1+ (fix num)))
    (fix num)))

(defun percentile (p lst / rlst pos left right n part)
  (if (>= p 100.0)
    (setq rlst (nth (- (length lst) 1) lst))
  )

  (if (not rlst)
    (progn

      (setq pos (/ (* (+ (length lst) 1) p) 100.0))
     
      (setq left
     0.0
    right 0.0
      )
     
      (setq n (+ (* (/ p 100.0) (- (length lst) 1)) 1.0))

      (if (>= pos 1)
(progn
  (setq left (nth (- (floor n) 1) lst))
  (setq right (nth (floor n) lst))
)
(progn
  (setq left (nth 0 lst))
  (setq right (nth 1 lst))
)
      )

      (if (= left right)
left
(progn
  (setq part (- n (floor n)))
  (* (+ left part) (- right left))
)
      )
    )
  )
)

Here's a simple challenge. Write a function to get the Nth percentile for a given list of numbers.

So for the 80 percentile of
(1 2 3 4 5 6 7 8 9) would be like.
Code: [Select]
(nth 80 '(1 2 3 4 5 6 7 8 9))would be 8,

and the 75 percentile of
Code: [Select]
(nth 75 '(2 5 6 3 1 9 7 10 14 3 12 8 4 11 13 5))??



pkohut

  • Guest
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #2 on: July 17, 2010, 07:05:18 PM »
:-o ... I get for the first: 7.4 and from the next: -33.0 --- :)  :-(

Good try Luis. Need to pluck the element from the list that represents the Nth percentile.

LE3

  • Guest
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #3 on: July 17, 2010, 07:21:22 PM »
:-o ... I get for the first: 7.4 and from the next: -33.0 --- :)  :-(

Good try Luis. Need to pluck the element from the list that represents the Nth percentile.
I'll check this, my lisp skills are way out of date...

pkohut

  • Guest
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #4 on: July 17, 2010, 07:23:00 PM »
:-o ... I get for the first: 7.4 and from the next: -33.0 --- :)  :-(

Good try Luis. Need to pluck the element from the list that represents the Nth percentile.

I muffed up the original answer to the first list. It is 7.

The answer to the second list is 10 11. (arg-doing this on my calculator and then looking at a zero index list is messing me up)
« Last Edit: July 17, 2010, 07:31:05 PM by pkohut »

pkohut

  • Guest
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #5 on: July 17, 2010, 08:24:49 PM »
My entry below. Scroll to peek.
Code: [Select]






































;;; Unsure if this is the right logic
(defun round (num)
  (if (minusp num)
    (fix (- num 0.5))
    (fix (+ num 0.5))
  )
  )

(defun percentile (num li)
  (setq index (round (+ (* (/ (length li) 100.0) num) 0.5)))
  (setq indexed_array (vl-sort-i li '<))
  (if (minusp index)
    nil
    (nth (nth (- index 1) indexed_array) li)
    )
  )

LE3

  • Guest
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #6 on: July 17, 2010, 09:43:13 PM »
the best (if it is) results I get are - read and noticed after peeking in your code posted that the list must be a sorted one:
Quote
75
(2 5 6 3 1 9 7 10 14 3 12 8 4 11 13 5)
(1 2 3 4 5 6 7 8 9 10 11 12 13 14)
Resultant=10.75

80
(1 2 3 4 5 6 7 8 9)
(1 2 3 4 5 6 7 8 9)
Resultant=7.4
_$ 

pkohut

  • Guest
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #7 on: July 17, 2010, 10:11:41 PM »
http://en.wikipedia.org/wiki/Percentile
Quote
One definition, usually given in texts, is that the p-th percentile of N  ordered values is obtained by first calculating the rank n = (N/100)*P + 0.5, rounding to the nearest integer, and taking the value that corresponds to that rank.

Note, the ranks have to be in ascending order.

This is one of a small number of formulas needed to assign a roughness value to a sampled set of points (x, y, z). I figured I'd give each formula as a separate challenge, then put them all together for the final package assembly. Figured it would be easier (and more participants) it done that way instead of posting the whole criteria first (might scare to many people away).

LE3

  • Guest
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #8 on: July 17, 2010, 10:17:38 PM »
Yes, I went exactly to that place before started.... but I am doing some C# stuff at the same time here, so did not put much of attention, I will as soon as I can, and also as for more participants it is the weekend, and they have a life :)


http://en.wikipedia.org/wiki/Percentile
Quote
One definition, usually given in texts, is that the p-th percentile of N  ordered values is obtained by first calculating the rank n = (N/100)*P + 0.5, rounding to the nearest integer, and taking the value that corresponds to that rank.

Note, the ranks have to be in ascending order.

This is one of a small number of formulas needed to assign a roughness value to a sampled set of points (x, y, z). I figured I'd give each formula as a separate challenge, then put them all together for the final package assembly. Figured it would be easier (and more participants) it done that way instead of posting the whole criteria first (might scare to many people away).


Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #9 on: July 18, 2010, 08:00:27 AM »
Not sure if I'm doing this right Paul

Code: [Select]
(defun LM:Percentile ( p l )
  (nth (nth (1- (fix (1+ (* p (/ (length l) 100.))))) (vl-sort-i l '<)) l)
)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #10 on: July 18, 2010, 08:04:49 AM »
Learnt something new today thanks to this challenge:

Code: [Select]
_$ (setq l '( 2 4 3 3 4 5 ))
( 2 4 3 3 4 5 )
_$ (vl-sort l '<)
( 2 3 4 5 )

Never knew that happened.
« Last Edit: July 18, 2010, 08:10:09 AM by Lee Mac »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #11 on: July 18, 2010, 08:37:29 AM »
Yes it's a real trap. Thankfully there are several workarounds
 ... I'm sure it has been discussed here previously several times.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #12 on: July 19, 2010, 04:19:52 AM »
Code: [Select]
 
(defun f (p l)
 (nth (nth (1- (fix (1+ (* p (length l) 0.01)))) (vl-sort-i l '<)) l)
)
(defun f (p l)
 (nth (nth (atoi (rtos (* p (length l) 0.01) 2 0)) (vl-sort-i l '<)) l)
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #13 on: July 19, 2010, 04:21:08 AM »
Yes it's a real trap. Thankfully there are several workarounds
 ... I'm sure it has been discussed here previously several times.


Code: [Select]
(setq l '(2 1 1 1 0))
 
(mapcar 'fix (vl-sort (mapcar 'float l) '<=))
(mapcar '(lambda (a) (nth a l)) (vl-sort-i l '<=))

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Topic: -={ Challenge }=- Nth Percentile
« Reply #14 on: July 19, 2010, 02:10:38 PM »
Yes it's a real trap. Thankfully there are several workarounds
 ... I'm sure it has been discussed here previously several times.


Code: [Select]
(setq l '(2 1 1 1 0))
 
(mapcar 'fix (vl-sort (mapcar 'float l) '<=))
(mapcar '(lambda (a) (nth a l)) (vl-sort-i l '<=))

Yes, I use the second option you posted  :-)