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

90 average calibration functions in utils.jl #97

Closed
wants to merge 48 commits into from

Conversation

pasq-cat
Copy link
Contributor

added functions in utils.jl for computing the average empirical frequency and changed glm_predictive_distribution to return a normal distribution in case of regression. it should pass all tests

@pasq-cat pasq-cat linked an issue Jun 22, 2024 that may be closed by this pull request
src/baselaplace/predicting.jl Outdated Show resolved Hide resolved
src/baselaplace/predicting.jl Outdated Show resolved Hide resolved
src/utils.jl Outdated Show resolved Hide resolved
src/utils.jl Outdated Show resolved Hide resolved
src/utils.jl Outdated Show resolved Hide resolved
pasq-cat and others added 2 commits June 22, 2024 11:41
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
src/utils.jl Outdated
Comment on lines 134 to 136



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

pasq-cat and others added 2 commits June 22, 2024 11:43
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Comment on lines 47 to 49
normal_distr = [
Distributions.Normal(fμ[i], fstd[i]) for i in 1:size(fμ, 1)]
return (normal_distr,fμ,fvar )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
normal_distr = [
Distributions.Normal(fμ[i], fstd[i]) for i in 1:size(fμ, 1)]
return (normal_distr,fμ,fvar )
normal_distr = [Distributions.Normal(fμ[i], fstd[i]) for i in 1:size(fμ, 1)]
return (normal_distr, fμ, fvar)

@@ -75,11 +83,12 @@ predict(la, hcat(x...))
function predict(
la::AbstractLaplace, X::AbstractArray; link_approx=:probit, predict_proba::Bool=true
)
fμ, fvar = glm_predictive_distribution(la, X)
normal_distr,fμ, fvar = glm_predictive_distribution(la, X)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
normal_distr,fμ, fvar = glm_predictive_distribution(la, X)
normal_distr, fμ, fvar = glm_predictive_distribution(la, X)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@@ -75,11 +82,12 @@ predict(la, hcat(x...))
function predict(
la::AbstractLaplace, X::AbstractArray; link_approx=:probit, predict_proba::Bool=true
)
fμ, fvar = glm_predictive_distribution(la, X)
normal_distr,fμ, fvar = glm_predictive_distribution(la, X)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
normal_distr,fμ, fvar = glm_predictive_distribution(la, X)
normal_distr, fμ, fvar = glm_predictive_distribution(la, X)

@JuliaTrustworthyAI JuliaTrustworthyAI deleted a comment from github-actions bot Jun 22, 2024
src/utils.jl Outdated Show resolved Hide resolved
pasq-cat and others added 2 commits July 1, 2024 00:45
yeah basically juliaformatter being a pain for nothing

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Member

@pat-alt pat-alt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice work here @Rockdeldiablo 👍🏽 just a few missing pieces:

  • check individual comments (minor things)
  • add a tutorial to the documentation
  • add unit tests (at least passing through functions and having simple tests for expected output type, then new issue for testing with synthetic data).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could just keep this consistent and return everything in both cases?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: my bad, let's indeed as discussed just add an option for classification to return distribution. By default, we should still return probabilities for now, but at least we give the option and add that to the docstring.

src/utils.jl Outdated
@@ -39,3 +40,128 @@ corresponding to the number of neurons on the last layer of the NN.
function outdim(model::Chain)::Number
return [size(p) for p in Flux.params(model)][end][1]
end

