emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* unexpected failure on all formulas
@ 2011-09-20  0:41 Jude DaShiell
  2011-09-20  6:17 ` Nick Dokos
  0 siblings, 1 reply; 8+ messages in thread
From: Jude DaShiell @ 2011-09-20  0:41 UTC (permalink / raw)
  To: emacs-orgmode

cut here.
| Date Stamp             | Systalic | Diastalic | Pulse |
|------------------------+----------+-----------+-------|
| [2011-07-19 Tue 02:26] |      138 |        92 |    74 |
| [2011-07-20 Wed 04:03] |      130 |        85 |    74 |
| [2011-07-21 Thu 03:50] |      128 |        79 |    76 |
| [2011-07-22 Fri 02:33] |      121 |        80 |    79 |
| [2011-07-23 Sat 02:52] |      118 |        75 |    68 |
| [2011-07-24 Sun 04:13] |      126 |        83 |    78 |
| [2011-07-25 Mon 02:12] |      122 |        79 |    76 |
| [2011-07-26 Tue 01:57] |      124 |        79 |    72 |
| [2011-07-27 Wed 04:03] |      137 |        74 |    76 |
| [2011-07-28 Thu 03:47] |      136 |        80 |    76 |
| [2011-07-29 Fri 04:06] |      120 |        72 |    76 |
| [2011-07-30 Sat 03:18] |      128 |        92 |    80 |
| [2011-07-31 Sun 07:11] |      124 |        84 |    72 |
| [2011-08-01 Mon 04:55] |      124 |        77 |    69 |
| [2011-08-02 Tue 02:47] |      128 |        80 |    67 |
| [2011-08-03 Wed 06:00] |      132 |        89 |    70 |
| [2011-08-04 Thu 04:30] |      137 |        82 |    77 |
| [2011-08-05 Fri 06:10] |      124 |        86 |    77 |
| [2011-08-06 Sat 04:04] |      139 |        88 |    78 |
| [2011-08-07 Sun 04:02] |      124 |        80 |    68 |
| [2011-08-08 Mon 06:36] |      137 |        93 |    79 |
| [2011-08-10 Wed04:03]  |      120 |        80 |    68 |
| [2011-08-11 Thu 04:30] |      128 |        78 |    77 |
| [2011-08-12 Fri 06:36] |      143 |        82 |    76 |
| [2011-08-13 Sat 06:18] |      125 |        76 |    69 |
| [2011-08-14 Sun 04:04] |      123 |        70 |    69 |
| [2011-08-15 Mon 04:04] |      135 |        83 |    76 |
| [2011-08-16 Tue 04:03] |      128 |        80 |    69 |
| [2011-08-17 Wed 04:04] |      124 |        80 |    68 |
| [2011-08-18 Thu 05:58] |      136 |        87 |    72 |
| [2011-08-19 Fri 05:50] |      116 |        80 |    64 |
| [2011-08-20 Sat 06:38] |      139 |        84 |    64 |
| [2011-08-21 Sun 08:20] |      124 |        84 |    73 |
| [2011-08-22 Mon 03:50] |      138 |        76 |    70 |
| [2011-08-23 Tue 05:52] |      126 |        79 |    68 |
| [2011-08-24 Wed 04:53] |      125 |        81 |    64 |
| [2011-08-25 Thu 05:37] |      124 |        79 |    68 |
| [2011-08-26 Fri 06:41] |      124 |        87 |    74 |
| [2011-08-27 Sat 05:44] |      133 |        74 |    61 |
| [2011-08-28 Sun 03:22] |      124 |        76 |    61 |
| [2011-08-29 Mon 04:06] |      124 |        76 |    65 |
| [2011-08-30 Tue 05:52] |      139 |        87 |    71 |
| [2011-08-31 Wed 06:47] |      152 |        86 |    68 |
| [2011-09-01 Thu 06:00] |      136 |        86 |    78 |
| [2011-09-02 Fri 01:22] |      142 |       100 |    78 |
| [2011-09-03 Sat 09:12] |      126 |        79 |    70 |
| [2011-09-04 Sun 11:53] |      130 |        90 |    72 |
| [2011-09-05 Mon 06:29] |      138 |        77 |    65 |
| [2011-09-06 Tue 05:42] |      121 |        76 |    68 |
| [2011-09-07 Wed 06:47] |      131 |        77 |    68 |
| [2011-09-08 Thu 06:02] |      122 |        76 |    72 |
| [2011-09-10 Sat 05:38] |      126 |        76 |    69 |
| [2011-09-11 Sun 10:12] |      137 |        87 |    64 |
| [2011-09-12 Mon 04:00] |      135 |        78 |    89 |
| [2011-09-13 Tue 06:00] |      132 |        82 |    66 |
| [2011-09-14 Wed 06:01] |      137 |        77 |    77 |
| [2011-09-15 Thu 04:08] |      141 |        87 |    64 |
| [2011-09-17 Sat 08:35] |      121 |        78 |    66 |
| [2011-09-18 Sun 07:04] |      124 |        69 |    65 |
| [2011-09-19 Mon 06:00] |      133 |        81 |    69 |
|                        |          |           |       |
|------------------------+----------+-----------+-------|
| mode                   |          |           |       |
| Min                    |          |           |       |
| Max                    |     127. |       81. |   74. |
#+TBLFM: @>>>$2..@>>>$4=vmode(@i..@ii) :: @>>$2..@>>$4=vmin(@i..@ii) :: @>$2..@>$4=vmax(@i..@ii)


# Local Variables:
# kept-new-versions: 100
# version-control: t
# End:
cut here.
What did I do wrong with this #+TBLFM: line?

Jude <jdashiel@shellworld.net>
"I love the Pope, I love seeing him in his Pope-Mobile, his three feet
of bullet proof plexi-glass. That's faith in action folks! You know he's
got God on his side."
~ Bill Hicks

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: unexpected failure on all formulas
  2011-09-20  0:41 unexpected failure on all formulas Jude DaShiell
@ 2011-09-20  6:17 ` Nick Dokos
  2011-09-20  8:26   ` Nick Dokos
  0 siblings, 1 reply; 8+ messages in thread
From: Nick Dokos @ 2011-09-20  6:17 UTC (permalink / raw)
  To: Jude DaShiell; +Cc: nicholas.dokos, emacs-orgmode

Jude DaShiell <jdashiel@shellworld.net> wrote:


> |------------------------+----------+-----------+-------|
> | mode                   |          |           |       |
> | Min                    |          |           |       |
> | Max                    |     127. |       81. |   74. |
> #+TBLFM: @>>>$2..@>>>$4=vmode(@i..@ii) :: @>>$2..@>>$4=vmin(@i..@ii) :: @>$2..@>$4=vmax(@i..@ii)
> 
> 
> # Local Variables:
> # kept-new-versions: 100
> # version-control: t
> # End:
> cut here.
> What did I do wrong with this #+TBLFM: line?
> 

Two things:

o as Christian Moe pointed out, calc does not provide a vmode() function.

o the separator lines in the formulas have to be specified using capital
I letters.

Nick

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: unexpected failure on all formulas
  2011-09-20  6:17 ` Nick Dokos
