r Programming 1
r Programming 1
The probability distribution in which the random variable assumes all its values
withequal probabilities, is known as discrete unifornm distribution. If the random variable
Xassumes the value x1, X2, ......, XK With equal probabilities, then its probability function
is given by
P(X = x) =for
k i = 1,2, . , k
If the outcomes are the natural numbers x; = i, (i=1,2, ...,k), the mean and
variance of X are obtained as
k+1
E(X) 2
1
Var(x) =(k2-
12 1).
How to do it with R
p)
= Probability mass function for Bin(50, 0.7)
prob 0.12
n, o
= 0,06
size
dbinom(%,
0.00
Lgooo0009oago000000oag0000000° Ooo0000o
20 30 40 50
10
plot of the PMF of a binomial distribution withn= 50 and p = 0.7 (i.e. B(50,
A
given in Figure 6.1. If we want to calculate the probability of observing an
0.)) is cumulative distribution
value, we can use the
Outcome less thanor egual to a particular
function.
p)
= 0.8
prob
=n, 0.6
size
0.4
pbinom(x,
0.2
0.0
10 20 30 40 50
qbinom(p_seq,
P_seq
Figurdreaws.64. FrR
in red) in
> hist(draws, breaks =
> points (x, dbinom(x, brks, =probability =TRUE)
size n, prob = p),
col=" red")
Histogram of draws
0.15
Density0.10
0.05
0.00
10 20 30 40 50
draws
E(X) = Var(X) = 2.
How to do it with R
science
111/R for data
Probability mass function for Po(2)
rate)
lambda
dpois(x,
&
.10
0.00
2 4 6 10
1.0
0.8 Cumulative distribution function for Po(2)
rate)
=
lambda
0.6
Ppois(x,
0.4
0.2
2 4 6 8 10
qpois(p_seq,
4
P_seq
science
113/ R for data
0.30 Random draws from Po(2)
0.20
Density
0.10
0.00
1 2 3 4 5
draws
when a<<b
f(z) =
0, when z<a or >b
x: input sequence
min, max= range of values
0.015
y_dunif
0.000
20 40 60 80 100
Index
1.0
0.8
0.6
y_punr
0.4
0.2
0.0
60 80 100
20 40
Index
Output:
> X_qunif <-
> X_qunif seq(0, 1, by = 0.01)
[1] 0.00
171 0.16 0.01 0.02 0.03 0.04 0.05
0.17
331 0. 32 0.33 0.18 0.19 0.20 0.21 0.06
0.22
0.07 0.08 0.09 0.10
0.11 0.12 0.13 0.14 0.15
0.34
[49] 0.48 0.49 0.50 0.35 0.23
0.36 0.37 O.38 0.39 0.24 0.25 0.26 0.27 0.28
[65] 0.64 0.65 0.66 0.51 0.52 0.53 0.54 0.55 0.40 0.41 0.42 0.43 0.44 0.29
0. 45
0.30 0.31
[81] 0.80 0.81 0.67 0.68 0.69 0.70 0.56 0.57 0.58 0.59 0.46 0.47
[97] 0.96 0.97 0.82 0.83 0.84 0.85 0.86 0.71 0.72 0.73 0.74 0.75 0.60 0.61 0.62 0.63
> y_qunif <- 0.98 0.99 1.00 0.87 0.88 0.76
0.89 0.90 0.91 0.92 0.77 0.78 0.79
> y_qunif
[1] 10.0 10.4
qunif(x_qunif,
min = 10, max = 50) 0.93 0.94 0.95
[17] 16.4 16.8 10.8
17.2
11.2 11.6 12.0 12.4
12.8
[331 22.8 23.2 17.6 18.0 18.4 13.2 13.6 14.0 14.4
18.8 19.2 14.8
[49] 29.2 29.6 23.6 24.0 24.4 24.8 25.2 25.6
19.6 20.0 20.4 20.8 21.2
15.2 15.6 16.0
[651 35.6 36.0 30.0 30.4 30.8 31.2 31.6 32.0
26.0 26.4 26.8 27.2 27.6
21.6 22.0 22.4
36.4
[81] 42.0 42.4 42.8 36.8 37.2 37.6 32.4 32.8 33.2 33.6
28.0 28.4 28.8
43.2 38.0 38.4 38.8 39.2 34.0 34.4 34.8
[97] 48.4 43.6 44.0 44.4 39.6 40.0 35.2
> plot(y 48.8 49.2 49.6 50.0 44.8 45.2 45.6 46.0 46.4
40.4 40.8 41.2 41.6
_qunif, type = "o") 46.8 47.2 47.6 48.0
50
40
Y_qunif
30
20
10
20 40 60 80 100
Index
Parameter:
250
Frequency
150
50
0
20 40 60 80 100
Y_runif
science
l19/ Rfor data
6.3 Normal Distribution:
While dealing with quantities whose
weight, height etc. a continuous probability magnitude is ofis
experiment involves discrete phenomena, the distribution
touse if the number of appropriate discrete
contneeded.inuous
natsoure
Al
a continuous model to observations are model may be difr
large. In such cases, it is often
most useful theoreticalapproximate discrete model. Normal Conveni e nt to
the
0.4
0.3
y_dnorm
0.2
0.1
0.0
oo0goooO0oo00000
Index
0.8
Y_pnorm
0.4
J.0
50
100
150
200
Index
-Z
150 200
50 100
Index
0.3
Density
0.2
0.1
0.0
-2 0 2
4
N= 1000 Bandwidth = 0.2167
4 R
for data
science