-
Notifications
You must be signed in to change notification settings - Fork 1
/
largescaleobserved.qmd
634 lines (542 loc) · 22.2 KB
/
largescaleobserved.qmd
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
---
fig-width: 4
fig-height: 3
fig-dpi: 150
fig-format: png
engine: julia
julia:
exeflags: ["--project"]
---
\newcommand{\bbtheta}{{\boldsymbol\theta}}
# A large-scale observational study {#sec-largescaleobserved}
Load the packages to be used,
```{julia}
#| code-fold: true
#| output: false
#| label: packages05
using AlgebraOfGraphics
using CairoMakie
using CategoricalArrays
using DataFrames
using EmbraceUncertainty: dataset
using GLM # for the lm function
using MixedModels
using MixedModelsMakie
using SparseArrays # for the nnz function
using Statistics # for the mean function
using TypedTables
```
and define some constants and a utility function
```{julia}
#| code-fold: true
#| output: false
#| label: constants05
optsumdir(paths::AbstractString...) =
joinpath(@__DIR__, "optsums", paths...)
@isdefined(contrasts) || const contrasts = Dict{Symbol,Any}()
@isdefined(progress) || const progress = false
```
In the previous chapter we explored and fit models to data from a large-scale designed experiment.
Here we consider an observational study - ratings of movies by users of [movielens.org](https://movielens.org) made available at the [grouplens.org download site](https://grouplens.org/datasets/movielens) [@Harper2016].
The version of the data that we analyze is the full movielens data set in the `ml-latest.zip` archive
at that site, consisting of over 27,000,000 ratings of movies by 280,000 users.
The total number of movies rated is over 58,000.
As the name `ml-latest.zip` could become ambiguous, we provide the table of contents of this zip file to help identify the dataset being used.
```
Archive: /home/bates/Downloads/ml-latest.zip
Length Date Time Name
--------- ---------- ----- ----
0 2018-09-26 16:46 ml-latest/
1267039 2018-09-26 16:37 ml-latest/links.csv
39744990 2018-09-26 16:11 ml-latest/tags.csv
18103 2018-09-26 16:44 ml-latest/genome-tags.csv
759200511 2018-09-26 16:31 ml-latest/ratings.csv
9784 2018-09-26 16:46 ml-latest/README.txt
414851573 2018-09-26 16:44 ml-latest/genome-scores.csv
2858223 2018-09-26 16:35 ml-latest/movies.csv
--------- -------
1217950223 8 files
```
## Structure of the data
One of the purposes of this chapter is to study which dimensions of the data have the greatest effect on the amount of memory used to represent the model and the time required to fit a model.
The two datasets examined in @sec-largescaledesigned, from the English Lexicon Project [@Balota_2007], consist of `trials` or `instances` associated with `subject` and `item` factors.
The `subject` and `item` factors are "incompletely crossed" in that each item occurred in trials with several subjects, but not all subjects, and each subject responded to several different items, but not to all items.
Similarly in the movie ratings, each instance of a rating is associated with a user and with a movie, and these factors are incompletely crossed.
```{julia}
ratings = dataset(:ratings)
```
Convert this Arrow table to a `Table` and drop the `timestamp` column, which we won't be using.
(For small data sets dropping such columns is not important but with over 27 million ratings it does help to drop unnecessary columns to save some memory space.)
```{julia}
ratings =
Table(getproperties(ratings, (:userId, :movieId, :rating)))
```
Information on the movies is available in the `movies` dataset, which we supplement with the mean rating for each movie in the `ratings` table.
```{julia}
movies = Table(
disallowmissing!(
leftjoin!(
combine(
groupby(DataFrame(ratings), :movieId),
:rating => mean => :mnrtng,
),
DataFrame(dataset(:movies));
on=:movieId,
);
error=false,
),
)
```
In contrast to data from a designed experiment, like the English Lexicon Project, the data from this observational study are extremely unbalanced with respect to the observational grouping factors, `userId` and `movieId`.
The `movies` table includes an `nrtngs` column that gives the number of ratings for each movie, which varies from 1 to nearly 100,000.
```{julia}
extrema(movies.nrtngs)
```
The number of ratings per user is also highly skewed
```{julia}
users = Table(
combine(
groupby(DataFrame(ratings), :userId),
nrow => :urtngs,
:rating => mean => :umnrtng,
),
)
```
```{julia}
extrema(users.urtngs)
```
One way of visualizing the imbalance in the number of ratings per movie or per user is as an [empirical cumulative distribution function](https://en.wikipedia.org/wiki/Empirical_distribution_function) (ecdf) plot, which is a "stair-step" plot where the vertical axis is the proportion of observations less than or equal to the corresponding value on the horizontal axis.
Because the distribution of the number of ratings per movie or per user is so highly skewed in the low range we use a logarithmic horizontal axis in @fig-nrtngsecdf.
```{julia}
#| code-fold: true
#| fig-cap: "Empirical distribution plots of the number of ratings per movie and per user. The horizontal axes are on a logarithmic scale."
#| label: fig-nrtngsecdf
#| warning: false
let
f = Figure(; size=(700, 350))
xscale = log10
xminorticksvisible = true
xminorgridvisible = true
yminorticksvisible = true
xminorticks = IntervalsBetween(10)
ylabel = "Relative cumulative frequency"
nrtngs = sort(movies.nrtngs)
ecdfplot(
f[1, 1],
nrtngs;
npoints=last(nrtngs),
axis=(
xlabel="Number of ratings per movie (logarithmic scale)",
xminorgridvisible,
xminorticks,
xminorticksvisible,
xscale,
ylabel,
yminorticksvisible,
),
)
urtngs = sort(users.urtngs)
ecdfplot(
f[1, 2],
urtngs;
npoints=last(urtngs),
axis=(
xlabel="Number of ratings per user (logarithmic scale)",
xminorgridvisible,
xminorticks,
xminorticksvisible,
xscale,
yminorticksvisible,
),
)
f
end
```
In this collection of over 27 million ratings, nearly 20% of the movies are rated only once and nearly half of the movies were rated 6 or fewer times.
```{julia}
count(≤(6), movies.nrtngs) / length(movies.nrtngs)
```
The ecdf plot of the number of ratings per user shows a similar pattern to that of the movies --- a few users with a very large number of ratings and many users with just a few ratings.
For example, about 20% of the users rated 10 movies or fewer; the median number of movies rated is around 30; but the maximum is close to 24,000 (which is a lot of movies - over 20 years of 3 movies per day every day - if this user actually watched all of them).
Movies with very few ratings provide little information about overall trends or even about the movie being rated.
We can imagine that the "shrinkage" of random effects for movies with just a few ratings pulls their adjusted rating strongly towards the overall average.
Furthermore, the distribution of ratings for movies with only one rating is systematically lower than the distribution of ratings for movies rated at least five times.
First, add `nrtngs` and `urtngs` as columns of the `ratings` table.
```{julia}
ratings = Table(
disallowmissing!(
leftjoin!(
leftjoin!(
DataFrame(ratings),
DataFrame(getproperties(movies, (:movieId, :nrtngs)));
on=:movieId,
),
DataFrame(getproperties(users, (:userId, :urtngs)));
on=:userId,
),
),
)
```
then create a bar chart of the two distributions
```{julia}
#| code-fold: true
#| fig-cap: "Distribution of ratings for movies with only one rating compared to movies with at least 5 ratings"
#| label: fig-ratingsbarcharts
#| warning: false
let
fiveplus = zeros(Int, 10)
onlyone = zeros(Int, 10)
for (i, n) in
zip(round.(Int, Float32(2) .* ratings.rating), ratings.nrtngs)
if n > 4
fiveplus[i] += 1
elseif isone(n)
onlyone[i] += 1
end
end
fiveprop = fiveplus ./ sum(fiveplus)
oneprop = onlyone ./ sum(onlyone)
draw(
data((;
props=vcat(fiveprop, oneprop),
rating=repeat(0.5:0.5:5.0; outer=2),
nratings=repeat(["≥5", "only 1"]; inner=10),
)) *
mapping(
:rating => nonnumeric,
:props => "Proportion of ratings";
color=:nratings => "Ratings/movie",
dodge=:nratings,
) *
visual(BarPlot);
figure=(; size=(600, 400)),
)
end
```
Similarly, users who rate very few movies add little information, even about the movies that they rated, because there isn't sufficient information to contrast a specific rating with a typical rating for the user.
One way of dealing with the extreme imbalance in the number of observations per user or per movie is to set a threshold on the number of observations for a user or a movie to be included in the data used to fit the model.
For example, a companion data set on grouplens.org, available in the [ml-25m.zip](https://files.grouplens.org/datasets/movielens/ml-25n.zip) archive, included only users who had rated at least 20 movies.
To be able to select ratings according to the number of ratings per user and the number of ratings per movie, we left-joined the `movies.nrtngs` and `users.urtngs` columns into the `ratings` data frame.
```{julia}
describe(DataFrame(ratings))
```
::: {.callout-note collapse="true"}
### Seemingly inconsistent medians of "nrtngs" and "urtngs"
The medians in this table of `nrtngs` and `urtngs` are much higher than the values from the `movies` and `users` tables because a movie with 98,000 ratings occurs 98,000 times in this table whereas it occurs only once in the `movies` table.
:::
## Models fit with lower bounds on ratings per user and per movie {#sec-lrgobsmods}
We fit a simple model to this dataset using different thresholds on the number of ratings per movie and the number of ratings per user.
These fits were performed on compute servers with generous amounts of memory (128 GiB/node) and numbers of compute cores (48/node).
A sample fit is shown in @sec-lrgobsmemprint.
The results are summarized in the following table
```{julia}
#| code-fold: true
sizespeed = Table(dataset(:sizespeed))
```
In this table, `mc` is the "movie cutoff" (i.e. the threshold on the number of ratings per movie); `uc` is the user cutoff (threshold on the number of ratings per user); `nratings`, `nusers` and `nmvie` are the number of ratings, users and movies in the resulting trimmed data set; `modelsz` is the size (in GiB) of the model fit; `L22sz` is the size of the [2,2] block of the `L` matrix in that model; `fittime` is the time (in seconds) required to fit the model; `nev` is the number of function evaluations until convergence; and `evtime` is the time (s) per function evaluation.
The "[2,2] block of the `L` matrix" is described in @sec-lrgobsmemprint.
### Dimensions of the model versus cut-off values
First we consider the effects of the minimum number of ratings per user and per movie on the dimensions of the data set, as shown in @fig-dimsbarchart.
```{julia}
#| label: legend-setup
#| echo: false
#| output: false
sizespeed = let
ord(x) = categorical(x; ordered=true) # replace this with sorter
transform!(
DataFrame(sizespeed),
:mc => ord => :mc,
:uc => ord => :uc,
[:L22sz, :modelsz] => ((x, y) -> x ./ y) => :L22prop,
)
end
llabels = string.(refpool(sizespeed.uc))
lelements = [
PolyElement(polycolor=c) for c in
get.(
Ref(Makie.colorschemes[:viridis]),
range(0, 1, length(llabels)),
)
]
ltitle = "Minimum number of ratings per user"
```
```{julia}
#| fig-cap: "Bar plot of data set dimensions by minimum number of ratings per user and per movie."
#| code-fold: true
#| label: fig-dimsbarchart
#| warning: false
let
f = Figure(size=(600, 800))
xlabel = "Minimum number of ratings per movie"
mc = refarray(sizespeed.mc)
xticks = (1:7, string.(refpool(sizespeed.mc)))
uc = refarray(sizespeed.uc)
Legend(
f[1, 1],
lelements,
llabels,
ltitle;
orientation=:horizontal,
tellwidth=false,
tellheight=true,
)
barplot!(
Axis(f[2, 1]; xticks, ylabel="Number of users"),
mc,
sizespeed.nusers;
xticks,
dodge=uc,
color=uc,
)
barplot!(
Axis(f[3, 1]; xticks, ylabel="Number of movies"),
mc,
sizespeed.nmvie;
xticks,
dodge=uc,
color=uc,
)
barplot!(
Axis(f[4, 1]; xlabel, xticks, ylabel="Number of observations"),
mc,
sizespeed.nratings;
dodge=uc,
color=uc,
)
f
end
```
Unsurprisingly, @fig-dimsbarchart shows that increasing the minimum number of ratings per user decreases the number of users, does not noticeably affect the number of movies, and results in small decreases in the total number of ratings.
Conversely, increasing the minimum number of ratings per movie does not noticeably affect the number of users, causes dramatic reductions in the number of movies and has very little effect on the total number of ratings.
### Memory footprint of the model representation {#sec-lrgobsmemprint}
To explain what "the [2,2] block of the `L` matrix" is and why its size is important, we provide a brief overview of the evaluation of the "profiled" log-likelihood for a `LinearMixedModel` representation.
To make the discussion concrete we consider one of the models represented in this table, with cut-offs of 20 ratings per user and 20 ratings per movie.
This, and any of the models shown in the table, can be restored in a few minutes from the saved `optsum` values, as opposed to taking up to two hours to perform the fit.
```{julia}
#| code-fold: true
function ratingsoptsum(
mcutoff::Integer,
ucutoff::Integer;
data=Table(ratings),
form=@formula(rating ~ 1 + (1 | userId) + (1 | movieId)),
contrasts=Dict(:movieId => Grouping(), :userId => Grouping()),
)
optsumfnm = optsumdir(
"mvm$(lpad(mcutoff, 2, '0'))u$(lpad(ucutoff, 2, '0')).json",
)
isfile(optsumfnm) ||
throw(ArgumentError("File $optsumfnm is not available"))
return restoreoptsum!(
LinearMixedModel(
form,
filter(
r -> (r.nrtngs ≥ mcutoff) & (r.urtngs ≥ ucutoff),
data,
);
contrasts,
),
optsumfnm,
)
end
mvm20u20 = ratingsoptsum(20, 20)
print(mvm20u20)
```
Creating the model representation and restoring the optimal parameter values can take a couple of minutes because the objective is evaluated twice --- at the initial parameter values and at the final parameter values --- during the call to `restoreoptsum!`.
Each evaluation of the objective, which requires setting the value of the parameter $\bbtheta$ in the numerical representation of the model, updating the blocked Cholesky factor, $\mathbf{L}$, and evaluating the scalar objective value from this factor, takes a little over a minute (71 seconds) on a server node and probably longer on a laptop.
The lower triangular `L` factor is large but sparse.
It is stored in six blocks of dimensions and types as shown in
```{julia}
BlockDescription(mvm20u20)
```
This display gives the types of two blocked matrices: `A` which is derived from the data and does not depend on the parameters, and `L`, which is derived from `A` and the $\bbtheta$ parameter.
The only difference in their structures is in the [2,2] block, which is diagonal in `A` and a dense, lower triangular matrix in `L`.
The memory footprint (bytes) of each of the blocks is
```{julia}
#| code-fold: true
let
block = String[]
for i in 1:3
for j in 1:i
push!(block, "[$i,$j]")
end
end
Table((;
block,
Abytes=Base.summarysize.(mvm20u20.A),
Lbytes=Base.summarysize.(mvm20u20.L),
))
end
```
resulting in total memory footprints (GiB) of
```{julia}
#| code-fold: true
NamedTuple{(:A, :L)}(
Base.summarysize.(getproperty.(Ref(mvm20u20), (:A, :L))) ./ 2^30,
)
```
That is, `L` requires roughly 10 times the amount of storage as does `A`, and that difference is entirely due to the different structure of the [2,2] block.
This phenomenon of the Cholesky factor requiring more storage than the sparse matrix being factored is described as [fill-in](https://en.wikipedia.org/wiki/Sparse_matrix).
Note that although the dimensions of the [2,1] block are larger than those of the [2,2] block its memory footprint is smaller because it is a sparse matrix.
The matrix is over 99% zeros or, equivalently, less than 1% nonzeros,
```{julia}
let
L21 = mvm20u20.L[2] # blocks are stored in a one-dimensional array
nnz(L21) / length(L21)
end
```
which makes the sparse representation much smaller than the dense representation.
This fill-in of the [2,2] block leads to a somewhat unintuitive conclusion.
The memory footprint of the model representation depends strongly on the number of movies, less strongly on the number of users and almost not at all on the number of ratings.
The first two parts of this conclusion are illustrated in @fig-memoryfootprint.
```{julia}
#| fig-cap: "Bar plot of memory footprint of the model representation by minimum number of ratings per user and per movie."
#| code-fold: true
#| label: fig-memoryfootprint
#| warning: false
let
f = Figure(; size=(600, 800))
xlabel = "Minimum number of ratings per movie"
mc = refarray(sizespeed.mc)
xticks = (1:7, string.(refpool(sizespeed.mc)))
uc = refarray(sizespeed.uc)
Legend(
f[1, 1],
lelements,
llabels,
ltitle;
orientation=:horizontal,
tellwidth=false,
tellheight=true,
)
barplot!(
Axis(f[2, 1]; xticks, ylabel="Memory requirement (GiB)"),
mc,
sizespeed.modelsz;
xticks,
dodge=uc,
color=uc,
)
barplot!(
Axis(f[3, 1]; xticks, ylabel="Size of L[2,2] (GiB)"),
mc,
sizespeed.L22sz;
xticks,
dodge=uc,
color=uc,
)
barplot!(
Axis(f[4, 1]; xlabel, xticks, ylabel="L[2,2] memory fraction"),
mc,
sizespeed.L22prop;
xticks,
dodge=uc,
color=uc,
)
f
end
```
@fig-memoryfootprint shows that when all the movies are included in the data to which the model is fit (i.e. `mc == 1`) the total memory footprint is over 20 GiB, and nearly 90% of that memory is that required for the `[2,2]` block of `L`.
Even when requiring a minimum of 50 ratings per movie, the `[2,2]` block of `L` is over 30% of the memory footprint.
In a sense this is good news because the amount of storage required for the `[2,2]` block can be nearly cut in half by taking advantage of the fact that it is a triangular matrix.
The [rectangular full packed format](https://netlib.org/lapack/lawnspdf/lawn199.pdf) looks especially promising for this purpose.
In general, for models with scalar random effects for two incompletely crossed grouping factors, the memory footprint depends strongly on the smaller of the number of levels of the grouping factors, less strongly on the larger number, and almost not at all on the number of observations.
### Speed of log-likelihood evaluation
The time required to fit a model to large data sets is dominated by the time required to evaluate the log-likelihood during the optimization of the parameter estimates.
The time for one evaluation is given in the `evtime` column of `sizespeed`.
Also given is the number of evaluations to convergence, `nev`, and the time to fit the model, `fittime`
The reason for considering `evtime` in addition to `fittime` and `nev` is because the `evtime` for one model, relative to other models, is reasonably stable across computers whereas `nev`, and hence, `fittime`, can be affected by seemingly trivial variations in function values resulting from different implementations of low-level calculations, such as the BLAS (Basic Linear Algebra Subroutines).
That is, we can't expect to reproduce `nev` exactly when fitting the same model on different computers or with slightly different versions of software but the pattern in `evtime` with respect to `uc` and `mc` can be expected to reproducible.
```{julia}
#| fig-cap: "Bar plot of function log-likelihood evaluation time by minimum number of ratings per user and per movie."
#| code-fold: true
#| label: fig-timetoconverge
#| warning: false
let
f = Figure(size=(600, 800))
xlabel = "Minimum number of ratings per movie"
mc = refarray(sizespeed.mc)
xticks = (1:7, string.(refpool(sizespeed.mc)))
uc = refarray(sizespeed.uc)
Legend(
f[1, 1],
lelements,
llabels,
ltitle;
orientation=:horizontal,
tellwidth=false,
tellheight=true,
)
barplot!(
Axis(
f[2, 1];
xticks,
ylabel="Log-likelihood evaluation time (m)",
),
mc,
sizespeed.evtime ./ 60;
xticks,
dodge=uc,
color=uc,
)
barplot!(
Axis(
f[3, 1];
xticks,
ylabel="Number of evaluations to convergence",
),
mc,
sizespeed.nv;
xticks,
dodge=uc,
color=uc,
)
barplot!(
Axis(
f[4, 1];
xlabel,
xticks,
ylabel="Time (hr) to fit the model",
),
mc,
sizespeed.fittime ./ 3600;
xticks,
dodge=uc,
color=uc,
)
f
end
```
@fig-timetoconverge shows that the average evaluation time for the log-likelihood function depends strongly on the number of movies and less strongly on the number of users.
However the middle panel shows that the number of iterations to convergence is highly variable.
Most of these models required between 20 and 25 evaluations but some required almost 50 evaluations.
The derivation of the log-likelihood for linear mixed-effects models is given in @sec-lmmtheory, which provides a rather remarkable result:
the profiled log-likelihood for a linear mixed-effects model can be evaluated from Cholesky factor of a blocked, positive-definite symmetric matrix.
There are two blocked matrices, `A` and `L`, stored in the model representation and, for large models such as we are considering these are the largest fields in
## Model size and speed for different thresholds
```{julia}
#| code-fold: true
#| fig-cap: "Amount of storage for L[2,2] versus amount of storage for the entire model. Colors are determined by the minimum number of ratings per user in the data to which the model was fit."
#| label: fig-L22vsmodelsz
#| warning: false
draw(
data(sizespeed) *
mapping(
:L22sz => "Size (GiB) of 2,2 block of L",
:modelsz => "Size (GiB) of model representation";
color=:uc,
) *
visual(Scatter);
figure=(; size=(600, 500)),
)
```
Linear regression of the `modelsz` versus the number of users and `L22sz`.
```{julia}
sizemod =
lm(@formula(Float64(modelsz) ~ 1 + nusers + L22sz), sizespeed)
coeftable(sizemod)
```
provides an $r^2$ value very close to one, indicating an almost perfect fit.
```{julia}
r²(sizemod)
```
*This page was rendered from git revision {{< git-rev short=true >}}.*