@ 2011-09-20  8:26   ` Nick Dokos
  2011-09-20  9:15     ` Lawrence Mitchell
  2011-09-20  9:29     ` Christian Moe
  0 siblings, 2 replies; 8+ messages in thread
From: Nick Dokos @ 2011-09-20  8:26 UTC (permalink / raw)
  To: Jude DaShiell; +Cc: nicholas.dokos, emacs-orgmode

Nick Dokos <nicholas.dokos@hp.com> wrote:

> Jude DaShiell <jdashiel@shellworld.net> wrote:
> 
> 
> > |------------------------+----------+-----------+-------|
> > | mode                   |          |           |       |
> > | Min                    |          |           |       |
> > | Max                    |     127. |       81. |   74. |
> > #+TBLFM: @>>>$2..@>>>$4=vmode(@i..@ii) :: @>>$2..@>>$4=vmin(@i..@ii) :: @>$2..@>$4=vmax(@i..@ii)
> > 
> > 
> > # Local Variables:
> > # kept-new-versions: 100
> > # version-control: t
> > # End:
> > cut here.
> > What did I do wrong with this #+TBLFM: line?
> > 
> 
> Two things:
> 
> o as Christian Moe pointed out, calc does not provide a vmode() function.
> 
> o the separator lines in the formulas have to be specified using capital
> I letters.
> 

Here is an implementation of vmode - afaict, it works correctly but
that may be because I've only tested it on a single set of data. If
you find something wrong, please let me know.

--8<---------------cut here---------------start------------->8---
| Date Stamp             | Systolic | Diastolic | Pulse |
|------------------------+----------+-----------+-------|
| [2011-07-19 Tue 02:26] |      138 |        92 |    74 |
| [2011-07-20 Wed 04:03] |      130 |        85 |    74 |
| [2011-07-21 Thu 03:50] |      128 |        79 |    76 |
| [2011-07-22 Fri 02:33] |      121 |        80 |    79 |
| [2011-07-23 Sat 02:52] |      118 |        75 |    68 |
| [2011-07-24 Sun 04:13] |      126 |        83 |    78 |
| [2011-07-25 Mon 02:12] |      122 |        79 |    76 |
| [2011-07-26 Tue 01:57] |      124 |        79 |    72 |
| [2011-07-27 Wed 04:03] |      137 |        74 |    76 |
| [2011-07-28 Thu 03:47] |      136 |        80 |    76 |
| [2011-07-29 Fri 04:06] |      120 |        72 |    76 |
| [2011-07-30 Sat 03:18] |      128 |        92 |    80 |
| [2011-07-31 Sun 07:11] |      124 |        84 |    72 |
| [2011-08-01 Mon 04:55] |      124 |        77 |    69 |
| [2011-08-02 Tue 02:47] |      128 |        80 |    67 |
| [2011-08-03 Wed 06:00] |      132 |        89 |    70 |
| [2011-08-04 Thu 04:30] |      137 |        82 |    77 |
| [2011-08-05 Fri 06:10] |      124 |        86 |    77 |
| [2011-08-06 Sat 04:04] |      139 |        88 |    78 |
| [2011-08-07 Sun 04:02] |      124 |        80 |    68 |
| [2011-08-08 Mon 06:36] |      137 |        93 |    79 |
| [2011-08-10 Wed04:03]  |      120 |        80 |    68 |
| [2011-08-11 Thu 04:30] |      128 |        78 |    77 |
| [2011-08-12 Fri 06:36] |      143 |        82 |    76 |
| [2011-08-13 Sat 06:18] |      125 |        76 |    69 |
| [2011-08-14 Sun 04:04] |      123 |        70 |    69 |
| [2011-08-15 Mon 04:04] |      135 |        83 |    76 |
| [2011-08-16 Tue 04:03] |      128 |        80 |    69 |
| [2011-08-17 Wed 04:04] |      124 |        80 |    68 |
| [2011-08-18 Thu 05:58] |      136 |        87 |    72 |
| [2011-08-19 Fri 05:50] |      116 |        80 |    64 |
| [2011-08-20 Sat 06:38] |      139 |        84 |    64 |
| [2011-08-21 Sun 08:20] |      124 |        84 |    73 |
| [2011-08-22 Mon 03:50] |      138 |        76 |    70 |
| [2011-08-23 Tue 05:52] |      126 |        79 |    68 |
| [2011-08-24 Wed 04:53] |      125 |        81 |    64 |
| [2011-08-25 Thu 05:37] |      124 |        79 |    68 |
| [2011-08-26 Fri 06:41] |      124 |        87 |    74 |
| [2011-08-27 Sat 05:44] |      133 |        74 |    61 |
| [2011-08-28 Sun 03:22] |      124 |        76 |    61 |
| [2011-08-29 Mon 04:06] |      124 |        76 |    65 |
| [2011-08-30 Tue 05:52] |      139 |        87 |    71 |
| [2011-08-31 Wed 06:47] |      152 |        86 |    68 |
| [2011-09-01 Thu 06:00] |      136 |        86 |    78 |
| [2011-09-02 Fri 01:22] |      142 |       100 |    78 |
| [2011-09-03 Sat 09:12] |      126 |        79 |    70 |
| [2011-09-04 Sun 11:53] |      130 |        90 |    72 |
| [2011-09-05 Mon 06:29] |      138 |        77 |    65 |
| [2011-09-06 Tue 05:42] |      121 |        76 |    68 |
| [2011-09-07 Wed 06:47] |      131 |        77 |    68 |
| [2011-09-08 Thu 06:02] |      122 |        76 |    72 |
| [2011-09-10 Sat 05:38] |      126 |        76 |    69 |
| [2011-09-11 Sun 10:12] |      137 |        87 |    64 |
| [2011-09-12 Mon 04:00] |      135 |        78 |    89 |
| [2011-09-13 Tue 06:00] |      132 |        82 |    66 |
| [2011-09-14 Wed 06:01] |      137 |        77 |    77 |
| [2011-09-15 Thu 04:08] |      141 |        87 |    64 |
| [2011-09-17 Sat 08:35] |      121 |        78 |    66 |
| [2011-09-18 Sun 07:04] |      124 |        69 |    65 |
| [2011-09-19 Mon 06:00] |      133 |        81 |    69 |
|------------------------+----------+-----------+-------|
| mode                   |      125 |        81 |    69 |
| min                    |      116 |        69 |    61 |
| max                    |      152 |       100 |    89 |
#+TBLFM: @>>>$2..@>>>$4=vmode(@I..@II) :: @>>$2..@>>$4=vmin(@I..@II) :: @>$2..@>$4=vmax(@I..@II)

#+begin_src elisp

(defun calcFunc-vmode (&rest vecs)
  (let* ((flat (copy-sequence (cdr (math-flatten-many-vecs vecs))))
	 (p flat)
	 (len (length flat))
	 (hlen (/ len 2)))
    (if (= len 0)
	(math-reject-arg nil "*Must be at least 1 argument")
      (if (and (= len 1) (memq (car-safe (car flat)) '(sdev intv)))
	  (calcFunc-vmean (car flat))
	(while p
	  (if (eq (car-safe (car p)) 'sdev)
	      (setcar p (nth 1 (car p))))
	  (or (Math-anglep (car p))
	      (math-reject-arg (car p) 'anglep))
	  (setq p (cdr p)))
	(setq flat (sort flat 'math-lessp))
	(setq freqs (vfreqs flat))
	(cadr (maxfreq freqs))
	))))

(defun vfreqs (v)
  "v is assumed to be sorted.
Return a list of pairs: (count element)
for each distinct element of v."
  (let ((count 0)
	(prev (car v))
	(vec v)
	freqs)
    (setq freqs nil)
    (while vec
      (if (= (nth 0 vec) prev)
	  (setq count (1+ count))
	(setq freqs (cons (list count (nth 0 vec)) freqs))
	(setq count 1 prev (nth 0 vec)))
      (setq vec (cdr vec)))
    freqs))

(defun freq-greater (a b)
  (if (> (car a) (car b))
      t
    nil))

(defun maxfreq (f)
  "Sort the frequency list by count and return the first pair."
  (let ((fs (sort f 'freq-greater)))
    (nth 0 fs)))

#+end_src
--8<---------------cut here---------------end--------------->8---

I cribbed heavily from the implementation of vmedian. Although there is
no key binding, you can even use the above in an interactive Calc
session, by entering the formula in algebraic form. I yanked the vector
twice onto the Calc stack and then entered 'vmode($) with the following
result:

,----
| --- Emacs Calculator Mode ---
| 2:  [138, 130, 128, 121, 118, 126, 122, 124, 137, 136, 120, 128, 124, 124, 128, 132, 137, 124, 139, 
|      124, 137, 120, 128, 143, 125, 123, 135, 128, 124, 136, 116, 139, 124, 138, 126, 125, 124, 124, 
|      133, 124, 124, 139, 152, 136, 142, 126, 130, 138, 121, 131, 122, 126, 137, 135, 132, 137, 141, 
|      121, 124, 133]
| 1:  125
|     .
`----

