Skip to content

Commit

Permalink
Further speedup bootstrap algo by 2x.
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushpatnaikgit committed Jan 22, 2023
1 parent dff1ea1 commit c60a95a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
9 changes: 3 additions & 6 deletions src/bootstrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ function bootweights(design::SurveyDesign; replicates=4000, rng=MersenneTwister(
psus = unique(cluster_sorted[!, design.cluster])
npsus = [(count(==(i), cluster_sorted[!, design.cluster])) for i in psus]
nh = length(psus)
randinds = rand(rng, 1:(nh), replicates, (nh-1))
cluster_weights = cluster_sorted[!, design.weights]
for replicate in 1:replicates
rh = zeros(Int, nh)
for i in randinds[replicate, :]
rh[i] += 1
end
cluster_sorted[!, "replicate_" * string(replicate)] = vcat([fill(rh[i] * (nh / (nh-1)), npsus[i]) for i in 1:length(rh)]...) .* cluster_sorted[!, design.weights]
randinds = rand(rng, 1:(nh), (nh-1))
cluster_sorted[!, "replicate_" * string(replicate)] = vcat([fill((count(==(i), randinds)) * (nh / (nh-1)), npsus[i]) for i in 1:nh]...) .* cluster_weights
end
push!(substrata_dfs, cluster_sorted)
end
Expand Down
24 changes: 12 additions & 12 deletions src/mean.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ julia> mean(:api00, clus_one_stage)
Row │ mean SE
│ Float64 Float64
─────┼──────────────────
1 │ 644.169 23.2877
1 │ 644.169 23.4107
julia> mean([:api00, :enroll], clus_one_stage)
2×3 DataFrame
Row │ names mean SE
│ String Float64 Float64
─────┼──────────────────────────
1 │ api00 644.169 23.2877
2 │ enroll 549.716 46.2597
1 │ api00 644.169 23.4107
2 │ enroll 549.716 45.7835
```
"""
function mean(x::Symbol, design::ReplicateDesign)
Expand Down Expand Up @@ -52,17 +52,17 @@ julia> mean(:api00, :cname, clus_one_stage)
Row │ cname mean SE
│ String15 Float64 Any
─────┼───────────────────────────────────
1 │ Santa Clara 732.077 59.6794
2 │ San Diego 659.436 2.63657
3 │ Merced 519.25 8.18989e-15
4 │ Los Angeles 647.267 47.7685
5 │ Orange 710.563 2.21461e-13
1 │ Santa Clara 732.077 58.2169
2 │ San Diego 659.436 2.66703
3 │ Merced 519.25 2.28936e-15
4 │ Los Angeles 647.267 47.6233
5 │ Orange 710.563 2.19826e-13
6 │ Fresno 472.0 1.13687e-13
7 │ Plumas 709.556 1.26823e-13
8 │ Alameda 669.0 1.26888e-13
9 │ San Joaquin 551.189 2.17297e-13
7 │ Plumas 709.556 1.26058e-13
8 │ Alameda 669.0 1.27527e-13
9 │ San Joaquin 551.189 2.1791e-13
10 │ Kern 452.5 0.0
11 │ Mendocino 623.25 1.09409e-13
11 │ Mendocino 623.25 1.09545e-13
```
"""
function mean(x::Symbol, domain::Symbol, design::ReplicateDesign)
Expand Down
5 changes: 3 additions & 2 deletions src/ratio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ julia> apiclus1 = load_data("apiclus1");
julia> clus_one_stage = SurveyDesign(apiclus1; clusters = :dnum, weights = :pw) |> bootweights;
ratio(:api00, :enroll, clus_one_stage)
julia> ratio(:api00, :enroll, clus_one_stage)
1×2 DataFrame
Row │ ratio SE
│ Float64 Float64
─────┼───────────────────
1 │ 1.17182 0.133361
1 │ 1.17182 0.131518
```
"""
function ratio(variable_num::Symbol, variable_den::Symbol, design::ReplicateDesign)
Expand Down
28 changes: 14 additions & 14 deletions src/total.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ julia> total(:api00, clus_one_stage)
Row │ total SE
│ Float64 Float64
─────┼──────────────────────
1 │ 3.98999e6 9.10443e5
1 │ 3.98999e6 9.01611e5
julia> total([:api00, :enroll], clus_one_stage)
2×3 DataFrame
Row │ names total SE
│ String Float64 Float64
─────┼──────────────────────────────
1 │ api00 3.98999e6 9.10443e5
2 │ enroll 3.40494e6 9.47987e5
1 │ api00 3.98999e6 9.01611e5
2 │ enroll 3.40494e6 9.33396e5
```
"""
function total(x::Symbol, design::ReplicateDesign)
Expand Down Expand Up @@ -52,17 +52,17 @@ julia> total(:api00, :cname, clus_one_stage)
Row │ cname total SE
│ String15 Float64 Any
─────┼────────────────────────────────────────
1 │ Santa Clara 6.44244e5 4.29558e5
2 │ San Diego 1.2276e6 8.60246e5
3 │ Merced 70300.2 70757.4
4 │ Los Angeles 3.2862e5 2.95688e5
5 │ Orange 3.84807e5 3.77128e5
6 │ Fresno 63903.1 64455.2
7 │ Plumas 2.16147e5 2.12279e5
8 │ Alameda 249080.0 2.5221e5
9 │ San Joaquin 6.90276e5 6.92353e5
10 │ Kern 30631.5 30333.5
11 │ Mendocino 84380.6 80774.4
1 │ Santa Clara 6.44244e5 4.2273e5
2 │ San Diego 1.2276e6 8.62727e5
3 │ Merced 70300.2 71336.3
4 │ Los Angeles 3.2862e5 2.93936e5
5 │ Orange 3.84807e5 3.88014e5
6 │ Fresno 63903.1 64781.7
7 │ Plumas 2.16147e5 2.12089e5
8 │ Alameda 249080.0 2.49228e5
9 │ San Joaquin 6.90276e5 6.81604e5
10 │ Kern 30631.5 30870.3
11 │ Mendocino 84380.6 80215.9
```
"""
function total(x::Symbol, domain::Symbol, design::ReplicateDesign)
Expand Down

0 comments on commit c60a95a

Please sign in to comment.