Skip to content

Commit

Permalink
Add UniformScaling as a covariance type
Browse files Browse the repository at this point in the history
  • Loading branch information
tsj5 committed Jan 28, 2022
1 parent 3d67c96 commit 72ed703
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/Emulator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct Emulator{FT<:AbstractFloat}
"square root of the inverse of the input covariance matrix; input_dim × input_dim"
normalize_inputs::Bool
"whether to fit models on normalized outputs outputs / standardize_outputs_factor"
sqrt_inv_input_cov::Union{AbstractMatrix{FT}, Nothing}
sqrt_inv_input_cov::Union{AbstractMatrix{FT}, UniformScaling{FT}, Nothing}
" if normalizing: whether to fit models on normalized inputs ((inputs - input_mean) * sqrt_inv_input_cov)"
standardize_outputs::Bool
"if standardizing: Standardization factors (characteristic values of the problem)"
Expand All @@ -64,7 +64,7 @@ end
function Emulator(
machine_learning_tool::MachineLearningTool,
input_output_pairs::PairedDataContainer{FT};
obs_noise_cov::Union{AbstractMatrix{FT}, Nothing} = nothing,
obs_noise_cov::Union{AbstractMatrix{FT}, UniformScaling{FT}, Nothing} = nothing,
normalize_inputs::Bool = true,
standardize_outputs::Bool = false,
standardize_outputs_factors::Union{AbstractVector{FT}, Nothing} = nothing,
Expand Down Expand Up @@ -223,7 +223,7 @@ normalize with the empirical Gaussian distribution of points
function normalize(
inputs::AbstractVecOrMat{FT},
input_mean::AbstractVector{FT},
sqrt_inv_input_cov::AbstractMatrix{FT}
sqrt_inv_input_cov::Union{AbstractMatrix{FT}, UniformScaling{FT}}
) where {FT<:AbstractFloat}
training_inputs = sqrt_inv_input_cov * (inputs .- input_mean)
return training_inputs
Expand All @@ -236,7 +236,7 @@ standardize with a vector of factors (size equal to output dimension)
"""
function standardize(
outputs::AbstractVecOrMat{FT},
output_covs::Vector{<:AbstractMatrix{FT}},
output_covs::Vector{<:Union{AbstractMatrix{FT}, UniformScaling{FT}}},
factors::AbstractVector{FT}
) where {FT<:AbstractFloat}
# Case where `output_covs` is a Vector of covariance matrices
Expand All @@ -250,7 +250,7 @@ end

function standardize(
outputs::AbstractVecOrMat{FT},
output_cov::AbstractMatrix{FT},
output_cov::Union{AbstractMatrix{FT}, UniformScaling{FT}},
factors::AbstractVector{FT}
) where {FT<:AbstractFloat}
# Case where `output_cov` is a single covariance matrix
Expand All @@ -268,7 +268,7 @@ dimension). `output_cov` is a Vector of covariance matrices, such as is returned
function reverse_standardize(
emulator::Emulator{FT},
outputs::AbstractVecOrMat{FT},
output_covs::Union{AbstractMatrix{FT}, Vector{<:AbstractMatrix{FT}}}
output_covs::Union{AbstractMatrix{FT}, UniformScaling{FT}, Vector{<:AbstractMatrix{FT}}}
) where {FT<:AbstractFloat}
if emulator.standardize_outputs
return standardize(outputs, output_covs, 1.0 ./ emulator.standardize_outputs_factors)
Expand Down Expand Up @@ -297,7 +297,7 @@ in S are sorted in descending order.
"""
function svd_transform(
data::AbstractMatrix{FT},
obs_noise_cov::Union{AbstractMatrix{FT}, Nothing};
obs_noise_cov::Union{AbstractMatrix{FT}, UniformScaling{FT}, Nothing};
truncate_svd::FT=1.0
) where {FT<:AbstractFloat}
if obs_noise_cov === nothing
Expand Down Expand Up @@ -327,7 +327,7 @@ end

function svd_transform(
data::AbstractVector{FT},
obs_noise_cov::Union{AbstractMatrix{FT}, Nothing};
obs_noise_cov::Union{AbstractMatrix{FT}, UniformScaling{FT}, Nothing};
truncate_svd::FT = 1.0
) where {FT<:AbstractFloat}
# method for 1D data
Expand Down
8 changes: 4 additions & 4 deletions src/MarkovChainMonteCarlo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mutable struct MCMC{FT<:AbstractFloat, IT<:Int}
"a single sample from the observations. Can e.g. be picked from an Obs struct using get_obs_sample"
obs_sample::AbstractVector{FT}
"covariance of the observational noise"
obs_noise_cov::AbstractMatrix{FT}
obs_noise_cov::Union{AbstractMatrix{FT}, UniformScaling{FT}}
"array of length N_parameters with the parameters' prior distributions"
prior::ParameterDistribution
"MCMC step size"
Expand Down Expand Up @@ -68,7 +68,7 @@ where max_iter is the number of MCMC steps to perform (e.g., 100_000)
"""
function MCMC(
obs_sample::AbstractVector{FT},
obs_noise_cov::AbstractMatrix{FT},
obs_noise_cov::Union{AbstractMatrix{FT}, UniformScaling{FT}},
prior::ParameterDistribution,
step::FT,
param_init::AbstractVector{FT},
Expand Down Expand Up @@ -154,7 +154,7 @@ end
function mcmc_sample!(
mcmc::MCMC{FT,IT},
g::AbstractVector{FT},
gcov::AbstractMatrix{FT}
gcov::Union{AbstractMatrix{FT}, UniformScaling{FT}}
) where {FT<:AbstractFloat, IT<:Int}
if mcmc.algtype == "rwm"
log_posterior = log_likelihood(mcmc, g, gcov) + log_prior(mcmc)
Expand Down Expand Up @@ -196,7 +196,7 @@ end
function log_likelihood(
mcmc::MCMC{FT,IT},
g::AbstractVector{FT},
gcov::AbstractMatrix{FT}
gcov::Union{AbstractMatrix{FT}, UniformScaling{FT}}
) where {FT<:AbstractFloat, IT<:Int}
log_rho = 0.0
#if gcov == nothing
Expand Down

0 comments on commit 72ed703

Please sign in to comment.