[racket-users] where to put this ordinary least squares code?

2016-11-30 Thread 'John Clements' via Racket Users
So: you want to perform ordinary least squares linear regression.

I’ve needed this code a bunch of times, and the matrix library actually 
suggests that something like this might be useful, but I my implementation 
technique is “transliterate from wikipedia,” and I have a strong suspicion that 
a numerical methods person would cry if they saw this code. On the other hand, 
I’m pretty sure that it’s ballpark correct; my test cases suggest that I’m not 
doing anything wildly incorrect.

Is there a better version of this floating around somewhere? If not, will 
seeing this crappy code spur someone else to do a better job?

John



#lang typed/racket

(require math/matrix)

(provide XY->Beta
 XYBeta-rms-error)

;; given a matrix X of inputs and a column-vector Y of responses,
;; compute Beta, the column vector mapping X to y-hat (minimizing
;; the sum of the squares of the residuals), which can be used
;; to estimate the response based on an input
(: XY->Beta ((Matrix Number) (Matrix Number) -> (Matrix Number)))
(define (XY->Beta X Y)
  (unless (= (matrix-num-rows X) (matrix-num-rows Y))
(raise-argument-error
 'XY->B "Y matrix with same # of rows as X matrix"
 1 X Y
 ))
  (unless (= (matrix-num-cols Y) 1)
(raise-argument-error
 'XY->B "Y matrix with 1 column"
 1 X Y))
  ;; this might be a really slow or inaccurate way to compute this
  (matrix*
   (matrix* (matrix-inverse (matrix* (matrix-transpose X) X))
(matrix-transpose X))
   Y))

;; given a matrix X of inputs, a column-vector Y of responses,
;; and a column vector B intended to map inputs to outputs,
;; return the rms error.
(: XYBeta-rms-error
   ((Matrix Number) (Matrix Number) (Matrix Number) -> Real))
(define (XYBeta-rms-error X y Beta)
  (define epsilon
(matrix- y (matrix* X Beta)))
  (match (matrix->list (matrix* (matrix-transpose epsilon) epsilon))
[(list s)
 (cond [(= s 0.0) 0]
   [else (sqrt (/ (cast s Positive-Real)
  (cast (matrix-num-rows y) Positive-Real)))])]))

(module+ test
  (require typed/rackunit
   math/array)

  ;; example:
  ;; suppose y = 2x_1 - 8x_2, with small perturbations
  ;; 23 -4  |  78.9 (error +0.9)
  ;; 4  -2  |  23.2 (error -0.8)
  ;; 0   2  |  -16.3 (error -0.3)

  (define X
(matrix [[23 -4]
 [4 -2]
 [0 2]]))
  (define Y
(matrix [[78.9]
 [23.2]
 [-16.3]]))

  (define estimated-b (array->list (XY->Beta X Y)))
  (check-= (cast (car estimated-b) Real) 2 0.2)
  (check-= (cast (cadr estimated-b) Real) -8 0.2)

  (check-= (XYBeta-rms-error X Y (col-matrix [2 -8]))
   (sqrt (* 1/3 (+ 0.81 0.64 0.09)))
   1e-5)
  

  ;; example from Wikipedia page on ordinary least squares:
  
  (define heights
'(1.47 1.50 1.52 1.55 1.57 1.60 1.63 1.65 1.68 1.70 1.73 1.75 1.78 1.80 
1.83))
  (define sqr-hts
(map (λ ([x : Real]) (* x x)) heights))
  (define units
(for/list : (Listof Real) ([h (in-list heights)]) 1.0))
  (define X2
(matrix-transpose (list*->array (list units heights sqr-hts) real?)))
  (define weights
(col-matrix [52.21 53.12 54.48 55.84 57.20 58.57 59.93 61.29 63.11 64.47 
66.28 68.10 69.92 72.19 74.46]))

  ;; wikipedia lists expected result:
  ;;  β 1 {\displaystyle \beta _{1}} \beta _{1} 128.812816.3083 
7.8986  0.
  ;; β 2 {\displaystyle \beta _{2}} \beta _{2}  –143.1620   19.8332 
–7.2183 0.
  ;; β 3 {\displaystyle \beta _{3}} \beta _{3}  61.9603 6.0084  10.3122 
0.

  (check-equal?
   (matrix->list (XY->Beta X2 weights))
   (list 128.8128035850009
 -143.16202287195165
 61.960325444600926))
  )




