Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Dirac weights #49

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/weight_fitting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,27 @@ end
"""
project_onto_nonnegative_orthant(vector)

Projects `vector` onto the nonnegative_orthant. This projection is trivial:
Projects `vector` onto the nonnegative orthant. This projection is trivial:
replace negative components of the vector with zeros.
"""
function project_onto_nonnegative_orthant(vector::Vector{Float64})
return max.(vector, 0.0)
end

"""
project_onto_standard_basis(vector)

Projects `vector` onto the standard basis. This projection is trivial:
replace all components of the vector with zeros, except for the largest one,
which is replaced with one.
"""
function project_onto_standard_basis(vector::Vector{Float64})
i = argmax(vector)
result = zeros(size(vector))
result[i] = 1.0
return result
end

"""
projected_subgradient_descent!(x; gradient, projection, niters, rtol, learning_rate, adaptive_grad)

Expand Down Expand Up @@ -166,7 +180,9 @@ function fit_rep_period_weights!(
args...,
)
# Determine the appropriate projection method
if weight_type == :convex
if weight_type == :dirac
projection = project_onto_standard_basis
elseif weight_type == :convex
projection = project_onto_simplex
elseif weight_type == :conical
projection = project_onto_nonnegative_orthant
Expand Down
34 changes: 34 additions & 0 deletions test/test-weight-fitting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@
TulipaClustering.project_onto_simplex(x) ≈ [0.0, 1.0]
end
end

@testset "Make sure that projection onto standard basis works correctly" begin
@test begin
x = [1.0, 10.0]
TulipaClustering.project_onto_standard_basis(x) == [0.0, 1.0]
end

@test begin
x = [10.0, 1.0]
TulipaClustering.project_onto_standard_basis(x) == [1.0, 0.0]
end

@test begin
x = [-2.0, 1.0]
TulipaClustering.project_onto_standard_basis(x) == [0.0, 1.0]
end
end
end

@testset "Subgradient descent" begin
Expand Down Expand Up @@ -42,6 +59,23 @@ end
return DBInterface.execute(con, "SELECT * FROM profiles") |> DataFrame
end

@testset "Make sure that weight fitting works correctly for Dirac weights" begin
@test begin
clustering_data = get_data()
split_into_periods!(clustering_data; period_duration = 24 * 7)
clustering_result = find_representative_periods(
clustering_data,
10;
drop_incomplete_last_period = false,
method = :k_means,
distance = SqEuclidean(),
init = :kmcen,
)
TulipaClustering.fit_rep_period_weights!(clustering_result; weight_type = :dirac, niters = 5)
all(sum(clustering_result.weight_matrix[1:(end - 1), :], dims = 2) .== 1.0)
end
end

@testset "Make sure that weight fitting works correctly for convex weights" begin
@test begin
clustering_data = get_data()
Expand Down
Loading