
we submitted to scheme workshop and if our paper gets in I’ll probably go :smiley:

my student will be going too, and I think he’ll probably want someone to split a room with

the conference is not very harmoniously planned with my teaching schedule, though :confused:. Don’t really want to miss four courses. Will probably compromise by doing two remote if I go

To the math inclined people, what’s the best way to draw an element from a uniform distribution table: #hasheq((a . 0.5) (b . 0.16) (c . 0.2))
? Note the table is not normalize. I can easily do that with a loop to get #hasheq((a . 0.58) (b . 0.19) (c . 0.23))
, but then I would probably compute the choice borders for 1 as a list, and get '((a . 0.58) (b . 0.77) (c . 1))
. After this I do a (random)
and loop the list until I find a cdr
larger than my random value, returning then the car
. This looks quite complex for what looks to be a simple thing to do. Also, there might be a library out there doing things the right way. Any suggestions?

So, I have taken the above approach to write this horrific piece of code: (define distribution
#hasheq((a . 0.5) (b . 0.16) (c . 0.2)))
(define sum (for/fold ([total 0])
([(_ v) (in-hash distribution)])
(+ total v)))
(define normalized
(for/hasheq ([(k v) (in-hash distribution)])
(values k (/ v sum))))
(define choice-edges
(for/fold ([total 0]
[l '()]
#:result (reverse l))
([p (in-list (sort (hash->list normalized) < #:key cdr))])
(define k (car p))
(define v (cdr p))
(define accum (+ total v))
(values accum (cons (cons k accum) l))))
(define (random-in-distribution)
(define v (random))
(let loop ([edges choice-edges])
(define edge (car edges))
(if (< v (cdr edge))
(car edge)
(loop (cdr edges)))))

Suggestions on how to improve this are welcome. There are so many loops that it feels really inefficient, however most of those could be done once since the distribution in my case is static. The bigger problem is that the function generating the values, requires looping through the choice-edges list.

So, I revised this to generate a table so that I only need a vector-ref
for the random choice: (define normalize-100
(for/hasheq ([(k v) (in-hash distribution)])
(values k (inexact->exact (round (/ (* v 100) sum))))))
(require mischief/for)
(define choice-table
(list->vector
(for/append (((k v) (in-hash normalize-100)))
(make-list v k))))
(define sz (vector-length choice-table)) ;; not necessarily 100
(define (random-in-choice-table)
(define v (random sz))
(vector-ref choice-table v))

Better but not great. I welcome any comments on this.

You can use discrete-dist
to make a finite distribution with your weights. Then use sample
to generate random samples.

Hello, can someone help me out getting unit testing working

This is my main script/module

called ABC.rkt

then I have ABC_test.rkt in the same folder


but I get the following error… ABC.rkt:3:9: ABC.rkt:3:9: cannot open module file
module path: math/distribution
path: /usr/share/racket/pkgs/math-lib/math/distribution.rkt
system error: No such file or directory; errno=2 in: math/distribution
no package suggestions are available .

math/distribution -> math/distributions

ah I hadn’t save the file :stuck_out_tongue:

the ABC.rkt

that helps too :slightly_smiling_face:

I made the change but forgot to save :man-facepalming:

but thanks anyways

should pay more attention to the trace

racket has a quickcheck that’s really cool :slightly_smiling_face:

do a lot of people use it?

strange binomial-dist returns a float

would be nicer that it be a integer

@soegaard2 thanks. I will take a look at the lib.

lang racket
(struct foo (name)) (struct colored-foo (color)) (struct sized-foo (size))
(match (sized-foo ’xl) [(colored-foo n) ’colored] ; <= expected error here! [(sized-foo n) ’sized] [(foo n) n])

@samth Is this a bug in match?

The result is ’sized.

nevermind - forgot foo

(vector-map sample (normal-dist))
Type Checker: missing type for top-level identifier;
either undefined or missing a type annotation
identifier: sample in: sample

Is this supposed to happen..?

ah wait

I meant (vector-map sample '#((normal-dist)))

still get the same error

(vector-map distribution? (vector (normal-dist)))

this works

(vector-map sample (vector (normal-dist)))
this doesn’t

any idea why..?

I’m not seeing the same error message, but if I run this: #lang typed/racket
(require math/distributions)
(vector-map sample (vector (normal-dist)))
then TR says Type Checker: Polymorphic function
vector-map’ could not be applied to arguments …`

yeah I am getting that now

and that usually means the program needs to use inst
to replace some type variables

hum still learning Racket and typed racket

not sure how to use inst

(vector-map (inst sample Real Real) (vector (normal-dist)))
seems to work

using inst
is hard

you need to figure out / guess which expressions have polymorphic type, then the number of variables, and finally what to fill them with

note that the error message tells you the first two things:

Type Checker: Polymorphic function `vector-map' could not be applied to arguments:
Domain: (-> a b ... b c) (Vectorof a) (Vectorof b) ... b
Arguments: (All (In Out) (case-> (-> (distribution In Out) Out) (-> (distribution In Out) Integer (Listof Out)))) (Vector Normal-Dist)
Result type: (Vectorof c)
Expected result: AnyValues

you need to instantiate the first argument (it’s the one with the All
type) and you need to supply two type arguments (for In
and Out
)

ok thanks :slightly_smiling_face:, let’s see if I can learn

is there any particular doc link that explains how to go about this?

I don’t quite understand why it’s Real

in this case

the double Real I mean

other numbers might work too

I understand that the result values within the vector will be Real

hum (inst sample Real) works

so I don’t need the extra Real?

I guess TR is able to figure out the result, when inferring a type for this application of vector-map

This is confusing (vector-map (inst sample Real Real) (vector (bernoulli-dist 1.0)))

This works fine

But then this doesn’t (vector-map (inst sample Real Real) (vector (bernoulli-dist 1.0) (normal-dist)))

…??

what does the error message say?

@maxim_jaffe it supplies a default type if you don’t provide all of them

Type Checker: Polymorphic function `vector-map' could not be applied to arguments:
Domain: (-> a b ... b c) (Vectorof a) (Vectorof b) ... b
Arguments: (case-> (-> (distribution Real Real) Real) (-> (distribution Real Real) Integer (Listof Real))) (Vector Bernoulli-Dist Normal-Dist)
in: (vector-map (inst sample Real Real) (vector (bernoulli-dist 1.0) (normal-dist)))

you mean I have to supply for distributionin the vector..? Or..?

I get the general idea ut don’t understand the specifics and the trace message is confusing me

Is this explained in detail in the Typed Racket Guide or Reference? I just looked at the basics to be honest

hm, I think this is because the default type for the vector is too specific for map

because this works: #lang typed/racket
(require math/distributions)
(define v : (Vectorof (U Normal-Dist Bernoulli-Dist)) (vector (bernoulli-dist 1.0) (normal-dist)))
(vector-map (inst sample Real Real) v)

but what if I want to use any distribution…

is there a supertype of all distributions?

think its just called distribution


hm, it also works to avoid using vector
: #lang typed/racket
(require math/distributions)
(define v (list->vector (list (bernoulli-dist 1.0) (normal-dist))))
(vector-map (inst sample Real Real) v)

> (distribution? (normal-dist)) - : Boolean [more precisely: True] #t

(vector
is trying to infer a Vector
type when possible, instead of a Vectorof
type)

what..

is there a constructor for Vectorof

Vectorof
is a type, not a value

there are two types for vectors, Vector
and Vectorof
. the former is a type like a tuple: (Vector Integer String Boolean)
describes a vector with exactly 3 elements, the first of which is an integer, the second of which is a string, and the third of which is a boolean. Vectorof
only takes one type argument, and it describes a vector of arbitrary length that only contains one type of element.

this is kind of confusing, but it’s done because untyped racket uses lists and vectors in both ways.

Also, the type names correspond to the related contract names in untyped racket, which are named vector/c
and vectorof
, respectively.

Well I am basically looking at a vector of a single type (sort like an array in other languages) not really interested in a tuple

(map (inst sample Real Real) (list (bernoulli-dist) (normal-dist)))

this seems to work ok

and that’s because lists are immutable

Works with different sizes (map (inst sample Real Real) (list (bernoulli-dist) (normal-dist) (poisson-dist)))

vector are mutable?

ah didn’t realise that

well there’s 2 kinds of vectors but usually they’re mutable

I don’t need to mutable data structure

(vector …) is mutable, '#( ... )
is immutable

hum ok

didn’t understand it was different

I should have said (vector-immutable ...)
above

Ok so my basic reasoning is I just want to process a fixed length data structure the fastest

from what I read I thought I would be better of with immutable vector

should I just stick to lists instead?

I just need to take a bunch of distributions and sample from them

for Approximate Bayesian Computation (ABC)

say 1000 or 10 000 samples (possible in each nth iteration)

I have some functions in regular racket

working

just wanted to change it to typed racket for th performance gains

if you want fast random access to a structure, vectors are better

people should be around here & on the mailing list to help with the type errors

well you guys @ben @samth and @lexi.lambda have already help quite a bit :slightly_smiling_face: thanks


Ok so I changed to functions to work with lists (at least for now). But I get another error

. Type Checker: No function domains matched in function application:
Types: Real Real Real -> (Sequenceof Real)
Real Real -> (Sequenceof Real)
Real -> (Sequenceof Nonnegative-Integer)
Arguments: Zero (U Exact-Complex Exact-Imaginary Float-Imaginary Inexact-Complex Single-Flonum-Imaginary) One
Expected result: AnyValues
in: (for/list ((i (range n))) (prior-sample priors))

hum think I figured it out

(: prior-samples (-> Real (Listof distribution) (Listof (Listof Real))))

Type Checker: type mismatch
expected: (Listof distribution)
given: (List Bernoulli-Dist) in: (list (bernoulli-dist 0.0))

hum so the same issue between List and Listof ?

I was tinkering a little, it didn’t seem to me that Bernoulli-Dist
was a subtype of distribution

#lang typed/racket
(require math/distributions)
(ann (bernoulli-dist 0.0) distribution)
;;Type Checker: type mismatch
;; expected: distribution
;; given: Bernoulli-Dist in: (bernoulli-dist 0.0)

strange, but with this test, it returns #t > (distribution? (bernoulli-dist))
- : Boolean [more precisely: True]
#t

shouldn’t it match?

I don’t know — I agree that is odd

by the way how do I generate a type Listof

..?

List
vs Listof
wasn’t the problem in the last error message

(List A ...)
is a subtype of (Listof B)
so long as A
is a subtype of B

hum ok!

so ann is used to check if it is a certain subtype

type or subtype

is there someway of getting the “inheritance” or something like that of the type

so you know to which subtypes it belongs?

ann
is just a type annotation. It tells the type checker “I expect it to be this type”, which sometimes helps terms type check that otherwise would not, and other times it can serve to confirm to the programmer a term is of the type they thought it was

I don’t think there’s a nice way to explore the “inheritance tree” of types directly…

ok sort of like type or isinstance in Python?

hum ok

maybe I should ready the guide more carefully

the TR guide

but ann
doesn’t do anything at runtime

it’s just for the type checker (and programmer xD)

@maxim_jaffe I’m not at all familiar with the “distribution” portion of the math library, but in peeking at the code, it seems indeed Bernoulli-Dist
should be a subtype of distribution
. I’ll submit an issue on the github repo.

seems to be even more general problem

(ann (normal-dist) distribution)
. Type Checker: type mismatch
expected: distribution
given: Normal-Dist in: (normal-dist)

> (ann (binomial-dist) distribution)
. Type Checker: type mismatch
expected: distribution
given: Binomial-Dist in: (binomial-dist)

> (ann (poisson-dist) distribution)
. Type Checker: type mismatch
expected: distribution
given: Poisson-Dist in: (poisson-dist)

hmm… perhaps there’s some more obvious reason it doesn’t work then? (or maybe it’s just a big glaring omission no one has stumbled upon until now? like I said, I’m not at all familiar with this code)

yeah I find it odd


aha!

#lang typed/racket
(require math/distributions)
(ann (bernoulli-dist 0.0) (distribution Real Real))

hehe ok?

> (ann (poisson-dist) (distribution Real Real))
- : (distribution Real Real)
(poisson-dist 0.5)

seems to work

so in the function what type annotation should I put


(Listof distribution Real Real) ..?

methinks (-> (Listof (distribution Real Real)) (Listof Real)))

cool thanks that works

so let me get this right

I don’t know CS deeply

but distribution type is polymorphic..? or something

yes

(struct: (In Out) distribution ([pdf : (PDF In)]
[sample : (Sample Out)])
#:transparent)

distributions are parameterized by an In
and Out
type

hum ok

guess I have to study some more hehe :wink:

but seems to working now

I think the docs here should probably be more clear about the distribution
(and other such) type: https://docs.racket-lang.org/math/Distribution_Types_and_Operations.html?q=bernoulli-dist

to be honest I didn’t read the whole docs I just went in intuition

but I did lookup distribution

but I didn’t understand at the time what In and Out was

thanks for the help @pnwamk this is great

was afraid I wouldn’t be able to use Typed Racket (which without the speed gains would maybe make my application to slow to work with in regular Racket)

Small but helpful community you’ve got :slightly_smiling_face:


I got to say even in it’s problems Racket is interesting

hehe

xD

I spent sometime learning Julia, the problems were not at all easy to grasp or to appreciate

anyway will try and go over the Typed Racket Guide with more care

but it’s great to have someone to talk to when you hit a wall

It’s something I never really experienced as a Python programmer, you mostly just find stuff in stackoverflow

First time I started going into chat rooms was when I started learning languages besides Python

I liked the little bit of Julia I learned. Don’t have time to learn it right now, but very interesting language :slightly_smiling_face:

They’re also always looking for ppl to work on their compiler, if people are interested in that stuff (like an internship over summer, etc)

@liamschumm has joined the channel

Stackoverflow can be OK for Racket questions, except certain times of year it gets depressing because so many people are looking for someone to do their Scheme or Racket homework. Then it’s a lot of (cond( (car l) (cdr l)
)
(else( null?( cadr l
)
)
)
)
:cry:

There is that…. :disappointed: I mean, I can handle (begrudgingly)
(define (foo x)
(if blah
(this that)
(the other thing)
)
)

But they can’t even manage something like that. :cry:

Probably means they haven’t found DrRacket/racket-mode yet.

Try writing Racket/Scheme in a standard editor without paren-highlighting and automatic indentation.

I have…..when I answer their SO questions.

But ya, they probably haven’t found it yet.

(But I frequently just write the samples in the SO editor, because I’m lazy like that.)

BTW, @robby, is the background expander found in drracket/private/expanding-place
?

(Or is there somewhere else I should look for it.)

that’s part of it, yes.

Okay. I’m looking to emulate it to do other things in DrRacket, where would you recommend I start looking?

Is there a way to extract all the lexical information from a syntax object and pass that around? I may be wrong but I think the lexical information would contain things like file, line, column, etc. I want to pass this information along. So, is there a way to bind that information in, a let block or something similar?

@plotnus You can get the source/location using these functions: https://docs.racket-lang.org/reference/stxops.html#(def._((quote._~23~25kernel)._syntax-source))

More about how Racket represents syntax (including what I think of as “lexical” info or scopes): https://www.cs.utah.edu/plt/scope-sets/index.html

@greg thanks I’ll check that out

btw just realized that you’re the one that wrote “Fear of Macros” Thank you for writing that!

You’re welcome!

thanks