/
BFM.jl
44 lines (40 loc) · 1.11 KB
/
BFM.jl
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
function num_conbination(n::Int64, d::Int64)
div(factorial(n+d), factorial(n)*factorial(d)) - 1
end
"""
make_design_matrix(x, dims)
This function return the design matrix.
# Parameters
- `x`: input data
- `dims`: degree of design matrix
# Example
```jldoctest regression
julia> make_design_matrix(x, dims = 2) |> size
(20, 5)
```
"""
function make_design_matrix(x; dims = 2)
n_datas, n_features = size(x)
out_features = num_conbination(n_features, dims)
y = Array{Float64}(undef, n_datas, out_features)
current = 1
y[:, current : current+n_features-1] = x
index = collect(current : current+n_features-1)
current += n_features
push!(index, current)
for _ in 2 : dims
new = []
stop = index[end]
for feature_idx in 1 : n_features
start = index[feature_idx]
push!(new, current)
next= current + stop - start
next <= current && break
@. y[:, current : next-1] = y[:, start:stop-1] * x[:, feature_idx]
current = next
end
push!(new, current)
index = new
end
return y
end