summaryrefslogtreecommitdiffstats
path: root/assignments/hw01/question2_to_8.jl
blob: 3cf9dc9e28bedd976a7bcba82c09f022cd8474f8 (plain)
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

using MAT

all_vars= matread("sampledata2.mat")
d = all_vars["data"]

# ----------------------- Q2
length(filter(x -> x > 5, d[:, 2])) / length(d[:, 2])
# 0.19, so first statement is false

length(filter(x -> x > 50, d[:, 1]))
# 56x over 50

length(filter(x -> x < 20, d[:, 1]))
# 4x under 20
# second statement is true

length(filter(x -> x < 5, d[:, 2]))
# 243
length(filter(x -> x > 6, d[:, 2]))
# 34
# third statement is false

length(filter(x -> x > 0.5, d[:, 3])) / length(d[:, 3])
# 0.5233333333333333
# last statement is false

# ----------------------- Q3

using Winston

# TODO: this foldl business is ugly
with_cancer = foldl(vcat, filter(x -> x[3] > 0.5, [d[row, :] for row = 1:size(d,1)]))
without_cancer = foldl(vcat, filter(x -> x[3] < 0.5, [d[row, :] for row = 1:size(d,1)]))

figure()
p = FramedPlot()
add(p, Histogram(hist(with_cancer[:, 1], 15:10:75)..., color="red"))

# 1 NO (this is "without")
# 2 yes
# 3 NO
# 4 NO

# ----------------------- Q4

add(p, Histogram(hist(without_cancer[:, 1], 15:10:75)..., color="green"))
# 1 YES

# ----------------------- Q5

function fractional(f, d)
    return length(filter(f, d)) / length(d)
end

fractional(x -> x>55, without_cancer[:, 1])
#0.013986013986013986

fractional(x -> x<25, without_cancer[:, 1])
#0.08391608391608392
# FALSE

fractional(x -> x<35, without_cancer[:, 1])
# FALSE

fractional(x -> x<30, with_cancer[:, 1]) / fractional(x -> x>60, with_cancer[:, 1])
# TRUE!

fractional(x -> x>45, with_cancer[:, 1])
# FALSE

# ----------------------- Q6

fractional(x -> x>35, with_cancer[:, 1])
# 0.802547770700637

# ----------------------- Q7

fractional(x -> x<25, d[:, 1])
# 0.06

# ----------------------- Q8

drinkers = foldl(vcat, filter(x -> x[2] > 3, [d[row, :] for row = 1:size(d,1)]))
fractional(x -> x >0.5, drinkers[:, 3])
# 0.6459627329192547