-
Notifications
You must be signed in to change notification settings - Fork 98
move implementations into type overloading (aka. functor) #139
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
85d8fe0
make evaluate a general API
johnnychen94 d7962c3
BhattacharyyaDist and HellingerDist
johnnychen94 200203c
Bregman
johnnychen94 a4d78ea
Haversine
johnnychen94 5484e25
SqMahalanobis and Mahalanobis
johnnychen94 83a2f3a
metrics in metrics.jl
johnnychen94 53307b4
remove specification of pairwise and colwise on CorrDist
johnnychen94 f1214a1
metrics in wmetrics.jl
johnnychen94 c79f914
colwise and pairwise
johnnychen94 c31007e
update README.md
johnnychen94 089243a
update test
johnnychen94 b0f1148
rename metric_list to metrics
johnnychen94 86f5d22
revert auto-formatted spaces
johnnychen94 498647f
update format
johnnychen94 51b4ab5
Merge branch 'master' into dispatch
johnnychen94 acd1095
rollback auto-format on whitespaces
johnnychen94 b885201
test if there are any ambiguities
johnnychen94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,48 @@ | ||
| # Bregman divergence | ||
| # Bregman divergence | ||
|
|
||
| """ | ||
| Implements the Bregman divergence, a friendly introduction to which can be found | ||
| [here](http://mark.reid.name/blog/meet-the-bregman-divergences.html). | ||
| Bregman divergences are a minimal implementation of the "mean-minimizer" property. | ||
| [here](http://mark.reid.name/blog/meet-the-bregman-divergences.html). | ||
| Bregman divergences are a minimal implementation of the "mean-minimizer" property. | ||
| It is assumed that the (convex differentiable) function F maps vectors (of any type or size) to real numbers. | ||
| The inner product used is `Base.dot`, but one can be passed in either by defining `inner` or by | ||
| passing in a keyword argument. If an analytic gradient isn't available, Julia offers a suite | ||
| of good automatic differentiation packages. | ||
| It is assumed that the (convex differentiable) function F maps vectors (of any type or size) to real numbers. | ||
| The inner product used is `Base.dot`, but one can be passed in either by defining `inner` or by | ||
| passing in a keyword argument. If an analytic gradient isn't available, Julia offers a suite | ||
| of good automatic differentiation packages. | ||
| function evaluate(dist::Bregman, p::AbstractVector, q::AbstractVector) | ||
| """ | ||
| struct Bregman{T1 <: Function, T2 <: Function, T3 <: Function} <: PreMetric | ||
| struct Bregman{T1 <: Function,T2 <: Function,T3 <: Function} <: PreMetric | ||
| F::T1 | ||
| ∇::T2 | ||
| inner::T3 | ||
| end | ||
|
|
||
| # Default costructor. | ||
| # Default costructor. | ||
| Bregman(F, ∇) = Bregman(F, ∇, LinearAlgebra.dot) | ||
|
|
||
| # Evaluation fuction | ||
| function evaluate(dist::Bregman, p::AbstractVector, q::AbstractVector) | ||
| # Evaluation fuction | ||
| function (dist::Bregman)(p::AbstractVector, q::AbstractVector) | ||
| # Create cache vals. | ||
| FP_val = dist.F(p); | ||
| FQ_val = dist.F(q); | ||
| FQ_val = dist.F(q); | ||
| DQ_val = dist.∇(q); | ||
| p_size = size(p); | ||
| # Check F codomain. | ||
| # Check F codomain. | ||
| if !(isa(FP_val, Real) && isa(FQ_val, Real)) | ||
| throw(ArgumentError("F Codomain Error: F doesn't map the vectors to real numbers")) | ||
| end | ||
| # Check vector size. | ||
| end | ||
| # Check vector size. | ||
| if !(p_size == size(q)) | ||
| throw(DimensionMismatch("The vector p ($(size(p))) and q ($(size(q))) are different sizes.")) | ||
| end | ||
| # Check gradient size. | ||
| # Check gradient size. | ||
| if !(size(DQ_val) == p_size) | ||
| throw(DimensionMismatch("The gradient result is not the same size as p and q")) | ||
| end | ||
| # Return the Bregman divergence. | ||
| return FP_val - FQ_val - dist.inner(DQ_val, p-q); | ||
| end | ||
| end | ||
| # Return the Bregman divergence. | ||
| return FP_val - FQ_val - dist.inner(DQ_val, p - q); | ||
johnnychen94 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| # Convenience function. | ||
| bregman(F, ∇, x, y; inner = LinearAlgebra.dot) = evaluate(Bregman(F, ∇, inner), x, y) | ||
| # Convenience function. | ||
| bregman(F, ∇, x, y; inner = LinearAlgebra.dot) = Bregman(F, ∇, inner)(x, y) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.