Skip to content

Commit

Permalink
Merge pull request #18 from fipelle/dev
Browse files Browse the repository at this point in the history
Implemented `compute_scaling_factor` in validation.jl
  • Loading branch information
fipelle authored Nov 18, 2022
2 parents 120096f + eda89bf commit 170ab71
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
20 changes: 20 additions & 0 deletions src/models/dfm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,26 @@ Model validation
--------------------------------------------------------------------------------------------------------------------------------
=#

"""
compute_scaling_factors(estim::DFMSettings)
Compute scaling factors for each series in `estim.Y`.
"""
function compute_scaling_factors(estim::DFMSettings)

# Initialise `scaling_factors`
scaling_factors = zeros(estim.n, 1); # the final `, 1` is needed to run rescale_estim_params!(...) since it expects a matrix of floats

# Compute scaling factors
for i in axes(estim.Y, 1)
coordinates_trends = findall(view(estim.trends_skeleton, i, :) .!= 0);
max_order = maximum(view(estim.drifts_selection, coordinates_trends)); # either 1 (smooth trend) or 0 (random walk)
scaling_factors[i] = compute_scaling_factor(estim.Y[i, :], max_order==0);
end

return scaling_factors;
end

"""
rescale_estim_params!(coordinates_params_rescaling::VectorsArray{Int64}, estim::DFMSettings, std_presample::FloatMatrix)
Expand Down
22 changes: 13 additions & 9 deletions src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,31 @@ function fc_err(validation_settings::ValidationSettings, p::Int64, λ::Number,
data = validation_settings.Y;
end

# Standardise data
data_presample = @view data[:, 1:t0];

# Stop if the estimation sample is entirely missing
if count(ismissing, data_presample) == validation_settings.n*t0
if count(ismissing, view(data, :, 1:t0)) == validation_settings.n*t0
return [0.0, 1.0];

# Standard run
# TBD: Generalise further the standardisation process for future models whose DataTypes arguments are transformations of the data
else
estim = validation_settings.model_struct(data[:, 1:t0], validation_settings.model_args, validation_settings.model_kwargs, p, λ, α, β);

# Standardise stationary data
if validation_settings.is_stationary
mean_presample = mean_skipmissing(data_presample);
std_presample = std_skipmissing(data_presample);
mean_presample = mean_skipmissing(estim.Y);
std_presample = std_skipmissing(estim.Y);
Y = (data .- mean_presample) ./ std_presample;
estim.Y .-= mean_presample; # first step of the estimation sample standardisation

# Standardise non-stationary data
else
std_presample = std_skipmissing(diff(data_presample, dims=2));
std_presample = compute_scaling_factors(estim);
Y = data ./ std_presample;
end

estim = validation_settings.model_struct(Y[:, 1:t0], validation_settings.model_args, validation_settings.model_kwargs, p, λ, α, β);
estim.Y ./= std_presample; # final step of the data estimation sample standardisation
rescale_estim_params!(validation_settings.coordinates_params_rescaling, estim, std_presample);

# Estimate model
sspace = ecm(estim, output_sspace_data=Y);

Expand Down

0 comments on commit 170ab71

Please sign in to comment.