-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] implementing make in racket

2016-11-30 Thread Dan Liebgold
On Wednesday, November 30, 2016 at 1:49:04 PM UTC-8, Jay McCarthy wrote:
> I just pushed a fix for this, btw.
> 
> 

Awesome, thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] implementing make in racket

2016-11-30 Thread Jay McCarthy
I just pushed a fix for this, btw.

Jay

On Wed, Nov 30, 2016 at 2:15 PM, Dan Liebgold 
wrote:

> On Wednesday, November 30, 2016 at 10:38:05 AM UTC-8, Jay McCarthy wrote:
> > The typed-racket code returns a value from the job, whereas this code
> > assumes the job is fully self-contained. Perhaps job-queue should
> > protect itself from job exceptions.
> >
>
> Probably a good idea. I got confused working on my thunk... I'm spawning a
> subprocess that errors out.  I was using an asynchronous spawn
> (process/ports) and it caused a silent hang in (stop-job-queue! jq). I
> switched to system* for other reasons and since that is synchronous is
> handles the exception differently such that it never escapes my thunk ...
> so things work.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Jay McCarthy
Associate Professor
PLT @ CS @ UMass Lowell
http://jeapostrophe.github.io

   "Wherefore, be not weary in well-doing,
  for ye are laying the foundation of a great work.
And out of small things proceedeth that which is great."
  - D&C 64:33

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] implementing make in racket

2016-11-30 Thread Dan Liebgold
On Wednesday, November 30, 2016 at 10:38:05 AM UTC-8, Jay McCarthy wrote:
> The typed-racket code returns a value from the job, whereas this code
> assumes the job is fully self-contained. Perhaps job-queue should
> protect itself from job exceptions.
> 

Probably a good idea. I got confused working on my thunk... I'm spawning a 
subprocess that errors out.  I was using an asynchronous spawn (process/ports) 
and it caused a silent hang in (stop-job-queue! jq). I switched to system* for 
other reasons and since that is synchronous is handles the exception 
differently such that it never escapes my thunk ... so things work.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] hyperlink in code:comment

2016-11-30 Thread Stephen Chang
Each code formatting form, like `racketblock`, allows specifying an
escape identifier for use within that form, though all forms default
to `unsyntax` if unspecified, I believe. Some forms, like `codeblock`
do not support escapes.

The docs here [1] has more details, and specifically the docs for
racketblock [2] has more examples.


[1]: http://docs.racket-lang.org/scribble/scribble_manual_code.html
[2]: 
http://docs.racket-lang.org/scribble/scribble_manual_code.html?#%28form._%28%28lib._scribble%2Fmanual..rkt%29._racketblock%29%29

On Tue, Nov 29, 2016 at 3:42 PM, Jos Koot  wrote:
> Thanks, I'll try that.
> It is not clear to me where I can use @#,
> But certainly your response will be a great help for me.
> Thanks again, Jos
>
> -Original Message-
> From: stchang...@gmail.com [mailto:stchang...@gmail.com] On Behalf Of Stephen 
> Chang
> Sent: martes, 29 de noviembre de 2016 21:31
> To: Jos Koot
> Cc: Racket Users
> Subject: Re: [racket-users] hyperlink in code:comment
>
> You can use the escape identifier:
>
> #lang scribble/manual
>
> @(require scribble/eval
>   (for-label racket))
>
> @interaction[
>  (define a (list 1))
>  (define b (list 1))
>  (code:comment @#,para{a and b are not @racket[eq?], but they are
> @racket[equal?]:})
>  (eq? a b)
>  (equal? a b)]
>
> On Tue, Nov 29, 2016 at 2:56 PM, Jos Koot  wrote:
>> A simplified fragment of scribble code:
>>
>> #lang scribble/manual
>>
>> @(require scribble/eval)
>>
>> @interaction[
>>  (define a (list 1))
>>  (define b (list 1))
>>  (code:comment "a and b are not eq, but they are equal:")
>>  (eq? a b)
>>  (equal? a b)]
>>
>> Within the comment I would like to hyperlink 'eq' to the doc of [eq?]
>> and 'equal' to the doc of [equal?]
>>
>> Is this possible?
>> If so how?
>> I have tried it without success.
>>
>> Thanks, Jos
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to racket-users+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] implementing make in racket

2016-11-30 Thread Jay McCarthy
The typed-racket code returns a value from the job, whereas this code
assumes the job is fully self-contained. Perhaps job-queue should
protect itself from job exceptions.