"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice detail here! Just a formatting thing: can you use maths notation that's recognized by Documenter.jl please? See here for an example and here for the docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i fixed the docstrings but for a new line ( without the empy line in the middle), i had to use the \ character.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i checked the results in the julia repl but i was not able to make the function appear in the documentation. i tried to go in docs/ and then type julia make.jl and i got "[ Info: SetupBuildDirectory: setting up build directory.
[ Info: Doctest: running doctests.
[ Info: ExpandTemplates: expanding markdown templates.
[ Info: CrossReferences: building cross-references.
[ Info: CheckDocument: running document checks.
[ Info: Populate: populating indices.
[ Info: RenderDocument: rendering document.
[ Info: HTMLWriter: rendering HTML pages.
┌ Warning: Unable to determine the repository root URL for the navbar link.
│ This can happen when a string is passed to the repo keyword of makedocs.

│ To remove this warning, either pass a Remotes.Remote object to repo to completely
│ specify the remote repository, or explicitly set the remote URL by setting repolink
│ via makedocs(format = HTML(repolink = "..."), ...).
└ @ Documenter.HTMLWriter C:\Users\Pasqu.julia\packages\Documenter\qoyeC\src\html\HTMLWriter.jl:732
[ Info: Automatic version="0.2.1" for inventory from ..\Project.toml
┌ Warning: Documenter could not auto-detect the building environment. Skipping deployment.
└ @ Documenter C:\Users\Pasqu.julia\packages\Documenter\qoyeC\src\deployconfig.jl:76 "
so i opened index.html with edge through vs code and my functions do not appear in the documentations when i search for them. do i have to modify some sort of flag?

src/utils.jl Outdated
return sharpness
end

"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above comment on maths notation

Copy link
Member

@pat-alt pat-alt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding your question on docs, just make sure that the function signature stated in the docstring matches the function

src/utils.jl Outdated
"""
function sharpness_regression(sampled_distributions)
sharpness = mean(var.(sampled_distributions))
return sharpness
end

"""
@doc raw"""
empirical_frequency-classification(y_binary, sampled_distributions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dash should be an underscore

src/utils.jl Outdated
@@ -144,21 +148,22 @@ function empirical_frequency_binary_classification(
return (num_p_per_interval, emp_avg, bin_centers)
end

"""
sharpness-classification(y_binary,sampled_distributions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, dash should be an underscore

src/utils.jl Outdated
- `bin_centers`: array with the centers of the bins.

"""
function empirical_frequency_binary_classification(y_binary, sampled_distributions, n_bins::Int=20)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
function empirical_frequency_binary_classification(y_binary, sampled_distributions, n_bins::Int=20)
function empirical_frequency_binary_classification(
y_binary, sampled_distributions, n_bins::Int=20
)

Comment on lines 7 to 8
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8;
0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8;
0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2

Comment on lines 10 to 13
mean_class_one, mean_class_zero = sharpness_classification(y_binary, sampled_distributions)
@test mean_class_one ≈ mean(sampled_distributions[1,[2,4,5,7]])
@test mean_class_zero ≈ mean(sampled_distributions[2,[1,3,6,8]])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
mean_class_one, mean_class_zero = sharpness_classification(y_binary, sampled_distributions)
@test mean_class_one mean(sampled_distributions[1,[2,4,5,7]])
@test mean_class_zero mean(sampled_distributions[2,[1,3,6,8]])
mean_class_one, mean_class_zero = sharpness_classification(
y_binary, sampled_distributions
)
@test mean_class_one mean(sampled_distributions[1, [2, 4, 5, 7]])
@test mean_class_zero mean(sampled_distributions[2, [1, 3, 6, 8]])

Comment on lines 16 to 17


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change


# Test for `sharpness_regression` function
@testset "sharpness_regression tests" begin
sampled_distributions = [[0.1, 0.2, 0.3, 0.7, 0.6], [0.2, 0.3, 0.4, 0.3 , 0.5 ], [0.3, 0.4, 0.5, 0.9, 0.2]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
sampled_distributions = [[0.1, 0.2, 0.3, 0.7, 0.6], [0.2, 0.3, 0.4, 0.3 , 0.5 ], [0.3, 0.4, 0.5, 0.9, 0.2]]
sampled_distributions = [
[0.1, 0.2, 0.3, 0.7, 0.6], [0.2, 0.3, 0.4, 0.3, 0.5], [0.3, 0.4, 0.5, 0.9, 0.2]
]

sharpness = sharpness_regression(sampled_distributions)

@test sharpness ≈ mean_variance
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
end
end


@testset "sharpness_classification tests" begin


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

# Test 1: Check that the function runs without errors and returns two scalars for a simple case
y_binary = [1, 0, 1, 0, 1]
sampled_distributions = [0.9 0.1 0.8 0.2 0.7; 0.1 0.9 0.2 0.8 0.3] # Sampled probabilities
mean_class_one, mean_class_zero = sharpness_classification(y_binary, sampled_distributions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
mean_class_one, mean_class_zero = sharpness_classification(y_binary, sampled_distributions)
mean_class_one, mean_class_zero = sharpness_classification(
y_binary, sampled_distributions
)

@test typeof(mean_class_one) <: Real # Check if mean_class_one is a scalar
@test typeof(mean_class_zero) <: Real # Check if mean_class_zero is a scalar


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

Comment on lines 18 to 19
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8;
0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8;
0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2

Comment on lines 21 to 23
mean_class_one, mean_class_zero = sharpness_classification(y_binary, sampled_distributions)
@test mean_class_one ≈ mean(sampled_distributions[1,[2,4,5,7]])
@test mean_class_zero ≈ mean(sampled_distributions[2,[1,3,6,8]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
mean_class_one, mean_class_zero = sharpness_classification(y_binary, sampled_distributions)
@test mean_class_one mean(sampled_distributions[1,[2,4,5,7]])
@test mean_class_zero mean(sampled_distributions[2,[1,3,6,8]])
mean_class_one, mean_class_zero = sharpness_classification(
y_binary, sampled_distributions
)
@test mean_class_one mean(sampled_distributions[1, [2, 4, 5, 7]])
@test mean_class_zero mean(sampled_distributions[2, [1, 3, 6, 8]])

# Test 4: Edge case with all zeros in y_binary
y_binary_all_zeros = [0, 0, 0]
sampled_distributions_all_zeros = [0.1 0.2 0.3; 0.9 0.8 0.7]
mean_class_one_all_zeros, mean_class_zero_all_zeros = sharpness_classification(y_binary_all_zeros, sampled_distributions_all_zeros)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
mean_class_one_all_zeros, mean_class_zero_all_zeros = sharpness_classification(y_binary_all_zeros, sampled_distributions_all_zeros)
mean_class_one_all_zeros, mean_class_zero_all_zeros = sharpness_classification(
y_binary_all_zeros, sampled_distributions_all_zeros
)

Comment on lines 38 to 39


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

Comment on lines 42 to 43


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

@test typeof(sharpness) <: Real # Check if the output is a scalar

# Test 2: Check the function with a known input
sampled_distributions = [[0.1, 0.2, 0.3, 0.7, 0.6], [0.2, 0.3, 0.4, 0.3 , 0.5 ], [0.3, 0.4, 0.5, 0.9, 0.2]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
sampled_distributions = [[0.1, 0.2, 0.3, 0.7, 0.6], [0.2, 0.3, 0.4, 0.3 , 0.5 ], [0.3, 0.4, 0.5, 0.9, 0.2]]
sampled_distributions = [
[0.1, 0.2, 0.3, 0.7, 0.6], [0.2, 0.3, 0.4, 0.3, 0.5], [0.3, 0.4, 0.5, 0.9, 0.2]
]

Comment on lines 62 to 64


end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
end
end

@pasq-cat
Copy link
Contributor Author

pasq-cat commented Jul 2, 2024

is it possible to stop this checks until i am ready? every commit github automatically runs checks .....


@testset "sharpness_classification tests" begin


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

# Test 1: Check that the function runs without errors and returns two scalars for a simple case
y_binary = [1, 0, 1, 0, 1]
sampled_distributions = [0.9 0.1 0.8 0.2 0.7; 0.1 0.9 0.2 0.8 0.3] # Sampled probabilities
mean_class_one, mean_class_zero = sharpness_classification(y_binary, sampled_distributions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
mean_class_one, mean_class_zero = sharpness_classification(y_binary, sampled_distributions)
mean_class_one, mean_class_zero = sharpness_classification(
y_binary, sampled_distributions
)

@test typeof(mean_class_one) <: Real # Check if mean_class_one is a scalar
@test typeof(mean_class_zero) <: Real # Check if mean_class_zero is a scalar


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

Comment on lines 19 to 20
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8;
0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8;
0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2

Comment on lines 22 to 24
mean_class_one, mean_class_zero = sharpness_classification(y_binary, sampled_distributions)
@test mean_class_one ≈ mean(sampled_distributions[1,[2,4,5,7]])
@test mean_class_zero ≈ mean(sampled_distributions[2,[1,3,6,8]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
mean_class_one, mean_class_zero = sharpness_classification(y_binary, sampled_distributions)
@test mean_class_one mean(sampled_distributions[1,[2,4,5,7]])
@test mean_class_zero mean(sampled_distributions[2,[1,3,6,8]])
mean_class_one, mean_class_zero = sharpness_classification(
y_binary, sampled_distributions
)
@test mean_class_one mean(sampled_distributions[1, [2, 4, 5, 7]])
@test mean_class_zero mean(sampled_distributions[2, [1, 3, 6, 8]])

Comment on lines 87 to 88


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

y_binary = rand(0:1, 10)
sampled_distributions = rand(Normal(0.5, 0.1), 10, 6)
n_bins = 4
num_p_per_interval, emp_avg, bin_centers = empirical_frequency_binary_classification(y_binary, sampled_distributions, n_bins=n_bins)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
num_p_per_interval, emp_avg, bin_centers = empirical_frequency_binary_classification(y_binary, sampled_distributions, n_bins=n_bins)
num_p_per_interval, emp_avg, bin_centers = empirical_frequency_binary_classification(
y_binary, sampled_distributions; n_bins=n_bins
)

Comment on lines 104 to 105


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

Comment on lines 107 to 110
Y_cal = [0, 1, 0, 1.2, 4]
sampled_distributions = rand(Normal(0.5, 0.1), 5, 6)
@test_throws ArgumentError empirical_frequency_binary_classification(Y_cal, sampled_distributions, n_bins=10)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
Y_cal = [0, 1, 0, 1.2, 4]
sampled_distributions = rand(Normal(0.5, 0.1), 5, 6)
@test_throws ArgumentError empirical_frequency_binary_classification(Y_cal, sampled_distributions, n_bins=10)
Y_cal = [0, 1, 0, 1.2, 4]
sampled_distributions = rand(Normal(0.5, 0.1), 5, 6)
@test_throws ArgumentError empirical_frequency_binary_classification(
Y_cal, sampled_distributions, n_bins=10
)

Comment on lines 114 to 116
sampled_distributions = rand(Normal(0.5, 0.1), 5, 6)
@test_throws ArgumentError empirical_frequency_binary_classification(Y_cal, sampled_distributions, n_bins=0)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
sampled_distributions = rand(Normal(0.5, 0.1), 5, 6)
@test_throws ArgumentError empirical_frequency_binary_classification(Y_cal, sampled_distributions, n_bins=0)
end
sampled_distributions = rand(Normal(0.5, 0.1), 5, 6)
@test_throws ArgumentError empirical_frequency_binary_classification(
Y_cal, sampled_distributions, n_bins=0
)
end

- `fμ::AbstractArray`: Mean of the predictive distribution if link function is set to `:plugin`, otherwise the probit approximation. The output shape is column-major as in Flux.
- `fvar::AbstractArray`: If regression, it also returns the variance of the predictive distribution. The output shape is column-major as in Flux.
For classification tasks, LaplaceRedux provides different options:
-`normal_distr::Distributions.Normal`:the array of Normal distributions computed by glm_predictive_distribution If the `link_approx` is set to :distribution
Copy link
Contributor Author

@pasq-cat pasq-cat Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pat-alt i am confused on how to proceed here. the issue is that the output of the chain may have already passed through a softmax layer, so the output should not be converted again. should we add a check for this or leave it to the educated reader lol?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I think we should probably add a check somewhere in the corestruct that contains the Flux chain. Let's open a separate issue for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either that or modify the predict function so that it checks that the la.model has a finaliser layer

@JuliaTrustworthyAI JuliaTrustworthyAI deleted a comment from pat-alt Jul 3, 2024
src/utils.jl Outdated
@doc raw"""
empirical_frequency_regression(Y_cal, sampled_distributions, n_bins=20)

FOR REGRESSION MODELS. \
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pat-alt maybe i can move this functions to a dedicated julia file ( calibration_functions.jl) , so that in the future i may add something else in a compartmentalized file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, good idea

Comment on lines 83 to 85
sampled_distributions = [rand(Distributions.Normal(1, 1.0),6) for _ in 1:5]
@test_throws ArgumentError empirical_frequency_regression(Y_cal, sampled_distributions, n_bins=0)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
sampled_distributions = [rand(Distributions.Normal(1, 1.0),6) for _ in 1:5]
@test_throws ArgumentError empirical_frequency_regression(Y_cal, sampled_distributions, n_bins=0)
sampled_distributions = [rand(Distributions.Normal(1, 1.0), 6) for _ in 1:5]
@test_throws ArgumentError empirical_frequency_regression(
Y_cal, sampled_distributions, n_bins=0
)

Comment on lines 88 to 89


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

y_binary = rand(0:1, 10)
sampled_distributions = rand(Normal(0.5, 0.1), 10, 6)
n_bins = 4
num_p_per_interval, emp_avg, bin_centers = empirical_frequency_binary_classification(y_binary, sampled_distributions, n_bins=n_bins)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
num_p_per_interval, emp_avg, bin_centers = empirical_frequency_binary_classification(y_binary, sampled_distributions, n_bins=n_bins)
num_p_per_interval, emp_avg, bin_centers = empirical_frequency_binary_classification(
y_binary, sampled_distributions; n_bins=n_bins
)

Comment on lines 105 to 106


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

Comment on lines 108 to 111
Y_cal = [0, 1, 0, 1.2, 4]
sampled_distributions = rand(Normal(0.5, 0.1), 5, 6)
@test_throws ArgumentError empirical_frequency_binary_classification(Y_cal, sampled_distributions, n_bins=10)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
Y_cal = [0, 1, 0, 1.2, 4]
sampled_distributions = rand(Normal(0.5, 0.1), 5, 6)
@test_throws ArgumentError empirical_frequency_binary_classification(Y_cal, sampled_distributions, n_bins=10)
Y_cal = [0, 1, 0, 1.2, 4]
sampled_distributions = rand(Normal(0.5, 0.1), 5, 6)
@test_throws ArgumentError empirical_frequency_binary_classification(
Y_cal, sampled_distributions, n_bins=10
)

Comment on lines 115 to 117
sampled_distributions = rand(Normal(0.5, 0.1), 5, 6)
@test_throws ArgumentError empirical_frequency_binary_classification(Y_cal, sampled_distributions, n_bins=0)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
sampled_distributions = rand(Normal(0.5, 0.1), 5, 6)
@test_throws ArgumentError empirical_frequency_binary_classification(Y_cal, sampled_distributions, n_bins=0)
end
sampled_distributions = rand(Normal(0.5, 0.1), 5, 6)
@test_throws ArgumentError empirical_frequency_binary_classification(
Y_cal, sampled_distributions, n_bins=0
)
end

Comment on lines 74 to 77
sampled_distributions = [rand(Distributions.Normal(1, 1.0),6) for _ in 1:5]
counts = empirical_frequency_regression(Y_cal, sampled_distributions, n_bins=n_bins)
@test typeof(counts) == Array{Float64, 1} # Check if the output is an array of Float64
@test length(counts) == n_bins + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
sampled_distributions = [rand(Distributions.Normal(1, 1.0),6) for _ in 1:5]
counts = empirical_frequency_regression(Y_cal, sampled_distributions, n_bins=n_bins)
@test typeof(counts) == Array{Float64, 1} # Check if the output is an array of Float64
@test length(counts) == n_bins + 1
sampled_distributions = [rand(Distributions.Normal(1, 1.0), 6) for _ in 1:5]
counts = empirical_frequency_regression(Y_cal, sampled_distributions; n_bins=n_bins)
@test typeof(counts) == Array{Float64,1} # Check if the output is an array of Float64
@test length(counts) == n_bins + 1

Comment on lines 84 to 86
sampled_distributions = [rand(Distributions.Normal(1, 1.0),6) for _ in 1:5]
@test_throws ArgumentError empirical_frequency_regression(Y_cal, sampled_distributions, n_bins=0)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
sampled_distributions = [rand(Distributions.Normal(1, 1.0),6) for _ in 1:5]
@test_throws ArgumentError empirical_frequency_regression(Y_cal, sampled_distributions, n_bins=0)
sampled_distributions = [rand(Distributions.Normal(1, 1.0), 6) for _ in 1:5]
@test_throws ArgumentError empirical_frequency_regression(
Y_cal, sampled_distributions, n_bins=0
)

Comment on lines 89 to 90


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

@testset "empirical_frequency_binary_classification tests" begin
# Test 1: Check that the function runs without errors and returns an array for a simple case
y_binary = rand(0:1, 10)
sampled_distributions = rand(2,10)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
sampled_distributions = rand(2,10)
sampled_distributions = rand(2, 10)

y_binary = rand(0:1, 10)
sampled_distributions = rand(2,10)
n_bins = 4
num_p_per_interval, emp_avg, bin_centers = empirical_frequency_binary_classification(y_binary, sampled_distributions, n_bins=n_bins)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
num_p_per_interval, emp_avg, bin_centers = empirical_frequency_binary_classification(y_binary, sampled_distributions, n_bins=n_bins)
num_p_per_interval, emp_avg, bin_centers = empirical_frequency_binary_classification(
y_binary, sampled_distributions; n_bins=n_bins
)

Comment on lines 106 to 107


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

Comment on lines 109 to 112
Y_cal = [0, 1, 0, 1.2, 4]
sampled_distributions = rand(2,5)
@test_throws ArgumentError empirical_frequency_binary_classification(Y_cal, sampled_distributions, n_bins=10)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
Y_cal = [0, 1, 0, 1.2, 4]
sampled_distributions = rand(2,5)
@test_throws ArgumentError empirical_frequency_binary_classification(Y_cal, sampled_distributions, n_bins=10)
Y_cal = [0, 1, 0, 1.2, 4]
sampled_distributions = rand(2, 5)
@test_throws ArgumentError empirical_frequency_binary_classification(
Y_cal, sampled_distributions, n_bins=10
)

Comment on lines 116 to 118
sampled_distributions = rand(2,5)
@test_throws ArgumentError empirical_frequency_binary_classification(Y_cal, sampled_distributions, n_bins=0)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
sampled_distributions = rand(2,5)
@test_throws ArgumentError empirical_frequency_binary_classification(Y_cal, sampled_distributions, n_bins=0)
end
sampled_distributions = rand(2, 5)
@test_throws ArgumentError empirical_frequency_binary_classification(
Y_cal, sampled_distributions, n_bins=0
)
end

@pat-alt
Copy link
Member

pat-alt commented Jul 3, 2024

is it possible to stop this checks until i am ready? every commit github automatically runs checks .....

You can remove this part here (I've already done this in #39). For now, please just run the formatter before you push, it takes 2s :)

@pasq-cat
Copy link
Contributor Author

pasq-cat commented Jul 3, 2024

is it possible to stop this checks until i am ready? every commit github automatically runs checks .....

You can remove this part here (I've already done this in #39). For now, please just run the formatter before you push, it takes 2s :)

I know, I just forget to do it. I wish it was automatically applied before synchronization

@JuliaTrustworthyAI JuliaTrustworthyAI deleted a comment from github-actions bot Jul 4, 2024
@pasq-cat pasq-cat closed this Jul 4, 2024
@pasq-cat pasq-cat deleted the 90-average-calibration-in-utilsjl branch July 6, 2024 02:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a function to compute the average calibration in utils.jl
2 participants