Plot Problem: No Line; Bounds not found

閲覧: 44 回
最初の未読メッセージにスキップ

Britt Anderson

未読、
2021/06/15 8:18:562021/06/15
To: Racket Users

I was starting to explore plot and I am getting a behavior I do not understand. plot doesn't seem to be able to figure out the bounds for something that seems pretty straight-forward. When I give bounds as an optional argument I don't see the line I expect to see. Can anyone provide me some pointers? I feel like I am just missing something conceptually and would appreciate some guidance. Here is some minimal code to demonstrate the problem.  I get a plot square, but no line, and I can demonstrate that the function generates reasonable numbers for this range of inputs.


   
    #lang racket
    (require plot)
    (require math/distributions)

    (define (norm-prior mu)
      (lambda (sd)
      (lambda (ind)
        (* ind (flnormal-pdf mu sd ind #f)))))

    (define d ((norm-prior 0.5) 0.1))

    (plot (function d 0 1 #:y-min 0.0 #:y-max 2.0 #:samples 100))

    (map d (range 0.0 1.0 0.01))


 

Ben Greenman

未読、
2021/06/15 9:46:372021/06/15
To: Britt Anderson、Racket Users
This is tricky.

Plot is calling your function `d` with inputs like 0, 1/99, and 1. All
of those give contract errors --- try (d 0) for yourself.

But when a plot function throws an error, the library ignores the
problem & keeps trying to draw a picture.

To fix, I'd change `norm-prior` to make a flonum:

(define (norm-prior mu)
(lambda (sd)
(lambda (ind)
(* ind (flnormal-pdf mu sd (exact->inexact ind) #f)))))

[[ Maybe plot should check if a function renderer produces no output
and throw an error then. ]]

Britt Anderson

未読、
2021/06/15 14:52:392021/06/15
To: Racket Users
Thanks for the help to my specific problem. More generally, what should have clued me in to a contract or type error when the only message received had to do with y plot bounds? Just looking for practical advice to help me figure out things on my own going forward.

Ben Greenman

未読、
2021/06/15 15:19:062021/06/15
To: Racket Users
On 6/15/21, Britt Anderson <britt.u...@gmail.com> wrote:
> Thanks for the help to my specific problem. More generally, what should
> have clued me in to a contract or type error when the only message received
> had to do with y plot bounds? Just looking for practical advice to help me
> figure out things on my own going forward.

There's nothing in this program that could have clued you in besides
the empty plot.

Next time, I'd write tests for my function before sending it to plot's
`function` renderer.

Robby Findler

未読、
2021/06/15 16:04:552021/06/15
To: Ben Greenman、Racket Users
While I completely agree that testing one's function is the best practical advice here, it also seems worth an improvement to plot. Perhaps, in addition to saying "could not determine sensible plot bounds", it could also say something like "because there were no points" and perhaps even follow up with "tried these points, but they all raised exceptions: ....".

Robby


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAFUu9R5FF6%3D4c0-KnkycMBeL0AaU9PHnKRELOH33J8b9NSXqzQ%40mail.gmail.com.

Alex Harsányi

未読、
2021/06/16 4:11:482021/06/16
To: Racket Users
I think this is an interesting case, and I am not sure that writing tests would have helped.  

With the benefit of hindsight, it is easy to see what is going on, but, if I were to write tests for this function, I would have noticed that `(d 0)` throws a contract error and made sure I only pass flonums to the test -- the function itself is correct after all, it is just that it expects only certain kind of numbers (i.e 0.0 but not 0, 1.0 but not 1, etc).  Even adding a contract to this function would not have helped, since the plot package would have discarded those as well.  For some examples how why this plot feature is useful, see this comment: https://github.com/racket/plot/issues/97#issuecomment-861887283

Alex.
 
全員に返信
投稿者に返信
転送
新着メール 0 件