On Wed, Nov 30, 2016 at 1:30 PM, Dan Liebgold
 wrote:
> On Tuesday, November 29, 2016 at 6:30:08 PM UTC-8, David K. Storrs wrote:
>>
>> Can you simply catch it and handle it inside the thunk?
>>
>
> That's probably best. I was looking at the machinery to serialize exceptions 
> in type racket* and thinking I needed that
>
>
> *: 
> https://github.com/racket/typed-racket/blob/master/typed-racket-test/send-places.rkt
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



-- 
Jay McCarthy
Associate Professor
PLT @ CS @ UMass Lowell
http://jeapostrophe.github.io

   "Wherefore, be not weary in well-doing,
  for ye are laying the foundation of a great work.
And out of small things proceedeth that which is great."
  - D&C 64:33

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] implementing make in racket

2016-11-30 Thread Dan Liebgold
On Tuesday, November 29, 2016 at 6:30:08 PM UTC-8, David K. Storrs wrote:
> 
> Can you simply catch it and handle it inside the thunk?
>  

That's probably best. I was looking at the machinery to serialize exceptions in 
type racket* and thinking I needed that


*: 
https://github.com/racket/typed-racket/blob/master/typed-racket-test/send-places.rkt

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Plot multiple lines from data set

2016-11-30 Thread Vincent St-Amour
Hi Stephen,

Renderers are values, so you could simply define each one separately,
then build the list, then pass it to plot. No need for macros.

(define line1 )
(define line2 )

(define all-lines (list line1 line2 ))
 (plot all-lines) 

Vincent



On Wed, 30 Nov 2016 09:24:44 -0600,
Stephen De Gabrielle wrote:
> 
> Hi, 
> 
> I'm trying to work out how I can include multiple datasets in a single plot.
> (plot (list (function) ...)) works, 
> 
> Should I write a macro that creates an enormous plot statement? Is there 
> another way to build the list of renderers?
> 
> I've attached my example 
> 
> Kind regards,
> 
> Stephen
> 
> -- 
> Kind regards,
> Stephen
> --
> Bigger than Scheme, cooler than Clojure & more fun than CL.(n=1)
> --
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Plot multiple lines from data set

2016-11-30 Thread Stephen De Gabrielle
Hi,

I'm trying to work out how I can include multiple datasets in a single plot.
(plot (list (function) ...)) works,

Should I write a macro that creates an enormous plot statement? Is there
another way to build the list of renderers?

I've attached my example

Kind regards,

Stephen

-- 
Kind regards,
Stephen
--
Bigger than Scheme, cooler than Clojure & more fun than CL.(n=1)
--

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


HbA1Cplot-test.rkt
Description: Binary data


Re: [racket-users] Scribble equivalent of rowspan

2016-11-30 Thread Matthew Flatt
Hi Philip,

There's nothing like rowspan currently.

If you want to try adding something, I think you'll end up changing the
"scribble-lib" package in several places: in "core.rkt" to adjust the
contract for tables, in "base.rkt" to adjust the contract for
`tabular`, and in "html-render.rkt", "latex-render.rkt",
"markdown-render.rkt", and "text-render.rkt" to adjust the various
renderers. The rendering part is the main work, naturally.

If you decide not to try, I can take a look in a few days.

Matthew

At Tue, 29 Nov 2016 21:47:03 -0600, Philip McGrath wrote:
> A very rookie question: I am trying to figure out how to specify the
> equivalent of HTML's rowspan attribute for tabular from scribble/base: that
> is, to have a cell which spans more than one row. In LaTeX, I think I would
> use "multirow" (but I'm no LaTeX expert).
> 
> I know so know about 'cont, but I believe that only lets a cell span
> multiple columns: I'm looking for the a vertical version. Is there a
> built-in way to do this? Or otherwise, if it's not too daunting to
> implement myself, could someone point me in the right direction?
> 
> Here's some ASCII art of what I'm going for, if that's clearer:
> 
> -
> | | Date: | Nov 29 | Dec 30 |
> | |---|||
> | | Time: | 12:18  |  3:06  |
> |---|
> | | Expected: |   3|   1|
> | Apples  |---|||
> | | Actual:   |   2|   1|
> |---|
> | Pears & | Expected: |   4|   9|
> | |---|||
> |  Plums  | Actual:   |   6|   3|
> -
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.