Nick

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: unexpected failure on all formulas
  2011-09-20  8:26   ` Nick Dokos
@ 2011-09-20  9:15     ` Lawrence Mitchell
  2011-09-20  9:29     ` Christian Moe
  1 sibling, 0 replies; 8+ messages in thread
From: Lawrence Mitchell @ 2011-09-20  9:15 UTC (permalink / raw)
  To: emacs-orgmode

Nick Dokos wrote:

[...]

> I cribbed heavily from the implementation of vmedian. Although there is
> no key binding, you can even use the above in an interactive Calc
> session, by entering the formula in algebraic form. I yanked the vector
> twice onto the Calc stack and then entered 'vmode($) with the following
> result:


This does the wrong thing for bimodal lists of numbers, since the
correct answer in that case is the list of modes.  Here's an
elisp implementation that's a bit shorter:

(require 'cl)

(defun mode (&rest list)
  (let ((ret nil) val count)
    (loop for n in list
          if (assq n ret)
          do (incf (cdr (assq n ret)))
          else
          do
          (push (cons n 1) ret))
    (setq ret (sort ret (lambda (a b) (> (cdr a) (cdr b)))))
    (setq val (list (caar ret))
          count (cdar ret))
    (loop for (n . c) in (cdr ret)
          while (= c count)
          do (push n val))
    val))

And here's how to use it in the proffered table:

| Date Stamp             | Systalic | Diastalic | Pulse |
|------------------------+----------+-----------+-------|
| [2011-07-19 Tue 02:26] |      138 |        92 |    74 |
| [2011-07-20 Wed 04:03] |      130 |        85 |    74 |
|------------------------+----------+-----------+-------|
| min                    |      130 |        85 |    74 |
| max                    |      138 |        92 |    74 |
| mode                   | 138, 130 |    92, 85 |    74 |
#+TBLFM: @II+1$2..@II+1$4=vmin(@I..@II)::@II+2$2..@II+2$4=vmax(@I..@II)::@II+3$2..@II+3$4='(mapconcat 'number-to-string (mode @I..@II) ", ");N

Lawrence
-- 
Lawrence Mitchell <wence@gmx.li>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: unexpected failure on all formulas
  2011-09-20  8:26   ` Nick Dokos
  2011-09-20  9:15     ` Lawrence Mitchell
@ 2011-09-20  9:29     ` Christian Moe
  2011-09-20 14:59       ` Nick Dokos
  1 sibling, 1 reply; 8+ messages in thread
From: Christian Moe @ 2011-09-20  9:29 UTC (permalink / raw)
  To: nicholas.dokos; +Cc: Jude DaShiell, emacs-orgmode

Hi, Nick,

I think your vmode() needs to subtract one from the result. The mode 
in the "systolic" column of Jude's dataset should be 124 (f=12), not 
125 (f=2).

For Jude's limited purposes I got by with less code (but note the 
caveats):

#+begin_src emacs-lisp
   (defmath vmode (vec)
     "Returns the mode (most frequent value) of a unimodal vector of
   integers VEC.  WARNING: Will not work with non-integer values or
   multiple vectors. Will only return the lowest mode for multimodal
   data."
     (let ((freq (histogram vec (1+ (vmax vec)))))
             (1- (find freq (vmax freq)))))
#+end_src

Using :=vmode(@I..@II), I get 124 (systolic), 80 (diastolic), 68 (pulse).

Yours,
Christian

On 9/20/11 10:26 AM, Nick Dokos wrote:
> Nick Dokos<nicholas.dokos@hp.com>  wrote:
>
>> Jude DaShiell<jdashiel@shellworld.net>  wrote:
>>
>>
>>> |------------------------+----------+-----------+-------|
>>> | mode                   |          |           |       |
>>> | Min                    |          |           |       |
>>> | Max                    |     127. |       81. |   74. |
>>> #+TBLFM: @>>>$2..@>>>$4=vmode(@i..@ii) :: @>>$2..@>>$4=vmin(@i..@ii) :: @>$2..@>$4=vmax(@i..@ii)
>>>
>>>
>>> # Local Variables:
>>> # kept-new-versions: 100
>>> # version-control: t
>>> # End:
>>> cut here.
>>> What did I do wrong with this #+TBLFM: line?
>>>
>>
>> Two things:
>>
>> o as Christian Moe pointed out, calc does not provide a vmode() function.
>>
>> o the separator lines in the formulas have to be specified using capital
>> I letters.
>>
>
> Here is an implementation of vmode - afaict, it works correctly but
> that may be because I've only tested it on a single set of data. If
> you find something wrong, please let me know.
>
> --8<---------------cut here---------------start------------->8---
> | Date Stamp             | Systolic | Diastolic | Pulse |
> |------------------------+----------+-----------+-------|
> | [2011-07-19 Tue 02:26] |      138 |        92 |    74 |
> | [2011-07-20 Wed 04:03] |      130 |        85 |    74 |
> | [2011-07-21 Thu 03:50] |      128 |        79 |    76 |
> | [2011-07-22 Fri 02:33] |      121 |        80 |    79 |
> | [2011-07-23 Sat 02:52] |      118 |        75 |    68 |
> | [2011-07-24 Sun 04:13] |      126 |        83 |    78 |
> | [2011-07-25 Mon 02:12] |      122 |        79 |    76 |
> | [2011-07-26 Tue 01:57] |      124 |        79 |    72 |
> | [2011-07-27 Wed 04:03] |      137 |        74 |    76 |
> | [2011-07-28 Thu 03:47] |      136 |        80 |    76 |
> | [2011-07-29 Fri 04:06] |      120 |        72 |    76 |
> | [2011-07-30 Sat 03:18] |      128 |        92 |    80 |
> | [2011-07-31 Sun 07:11] |      124 |        84 |    72 |
> | [2011-08-01 Mon 04:55] |      124 |        77 |    69 |
> | [2011-08-02 Tue 02:47] |      128 |        80 |    67 |
> | [2011-08-03 Wed 06:00] |      132 |        89 |    70 |
> | [2011-08-04 Thu 04:30] |      137 |        82 |    77 |
> | [2011-08-05 Fri 06:10] |      124 |        86 |    77 |
> | [2011-08-06 Sat 04:04] |      139 |        88 |    78 |
> | [2011-08-07 Sun 04:02] |      124 |        80 |    68 |
> | [2011-08-08 Mon 06:36] |      137 |        93 |    79 |
> | [2011-08-10 Wed04:03]  |      120 |        80 |    68 |
> | [2011-08-11 Thu 04:30] |      128 |        78 |    77 |
> | [2011-08-12 Fri 06:36] |      143 |        82 |    76 |
> | [2011-08-13 Sat 06:18] |      125 |        76 |    69 |
> | [2011-08-14 Sun 04:04] |      123 |        70 |    69 |
> | [2011-08-15 Mon 04:04] |      135 |        83 |    76 |
> | [2011-08-16 Tue 04:03] |      128 |        80 |    69 |
> | [2011-08-17 Wed 04:04] |      124 |        80 |    68 |
> | [2011-08-18 Thu 05:58] |      136 |        87 |    72 |
> | [2011-08-19 Fri 05:50] |      116 |        80 |    64 |
> | [2011-08-20 Sat 06:38] |      139 |        84 |    64 |
> | [2011-08-21 Sun 08:20] |      124 |        84 |    73 |
> | [2011-08-22 Mon 03:50] |      138 |        76 |    70 |
> | [2011-08-23 Tue 05:52] |      126 |        79 |    68 |
> | [2011-08-24 Wed 04:53] |      125 |        81 |    64 |
> | [2011-08-25 Thu 05:37] |      124 |        79 |    68 |
> | [2011-08-26 Fri 06:41] |      124 |        87 |    74 |
> | [2011-08-27 Sat 05:44] |      133 |        74 |    61 |
> | [2011-08-28 Sun 03:22] |      124 |        76 |    61 |
> | [2011-08-29 Mon 04:06] |      124 |        76 |    65 |
> | [2011-08-30 Tue 05:52] |      139 |        87 |    71 |
> | [2011-08-31 Wed 06:47] |      152 |        86 |    68 |
> | [2011-09-01 Thu 06:00] |      136 |        86 |    78 |
> | [2011-09-02 Fri 01:22] |      142 |       100 |    78 |
> | [2011-09-03 Sat 09:12] |      126 |        79 |    70 |
> | [2011-09-04 Sun 11:53] |      130 |        90 |    72 |
> | [2011-09-05 Mon 06:29] |      138 |        77 |    65 |
> | [2011-09-06 Tue 05:42] |      121 |        76 |    68 |
> | [2011-09-07 Wed 06:47] |      131 |        77 |    68 |
> | [2011-09-08 Thu 06:02] |      122 |        76 |    72 |
> | [2011-09-10 Sat 05:38] |      126 |        76 |    69 |
> | [2011-09-11 Sun 10:12] |      137 |        87 |    64 |
> | [2011-09-12 Mon 04:00] |      135 |        78 |    89 |
> | [2011-09-13 Tue 06:00] |      132 |        82 |    66 |
> | [2011-09-14 Wed 06:01] |      137 |        77 |    77 |
> | [2011-09-15 Thu 04:08] |      141 |        87 |    64 |
> | [2011-09-17 Sat 08:35] |      121 |        78 |    66 |
> | [2011-09-18 Sun 07:04] |      124 |        69 |    65 |
> | [2011-09-19 Mon 06:00] |      133 |        81 |    69 |
> |------------------------+----------+-----------+-------|
> | mode                   |      125 |        81 |    69 |
> | min                    |      116 |        69 |    61 |
> | max                    |      152 |       100 |    89 |
> #+TBLFM: @>>>$2..@>>>$4=vmode(@I..@II) :: @>>$2..@>>$4=vmin(@I..@II) :: @>$2..@>$4=vmax(@I..@II)
>
> #+begin_src elisp
>
> (defun calcFunc-vmode (&rest vecs)
>    (let* ((flat (copy-sequence (cdr (math-flatten-many-vecs vecs))))
> 	 (p flat)
> 	 (len (length flat))
> 	 (hlen (/ len 2)))
>      (if (= len 0)
> 	(math-reject-arg nil "*Must be at least 1 argument")
>        (if (and (= len 1) (memq (car-safe (car flat)) '(sdev intv)))
> 	  (calcFunc-vmean (car flat))
> 	(while p
> 	  (if (eq (car-safe (car p)) 'sdev)
> 	      (setcar p (nth 1 (car p))))
> 	  (or (Math-anglep (car p))
> 	      (math-reject-arg (car p) 'anglep))
> 	  (setq p (cdr p)))
> 	(setq flat (sort flat 'math-lessp))
> 	(setq freqs (vfreqs flat))
> 	(cadr (maxfreq freqs))
> 	))))
>
> (defun vfreqs (v)
>    "v is assumed to be sorted.
> Return a list of pairs: (count element)
> for each distinct element of v."
>    (let ((count 0)
> 	(prev (car v))
> 	(vec v)
> 	freqs)
>      (setq freqs nil)
>      (while vec
>        (if (= (nth 0 vec) prev)
> 	  (setq count (1+ count))
> 	(setq freqs (cons (list count (nth 0 vec)) freqs))
> 	(setq count 1 prev (nth 0 vec)))
>        (setq vec (cdr vec)))
>      freqs))
>
> (defun freq-greater (a b)
>    (if (>  (car a) (car b))
>        t
>      nil))
>
> (defun maxfreq (f)
>    "Sort the frequency list by count and return the first pair."
>    (let ((fs (sort f 'freq-greater)))
>      (nth 0 fs)))
>
> #+end_src
> --8<---------------cut here---------------end--------------->8---
>
> I cribbed heavily from the implementation of vmedian. Although there is
> no key binding, you can even use the above in an interactive Calc
> session, by entering the formula in algebraic form. I yanked the vector
> twice onto the Calc stack and then entered 'vmode($) with the following
> result:
>
> ,----
> | --- Emacs Calculator Mode ---
> | 2:  [138, 130, 128, 121, 118, 126, 122, 124, 137, 136, 120, 128, 124, 124, 128, 132, 137, 124, 139,
> |      124, 137, 120, 128, 143, 125, 123, 135, 128, 124, 136, 116, 139, 124, 138, 126, 125, 124, 124,
> |      133, 124, 124, 139, 152, 136, 142, 126, 130, 138, 121, 131, 122, 126, 137, 135, 132, 137, 141,
> |      121, 124, 133]
> | 1:  125
> |     .
> `----
>
> Nick
>
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: unexpected failure on all formulas
  2011-09-20  9:29     ` Christian Moe
@ 2011-09-20 14:59       ` Nick Dokos
  2011-09-20 16:30         ` Nick Dokos
  2011-09-20 20:28         ` Christian Moe
  0 siblings, 2 replies; 8+ messages in thread
