forked from incanter/incanter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobability_plots.clj
503 lines (378 loc) · 16.4 KB
/
probability_plots.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
(ns examples.probability-plots
(:use (incanter core stats charts)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; NORMAL DISTRIBUTION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Examples of plots from the Normal Distribution page at
;; Wikipedia (http://en.wikipedia.org/wiki/Normal_distribution)
(def x (range -3 3 0.01))
;; plot the PDFs of the normal distributions
(doto (xy-plot x (pdf-normal x)
:title "Normal PDF"
:y-label "Density"
:legend true)
(add-lines x (pdf-normal x :sd (sqrt 0.2)))
(add-lines x (pdf-normal x :sd (sqrt 5.0)))
(add-lines x (pdf-normal x :mean -2 :sd (sqrt 0.5)))
view)
;; plot the CDFs of the normal distributions
(doto (xy-plot x (cdf-normal x)
:title "Normal CDF"
:y-label "Probability"
:legend true)
(add-lines x (cdf-normal x :sd (sqrt 0.2)))
(add-lines x (cdf-normal x :sd (sqrt 5.0)))
(add-lines x (cdf-normal x :mean -2 :sd (sqrt 0.5)))
view)
;; make box-plots for each of the normal distributions
(doto (box-plot (sample-normal 1000)
:title "Normal Boxplot"
:legend true)
(add-box-plot (sample-normal 1000 :sd (sqrt 0.2)))
(add-box-plot (sample-normal 1000 :sd (sqrt 5.0)))
(add-box-plot (sample-normal 1000 :mean -2 :sd (sqrt 0.5)))
view)
;; make a histogram of a sample of 1000 standard normal deviates
(view (histogram (sample-normal 1000)
:title "Normal Histogram (mean, sd)"
:legend true))
;; plot the inverse of the normal distribution
(def p (range 0.01 1 0.01))
(doto (xy-plot p (quantile-normal p)
:title "Normal Inverse"
:x-label "Probability"
:y-label "X"
:legend true)
(add-lines p (quantile-normal p :sd (sqrt 0.2)))
(add-lines p (quantile-normal p :sd (sqrt 5.0)))
(add-lines p (quantile-normal p :mean -2 :sd (sqrt 0.5)))
view)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; GAMMA DISTRIBUTION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Examples of plots from the Gamma Distribution page at
;; Wikipedia (http://en.wikipedia.org/wiki/Gamma_distribution)
(def x (range 0 20 0.1))
(doto (xy-plot x (pdf-gamma x :shape 1 :rate 2)
:legend true
:title "Gamma PDF"
:y-label "Density")
(add-lines x (pdf-gamma x :shape 2 :rate 2))
(add-lines x (pdf-gamma x :shape 3 :rate 2))
(add-lines x (pdf-gamma x :shape 5 :rate 1))
(add-lines x (pdf-gamma x :shape 9 :rate 0.5))
view)
(doto (xy-plot x (cdf-gamma x :shape 1 :rate 2)
:title "Gamma CDF"
:legend true
:y-label "Probability")
(add-lines x (cdf-gamma x :shape 2 :rate 2))
(add-lines x (cdf-gamma x :shape 3 :rate 2))
(add-lines x (cdf-gamma x :shape 5 :rate 1))
(add-lines x (cdf-gamma x :shape 9 :rate 0.5))
view)
(doto (box-plot (sample-gamma 1000 :shape 1 :rate 2)
:title "Gamma Boxplot"
:legend true)
(add-box-plot (sample-gamma 1000 :shape 2 :rate 2))
(add-box-plot (sample-gamma 1000 :shape 3 :rate 2))
(add-box-plot (sample-gamma 1000 :shape 5 :rate 1))
(add-box-plot (sample-gamma 1000 :shape 9 :rate 0.5))
view)
;; make a histogram of a sample of 1000 Gamma deviates
(view (histogram (sample-gamma 1000 :shape 1 :rate 2)
:title "Gamma Histogram"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; BETA DISTRIBUTION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Examples of plots from the Beta Distribution page at
;; Wikipedia (http://en.wikipedia.org/wiki/Beta_distribution)
(def x (range 0 1 0.01))
(doto (xy-plot x (pdf-beta x :alpha 1 :beta 1)
:title "Beta PDF"
:y-label "Density"
:legend true)
(add-lines x (pdf-beta x :alpha 5 :beta 1))
(add-lines x (pdf-beta x :alpha 1 :beta 3))
(add-lines x (pdf-beta x :alpha 2 :beta 2))
(add-lines x (pdf-beta x :alpha 2 :beta 5))
view)
(doto (xy-plot x (cdf-beta x :alpha 1 :beta 1)
:title "Beta CDF"
:y-label "Probability"
:legend true)
(add-lines x (cdf-beta x :alpha 5 :beta 1))
(add-lines x (cdf-beta x :alpha 1 :beta 3))
(add-lines x (cdf-beta x :alpha 2 :beta 2))
(add-lines x (cdf-beta x :alpha 2 :beta 5))
view)
;; make box-plots for each of the Beta distributions
(doto (box-plot (sample-beta 1000 :alpha 1 :beta 1)
:title "Beta Boxplot"
:legend true)
(add-box-plot (sample-beta 1000 :alpha 5 :beta 1))
(add-box-plot (sample-beta 1000 :alpha 1 :beta 3))
(add-box-plot (sample-beta 1000 :alpha 2 :beta 2))
(add-box-plot (sample-beta 1000 :alpha 2 :beta 5))
view)
;; make a histogram of a sample of 1000 beta deviates
(view (histogram (sample-beta 1000 :alpha 5 :beta 1)
:title "Beta Histogram (alpha,beta)"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CHI SQUARE DISTRIBUTION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Examples of plots from the Chi Square Distribution page at
;; Wikipedia (http://en.wikipedia.org/wiki/Chi_square_distribution)
(def x (range 0.1 8 0.01))
(doto (xy-plot x (pdf-chisq x :df 1)
:title "Chi Square PDF"
:x-label "X"
:y-label "Density"
:legend true)
(add-lines x (pdf-chisq x :df 2))
(add-lines x (pdf-chisq x :df 3))
(add-lines x (pdf-chisq x :df 4))
(add-lines x (pdf-chisq x :df 5))
view)
(doto (xy-plot x (cdf-chisq x :df 1)
:title "Chi Square CDF"
:x-label "X"
:y-label "Probability"
:legend true)
(add-lines x (cdf-chisq x :df 2))
(add-lines x (cdf-chisq x :df 3))
(add-lines x (cdf-chisq x :df 4))
(add-lines x (cdf-chisq x :df 5))
view)
;; make box-plots for each of the Chi Square distributions
(doto (box-plot (sample-chisq 1000 :df 1)
:title "Chi Square Boxplot"
:legend true)
(add-box-plot (sample-chisq 1000 :df 2))
(add-box-plot (sample-chisq 1000 :df 3))
(add-box-plot (sample-chisq 1000 :df 4))
(add-box-plot (sample-chisq 1000 :df 5))
view)
;; make a histogram of a sample of 1000 Chi Square deviates
(view (histogram (sample-chisq 1000 :df 1)
:title "Chi Square Histogram"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; STUDENT'S T DISTRIBUTION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Examples of plots from the Student's T-Distribution page at
;; Wikipedia (http://en.wikipedia.org/wiki/Student-t_distribution)
(def x (range -5 5 0.01))
(doto (xy-plot x (pdf-t x :df 1)
:title "Student's T PDF"
:legend true
:x-label "X"
:y-label "Density")
(add-lines x (pdf-t x :df 2))
(add-lines x (pdf-t x :df 5))
(add-lines x (pdf-t x :df 10))
(add-lines x (pdf-t x :df 1000))
view)
(doto (xy-plot x (cdf-t x :df 1)
:title "Student's T CDF"
:legend true
:x-label "X"
:y-label "Probability")
(add-lines x (cdf-t x :df 2))
(add-lines x (cdf-t x :df 5))
(add-lines x (cdf-t x :df 10))
(add-lines x (cdf-t x :df 1000))
view)
;; make box-plots for each of the Student's t distributions
(doto (box-plot (sample-t 1000 :df 1)
:title "Student's t Boxplot"
:legend true)
(add-box-plot (sample-t 1000 :df 2))
(add-box-plot (sample-t 1000 :df 5))
(add-box-plot (sample-t 1000 :df 10))
(add-box-plot (sample-t 1000 :df 1000))
view)
;; make a histogram of a sample of 1000 Chi Square deviates
(view (histogram (sample-t 1000 :df 10) :title "Student's t Histogram"))
;; plot the quantiles of the Student's t distribution
(def p (range 0.05 1 0.01))
(doto (xy-plot p (quantile-t p :df 1)
:title "Student's t Quantiles (df)"
:x-label "Probability"
:y-label "X"
:legend true)
(add-lines p (quantile-t p :df 2))
(add-lines p (quantile-t p :df 5))
(add-lines p (quantile-t p :df 10))
(add-lines p (quantile-t p :df 50))
view)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; EXPONENTIAL DISTRIBUTION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Examples of plots from the Exponential Distribution page at
;; Wikipedia (http://en.wikipedia.org/wiki/Exponential_distribution)
(def x (range 0 5 0.01))
(doto (xy-plot x (pdf-exp x :rate 1/2)
:title "Exponential PDF"
:x-label "X"
:y-label "Density"
:legend true)
(add-lines x (pdf-exp x :rate 1))
(add-lines x (pdf-exp x :rate 1.5))
view)
(doto (xy-plot x (cdf-exp x :rate 1/2)
:title "Exponential CDF"
:x-label "X"
:y-label "Probability"
:legend true)
(add-lines x (cdf-exp x :rate 1))
(add-lines x (cdf-exp x :rate 1.5))
view)
;; make box-plots for each of the Exponentials distributions
(doto (box-plot (sample-exp 1000 :rate 1/2)
:title "Exponential Boxplot"
:legend true)
(add-box-plot (sample-exp 1000 :rate 1))
(add-box-plot (sample-exp 1000 :rate 1.5))
view)
;; make a histogram of a sample of 1000 Exponential deviates
(view (histogram (sample-exp 1000 :rate 1.5)
:title "Exponential Histogram (rate)"
:nbins 20))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; UNIFORM DISTRIBUTION FUNCTIONS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Examples of plots from the Continuous Uniform Distribution page at
;; Wikipedia (http://en.wikipedia.org/wiki/Uniform_distribution)
(def x (range 1 10.01 0.01))
(doto (xy-plot x (pdf-uniform x :min 1 :max 10)
:title "Uniform PDF"
:y-label "Density")
view)
(doto (xy-plot x (cdf-uniform x :min 1 :max 10)
:title "Uniform CDF"
:y-label "Probability")
view)
;; make a histogram of a sample of 1000 standard normal deviates
(view (histogram (sample-uniform 1000) :title "Uniform Histogram"))
(view (histogram (sample-uniform 1000 :min 0 :max 100 :integers true)
:title "Uniform Histogram"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; DISCRETE DISTRIBUTIONS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; BINOMIAL DISTRIBUTION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Examples of plots from the Binomial Distribution page at
;; Wikipedia (http://en.wikipedia.org/wiki/Binomial_distribution)
(def x1 (range 0 20))
(def x2 (range 0 40))
(doto (scatter-plot x1 (pdf-binomial x1 :prob 1/2 :size 20)
:title "Binomial PDF"
:y-label "Density"
:legend true)
(add-points x1 (pdf-binomial x1 :prob 0.7 :size 20))
(add-points x2 (pdf-binomial x2 :prob 1/2 :size 40))
view)
(doto (scatter-plot x1 (cdf-binomial x1 :prob 1/2 :size 20)
:title "Binomial CDF"
:y-label "Probability"
:legend true)
(add-points x1 (cdf-binomial x1 :prob 0.7 :size 20))
(add-points x2 (cdf-binomial x2 :prob 1/2 :size 40))
view)
;; make box-plots for each of the Binomial distributions
(doto (box-plot (sample-binomial 1000 :prob 1/2 :size 20)
:title "Binomial Boxplot"
:legend true)
(add-box-plot (sample-binomial 1000 :prob 0.7 :size 20))
(add-box-plot (sample-binomial 1000 :prob 1/2 :size 40))
view)
;; make a histogram of a sample of 1000 Exponential deviates
(view (histogram (sample-binomial 1000 :prob 1/2 :size 20)
:title "Binomial Histogram"
:nbins 10))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; NEGATIVE BINOMIAL DISTRIBUTION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Examples of plots from the Negative Binomial Distribution page at
;; Wikipedia (http://en.wikipedia.org/wiki/Negative_binomial_distribution)
(def x1 (range 0 100))
(def x2 (range 0 50))
(def x3 (range 0 100))
(doto (scatter-plot x2 (pdf-neg-binomial x2 :prob 1/10 :size 50)
:title "Negative Binomial PDF"
:y-label "Density"
:legend true)
(add-points x2 (pdf-neg-binomial x2 :prob 1/8 :size 75))
(add-points x3 (pdf-neg-binomial x3 :prob 1/4 :size 150))
view)
(doto (scatter-plot x1 (cdf-neg-binomial x1 :prob 1/2 :size 25)
:title "Negative Binomial CDF"
:y-label "Probability"
:legend true)
(add-points x1 (cdf-neg-binomial x1 :prob 2/3 :size 25))
(add-points x1 (cdf-neg-binomial x1 :prob 3/4 :size 25))
view)
;; make box-plots for each of the Negative Binomial distributions
(doto (box-plot (sample-neg-binomial 1000 :prob 1/2 :size 20)
:title "Negative Binomial Boxplot"
:legend true)
(add-box-plot (sample-neg-binomial 1000 :prob 0.7 :size 20))
(add-box-plot (sample-neg-binomial 1000 :prob 1/2 :size 40))
view)
;; make a histogram of a sample of 1000 Exponential deviates
(view (histogram (sample-neg-binomial 1000 :prob 1/4 :size 500)
:title "Negative Binomial Histogram"
:nbins 10))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; POISSON DISTRIBUTION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Examples of plots from the Poisson Distribution page at
;; Wikipedia (http://en.wikipedia.org/wiki/Poisson_distribution)
(def x1 (range 0 20))
(doto (scatter-plot x1 (pdf-poisson x1 :lambda 1)
:title "Poisson PDF"
:legend true)
(add-points x1 (pdf-poisson x1 :lambda 4))
(add-points x1 (pdf-poisson x1 :lambda 10))
view)
(doto (scatter-plot x1 (cdf-poisson x1 :lambda 1)
:title "Poisson CDF"
:legend true)
(add-points x1 (cdf-poisson x1 :lambda 4))
(add-points x1 (cdf-poisson x1 :lambda 10))
view)
;; make box-plots for each of the Poisson distributions
(doto (box-plot (sample-poisson 1000 :lambda 1)
:title "Poisson Boxplot"
:legend true)
(add-box-plot (sample-poisson 1000 :lambda 4))
(add-box-plot (sample-poisson 1000 :lambda 10))
view)
;; make a histogram of a sample of 1000 Exponential deviates
(view (histogram (sample-poisson 1000 :lambda 10)
:title "Poisson Histogram (lambda)"
:nbins 10))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; F DISTRIBUTION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Examples of plots from the F Distribution page at
;; Wikipedia (http://en.wikipedia.org/wiki/F_distribution)
(def x (range 0 5 0.01))
(doto (xy-plot x (pdf-f x :df1 2 :df2 1)
:title "F PDF"
:y-label "Density"
:legend true)
(add-lines x (pdf-f x :df1 5 :df2 2))
(add-lines x (pdf-f x :df1 100 :df2 1))
(add-lines x (pdf-f x :df1 100 :df2 100))
view)
(doto (xy-plot x (cdf-f x :df1 1 :df2 1)
:title "F CDF"
:y-label "Probability"
:legend true)
(add-lines x (cdf-f x :df1 2 :df2 1))
(add-lines x (cdf-f x :df1 5 :df2 2))
(add-lines x (cdf-f x :df1 100 :df2 1))
(add-lines x (cdf-f x :df1 100 :df2 100))
view)