From: Nick Dokos @ 2011-09-20 14:59 UTC (permalink / raw)
  To: mail; +Cc: Jude DaShiell, nicholas.dokos, emacs-orgmode

Christian Moe <mail@christianmoe.com> wrote:


> I think your vmode() needs to subtract one from the result. The mode
> in the "systolic" column of Jude's dataset should be 124 (f=12), not
> 125 (f=2).
> 

Yes, this is an off-by-one error: in vfreqs, I should cons prev onto
freqs, not (nth 0 vec). In addition to this and the multimodal problem
that both you and Lawrence Mitchell pointed out, there is another
off-by-one error when vfreqs gets to the end of the list.

All in all, a disaster: can a posting be deleted? or at least marked
with a big, red X so that it won't mislead in the future? I'll try to
post a corrected version later on.

OTOH, my main interest in this was the hooking up of a user function
onto calc and the example, though deeply flawed, does illustrate that.

Thanks to both Christian and Lawrence for the whacks in the head
(although now I have a headache...)

Nick


> For Jude's limited purposes I got by with less code (but note the
> caveats):
> 
> #+begin_src emacs-lisp
>   (defmath vmode (vec)
>     "Returns the mode (most frequent value) of a unimodal vector of
>   integers VEC.  WARNING: Will not work with non-integer values or
>   multiple vectors. Will only return the lowest mode for multimodal
>   data."
>     (let ((freq (histogram vec (1+ (vmax vec)))))
>             (1- (find freq (vmax freq)))))
> #+end_src
> 
> Using :=vmode(@I..@II), I get 124 (systolic), 80 (diastolic), 68 (pulse).
> 
> Yours,
> Christian
> 
> On 9/20/11 10:26 AM, Nick Dokos wrote:
> > Nick Dokos<nicholas.dokos@hp.com>  wrote:
> >
> >> Jude DaShiell<jdashiel@shellworld.net>  wrote:
> >>
> >>
> >>> |------------------------+----------+-----------+-------|
> >>> | mode                   |          |           |       |
> >>> | Min                    |          |           |       |
> >>> | Max                    |     127. |       81. |   74. |
> >>> #+TBLFM: @>>>$2..@>>>$4=vmode(@i..@ii) :: @>>$2..@>>$4=vmin(@i..@ii) :: @>$2..@>$4=vmax(@i..@ii)
> >>>
> >>>
> >>> # Local Variables:
> >>> # kept-new-versions: 100
> >>> # version-control: t
> >>> # End:
> >>> cut here.
> >>> What did I do wrong with this #+TBLFM: line?
> >>>
> >>
> >> Two things:
> >>
> >> o as Christian Moe pointed out, calc does not provide a vmode() function.
> >>
> >> o the separator lines in the formulas have to be specified using capital
> >> I letters.
> >>
> >
> > Here is an implementation of vmode - afaict, it works correctly but
> > that may be because I've only tested it on a single set of data. If
> > you find something wrong, please let me know.
> >
> > --8<---------------cut here---------------start------------->8---
> > | Date Stamp             | Systolic | Diastolic | Pulse |
> > |------------------------+----------+-----------+-------|
> > | [2011-07-19 Tue 02:26] |      138 |        92 |    74 |
> > | [2011-07-20 Wed 04:03] |      130 |        85 |    74 |
> > | [2011-07-21 Thu 03:50] |      128 |        79 |    76 |
> > | [2011-07-22 Fri 02:33] |      121 |        80 |    79 |
> > | [2011-07-23 Sat 02:52] |      118 |        75 |    68 |
> > | [2011-07-24 Sun 04:13] |      126 |        83 |    78 |
> > | [2011-07-25 Mon 02:12] |      122 |        79 |    76 |
> > | [2011-07-26 Tue 01:57] |      124 |        79 |    72 |
> > | [2011-07-27 Wed 04:03] |      137 |        74 |    76 |
> > | [2011-07-28 Thu 03:47] |      136 |        80 |    76 |
> > | [2011-07-29 Fri 04:06] |      120 |        72 |    76 |
> > | [2011-07-30 Sat 03:18] |      128 |        92 |    80 |
> > | [2011-07-31 Sun 07:11] |      124 |        84 |    72 |
> > | [2011-08-01 Mon 04:55] |      124 |        77 |    69 |
> > | [2011-08-02 Tue 02:47] |      128 |        80 |    67 |
> > | [2011-08-03 Wed 06:00] |      132 |        89 |    70 |
> > | [2011-08-04 Thu 04:30] |      137 |        82 |    77 |
> > | [2011-08-05 Fri 06:10] |      124 |        86 |    77 |
> > | [2011-08-06 Sat 04:04] |      139 |        88 |    78 |
> > | [2011-08-07 Sun 04:02] |      124 |        80 |    68 |
> > | [2011-08-08 Mon 06:36] |      137 |        93 |    79 |
> > | [2011-08-10 Wed04:03]  |      120 |        80 |    68 |
> > | [2011-08-11 Thu 04:30] |      128 |        78 |    77 |
> > | [2011-08-12 Fri 06:36] |      143 |        82 |    76 |
> > | [2011-08-13 Sat 06:18] |      125 |        76 |    69 |
> > | [2011-08-14 Sun 04:04] |      123 |        70 |    69 |
> > | [2011-08-15 Mon 04:04] |      135 |        83 |    76 |
> > | [2011-08-16 Tue 04:03] |      128 |        80 |    69 |
> > | [2011-08-17 Wed 04:04] |      124 |        80 |    68 |
> > | [2011-08-18 Thu 05:58] |      136 |        87 |    72 |
> > | [2011-08-19 Fri 05:50] |      116 |        80 |    64 |
> > | [2011-08-20 Sat 06:38] |      139 |        84 |    64 |
> > | [2011-08-21 Sun 08:20] |      124 |        84 |    73 |
> > | [2011-08-22 Mon 03:50] |      138 |        76 |    70 |
> > | [2011-08-23 Tue 05:52] |      126 |        79 |    68 |
> > | [2011-08-24 Wed 04:53] |      125 |        81 |    64 |
> > | [2011-08-25 Thu 05:37] |      124 |        79 |    68 |
> > | [2011-08-26 Fri 06:41] |      124 |        87 |    74 |
> > | [2011-08-27 Sat 05:44] |      133 |        74 |    61 |
> > | [2011-08-28 Sun 03:22] |      124 |        76 |    61 |
> > | [2011-08-29 Mon 04:06] |      124 |        76 |    65 |
> > | [2011-08-30 Tue 05:52] |      139 |        87 |    71 |
> > | [2011-08-31 Wed 06:47] |      152 |        86 |    68 |
> > | [2011-09-01 Thu 06:00] |      136 |        86 |    78 |
> > | [2011-09-02 Fri 01:22] |      142 |       100 |    78 |
> > | [2011-09-03 Sat 09:12] |      126 |        79 |    70 |
> > | [2011-09-04 Sun 11:53] |      130 |        90 |    72 |
> > | [2011-09-05 Mon 06:29] |      138 |        77 |    65 |
> > | [2011-09-06 Tue 05:42] |      121 |        76 |    68 |
> > | [2011-09-07 Wed 06:47] |      131 |        77 |    68 |
> > | [2011-09-08 Thu 06:02] |      122 |        76 |    72 |
> > | [2011-09-10 Sat 05:38] |      126 |        76 |    69 |
> > | [2011-09-11 Sun 10:12] |      137 |        87 |    64 |
> > | [2011-09-12 Mon 04:00] |      135 |        78 |    89 |
> > | [2011-09-13 Tue 06:00] |      132 |        82 |    66 |
> > | [2011-09-14 Wed 06:01] |      137 |        77 |    77 |
> > | [2011-09-15 Thu 04:08] |      141 |        87 |    64 |
> > | [2011-09-17 Sat 08:35] |      121 |        78 |    66 |
> > | [2011-09-18 Sun 07:04] |      124 |        69 |    65 |
> > | [2011-09-19 Mon 06:00] |      133 |        81 |    69 |
> > |------------------------+----------+-----------+-------|
> > | mode                   |      125 |        81 |    69 |
> > | min                    |      116 |        69 |    61 |
> > | max                    |      152 |       100 |    89 |
> > #+TBLFM: @>>>$2..@>>>$4=vmode(@I..@II) :: @>>$2..@>>$4=vmin(@I..@II) :: @>$2..@>$4=vmax(@I..@II)
> >
> > #+begin_src elisp
> >
> > (defun calcFunc-vmode (&rest vecs)
> >    (let* ((flat (copy-sequence (cdr (math-flatten-many-vecs vecs))))
> > 	 (p flat)
> > 	 (len (length flat))
> > 	 (hlen (/ len 2)))
> >      (if (= len 0)
> > 	(math-reject-arg nil "*Must be at least 1 argument")
> >        (if (and (= len 1) (memq (car-safe (car flat)) '(sdev intv)))
> > 	  (calcFunc-vmean (car flat))
> > 	(while p
> > 	  (if (eq (car-safe (car p)) 'sdev)
> > 	      (setcar p (nth 1 (car p))))
> > 	  (or (Math-anglep (car p))
> > 	      (math-reject-arg (car p) 'anglep))
> > 	  (setq p (cdr p)))
> > 	(setq flat (sort flat 'math-lessp))
> > 	(setq freqs (vfreqs flat))
> > 	(cadr (maxfreq freqs))
> > 	))))
> >
> > (defun vfreqs (v)
> >    "v is assumed to be sorted.
> > Return a list of pairs: (count element)
> > for each distinct element of v."
> >    (let ((count 0)
> > 	(prev (car v))
> > 	(vec v)
> > 	freqs)
> >      (setq freqs nil)
> >      (while vec
> >        (if (= (nth 0 vec) prev)
> > 	  (setq count (1+ count))
> > 	(setq freqs (cons (list count (nth 0 vec)) freqs))
> > 	(setq count 1 prev (nth 0 vec)))
> >        (setq vec (cdr vec)))
> >      freqs))
> >
> > (defun freq-greater (a b)
> >    (if (>  (car a) (car b))
> >        t
> >      nil))
> >
> > (defun maxfreq (f)
> >    "Sort the frequency list by count and return the first pair."
> >    (let ((fs (sort f 'freq-greater)))
> >      (nth 0 fs)))
> >
> > #+end_src
> > --8<---------------cut here---------------end--------------->8---
> >
> > I cribbed heavily from the implementation of vmedian. Although there is
> > no key binding, you can even use the above in an interactive Calc
> > session, by entering the formula in algebraic form. I yanked the vector
> > twice onto the Calc stack and then entered 'vmode($) with the following
> > result:
> >
> > ,----
> > | --- Emacs Calculator Mode ---
> > | 2:  [138, 130, 128, 121, 118, 126, 122, 124, 137, 136, 120, 128, 124, 124, 128, 132, 137, 124, 139,
> > |      124, 137, 120, 128, 143, 125, 123, 135, 128, 124, 136, 116, 139, 124, 138, 126, 125, 124, 124,
> > |      133, 124, 124, 139, 152, 136, 142, 126, 130, 138, 121, 131, 122, 126, 137, 135, 132, 137, 141,
> > |      121, 124, 133]
> > | 1:  125
> > |     .
> > `----
> >
> > Nick
> >
> >
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: unexpected failure on all formulas
  2011-09-20 14:59       ` Nick Dokos
@ 2011-09-20 16:30         ` Nick Dokos
  2011-09-20 20:28         ` Christian Moe
  1 sibling, 0 replies; 8+ messages in thread
From: Nick Dokos @ 2011-09-20 16:30 UTC (permalink / raw)
  To: Jude DaShiell; +Cc: nicholas.dokos, emacs-orgmode

Nick Dokos <nicholas.dokos@hp.com> wrote:

> All in all, a disaster: can a posting be deleted? or at least marked
> with a big, red X so that it won't mislead in the future? I'll try to
> post a corrected version later on.
> 

Here's an attempt to correct the problems that Christian, Lawrence and I
found in my previous attempt. I include two tables, Jude's original and
a truncated version to force multimodality. I hope that this is correct
(but I would not be surprised if there are other errors lurking - but at
least it is better than the previous version). As always, I'd appreciate
corrections etc.

Nick

--8<---------------cut here---------------start------------->8---
| Date Stamp             | Systolic | Diastolic | Pulse |
|------------------------+----------+-----------+-------|
| [2011-07-19 Tue 02:26] |      138 |        92 |    74 |
| [2011-07-20 Wed 04:03] |      130 |        85 |    74 |
| [2011-07-21 Thu 03:50] |      128 |        79 |    76 |
| [2011-07-22 Fri 02:33] |      121 |        80 |    79 |
| [2011-07-23 Sat 02:52] |      118 |        75 |    68 |
| [2011-07-24 Sun 04:13] |      126 |        83 |    78 |
| [2011-07-25 Mon 02:12] |      122 |        79 |    76 |
| [2011-07-26 Tue 01:57] |      124 |        79 |    72 |
| [2011-07-27 Wed 04:03] |      137 |        74 |    76 |
| [2011-07-28 Thu 03:47] |      136 |        80 |    76 |
| [2011-07-29 Fri 04:06] |      120 |        72 |    76 |
| [2011-07-30 Sat 03:18] |      128 |        92 |    80 |
| [2011-07-31 Sun 07:11] |      124 |        84 |    72 |
| [2011-08-01 Mon 04:55] |      124 |        77 |    69 |
| [2011-08-02 Tue 02:47] |      128 |        80 |    67 |
| [2011-08-03 Wed 06:00] |      132 |        89 |    70 |
| [2011-08-04 Thu 04:30] |      137 |        82 |    77 |
| [2011-08-05 Fri 06:10] |      124 |        86 |    77 |
| [2011-08-06 Sat 04:04] |      139 |        88 |    78 |
| [2011-08-07 Sun 04:02] |      124 |        80 |    68 |
| [2011-08-08 Mon 06:36] |      137 |        93 |    79 |
| [2011-08-10 Wed 04:03] |      120 |        80 |    68 |
| [2011-08-11 Thu 04:30] |      128 |        78 |    77 |
| [2011-08-12 Fri 06:36] |      143 |        82 |    76 |
| [2011-08-13 Sat 06:18] |      125 |        76 |    69 |
| [2011-08-14 Sun 04:04] |      123 |        70 |    69 |
| [2011-08-15 Mon 04:04] |      135 |        83 |    76 |
| [2011-08-16 Tue 04:03] |      128 |        80 |    69 |
| [2011-08-17 Wed 04:04] |      124 |        80 |    68 |
| [2011-08-18 Thu 05:58] |      136 |        87 |    72 |
| [2011-08-19 Fri 05:50] |      116 |        80 |    64 |
| [2011-08-20 Sat 06:38] |      139 |        84 |    64 |
| [2011-08-21 Sun 08:20] |      124 |        84 |    73 |
| [2011-08-22 Mon 03:50] |      138 |        76 |    70 |
| [2011-08-23 Tue 05:52] |      126 |        79 |    68 |
| [2011-08-24 Wed 04:53] |      125 |        81 |    64 |
| [2011-08-25 Thu 05:37] |      124 |        79 |    68 |
| [2011-08-26 Fri 06:41] |      124 |        87 |    74 |
| [2011-08-27 Sat 05:44] |      133 |        74 |    61 |
| [2011-08-28 Sun 03:22] |      124 |        76 |    61 |
| [2011-08-29 Mon 04:06] |      124 |        76 |    65 |
| [2011-08-30 Tue 05:52] |      139 |        87 |    71 |
| [2011-08-31 Wed 06:47] |      152 |        86 |    68 |
| [2011-09-01 Thu 06:00] |      136 |        86 |    78 |
| [2011-09-02 Fri 01:22] |      142 |       100 |    78 |
| [2011-09-03 Sat 09:12] |      126 |        79 |    70 |
| [2011-09-04 Sun 11:53] |      130 |        90 |    72 |
| [2011-09-05 Mon 06:29] |      138 |        77 |    65 |
| [2011-09-06 Tue 05:42] |      121 |        76 |    68 |
| [2011-09-07 Wed 06:47] |      131 |        77 |    68 |
| [2011-09-08 Thu 06:02] |      122 |        76 |    72 |
| [2011-09-10 Sat 05:38] |      126 |        76 |    69 |
| [2011-09-11 Sun 10:12] |      137 |        87 |    64 |
| [2011-09-12 Mon 04:00] |      135 |        78 |    89 |
| [2011-09-13 Tue 06:00] |      132 |        82 |    66 |
| [2011-09-14 Wed 06:01] |      137 |        77 |    77 |
| [2011-09-15 Thu 04:08] |      141 |        87 |    64 |
| [2011-09-17 Sat 08:35] |      121 |        78 |    66 |
| [2011-09-18 Sun 07:04] |      124 |        69 |    65 |
| [2011-09-19 Mon 06:00] |      133 |        81 |    69 |
|------------------------+----------+-----------+-------|
| mode                   |      124 |        80 |    68 |
| min                    |      116 |        69 |    61 |
| max                    |      152 |       100 |    89 |
#+TBLFM: @>>>$2..@>>>$4=vmode(@I..@II) :: @>>$2..@>>$4=vmin(@I..@II) :: @>$2..@>$4=vmax(@I..@II)


| Date Stamp             |   Systolic | Diastolic | Pulse |
|------------------------+------------+-----------+-------|
| [2011-07-19 Tue 02:26] |        138 |        92 |    74 |
| [2011-07-20 Wed 04:03] |        130 |        85 |    74 |
| [2011-07-21 Thu 03:50] |        128 |        79 |    76 |
| [2011-07-22 Fri 02:33] |        121 |        80 |    79 |
| [2011-07-23 Sat 02:52] |        118 |        75 |    68 |
| [2011-07-24 Sun 04:13] |        126 |        83 |    78 |
| [2011-07-25 Mon 02:12] |        122 |        79 |    76 |
| [2011-07-26 Tue 01:57] |        124 |        79 |    72 |
| [2011-07-27 Wed 04:03] |        137 |        74 |    76 |
| [2011-07-28 Thu 03:47] |        136 |        80 |    76 |
| [2011-07-29 Fri 04:06] |        120 |        72 |    76 |
| [2011-07-30 Sat 03:18] |        128 |        92 |    80 |
| [2011-07-31 Sun 07:11] |        124 |        84 |    72 |
| [2011-08-01 Mon 04:55] |        124 |        77 |    69 |
| [2011-08-02 Tue 02:47] |        128 |        80 |    67 |
| [2011-08-03 Wed 06:00] |        132 |        89 |    70 |
|------------------------+------------+-----------+-------|
| mode                   | [128, 124] |  [80, 79] |    76 |
| min                    |        118 |        72 |    67 |
| max                    |        138 |        92 |    80 |
#+TBLFM: @>>>$2..@>>>$4=vmode(@I..@II) :: @>>$2..@>>$4=vmin(@I..@II) :: @>$2..@>$4=vmax(@I..@II)


#+begin_src elisp

(defun calcFunc-vmode (&rest vecs)
  (let* ((flat (copy-sequence (cdr (math-flatten-many-vecs vecs))))
	 (p flat)
	 (len (length flat))
	 freqs ret)
    (if (= len 0)
	(math-reject-arg nil "*Must be at least 1 argument")
      (if (and (= len 1) (memq (car-safe (car flat)) '(sdev intv)))
	  (calcFunc-vmean (car flat))
	(while p
	  (if (eq (car-safe (car p)) 'sdev)
	      (setcar p (nth 1 (car p))))
	  (or (Math-anglep (car p))
	      (math-reject-arg (car p) 'anglep))
	  (setq p (cdr p)))
	(setq flat (sort flat 'math-lessp))
	(setq freqs (vfreqs flat))
	(setq ret (maxfreq freqs))
	(if (null (cdr ret))
	    ;; singleton
	    (car ret)
	  (cons 'vec (nreverse ret)))
	))))

(defun vfreqs (v)
  "v is assumed to be sorted.
  Return a list of pairs: (count element)
  for each distinct element of v."
  
  (let ((count 0)
	(prev (car v))
	(vec v)
	freqs)
    (setq freqs nil)
    (while vec
      (if (= (nth 0 vec) prev)
	  (setq count (1+ count))
	(setq freqs (cons (list count prev) freqs))
	(setq count 1 prev (nth 0 vec)))
      (setq vec (cdr vec)))
    (cons (list count prev) freqs)))

(defun freq-greater (a b)
  (if (> (car a) (car b))
      t
    nil))

(defun maxfreq (f)
  "Sort the frequency list by count and return a list of
  elements with maximal count."
  (let* ((fs (sort f 'freq-greater))
	 (maxf (car (nth 0 fs)))
	 ret)
    (while (and fs (= (car (nth 0 fs)) maxf))
      (setq ret (cons (cadr (nth 0 fs)) ret))
      (setq fs (cdr fs)))
    ret))


#+end_src
--8<---------------cut here---------------end--------------->8---

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: unexpected failure on all formulas
  2011-09-20 14:59       ` Nick Dokos
  2011-09-20 16:30         ` Nick Dokos
@ 2011-09-20 20:28         ` Christian Moe
  1 sibling, 0 replies; 8+ messages in thread
From: Christian Moe @ 2011-09-20 20:28 UTC (permalink / raw)
  To: nicholas.dokos; +Cc: Jude DaShiell, emacs-orgmode

Hi, Nick,

No whacks intended! It was an easy catch because I was mulling over 
similar problems with my code. My own second pass at a vmode 
implementation for Calc is below, now with support for negative values 
and bimodal data. It takes a different route and is a bit shorter, but 
doesn't handle all the cases your code does (I guess; I don't grok it 
all).

Yours,
Christian


(defmath vmode (vec)
   "Return the mode(s), i.e. most frequent value(s), of a vector
   of integers VEC."
   (let ((results (vec)) ; hold results
         (index 1)
         offset maxfreq)
     (setq offset (- 1 (vmin vec))
           vec (+ offset vec) ; bump all ints positive
           freq (histogram vec (1+ (vmax vec)))
           offset (1+ offset) ; histogram added `0' slot
           maxfreq (vmax freq))
     ;; Collect indexes (less offset) of all max freqs
     (while (> (setq index (find freq maxfreq index)) 0)
       (setq results (vconcat results index)
             index (1+ index)))
     (setq results (- results offset)) ; get neg ints back
     ;; Return results as vector or number
     (if (> (vlen results) 1)
         results
       (head results))))


On 9/20/11 4:59 PM, Nick Dokos wrote:
> Christian Moe<mail@christianmoe.com>  wrote:
>
>
>> I think your vmode() needs to subtract one from the result. The mode
>> in the "systolic" column of Jude's dataset should be 124 (f=12), not
>> 125 (f=2).
>>
>
> Yes, this is an off-by-one error: in vfreqs, I should cons prev onto
> freqs, not (nth 0 vec). In addition to this and the multimodal problem
> that both you and Lawrence Mitchell pointed out, there is another
> off-by-one error when vfreqs gets to the end of the list.
>
> All in all, a disaster: can a posting be deleted? or at least marked
> with a big, red X so that it won't mislead in the future? I'll try to
> post a corrected version later on.
>
> OTOH, my main interest in this was the hooking up of a user function
> onto calc and the example, though deeply flawed, does illustrate that.
>
> Thanks to both Christian and Lawrence for the whacks in the head
> (although now I have a headache...)
>
> Nick

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-09-20 20:27 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-20  0:41 unexpected failure on all formulas Jude DaShiell
2011-09-20  6:17 ` Nick Dokos
2011-09-20  8:26   ` Nick Dokos
2011-09-20  9:15     ` Lawrence Mitchell
2011-09-20  9:29     ` Christian Moe
2011-09-20 14:59       ` Nick Dokos
2011-09-20 16:30         ` Nick Dokos
2011-09-20 20:28         ` Christian Moe

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).