├── .gitignore ├── LICENSE ├── README.md ├── algebra ├── cellsum.m ├── colsum.m ├── eminus.m ├── meanmv.m ├── rmse.m ├── rmsew.m ├── rowsum.m ├── stdmv.m ├── sumall.m ├── sumcomb.m ├── sumfunc.m └── wsum.m ├── datasets ├── hadsst2 │ └── hadsst2_parsedata.m ├── hadsst3 │ ├── hadsst3_experiment_gpfa_run.m │ ├── hadsst3_experiment_gpkron_run.m │ ├── hadsst3_experiment_vbpca_run.m │ └── hadsst3_load_data.m ├── metoffice │ ├── get_valset.m │ ├── metoffice_add_climatology.m │ ├── metoffice_add_land.m │ ├── metoffice_compute_rmse.m │ ├── metoffice_experiment_comparison.m │ ├── metoffice_experiment_gpfa.m │ ├── metoffice_experiment_gpfa1.m │ ├── metoffice_experiment_gpfa1_old.m │ ├── metoffice_experiment_gpfa2.m │ ├── metoffice_experiment_gpfa_gpkron.m │ ├── metoffice_experiment_gpfa_gpkron2.m │ ├── metoffice_experiment_gpkron.m │ ├── metoffice_experiment_gpkron2.m │ ├── metoffice_experiment_gpkron_old.m │ ├── metoffice_experiment_pca.m │ ├── metoffice_get_data.m │ ├── metoffice_get_mask.m │ ├── metoffice_get_path.m │ ├── metoffice_get_testdata.m │ ├── metoffice_get_testpath.m │ ├── metoffice_get_valset.m │ ├── metoffice_getdata.m │ ├── metoffice_is_land_index.m │ ├── metoffice_plot_fa.m │ ├── metoffice_remove_bins.m │ ├── metoffice_remove_land.m │ └── metoffice_remove_mask.m ├── mohsst5 │ ├── mohsst5_add_land.m │ ├── mohsst5_compare_ships2satellite.m │ ├── mohsst5_experiment.m │ ├── mohsst5_experiment_gpfa.m │ ├── mohsst5_experiment_gpkron.m │ ├── mohsst5_experiment_pca.m │ ├── mohsst5_experiment_periodic.m │ ├── mohsst5_experiment_vbinit.m │ ├── mohsst5_experiment_vbtdsepinit.m │ ├── mohsst5_explained_variance_fa.m │ ├── mohsst5_is_land_index.m │ ├── mohsst5_land_indeces.m │ ├── mohsst5_loaddata.m │ ├── mohsst5_mapplot.m │ ├── mohsst5_paperplots.m │ ├── mohsst5_parsedata.m │ ├── mohsst5_pca.m │ ├── mohsst5_performance.m │ ├── mohsst5_performance_rmsew.m │ ├── mohsst5_plotexperiment.m │ ├── mohsst5_points_to_grid_index.m │ ├── mohsst5_posterplots.m │ ├── mohsst5_reconstruction_movie.m │ ├── mohsst5_remove_land.m │ ├── mohsst5_rmse_sparsepart.m │ ├── mohsst5_rmsew.m │ ├── mohsst5_rotate_to_pca.m │ ├── mohsst5_subset.m │ ├── mohsst5_thesisplots.m │ ├── mohsst5_weights.m │ └── script_matlab.m └── testbed │ ├── tbload_temperature.m │ ├── tbpreprocess.m │ ├── tbremove_stations.m │ ├── tbsubset.m │ ├── test_tbgppca_subset.m │ ├── testbed_coast.m │ ├── testbed_data_coast.mat │ ├── testbed_experiment_vbrfa.m │ ├── testbed_grid.m │ ├── testbed_loaddata.m │ ├── testbed_loadings.m │ ├── testbed_plot_data.m │ ├── testbed_plot_experiment.m │ ├── testbed_plot_finland.m │ ├── testbed_plot_stations.m │ ├── testbed_plot_topography.m │ ├── testbed_preprocess.m │ ├── testbed_projection.m │ ├── testbed_reconstructions.m │ ├── testbed_remove_stations.m │ ├── testbed_states.m │ ├── testbed_stationplot.m │ └── testbed_stationtext.m ├── deprecated ├── cols.m ├── dss.m ├── gam_vbcost.m ├── gament.m ├── gamentropy.m ├── gamkl.m ├── kalman_filter.m ├── kalman_filter_old.m ├── mnorm_entropy.m ├── mnorm_vbcost.m ├── mnormrnd.m ├── mvn_vbcost.m ├── mvnentropy.m ├── mymvnrnd.m ├── rows.m ├── solve_tril.m ├── solve_triu.m ├── spinv_jaakko.m ├── split_data.m ├── statchol.m ├── studpdf.m ├── t2_dlpdf.m ├── t2_lpdf.m ├── test_argparse.m ├── test_dist_earth.m ├── test_dist_earth_gradient.m ├── test_nested_functions.m ├── test_sample_kron.m ├── test_sample_kron2.m ├── test_sample_kron2d.m ├── test_solve_kron.m ├── test_sparse_chol.m └── test_t_ml.m ├── discrete ├── ngroupsk.m ├── npairsk.m ├── ntuples.m └── tuples.m ├── distributions ├── cov2corr.m ├── gamma_dlogpdf.m ├── gamma_entropy.m ├── gamma_logpdf.m ├── gaussian_dlogpdf.m ├── gaussian_dlogpdf_cov.m ├── gaussian_entropy.m ├── gaussian_logpdf.m ├── gaussian_rand.m ├── gaussian_rand_conjgradmv.m ├── gaussian_rand_kron.m ├── gaussian_rand_pcg.m ├── gaussian_rand_pcg_kron.m ├── l1_rand.m ├── laplace_logpdf.m ├── laplace_rand.m ├── lognormal_logpdf.m ├── lognormal_rand.m ├── mstud_lpdf.m ├── normal_logpdf.m ├── t_logpdf.m ├── t_lpdf.m └── t_ml.m ├── fa ├── demo_fa.m ├── demo_rfa.m ├── factor_module_ard.m ├── factor_module_ard_old.m ├── factor_module_iid.m ├── factor_module_iid_old.m ├── factor_module_lds.m ├── noise_module_diagonal.m ├── noise_module_fixed.m ├── noise_module_independent_t.m ├── noise_module_isotropic.m ├── noise_module_laplace.m ├── noise_module_multivariate_t.m ├── noise_module_product.m ├── old_factor_module_ard.m ├── old_factor_module_iid.m ├── old_noise_module_diagonal.m ├── old_noise_module_fixed.m ├── old_noise_module_independent_t.m ├── old_noise_module_isotropic.m ├── old_noise_module_laplace.m ├── old_noise_module_multivariate_t.m ├── old_noise_module_product.m ├── old_test_vbpca.m ├── old_vbfa.m ├── test_comparison_to_scipy.m ├── test_lds.m ├── test_rfa.m ├── test_vbpca.m ├── test_vbpca_old.m ├── vbfa.m ├── vbfa_old.m └── vbfa_probtest.m ├── geometry ├── dist.m ├── dist_earth.m ├── dist_haversine.m ├── geographic_to_euclidean.m ├── interpolate.m ├── lonlat2standard.m ├── points_on_sphere.m ├── sq_dist.c └── sq_dist.m ├── gp ├── gp_cov.m ├── gp_cov_cos.m ├── gp_cov_dampedcos.m ├── gp_cov_delta.m ├── gp_cov_jitter.m ├── gp_cov_kron.m ├── gp_cov_periodic.m ├── gp_cov_pp.m ├── gp_cov_pp_backup.m ├── gp_cov_pp_init.m ├── gp_cov_pp_proto.m ├── gp_cov_product.m ├── gp_cov_pseudo.m ├── gp_cov_remove.m ├── gp_cov_rq.m ├── gp_cov_scale.m ├── gp_cov_se.m ├── gp_cov_se_backup.m ├── gp_cov_select.m ├── gp_cov_select_pseudo.m ├── gp_cov_sum.m ├── gp_cov_toeplitz.m ├── gp_cov_toeplitz_block.m ├── gp_cov_wrap.m ├── gp_dlogpdf.m ├── gp_get_randfunc.m ├── gp_init_kron.m ├── gp_init_kron1.m ├── gp_init_kron2.m ├── gp_init_kron3.m ├── gp_init_kron4.m ├── gp_init_kron5.m ├── gp_init_kron6.m ├── gp_init_kron7.m ├── gp_initialize.m ├── gp_kron.m ├── gp_learn.m ├── gp_learn_pseudo.m ├── gp_loglikelihood.m ├── gp_loglikelihood_pseudo.m ├── gp_logpdf.m ├── gp_mcmc.m ├── gp_predict.m ├── gp_predict_pseudo.m ├── test_gp.m ├── test_gp_geographic.m ├── test_gp_get_randfunc.m ├── test_gp_get_randfunc_backup.m ├── test_gp_kron.m ├── test_gp_kron2.m ├── test_gp_kron_backup.m ├── test_gp_kron_eigs.m ├── test_gp_kron_iteration.m ├── test_gp_pseudo.m ├── test_localglobal_gp.m └── test_preconditioner.m ├── gppca ├── argschk.m ├── chol_comparison.m ├── cholsolve.m ├── demo_gpfa.m ├── factor_module_gp.m ├── factor_module_gp_factorized.m ├── gamvbcost.m ├── gpcov.m ├── gpcovConstScale.m ├── gpcovDot.m ├── gpcovNoise.m ├── gpcovPP.m ├── gpcovPeriodic.m ├── gpcovProduct.m ├── gpcovScale.m ├── gpcovSum.m ├── gpcov_decper.m ├── gpcov_old.m ├── gpcov_ratquad.m ├── gpcov_sqexp.m ├── gpdist.m ├── gpeval.m ├── gpfa.m ├── gplearn.m ├── gplearn_backup.m ├── gpmapcolor.m ├── gppca_demo.m ├── gppca_demo_ijcnn.m ├── gppca_demo_nips.m ├── gppca_demo_poster.m ├── gppca_demo_thesis.m ├── gppca_loglikelihood.m ├── gppred.m ├── gprnd.m ├── gprotate2pca.m ├── index_selected.m ├── logdet.m ├── logdetchol.m ├── logdettri.m ├── mvnvbcost.m ├── myldl.m ├── regularize.m ├── safechol.m ├── solvegpmvn.m ├── spgpr.m ├── spgpr2.m ├── sqdistEuclidean.m ├── test_gpfa.m ├── test_gradient_Kxx.m ├── test_spgpr.m ├── test_vbgppcamv.m ├── test_vbgppcamv_cs.m ├── test_vbgppcamv_earth.m ├── tmpsin.m ├── tsgpplot.m ├── vbgppcamv.m ├── vbgppcamv_backup20090326.m └── vbgppcamv_full.m ├── gppde ├── gppde.m ├── gppde_new.m ├── test_gppde_1d.m ├── test_gppde_2d.m └── test_gppde_3d.m ├── matrix_computations ├── adddiag.m ├── diagprod.m ├── finddiag.m ├── get_linsolve_cov.m ├── inv_chol.m ├── kronprod.m ├── lchol_grad.m ├── lchol_grad_old.m ├── lchol_grad_sparse.c ├── ldlchol2lchol.m ├── linsolve_chol.m ├── linsolve_cov.m ├── linsolve_kron.m ├── linsolve_kron_ldlchol.m ├── linsolve_lchol.m ├── linsolve_ldlchol.m ├── linsolve_neumann.m ├── linsolve_tril.m ├── linsolve_triu.m ├── logdet.m ├── logdet_chol.m ├── logdet_cov.m ├── logdet_diag.m ├── logdet_kron_ldlchol.m ├── logdet_ldlchol.m ├── logdet_tri.m ├── mdiag.m ├── mtraceprod.m ├── setdiag.m ├── sparsity.m ├── spdiag.m ├── spinv.m ├── spinv_ldlchol.m ├── sptoeplitz.m ├── sqrt_cov.m ├── takagi.m ├── test_lchol_grad.m ├── toeplitz_block.m ├── traceinv_chol.m ├── traceinv_qr.m ├── traceprod.m ├── traceprodm.m └── vec.m ├── mcmc ├── mcmc_ess_acorr.m ├── mcmc_init_hamiltonian.m ├── mcmc_init_metropolishastings.m ├── mcmc_init_metropolishastings_backup.m ├── mcmc_init_reflective.m ├── mcmc_init_slicesampling.m ├── mcmc_init_slicesampling_backup.m └── test_mcmc.m ├── optimization ├── checkgrad.m ├── conjgrad.m ├── conjgrad_kron.m ├── conjgradmv.m ├── minimize.m ├── minimize_binary.m ├── mycheckgrad.m └── test_conjgradmv.m ├── pca ├── alex2jaakko.m ├── covarray_to_covcell.m ├── covcell_to_covarray.m ├── demo_rvbpcamv.m ├── demo_vbrfa.m ├── lcm.m ├── rotate_to_pca.m ├── rotationpaper_illustration.m ├── rppca.m ├── rppcamv.m ├── rppcamv_loglikelihood.m ├── rvbpca_loglikelihood.m ├── rvbpcamv.m ├── test_rotationcost_gaussian.m ├── test_rvbpcamv.m ├── test_vbrfa.m ├── vb_rotationcost_gamma.m ├── vb_rotationcost_gaussian.m ├── vbpca_rotate.m ├── vbpcamv.m ├── vbrfa.m └── vbrfa_backup20100706.m ├── plotting ├── addtsplot.m ├── clim_centered.m ├── clim_positive.m ├── climcm_2.mat ├── colorbar_position.m ├── colorbars.m ├── colormap_centered.m ├── colormap_redblue.m ├── colormap_scale.m ├── dateticks.m ├── decades_equal.m ├── draw_gaussian.m ├── error_ellipse.m ├── errorplot.m ├── gpplot.m ├── laprint.m ├── m_scatter.m ├── map_coast.m ├── map_colormap.m ├── map_grid.m ├── map_interpolate.m ├── map_pcolor.m ├── map_plot.m ├── map_projection.m ├── map_text.m ├── map_topography.m ├── mapproj.m ├── plot_acorr.m ├── plot_scatterhist.m ├── print2latex.m ├── set_colorbar_position.m ├── set_figure_fontsize.m ├── set_figure_size.m ├── set_label_fontsize.m ├── set_legend_fontsize.m ├── set_plot_style.m ├── set_subplot_positions.m ├── set_ticks_fontsize.m ├── tserrorplot.m ├── tsplot.m ├── xlabels.m ├── ylim_centered.m └── ylim_scale.m ├── publications ├── aistats2012 │ ├── aistats2012_comparison.m │ ├── aistats2012_comparison_plot.m │ ├── aistats2012_experiment_mohsst5.m │ ├── aistats2012_mohsst5_results.m │ ├── aistats2012_mohsst5_rmse.m │ ├── aistats2012_mohsst5_sets.m │ ├── aistats2012_plot_slides.m │ ├── aistats2012_run_comparison.m │ └── aistats2012_slides_gp.m ├── icml2011 │ └── icml2011_mohsst5_parse_results.m ├── luosto2010 │ └── luosto2010_inference.m ├── neurocomputing2010 │ ├── nc2010_plot_compexperiment.m │ ├── nc2010_plot_mlexperiment.m │ ├── nc2010_plot_mnistexperiment.m │ ├── nc2010_plot_rppca_toyexperiment.m │ ├── nc2010_plot_toyexperiment.m │ ├── nc2010_plot_varianceexperiment.m │ ├── nc2010_publish_compexperiment.m │ ├── nc2010_publish_datasets.m │ ├── nc2010_publish_realexperiment.m │ ├── nc2010_publish_toyexperiment.m │ ├── nc2010_rppca_toyexperiment.m │ ├── nc2010_vbpca_compexperiment.m │ ├── nc2010_vbpca_mlexperiment.m │ ├── nc2010_vbpca_mnistexperiment.m │ ├── nc2010_vbpca_toyexperiment.m │ └── nc2010_vbpca_varianceexperiment.m ├── nips2011 │ ├── nips2011_comparison.m │ ├── nips2011_experiment_mohsst5.m │ ├── nips2011_plot_comparison.m │ ├── nips2011_plot_mohsst5.m │ └── nips2011_run_comparison.m ├── novac2010 │ ├── gppca_demo_novac2010.m │ └── novac2010_mohsst5.m ├── vbrfa2010 │ ├── vbrfa2010_demo.m │ ├── vbrfa2010_testbed_experiment.m │ ├── vbrfa2010_testbed_plot.m │ └── vbrfa2010_vbrfa.m └── vbrfa2011 │ ├── vbrfa2011_artificial_experiment.m │ ├── vbrfa2011_artificial_results.m │ ├── vbrfa2011_demo.m │ ├── vbrfa2011_testbed_data.m │ ├── vbrfa2011_testbed_experiment.m │ ├── vbrfa2011_testbed_measure.m │ └── vbrfa2011_testbed_plot.m ├── signal_processing ├── acorr.m ├── acorr_backup.m ├── kalman_filter.m ├── kalman_filter_step.m ├── moving_average.m ├── periodic.m ├── rts_smoother.m ├── rts_smoother_step.m └── test_kalman_filter.m ├── startup.m └── utilities ├── argparse.m ├── broadcast.m ├── columns_to_cells.m ├── mat2str.m └── struct2list.m /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | .svn 3 | *.mex* 4 | *.backup 5 | *.eps 6 | *.pdf 7 | *.sh 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2006-2013 Jaakko Luttinen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MATLAB Scripts 2 | ============== 3 | 4 | This repository contains a collection of MATLAB scripts that I have implemented for my personal use. 5 | 6 | Contents 7 | -------- 8 | 9 | * *algebra* - miscallaneous simple computations 10 | * *datasets* - dataset specific functions 11 | * *deprecated* - **DEPRECATED** miscallaneous out-dated functions for backward compatibility 12 | * *discrete* - functions for discrete math 13 | * *distributions* - functions for probability distributions 14 | * *fa* - modular blocks for variational Bayesian factor analysis extensions 15 | * *geometry* - functions for geometric tasks 16 | * *gp* - Gaussian process related functions 17 | * *gppca* - Gaussian-process factor analysis functions 18 | * *gppde* - Gaussian processes for partial differential equations 19 | * *matrix_computations* - numeric methods for linear algebra 20 | * *mcmc* - Markov chain Monte Carlo functions 21 | * *optimization* - optimization related functions 22 | * *pca* - **DEPRECATED** principal component analysis variants 23 | * *plotting* - scripts for plotting 24 | * *publications* - publication specific functions 25 | * *signal_processing* - signal processing related functions 26 | * *utilities* - miscallaneous functions 27 | 28 | License 29 | ------- 30 | 31 | Copyright (c) 2009-2012 Jaakko Luttinen jaakko.luttinen@iki.fi 32 | 33 | MIT license 34 | -------------------------------------------------------------------------------- /algebra/cellsum.m: -------------------------------------------------------------------------------- 1 | function c = cellsum(c1, c2) 2 | 3 | c = cell(size(c1)); 4 | for n=1:numel(c1) 5 | c{n} = c1{n} + c2{n}; 6 | end 7 | -------------------------------------------------------------------------------- /algebra/colsum.m: -------------------------------------------------------------------------------- 1 | function y = colsum(x) 2 | 3 | y = sum(x,2); -------------------------------------------------------------------------------- /algebra/eminus.m: -------------------------------------------------------------------------------- 1 | function Y = eminus(A,B) 2 | Y = bsxfun(@minus, A, B); -------------------------------------------------------------------------------- /algebra/meanmv.m: -------------------------------------------------------------------------------- 1 | function mu = meanmv(X, dim); 2 | % handles missing values (NaN). 3 | 4 | error(nargchk(1,2,nargin)); 5 | 6 | if nargin < 2 7 | dim = min(find(size(X)~=1)); 8 | if isempty(dim), dim = 1; end 9 | end 10 | 11 | % NaNs to zero 12 | Imv = isnan(X); 13 | X(Imv) = 0; 14 | 15 | % Sum and divide by the number of non-missing values 16 | div = sum(~Imv, dim); 17 | div(div==0) = 1; 18 | mu = sum(X, dim) ./ div; 19 | %mu(isinf(xmean)) = NaN; 20 | -------------------------------------------------------------------------------- /algebra/rmse.m: -------------------------------------------------------------------------------- 1 | % RMSE - Evaluates root mean squared error. 2 | % 3 | % E = RMSE(Y) 4 | % 5 | % E = RMSE(Y1,Y2) 6 | 7 | % Last modified 2010-10-21 8 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 9 | 10 | function e = rmse(Y, Yh) 11 | 12 | if nargin < 2 13 | e = sqrt(Y(:)'*Y(:)/numel(Y)); 14 | else 15 | z = Y(:)-Yh(:); 16 | e = sqrt(z'*z/numel(z)); 17 | end 18 | -------------------------------------------------------------------------------- /algebra/rmsew.m: -------------------------------------------------------------------------------- 1 | % RMSEW - Evaluates weighted root mean squared error. 2 | % 3 | % E = RMSEW(Y,W) 4 | % 5 | % Y'*DIAG(W)*Y / SUM(W) 6 | 7 | % Last modified 2011-01-11 8 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 9 | 10 | function e = rmsew(Y, W) 11 | 12 | if nargin < 2 13 | W = ones(size(Y)); 14 | end 15 | 16 | e = sqrt( ((W(:).*Y(:))'*Y(:)) / sum(W(:)) ); 17 | -------------------------------------------------------------------------------- /algebra/rowsum.m: -------------------------------------------------------------------------------- 1 | function y = rowsum(x) 2 | % y = sum(x,1); 3 | 4 | y = sum(x,1); -------------------------------------------------------------------------------- /algebra/stdmv.m: -------------------------------------------------------------------------------- 1 | function s = stdmv(X, dim); 2 | % handles missing values (NaN). 3 | 4 | error(nargchk(1,2,nargin)); 5 | 6 | if nargin < 2 7 | dim = min(find(size(X)~=1)); 8 | if isempty(dim), dim = 1; end 9 | end 10 | 11 | sz = ones(length(size(X)), 1); 12 | sz(dim) = size(X,dim); 13 | 14 | % NaNs to zero 15 | Imv = isnan(X); 16 | X0 = X - repmat(meanmv(X,dim), sz); 17 | X0(Imv) = 0; 18 | 19 | % Sum and divide by the number of non-missing values 20 | s = sum(X0.^2, dim) ./ sum(~Imv, dim); 21 | %mu(isinf(xmean)) = NaN; 22 | -------------------------------------------------------------------------------- /algebra/sumall.m: -------------------------------------------------------------------------------- 1 | % SUMALL - Sums all the elements of the given matrix. 2 | % 3 | % Usage: 4 | % 5 | % Y = SUMALL(X) 6 | % 7 | % Evaluates Y = SUM(X(:)) 8 | 9 | % Last modified 2010-11-09 10 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 11 | 12 | function y = sumall(x) 13 | y = sum(x(:)); 14 | -------------------------------------------------------------------------------- /algebra/sumcomb.m: -------------------------------------------------------------------------------- 1 | % SUMCOMB 2 | % 3 | % y = sum(sum(bsxfun(@plus, x1(:), x2(:)'))); 4 | 5 | % Last modified 2010-11-09 6 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 7 | 8 | function y = sumcomb(x1,x2) 9 | 10 | % there must some more efficient way.. 11 | y = sum(sum(bsxfun(@plus, x1(:), x2(:)'))); 12 | -------------------------------------------------------------------------------- /algebra/sumfunc.m: -------------------------------------------------------------------------------- 1 | function g = sumfunc(f1, f2) 2 | g = @summing; 3 | function varargout = summing(varargin) 4 | nout = max(nargout,1); 5 | out1 = cell(1,nout); 6 | [out1{:}] = feval(f1,varargin{:}); 7 | out2 = cell(1,nout); 8 | [out2{:}] = feval(f2,varargin{:}); 9 | varargout = cellsum(out1,out2); 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /algebra/wsum.m: -------------------------------------------------------------------------------- 1 | % WSUM - Weighted sum of a matrix along one dimension. 2 | % 3 | % Y = WSUM(X,W,DIM) 4 | % 5 | % X : matrix 6 | % W : vector of weights 7 | % DIM : dimension to sum along (default: first non-singleton dimension) 8 | % 9 | % When X is a 2D-matrix, one can simply use W'*X or X*W for more 10 | % efficient performace. 11 | 12 | % Last modified 2010-06-09 13 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 14 | 15 | function y = wsum(X,w,dim) 16 | 17 | % warning('This function seems to be inefficient..') 18 | 19 | if ~isvector(w) 20 | error('Weights must be given as a vector.'); 21 | end 22 | 23 | if nargin < 3 24 | % Default dimension: first non-singleton dimension 25 | dim = find(size(X)>1, 1); 26 | if isempty(dim) 27 | dim = 1; 28 | end 29 | end 30 | 31 | if size(X,dim) ~= length(w) 32 | error(['The length of the weight vector does not match with the size ' ... 33 | 'of the matrix.']); 34 | end 35 | 36 | s = ones(1,max(dim,2)); 37 | s(dim) = length(w); 38 | y = sum(bsxfun(@times, X, reshape(w,s)), dim); -------------------------------------------------------------------------------- /datasets/hadsst2/hadsst2_parsedata.m: -------------------------------------------------------------------------------- 1 | 2 | % DATA = HADSST2_PARSEDATA() 3 | 4 | % Last modified 2011-01-11 5 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 6 | 7 | function data = hadsst2_parsedata 8 | 9 | filename = '/share/climate/data/UK_Met_Office/HadSST2/HadSST2_nobs.nc'; 10 | 11 | longitude = nc_varget(filename, 'longitude'); 12 | latitude = nc_varget(filename, 'latitude'); 13 | [LON,LAT] = meshgrid(longitude, latitude); 14 | coordinates = [LON(:)';LAT(:)']; 15 | 16 | time = nc_varget(filename, 'time'); 17 | time = time(:)' + datenum([1826 1 1]); 18 | 19 | observations = nc_varget(filename, 'data'); 20 | %observations(observations==raw.data.FillValue) = NaN; 21 | observations = reshape(observations, [length(time), size(coordinates, 2)])'; 22 | 23 | data = struct('observations', observations, ... 24 | 'coordinates', coordinates, ... 25 | 'longitude', longitude, ... 26 | 'latitude', latitude, ... 27 | 'time', time); 28 | -------------------------------------------------------------------------------- /datasets/hadsst3/hadsst3_load_data.m: -------------------------------------------------------------------------------- 1 | 2 | % DATA = HADSST2_PARSEDATA() 3 | 4 | % Last modified 2011-01-11 5 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 6 | 7 | function data = hadsst3_load_data() 8 | 9 | filename = '/share/climate/data/HadSST3/HadSST3_mostlikely.nc'; 10 | %filename = '/share/climate/data/UK_Met_Office/HadSST2/HadSST2_nobs.nc'; 11 | 12 | longitude = nc_varget(filename, 'longitude'); 13 | latitude = nc_varget(filename, 'latitude'); 14 | [LON,LAT] = meshgrid(longitude, latitude); 15 | coordinates = [LON(:)';LAT(:)']; 16 | 17 | time = nc_varget(filename, 'time'); 18 | time = time(:)' + datenum([1850 0 0]); 19 | 20 | observations = nc_varget(filename, 'sst'); 21 | %observations(observations==raw.data.FillValue) = NaN; 22 | observations = reshape(observations, [length(time), size(coordinates, 2)])'; 23 | 24 | data = struct('observations', double(observations), ... 25 | 'coordinates', double(coordinates), ... 26 | 'longitude', double(longitude), ... 27 | 'latitude', double(latitude), ... 28 | 'time', double(time)); 29 | -------------------------------------------------------------------------------- /datasets/metoffice/metoffice_add_climatology.m: -------------------------------------------------------------------------------- 1 | % Add climatology to the full fields 2 | function Yrec = add_climatology( Yrec, dataset, anomalies ) 3 | 4 | if anomalies 5 | datapath = metoffice_get_path(); 6 | load( [ datapath, dataset ], 'time', 'clim' ) 7 | 8 | dv = datevec( time ); month = dv(:,2); 9 | 10 | Yrec = Yrec + clim(:,month); 11 | 12 | end 13 | -------------------------------------------------------------------------------- /datasets/metoffice/metoffice_add_land.m: -------------------------------------------------------------------------------- 1 | % Add land and sea-ice grid boxes to the reconstructed fields 2 | function Yrec_out = metoffice_add_land( Yrec, dataset ) 3 | 4 | [ mask, lat, lon ] = metoffice_get_mask( dataset ); 5 | 6 | % path from system 7 | datapath = metoffice_get_path(); 8 | load( [ datapath, dataset ], 'time' ) 9 | 10 | % Add land and ice areas to the reconstruction fields 11 | Yrec_out = NaN( length(lat)*length(lon), length(time) ); 12 | Yrec_out( logical(mask(:)), : ) = Yrec; 13 | 14 | 15 | 16 | function Y = metoffice_add_land_old(X, maskfile) 17 | 18 | S = load('/share/climate/data/UK_Met_Office/RecTest/mask'); 19 | Y = nan(numel(S.mask),size(X,2)); 20 | Y(S.mask(:)==1,:) = X; 21 | % $$$ S = load('/share/climate/data/UK_Met_Office/RecTest/land_hadgem'); 22 | % $$$ land = (S.land(:) >= 0.5); 23 | % $$$ Y = nan(length(land),size(X,2)); 24 | % $$$ Y(~land,:) = X; 25 | -------------------------------------------------------------------------------- /datasets/metoffice/metoffice_get_mask.m: -------------------------------------------------------------------------------- 1 | % Get the mask defining the points where the reconstructions should be 2 | % computed. 3 | function [ mask, lat, lon ] = metoffice_get_mask( dataset ) 4 | 5 | % filename from dataset 6 | switch dataset 7 | case {'hadsst2d1','hadsst2d2','mohsst5d1','mohsst5d2'} 8 | maskfile = 'mask'; 9 | case {'aari','aari8'} 10 | maskfile = 'mask_seaice'; 11 | otherwise 12 | error(sprintf('Unknown dataset %s',dataset)); 13 | end 14 | 15 | % path from system 16 | datapath = metoffice_get_path(); 17 | 18 | % load mask 19 | if nargout == 3 20 | load( [ datapath, maskfile ], 'mask', 'lat', 'lon' ) 21 | elseif nargout == 1 22 | load( [ datapath, maskfile ], 'mask' ) 23 | end 24 | 25 | 26 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 27 | 28 | 29 | function m = metoffice_get_mask_old(maskfile) 30 | 31 | if nargin == 0 32 | warning('Give the maskfile'); 33 | maskfile = 'mask'; 34 | end 35 | 36 | os = system_dependent('getos'); 37 | 38 | if ~isempty(strfind(os,'Ubuntu')) 39 | % Finland 40 | maskfile = [ '/share/climate/data/UK_Met_Office/RecTest/DATASETS/' maskfile ]; 41 | elseif ~isempty(strfind(os,'Linux')) 42 | % ICS GridEngine 43 | maskfile = [ '/share/climate/data/UK_Met_Office/RecTest/DATASETS/' maskfile ]; 44 | else 45 | % MetOffice 46 | maskfile = [ '/net/home/h03/hadia/data/RecTest/' maskfile ]; 47 | end 48 | 49 | S = load(maskfile); 50 | m = S.mask(:)~=0; 51 | -------------------------------------------------------------------------------- /datasets/metoffice/metoffice_get_path.m: -------------------------------------------------------------------------------- 1 | function datapath = metoffice_get_path() 2 | 3 | os = system_dependent('getos'); 4 | if ~isempty(strfind(os,'Ubuntu')) 5 | % Finland 6 | datapath = '/share/climate/data/UK_Met_Office/RecTest/DATASETS/'; 7 | elseif ~isempty(strfind(os,'Linux')) 8 | % ICS GridEngine 9 | datapath = '/share/climate/data/UK_Met_Office/RecTest/DATASETS/'; 10 | else 11 | % MetOffice 12 | datapath = '/net/home/h03/hadia/data/RecTest/'; 13 | end 14 | -------------------------------------------------------------------------------- /datasets/metoffice/metoffice_get_testpath.m: -------------------------------------------------------------------------------- 1 | function datapath = metoffice_get_testpath() 2 | 3 | os = system_dependent('getos'); 4 | if ~isempty(strfind(os,'Ubuntu')) 5 | % Finland 6 | datapath = '/share/climate/data/UK_Met_Office/HadGem1/'; 7 | elseif ~isempty(strfind(os,'Linux')) 8 | % ICS GridEngine 9 | datapath = '/share/climate/data/UK_Met_Office/HadGem1/'; 10 | else 11 | % MetOffice 12 | datapath = '/net/home/h03/hadia/data/RecTest/'; 13 | end 14 | -------------------------------------------------------------------------------- /datasets/metoffice/metoffice_is_land_index.m: -------------------------------------------------------------------------------- 1 | function b = metoffice_is_land_index(ind) 2 | 3 | error('don''t use this function') 4 | %data = mohsst5_loaddata(); 5 | 6 | %land = sum(~isnan(data.observations),2) == 0; 7 | m = metoffice_get_mask(); 8 | b = ~m(ind); -------------------------------------------------------------------------------- /datasets/metoffice/metoffice_plot_fa.m: -------------------------------------------------------------------------------- 1 | function metoffice_plot_fa(Q) 2 | 3 | [D,N] = size(Q.X); 4 | M = size(Q.W,2); 5 | 6 | E2 = reshape(Q.CovX, [D^2, N]); 7 | E = sqrt(E2(1:(D+1):end,:)); 8 | 9 | hax = tserrorplot(Q.X',2*E'); 10 | ylim_centered(hax); 11 | ylim_scale(hax, 1.2); 12 | -------------------------------------------------------------------------------- /datasets/metoffice/metoffice_remove_bins.m: -------------------------------------------------------------------------------- 1 | function X = metoffice_remove_bins(X,maskfile) 2 | 3 | X(~metoffice_get_mask(maskfile),:) = []; 4 | -------------------------------------------------------------------------------- /datasets/metoffice/metoffice_remove_land.m: -------------------------------------------------------------------------------- 1 | function X = metoffice_remove_land(X) 2 | 3 | X(~metoffice_get_mask(),:) = []; 4 | %S = load('/share/climate/data/UK_Met_Office/RecTest/land_hadgem'); 5 | %land = (S.land(:) >= 0.5); 6 | %X(land,:) = []; 7 | -------------------------------------------------------------------------------- /datasets/metoffice/metoffice_remove_mask.m: -------------------------------------------------------------------------------- 1 | function X = metoffice_remove_mask(X,dataset) 2 | 3 | X(~metoffice_get_mask(dataset),:) = []; 4 | 5 | function X = metoffice_remove_bins_old(X,maskfile) 6 | 7 | X(~metoffice_get_mask(maskfile),:) = []; 8 | -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_add_land.m: -------------------------------------------------------------------------------- 1 | function X = mohsst5_remove_land(X) 2 | 3 | data = mohsst5_loaddata(); 4 | 5 | land = sum(~isnan(data.observations),2) == 0; 6 | 7 | Y = nan(length(land),size(X,2)); 8 | Y(~land,:) = X; 9 | -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_compare_ships2satellite.m: -------------------------------------------------------------------------------- 1 | function mohsst5_compare_ships2satellite 2 | 3 | data = mohsst5_loaddata; 4 | verdata = load('/home/alexilin/matlab/kaplan/verifdata', 'vd', 'lat', ... 5 | 'lon', 'time'); 6 | 7 | i0 = find(data.time == verdata.time(1)); 8 | i1 = find(data.time == verdata.time(end)); 9 | 10 | time = data.time(i0:i1); 11 | Y = data.observations(:,i0:i1); 12 | Yver = verdata.vd; 13 | 14 | figure 15 | plot(Y(:), Yver(:), '.'); 16 | xlabel('Ship') 17 | ylabel('Satellite') 18 | set(gca, 'DataAspectRatio', [1 1 1]); 19 | set(gca, 'XLimMode', 'manual'); 20 | set(gca, 'YLimMode', 'manual'); 21 | 22 | % Draw a line for comparison 23 | hold on 24 | x = [-100, 100]; 25 | y = [-100, 100]; 26 | plot(x(:), y(:), 'r'); 27 | 28 | ind = ~isnan(Y(:)) & ~isnan(Yver(:)); 29 | correlation = corr(Y(ind), Yver(ind)) 30 | Cov = cov(Y(ind)+10, Yver(ind)) 31 | 32 | eigenvalues = eig(Cov) 33 | sqrt_eigenvalues = sqrt(eigenvalues) 34 | 35 | inds = [200 300 299 750 1050 1270 1580]; 36 | hax = tsplot(time, Y(inds,:), 'r.-'); 37 | hold on 38 | addtsplot(time, Yver(inds,:), 'k.-'); 39 | for i=1:length(hax) 40 | datetick(hax(i), 'x') 41 | end -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_explained_variance_fa.m: -------------------------------------------------------------------------------- 1 | function v = mohsst5_explained_variance_fa(W, CovW, X, CovX) 2 | 3 | % This is approximate, because this does not take into account the 4 | % correlations between different time instances or spatial locations. 5 | 6 | % Remove temporal mean 7 | X = bsxfun(@minus, X, mean(X,2)); 8 | 9 | % Compute 10 | XX = X*X'; 11 | if ndims(CovX) == 2 && all(size(CovX)==size(X)) 12 | XX = XX + diag(sum(CovX,2)); 13 | else 14 | XX = XX + sum(CovX,3); 15 | end 16 | 17 | % Compute weighted 18 | w = mohsst5_weights(); 19 | w = mohsst5_remove_land(w); 20 | WW = W*diag(w)*W'; 21 | if ndims(CovW) == 2 && all(size(CovW)==size(W)) 22 | WW = WW + diag(wsum(CovW,w,2)); 23 | else 24 | WW = WW + wsum(CovW,w,3); 25 | end 26 | 27 | % Effective number of samples 28 | %N = sum(sum( w(:)*ones(1,size(X,2)) )); 29 | N = sum(w) * size(X,2); 30 | 31 | % Explained variance 32 | v = diagprod(WW,XX) / N; -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_is_land_index.m: -------------------------------------------------------------------------------- 1 | function b = mohsst5_is_land_index(ind) 2 | 3 | data = mohsst5_loaddata(); 4 | 5 | land = sum(~isnan(data.observations),2) == 0; 6 | 7 | b = land(ind); -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_land_indeces.m: -------------------------------------------------------------------------------- 1 | function ind = mohsst5_land_indeces() 2 | 3 | data = mohsst5_loaddata(); 4 | 5 | land = sum(~isnan(data.observations),2) == 0; 6 | 7 | vec = 1:length(land); 8 | ind = vec(land); -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_loaddata.m: -------------------------------------------------------------------------------- 1 | 2 | % DATA = MOHSST5_LOADDATA() 3 | 4 | % Last modified 2011-01-11 5 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 6 | 7 | function data = mohsst5_loaddata() 8 | 9 | data = load('mohsst5_data.mat'); 10 | -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_mapplot.m: -------------------------------------------------------------------------------- 1 | % mohsst5_mapplot(w, data) 2 | 3 | function hax = mohsst5_mapplot(w, varargin) 4 | 5 | options = struct('colormap', 'centered', ... 6 | 'data', [], ... 7 | 'ytick', []); 8 | 9 | [options, errmsg] = argparse(options, varargin{:}); 10 | error(errmsg); 11 | 12 | if nargin < 2 || isempty(options.data) 13 | data = mohsst5_loaddata(); 14 | else 15 | data = options.data; 16 | end 17 | 18 | if ~isvector(w) 19 | error('The loadings w should be a vector'); 20 | end 21 | 22 | M = size(data.observations,1); 23 | 24 | lands = colsum(~isnan(data.observations)) == 0; 25 | if length(w) == 1727 26 | % Fill the land areas to the matrix 27 | tmp = nan(M,1); 28 | tmp(~lands) = w; 29 | w = tmp; 30 | end 31 | 32 | map_projection('global-ellipse'); 33 | hax = map_pcolor(data.longitude, ... 34 | data.latitude, ... 35 | reshape(w, [length(data.longitude) length(data.latitude)])); 36 | 37 | switch options.colormap 38 | case 'centered' 39 | colormap_redblue(); 40 | clim_centered(); 41 | case 'positive' 42 | colormap_scale(); 43 | clim_positive(); 44 | end 45 | 46 | map_coast(); 47 | map_grid('yticklabels', [], 'xtick', [], 'ytick', options.ytick); 48 | 49 | if nargout < 1 50 | clear hax; 51 | end 52 | 53 | -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_parsedata.m: -------------------------------------------------------------------------------- 1 | 2 | % [DATA, DATA_RAW] = MOHSST5_PARSEDATA() 3 | 4 | % Last modified 2011-01-11 5 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 6 | 7 | function [data, data_raw] = mohsst5_parsedata 8 | 9 | filename = ['/share/climate/data/GOSTA_atlas7_MOHSST5_ssta/' ... 10 | 'MOHSST5_ssta.cdf' ]; 11 | data = nc_getall( filename ); 12 | data_raw = data; 13 | 14 | x = double(data.ssta.data); 15 | x = shiftdim(x,1); 16 | x = reshape( x, [ size(x,1)*size(x,2) size(x,3) ]); 17 | 18 | tmp = sum( sum( isnan(x) ) ) / prod(size(x)) * 100; 19 | fprintf( '%.2f%% of values are missing\n', tmp ) 20 | 21 | lat = double(data.Y.data); 22 | lon = double(data.X.data); 23 | m60 = double(data.T.data); 24 | 25 | year = 1960 + floor(m60/12); 26 | month = ceil(mod(m60,12)); 27 | timevec = [ year month repmat(15,length(m60),1) ]; 28 | %timevec 29 | time = datenum( timevec )'; 30 | 31 | %save ssta x lat lon time 32 | [LON, LAT] = meshgrid(lon,lat); 33 | %[LAT, LON] = meshgrid(lat,lon); 34 | data = struct('observations',x, 'coordinates',[LON(:)';LAT(:)'], 'longitude', ... 35 | lon, 'latitude', lat, 'time',time); -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_pca.m: -------------------------------------------------------------------------------- 1 | function Q = mohsst5_pca(data, D, maxiter) 2 | 3 | if nargin < 3 4 | maxiter = 10; 5 | end 6 | 7 | Q = pca_full(data.observations,D,'maxiters',maxiter,'rotate2pca',true, ... 8 | 'algorithm','ppca'); 9 | 10 | Yh_ppca = Q.A * Q.S + repmat(Q.Mu,1,N); 11 | -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_performance_rmsew.m: -------------------------------------------------------------------------------- 1 | % e = mohsst5_performance_rmsew(Yh, I) 2 | 3 | function e = mohsst5_performance_rmsew(Yh, I) 4 | 5 | load('mohsst5_data.mat', 'observations'); 6 | 7 | Y = observations; 8 | 9 | if all(size(Yh) == [1727 1632]) 10 | % The seas have been removed 11 | disp('Adding the land areas to the matrix.') 12 | sea = sum(~isnan(Y),2) > 0; 13 | F = nan(size(Y)); 14 | F(sea,:) = Yh; 15 | Yh = F; 16 | Itmp = false(size(Y)); 17 | Itmp(sea,:) = I; 18 | I = Itmp; 19 | end 20 | 21 | Y(~I) = nan; 22 | 23 | e = mohsst5_rmsew(Y,Yh); 24 | -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_points_to_grid_index.m: -------------------------------------------------------------------------------- 1 | function ind = mohsst5_points_to_grid_index(C) 2 | 3 | %data = mohsst5_loaddata(); 4 | 5 | lon = C(1,:); 6 | lat = C(2,:); 7 | 8 | N_lon = 72; 9 | N_lat = 36; 10 | 11 | % Random locations to 5x5-grid 12 | ind_lon = floor(lon/5)+(N_lon/2+1); 13 | ind_lat = N_lat - (floor(lat/5)+(N_lat/2+1)) + 1; 14 | 15 | ind = (ind_lon-1)*N_lat + ind_lat; -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_reconstruction_movie.m: -------------------------------------------------------------------------------- 1 | function mohsst5_reconstruction_movie(Yh, errYh) 2 | 3 | warning('Under construction') 4 | 5 | data = mohsst5_loaddata(); 6 | Y = data.observations; 7 | 8 | N = size(Yh,1); 9 | 10 | c1 = max(abs(Y(:))); 11 | c2 = max(abs(Yh(:))); 12 | cl = max(c1,c2); 13 | cl = prctile(abs([Yh(:);Y(:)]), 99.9) 14 | 15 | cl_err = max(abs(errYh(:))); 16 | 17 | N = 4; 18 | for n=1:N 19 | figure 20 | hax = subplot(3,1,1); 21 | mohsst5_mapplot(Y(:,n), 'colormap', 'centered'); 22 | set(hax, 'clim', [-cl cl]); 23 | hax = subplot(3,1,2); 24 | mohsst5_mapplot(Yh(:,n), 'colormap', 'centered'); 25 | set(hax, 'clim', [-cl cl]); 26 | hax = subplot(3,1,3); 27 | mohsst5_mapplot(errYh(:,n), 'colormap', 'centered'); 28 | set(hax, 'clim', [-cl_err cl_err]); 29 | end -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_remove_land.m: -------------------------------------------------------------------------------- 1 | function X = mohsst5_remove_land(X) 2 | 3 | data = mohsst5_loaddata(); 4 | 5 | land = sum(~isnan(data.observations),2) == 0; 6 | 7 | X(land,:) = []; -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_rmse_sparsepart.m: -------------------------------------------------------------------------------- 1 | function weighted_rmse = mohsst5_rmse_sparsepart(Yh, data) 2 | 3 | Itest = load('ind20test.mat'); 4 | Itest = Itest.Itest; 5 | if nargin < 2 || isempty(data) 6 | data = mohsst5_loaddata; 7 | end 8 | 9 | % Test set 10 | Ytest = data.observations; 11 | Ytest(~Itest) = nan; 12 | missing = sum(isnan(Ytest(:))) / numel(Ytest); 13 | 14 | % Take only data from EARLY parts 15 | rows = size(data.observations,1); 16 | rminds = data.time > datenum([1900 1 1 0 0 0]); % remove by time period 17 | rminds = rowsum(~isnan(data.observations)) > 0.3 * rows; % remove by sparsity 18 | rminds = data.time > datenum([9999 1 1 0 0 0]); % full data 19 | Ytest(:,rminds) = nan; 20 | missing = sum(isnan(Ytest(:))) / numel(Ytest); 21 | testsize = sum(~isnan(Ytest(:))); 22 | 23 | % $$$ % Take only data from sparse parts 24 | % $$$ rows = size(data.observations,1); 25 | % $$$ rminds = rowsum(~isnan(data.observations)) > 0.7 * rows; 26 | % $$$ Ytest(:,rminds) = nan; 27 | % $$$ missing = sum(isnan(Ytest(:))) / numel(Ytest) 28 | % $$$ testsize = sum(~isnan(Ytest(:))) 29 | 30 | weights = sqrt(cosd(data.coordinates(2,:))); 31 | weights = weights(:); 32 | Weights = bsxfun(@times, ~isnan(Ytest), weights(:)); % mask with missing values 33 | Yhw = bsxfun(@times, Yh, weights(:)); 34 | Ytestw = bsxfun(@times, Ytest, weights(:)); 35 | err = Yhw(:) - Ytestw(:); 36 | err(isnan(err)) = []; % remove missing values 37 | weighted_rmse = sqrt(err'*err / sum(Weights(:).^2)); 38 | -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_rmsew.m: -------------------------------------------------------------------------------- 1 | function e = mohsst5_rmsew(Y, Yh) 2 | 3 | Z = Y-Yh; 4 | I = ~isnan(Z); 5 | 6 | % Weight proportional to area size 7 | load('mohsst5_data.mat', 'latitude', 'longitude'); 8 | %[lat,lon] = meshgrid(latitude, longitude); 9 | [lon,lat] = meshgrid(longitude, latitude); 10 | W = repmat(cosd(lat(:)), [1, size(Y,2)]); 11 | 12 | e = rmsew(Z(I), W(I)); 13 | -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_rotate_to_pca.m: -------------------------------------------------------------------------------- 1 | function [W,CovW,X,CovX] = mohsst5_rotate_to_pca(W,CovW,X,CovX) 2 | 3 | % Remove temporal mean 4 | mu_X = mean(X,2); 5 | X = bsxfun(@minus, X, mu_X); 6 | 7 | % Compute 8 | XX = X*X'; 9 | if ndims(CovX) == 2 && all(size(CovX)==size(X)) 10 | XX = XX + diag(sum(CovX,2)); 11 | else 12 | XX = XX + sum(CovX,3); 13 | end 14 | 15 | % Compute weighted 16 | w = mohsst5_weights(); 17 | w = mohsst5_remove_land(w); 18 | WW = W*diag(w)*W'; 19 | if ndims(CovW) == 2 && all(size(CovW)==size(W)) 20 | WW = WW + diag(wsum(CovW,w,2)); 21 | else 22 | WW = WW + wsum(CovW,w,3); 23 | end 24 | 25 | % Find the SVD of the rotation: 26 | % R = Ur * Dr * Vr' 27 | 28 | % Whiten 29 | [Ux,Dx] = svd(XX/size(X,2)); 30 | Vr = Ux; 31 | Dr = diag(diag(Dx).^(-0.5)); 32 | 33 | % Orthogonalize 34 | [Uw,~] = svd(inv(Dr)*Vr'*WW*Vr*inv(Dr)); 35 | Ur = Uw'; 36 | 37 | R = Ur*Dr*Vr'; 38 | 39 | % $$$ figure 40 | % $$$ imagesc(XX) 41 | % $$$ figure 42 | % $$$ imagesc(R*XX*R') 43 | % $$$ figure 44 | % $$$ imagesc(WW) 45 | % $$$ figure 46 | % $$$ imagesc(R'\WW/R) 47 | % $$$ return 48 | 49 | X = R*X; 50 | W = R'\W; 51 | 52 | if ndims(CovX) == 2 && all(size(CovX)==size(X)) 53 | CovX = R.^2*CovX; 54 | else 55 | for n=1:size(CovX,3) 56 | CovX(:,:,n) = R*CovX(:,:,n)*R'; 57 | end 58 | end 59 | 60 | if ndims(CovW) == 2 && all(size(CovW)==size(W)) 61 | CovW = inv(R').^2*CovW; 62 | else 63 | for n=1:size(CovW,3) 64 | CovW(:,:,n) = R'\CovW(:,:,n)/R; 65 | end 66 | end 67 | 68 | X = bsxfun(@plus, X, R*mu_X); 69 | -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_subset.m: -------------------------------------------------------------------------------- 1 | function data = mohsst5_subset(data, stations, times) 2 | 3 | [M,N] = size(data.observations); 4 | 5 | if numel(stations) == 1 && stations < 1 6 | permM = randperm(M); 7 | subM = ceil(stations * M); 8 | stations = permM(1:subM); 9 | stations = sort(stations); 10 | end 11 | if numel(times) == 1 && times < 1 12 | permN = randperm(N); 13 | subN = ceil(times * N); 14 | times = permN(1:subN); 15 | times = sort(times); 16 | end 17 | if all(size(stations) == [2 2]) 18 | minlon = stations(1,1); 19 | maxlon = stations(1,2); 20 | minlat = stations(2,1); 21 | maxlat = stations(2,2); 22 | stations = find(data.coordinates(1,:) >= minlon & ... 23 | data.coordinates(1,:) <= maxlon & ... 24 | data.coordinates(2,:) >= minlat & ... 25 | data.coordinates(2,:) <= maxlat); 26 | end 27 | 28 | data.observations = data.observations(stations, times); 29 | data.coordinates = data.coordinates(:,stations); 30 | data.time = data.time(times); -------------------------------------------------------------------------------- /datasets/mohsst5/mohsst5_weights.m: -------------------------------------------------------------------------------- 1 | % MOHSST5_WEIGHTS - Returns weights for the spatial locations in MOHSST5 2 | % dataset. 3 | % 4 | % Polar regions have zero weight and equator regions unit weight. 5 | % 6 | % The weight is the cosine of the latitude. It can be used to scale the 7 | % noise precision or divide the noise variance. 8 | 9 | function W = mohsst5_weights() 10 | 11 | data = mohsst5_loaddata(); 12 | 13 | [LON,LAT] = meshgrid(data.longitude,data.latitude); 14 | 15 | W = cosd(LAT(:)); 16 | 17 | -------------------------------------------------------------------------------- /datasets/mohsst5/script_matlab.m: -------------------------------------------------------------------------------- 1 | 2 | data = mohsst5_loaddata; 3 | 4 | mohsst5_experiment_periodic(data, 15, 45, 0.3, 0.2, false, false); 5 | 6 | quit -------------------------------------------------------------------------------- /datasets/testbed/tbload_temperature.m: -------------------------------------------------------------------------------- 1 | function data = tbload_temperature 2 | 3 | load('testbed_temperature.mat'); 4 | -------------------------------------------------------------------------------- /datasets/testbed/tbpreprocess.m: -------------------------------------------------------------------------------- 1 | function data = tbpreprocess(data) 2 | 3 | % Remove stations with no observations 4 | I = sum(~isnan(data.observations),2) == 0; 5 | data = tbremove_stations(data, I); 6 | 7 | % Remove station levels 8 | data = remove_station_levels(data); 9 | 10 | % Remove outregion stations 11 | G_MINLON = 22.5; 12 | G_MAXLON = 26.8; 13 | G_MINLAT = 59.7; 14 | G_MAXLAT = 61.0; 15 | n = size(data.coordinates, 1); 16 | I = []; 17 | for i=1:n 18 | lon = data.coordinates(i,1); 19 | lat = data.coordinates(i,2); 20 | if lon < G_MINLON || lon > G_MAXLON || lat < G_MINLAT || lat > G_MAXLAT 21 | I = [I; i]; 22 | end 23 | end 24 | data = tbremove_stations(data, I); 25 | 26 | 27 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 28 | function [coordinates, I] = remove_station_levels(coordinates) 29 | % I is the indeces of the remaining stations 30 | 31 | if isstruct(coordinates) 32 | coords = coordinates.coordinates; 33 | else 34 | coords = coordinates; 35 | end 36 | 37 | lon = coords(:, 1); 38 | lat = coords(:, 2); 39 | n = size(coords, 1); 40 | 41 | I = []; 42 | 43 | for i=1:n 44 | ind = find(coords(I,1)==lon(i) & coords(I,2)==lat(i)); 45 | if isempty(ind) 46 | I = [I; i]; 47 | else 48 | s = [I(ind), i]; %indeces with same location 49 | [temp, min_i] = min(coords(s,3)); 50 | I(ind) = s(min_i); 51 | end 52 | end 53 | 54 | if isstruct(coordinates) 55 | I_rm = 1:n; 56 | I_rm(I) = []; 57 | coordinates = tbremove_stations(coordinates, I_rm); 58 | else 59 | coordinates = coordinates(I, :); 60 | end 61 | -------------------------------------------------------------------------------- /datasets/testbed/tbremove_stations.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | function data_struct = remove_stations(data_struct, stations) 3 | 4 | data_struct.observations(stations,:) = []; 5 | data_struct.stations(stations) = []; 6 | data_struct.coordinates(stations,:) = []; 7 | -------------------------------------------------------------------------------- /datasets/testbed/tbsubset.m: -------------------------------------------------------------------------------- 1 | function data = tbsubset(data, stations, times) 2 | 3 | remove = setdiff(1:rows(data.observations), stations); 4 | data = tbremove_stations(data, remove); 5 | data.time = data.time(times); 6 | data.observations = data.observations(:,times); -------------------------------------------------------------------------------- /datasets/testbed/testbed_coast.m: -------------------------------------------------------------------------------- 1 | % TESTBED_COAST - Draws the coast line of the Testbed research area. 2 | % 3 | % TESTBED_COAST() 4 | % TESTBED_COAST(HAX) 5 | % 6 | % HAX : a vector of axes handles [default: GCA] 7 | 8 | % Last modified 2010-06-11 9 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 10 | 11 | function testbed_coast(hax) 12 | 13 | if nargin < 1 14 | hax = gca; 15 | end 16 | 17 | map_coast(hax, 'usercoast','testbed_data_coast'); 18 | -------------------------------------------------------------------------------- /datasets/testbed/testbed_data_coast.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jluttine/matlab/63406c7782b0869948f06e1dbc594460c165d24e/datasets/testbed/testbed_data_coast.mat -------------------------------------------------------------------------------- /datasets/testbed/testbed_grid.m: -------------------------------------------------------------------------------- 1 | % [LON,LAT] = TESTBED_GRID(RES_LON, RES_LAT) 2 | 3 | % Copyright (c) 2010 Jaakko Luttinen 4 | 5 | function [lon,lat] = testbed_grid(res_lon, res_lat) 6 | 7 | [minlon,maxlon,minlat,maxlat] = testbed_projection(); 8 | lon = linspace(minlon, maxlon, res_lon); 9 | lat = linspace(minlat, maxlat, res_lat); 10 | -------------------------------------------------------------------------------- /datasets/testbed/testbed_loaddata.m: -------------------------------------------------------------------------------- 1 | function data = testbed_loaddata 2 | 3 | load('testbed_temperature.mat'); 4 | -------------------------------------------------------------------------------- /datasets/testbed/testbed_loadings.m: -------------------------------------------------------------------------------- 1 | % TESTBED_LOADINGS - Plots interpolated spatial components. 2 | % 3 | % Plots interpolated spatial components Q.W Q on map according to the given 4 | % coordinates Q.coordinates. 5 | % 6 | % HAX = TESTBED_LOADINGS(Q,...) 7 | % 8 | % Optional parameters: 9 | % 10 | % 'resolution' : vector of longitudinal and latitudinal resolution 11 | % 'components' : indeces of the components to plot 12 | % 13 | % also, the optional parameters of MAP_INTERPOLATE may be used. 14 | % 15 | % See also MAP_INTERPOLATE. 16 | 17 | % Last modified 2010-06-11 18 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 19 | 20 | function hax = testbed_loadings(W, coordinates, varargin) 21 | 22 | options = struct( 'resolution', [60 40]); 23 | 24 | % Check arguments 25 | [options, errmsg, remopts] = argparse(options, varargin{:}); 26 | error(errmsg); 27 | 28 | figure 29 | [lon,lat] = testbed_grid(options.resolution(1), options.resolution(2)); 30 | hax = map_interpolate(coordinates(:,1:2)', ... 31 | W, ... 32 | lon, lat, ... 33 | remopts{:}); 34 | 35 | %hcb = colorbars(hax, 'SouthOutside'); 36 | testbed_coast(hax); 37 | %map_grid(hax, 'ytick', [], 'xtick', []); 38 | 39 | if nargout < 1 40 | clear hax; 41 | end -------------------------------------------------------------------------------- /datasets/testbed/testbed_plot_data.m: -------------------------------------------------------------------------------- 1 | 2 | function data = testbed_plot_data(data) 3 | 4 | if nargin < 1 5 | data = testbed_loaddata(); 6 | data = testbed_preprocess(data); 7 | end 8 | 9 | M = size(data.observations,1); 10 | group = 10; 11 | for m=1:group:M 12 | stations = m:min(m+group-1, M); 13 | tsplot(data.time, data.observations(stations, :), 'r'); 14 | end 15 | 16 | if nargout == 0 17 | clear data; 18 | end -------------------------------------------------------------------------------- /datasets/testbed/testbed_plot_experiment.m: -------------------------------------------------------------------------------- 1 | function testbed_plot_experiment(Q) 2 | % testbed_plot_experiment(Q) 3 | % 4 | % Plots latent signals, spatial components and reconstructions. 5 | 6 | % Plot latent signals 7 | testbed_states(Q); 8 | 9 | % Plot spatial components 10 | testbed_loadings(Q, 'resolution', [50 30], 'method', 'rbf'); 11 | 12 | % Plot reconstructions of problematic stations 13 | testbed_reconstructions(Q, 'stations', [12 13 20 21 28 48 57 59]); 14 | 15 | % $$$ M = size(Q.W,1); 16 | % $$$ group = 10; 17 | % $$$ for m=1:group:M 18 | % $$$ stations = m:min(m+group-1, M); 19 | % $$$ testbed_rcplot(Q, 'stations', stations); 20 | % $$$ end 21 | -------------------------------------------------------------------------------- /datasets/testbed/testbed_plot_finland.m: -------------------------------------------------------------------------------- 1 | % TESTBED_PLOT_FINLAND - Plot the map of Finland with a red rectangle 2 | % showing the Testbed research area. 3 | 4 | % Last modified 2010-06-11 5 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 6 | 7 | function testbed_plot_finland() 8 | 9 | % Get the research area 10 | [minlon,maxlon,minlat,maxlat] = testbed_projection(); 11 | 12 | % Set the projection 13 | m_proj('Mercator', 'lon',[19,32], 'lat',[59.3,70.2]) 14 | 15 | % Plot the country boundary 16 | m_plotbndry('finland', 'color', 'k'); 17 | 18 | % Plot the research area 19 | m_line([minlon,minlon,maxlon,maxlon,minlon], ... 20 | [minlat,maxlat,maxlat,minlat,minlat], ... 21 | 'color', 'r', 'LineWidth', 2); 22 | m_grid('box','off', 'linestyle','none', 'xticklabels',[], 'yticklabels',[], ... 23 | 'xtick',[], 'ytick',[]); 24 | -------------------------------------------------------------------------------- /datasets/testbed/testbed_plot_stations.m: -------------------------------------------------------------------------------- 1 | % TESTBED_PLOT_STATIONS - Plots the stations in the Testbed dataset. 2 | % 3 | % TESTBED_PLOT_STATIONS(COORDINATES, ...) 4 | % 5 | % COORDINATES is a matrix that has longitudes as the first row and latitudes 6 | % as the second row. It can also be a struct which has COORDINATES as a 7 | % field: COORDINATES.coordinates. 8 | 9 | % Last modified 2010-06-11 10 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 11 | 12 | function testbed_plot_stations(coordinates, varargin) 13 | 14 | % Default options 15 | options = struct(... 16 | 'Style', 'o', ... 17 | 'MarkerSize', 4, ... 18 | 'MarkerEdgeColor', 'k', ... 19 | 'MarkerFillColor', [0.7 0 0.7]); 20 | [options, errmsg, remopts] = argparse(options, varargin{:}); 21 | error(errmsg); 22 | 23 | if nargin < 1 24 | % Default set of stations 25 | data = testbed_loaddata(); 26 | data = testbed_preprocess(data); 27 | coordinates = data.coordinates; 28 | end 29 | 30 | if ~isnumeric(coordinates) && isstruct(coordinates) 31 | coordinates = coordinates.coordinates; 32 | end 33 | 34 | % Plot the stations 35 | map_plot(coordinates', ... 36 | options.Style, ... 37 | 'MarkerSize', options.MarkerSize, ... 38 | 'MarkerEdgeColor', options.MarkerEdgeColor, ... 39 | 'MarkerFaceColor', options.MarkerFillColor, ... 40 | remopts{:}); 41 | 42 | -------------------------------------------------------------------------------- /datasets/testbed/testbed_plot_topography.m: -------------------------------------------------------------------------------- 1 | % Plot the topography. 2 | function testbed_plot_topography() 3 | 4 | % Set projection 5 | testbed_projection(); 6 | 7 | % Plot topography 8 | map_topography(); -------------------------------------------------------------------------------- /datasets/testbed/testbed_projection.m: -------------------------------------------------------------------------------- 1 | % TESTBED_PROJECTION - Set the map projection for Testbed plots. 2 | % 3 | % [MINLON,MAXLON,MINLAT,MAXLAT] = TESTBED_PROJECTION() 4 | 5 | % Last modified 2010-06-11 6 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 7 | 8 | function [minlon, maxlon, minlat, maxlat] = testbed_projection() 9 | 10 | minlon = 22.5; 11 | maxlon = 26.8; 12 | minlat = 59.75; 13 | maxlat = 61.0; 14 | m_proj('Mercator', 'lon',[minlon,maxlon], 'lat',[minlat,maxlat]); 15 | 16 | if nargout == 0 17 | clear minlon maxlon minlat maxlat; 18 | end 19 | -------------------------------------------------------------------------------- /datasets/testbed/testbed_remove_stations.m: -------------------------------------------------------------------------------- 1 | % DATA = TESTBED_REMOVE_STATIONS(DATA, STATIONS) 2 | % 3 | % Removes stations from the data struct. 4 | % 5 | % DATA is a struct of Testbed data. 6 | % STATIONS is the indeces of the stations to be removed. 7 | 8 | % Last modified 2010-06-02 9 | % Copyright (c) Jaakko Luttinen 10 | 11 | function data_struct = testbed_remove_stations(data_struct, stations) 12 | 13 | data_struct.observations(stations,:) = []; 14 | data_struct.stations(stations) = []; 15 | data_struct.coordinates(stations,:) = []; 16 | -------------------------------------------------------------------------------- /datasets/testbed/testbed_states.m: -------------------------------------------------------------------------------- 1 | 2 | % TESTBED_TSPLOT 3 | % 4 | % Plot the latent signals. 5 | 6 | % Copyright (c) 2010 Jaakko Luttinen 7 | 8 | function hax = testbed_tsplot(Q, varargin) 9 | 10 | % Parse optional arguments 11 | options = struct(); 12 | [options, errmsg] = argparse(options, varargin{:}); 13 | error(errmsg); 14 | 15 | % Plot the signals 16 | time = Q.options.user_data.time(:); 17 | hax = tserrorplot(time, Q.X', 2*sqrt(mdiag(Q.CovX))'); 18 | N = length(hax); 19 | set(hax, 'xlim', [min(time), max(time)]); 20 | dateticks(hax, 'x', 'mmmyy', 'keeplimits'); 21 | set(hax(1:(N-1)), 'xticklabel', []); 22 | set_subplot_positions(hax, N, 1, [0.05 0.05 0.05 0.1], [0.02 0.02]); 23 | 24 | if nargout < 1 25 | clear hax; 26 | end 27 | -------------------------------------------------------------------------------- /datasets/testbed/testbed_stationplot.m: -------------------------------------------------------------------------------- 1 | function testbed_stationplot(Q, varargin) 2 | 3 | options = struct(... 4 | 'stations', 1:size(Q.W,1), ... 5 | 'linespec', 'x'); 6 | 7 | % Check arguments 8 | [options, errmsg, remopts] = argparse(options, varargin{:}); 9 | error(errmsg); 10 | 11 | mapplot(Q.options.user_data.coordinates(options.stations,1:2)', options.linespec, ... 12 | remopts{:}); 13 | 14 | -------------------------------------------------------------------------------- /datasets/testbed/testbed_stationtext.m: -------------------------------------------------------------------------------- 1 | function testbed_stationtext(Q, varargin) 2 | 3 | options = struct(... 4 | 'stations', 1:size(Q.W,1), ... 5 | 'string', {{}}, ... 6 | 'color', 'k', ... 7 | 'verticalalignment', 'middle'); 8 | 9 | % Check arguments 10 | [options, errmsg, remopts] = argparse(options, varargin{:}); 11 | error(errmsg); 12 | 13 | % By default, put the stations number 14 | if isempty(options.string) 15 | options.string = mat2str(options.stations); 16 | end 17 | 18 | maptext(Q.options.user_data.coordinates(options.stations,1:2)', options.string, ... 19 | 'Color', options.color, 'VerticalAlignment', options.verticalalignment, ... 20 | remopts{:}); 21 | 22 | -------------------------------------------------------------------------------- /deprecated/cols.m: -------------------------------------------------------------------------------- 1 | function d = cols(X) 2 | 3 | warning('This function is deprecated') 4 | 5 | d = size(X,2); -------------------------------------------------------------------------------- /deprecated/dss.m: -------------------------------------------------------------------------------- 1 | function [Af,Sf,S,Sv,Vf] = dss(A, S, Sv, cutoff, nComp) 2 | warning('This function is deprecated') 3 | 4 | % [Af,Sf,S,Vf] = dss(A, S, Sv, filter, nComp) 5 | 6 | % $$$ error(nargchk(4,5,nargin)); 7 | % $$$ if nargin == 4 8 | % $$$ filter = Sv; 9 | % $$$ nComp = filter; 10 | % $$$ Sv = []; 11 | % $$$ end 12 | 13 | % Denoise 14 | %filter = filter(:)'; 15 | n = size(S,2); 16 | [b] = fir1(floor(n/4), cutoff); 17 | %figure 18 | %plot(b) 19 | Sf = filtfilt(b,1,S')'; 20 | 21 | % Identify directions with PCA 22 | Vf = princomp(Sf'); 23 | Vf=Vf(:,1:nComp); 24 | 25 | % Rotate 26 | Af = A * Vf; 27 | Sf = Vf' * Sf; 28 | S = Vf' * S; 29 | for j = 1:length(Sv) 30 | Sv{j} = Vf'*Sv{j}*Vf; 31 | end 32 | 33 | -------------------------------------------------------------------------------- /deprecated/gam_vbcost.m: -------------------------------------------------------------------------------- 1 | 2 | function c = gam_vbcost(x,logx,apost,bpost,aprior,bprior) 3 | warning('This function is deprecated') 4 | 5 | % function c = gam_vbcost(x,logx,apost,bpost,aprior,bprior) 6 | % 7 | % Calculates c = -KL(q||p) = - 8 | % where the expectation <.> is taken over q(X). This is used for 9 | % calculating the lower bound of the marginal loglikelihood in VB 10 | % models. 11 | % 12 | % Let X be a Gamma distributed variable: 13 | % p(X) = G(aprior,bprior) 14 | % q(X) = G(apost,bpost) 15 | % = x 16 | % = logx 17 | 18 | % Cost from prior 19 | c = aprior.*log(bprior) - gammaln(aprior) + (aprior-1).*logx - bprior.*x; 20 | % Cost from q-posterior 21 | c = c + gam_entropy(apost,bpost); 22 | -------------------------------------------------------------------------------- /deprecated/gament.m: -------------------------------------------------------------------------------- 1 | function H = gament(a,b) 2 | warning('This function is deprecated') 3 | 4 | % a is shape, b is inverse scale 5 | % Entropy of a Gamma distribution. 6 | 7 | H = a - log(b) + gammaln(a) - (a-1).*psi(a); 8 | 9 | -------------------------------------------------------------------------------- /deprecated/gamentropy.m: -------------------------------------------------------------------------------- 1 | function H = gamentropy(a,b) 2 | warning('This function is deprecated') 3 | 4 | % H = entropy_gamma(a,b) 5 | % a is shape, b is inverse scale, that is, the mean is a/b. 6 | 7 | H = a - log(b) + gammaln(a) - (a-1).*psi(a); 8 | 9 | -------------------------------------------------------------------------------- /deprecated/gamkl.m: -------------------------------------------------------------------------------- 1 | function d = gamkl(x,logx,apost,bpost,aprior,bprior) 2 | warning('This function is deprecated') 3 | 4 | % KL divergence for Gamma distributions. 5 | % A lower bound for a Gamma distributed X: 6 | % p(X) = G(aprior,bprior) 7 | % q(X) = G(apost,bpost) 8 | % = x 9 | % = logx 10 | % 11 | % KL(q||p) = - = -H(q) - 12 | % where expectation is taken over q(X) and H() is entropy 13 | 14 | % Cost from prior 15 | d = -aprior*log(bprior) + gammaln(aprior) - (aprior-1)*logx + bprior*x; 16 | % Cost from q-posterior 17 | d = d - gament(apost,bpost); 18 | -------------------------------------------------------------------------------- /deprecated/kalman_filter.m: -------------------------------------------------------------------------------- 1 | % KALMAN_FILTER - Under construction.. 2 | 3 | function [X, CovX] = kalman_filter(measurement, ... 4 | transition_matrix, ... 5 | process_noise, ... 6 | measurement_model_matrix, ... 7 | measurement_noise, ... 8 | x, ... 9 | C) 10 | 11 | warning('This function is deprecated') 12 | 13 | error('Not ready yet..') 14 | return 15 | 16 | N = 0; 17 | D = 1000; 18 | 19 | X = zeros(D,N); 20 | if nargout >= 2 21 | CovX = zeros(D,D,10); 22 | end 23 | 24 | for n=1:N 25 | % Get the parameters 26 | A = transition_matrix(n); 27 | Q = process_noise(n); 28 | y = measurement(n); 29 | H = measurement_model_matrix(n); 30 | R = measurement_noise(n); 31 | 32 | % Prediction step 33 | x = A*x; 34 | C = A*C*A' + Q; 35 | 36 | % Update step 37 | if length(y) < D 38 | v = y - H*x; 39 | S = H*C*H' + R; 40 | K = C*H'/S; 41 | x = m + K*v; 42 | C = C - K*S*K'; 43 | else 44 | K = C*H'; 45 | L = chol(C + 46 | end 47 | 48 | % Store the distribution 49 | X(:,n) = x; 50 | if nargout >= 2 51 | CovX(:,:,n) = C; 52 | end 53 | end -------------------------------------------------------------------------------- /deprecated/kalman_filter_old.m: -------------------------------------------------------------------------------- 1 | % KALMAN_FILTER - Under construction.. 2 | 3 | function [X, CovX] = kalman_filter_old(measurement, ... 4 | transition_matrix, ... 5 | process_noise, ... 6 | measurement_model_matrix, ... 7 | measurement_noise, ... 8 | x, ... 9 | C) 10 | 11 | warning('This function is deprecated') 12 | 13 | error('Not ready yet..') 14 | return 15 | 16 | N = 0; 17 | D = 1000; 18 | 19 | X = zeros(D,N); 20 | if nargout >= 2 21 | CovX = zeros(D,D,10); 22 | end 23 | 24 | for n=1:N 25 | % Get the parameters 26 | A = transition_matrix(n); 27 | Q = process_noise(n); 28 | y = measurement(n); 29 | H = measurement_model_matrix(n); 30 | R = measurement_noise(n); 31 | 32 | % Prediction step 33 | x = A*x; 34 | C = A*C*A' + Q; 35 | 36 | % Update step 37 | if length(y) < D 38 | v = y - H*x; 39 | S = H*C*H' + R; 40 | K = C*H'/S; 41 | x = m + K*v; 42 | C = C - K*S*K'; 43 | else 44 | K = C*H'; 45 | L = chol(C + 46 | end 47 | 48 | % Store the distribution 49 | X(:,n) = x; 50 | if nargout >= 2 51 | CovX(:,:,n) = C; 52 | end 53 | end -------------------------------------------------------------------------------- /deprecated/mnorm_entropy.m: -------------------------------------------------------------------------------- 1 | function H = mnorm_entropy(U) 2 | warning('This function is deprecated') 3 | 4 | % H = mnorm_entropy(U) 5 | % 6 | % U is the Cholesky factor of a covariance matrix C, that is, a lower or 7 | % upper triangylar matrix such that C=U*U' or C=U'*U. 8 | 9 | d = size(U,1); 10 | 11 | H = d/2*log(2*pi) + 0.5*logdet_chol(U) + d/2; 12 | -------------------------------------------------------------------------------- /deprecated/mnorm_vbcost.m: -------------------------------------------------------------------------------- 1 | function c = mnorm_vbcost(x, L, mu, L_mu, L_K, logdet_K) 2 | warning('This function is deprecated') 3 | 4 | % C = MNORM_VBCOST(X, L, MU, L_MU, L_K, LOGDET_K) 5 | % 6 | % Evaluates C = -KL(q||p) = - where the expectation 7 | % <.> is taken over q. This is used for calculating the terms of the lower 8 | % bound of the log marginal likelihood in variational Bayesian inference. 9 | % 10 | % p(X) = N(M, K) 11 | % q(X) = N(X, L*L') 12 | % 13 | % Rest of the parameters are defined as: 14 | % q(M) = N(MU, L_MU * L_MU') 15 | % = inv(L_K * L_K') 16 | % = LOGDET_K 17 | % 18 | % If L==[], then the term is not calculated. 19 | % This is useful, for instance, when X is observations. 20 | 21 | % (c) 2010 Jaakko Luttinen 22 | 23 | 24 | lq = 0; 25 | d = length(x); 26 | 27 | %% Cost from approximate posterior 28 | 29 | if ~isempty(L) 30 | lq = mnorm_entropy(L); 31 | else 32 | L = 0; 33 | end 34 | 35 | %% Cost from prior 36 | 37 | % Use Cholesky of the prior covariance 38 | z = linsolve_tril(L_K, x-mu); 39 | V = linsolve_tril(L_K, L+L_mu) 40 | z2 = z'*z + traceprod(V, V); 41 | 42 | lp = - 0.5*logdet_K - 0.5*z2 - 0.5*d*log(2*pi); 43 | 44 | %% Total cost 45 | c = lp + lq; -------------------------------------------------------------------------------- /deprecated/mnormrnd.m: -------------------------------------------------------------------------------- 1 | 2 | % function y = mnormrnd(mu, Cov, n) 3 | function y = mnormrnd(mu, Cov, n) 4 | warning('This function is deprecated') 5 | 6 | 7 | m = max(length(mu), length(Cov)); 8 | 9 | opts.issym = true; 10 | opts.isreal = true; 11 | [V,D] = svd(Cov); 12 | D(D<0) = 0; 13 | D = sqrt(D); 14 | A = V * D; 15 | 16 | y = repmat(mu,1,n) + A*randn(m,n); -------------------------------------------------------------------------------- /deprecated/mvn_vbcost.m: -------------------------------------------------------------------------------- 1 | function c = mvn_vbcost(x,L,mu,L_mu,L_K,logdet_K) 2 | warning('This function is deprecated') 3 | 4 | % c = mvnvbcost(x,Covx,mu,Covmu,pCholCov,pLogDetCov) 5 | % 6 | % Calculates c = -KL(q||p) = - 7 | % where the expectation <.> is taken over q(X). This is used for 8 | % calculating the lower bound of the marginal loglikelihood in VB 9 | % models. 10 | % 11 | % p(X) = N(M,K) 12 | % q(X) = N(x,Covx) 13 | % 14 | % Rest of the parameters are defined as: 15 | % q(M) = N(mu,Covmu) 16 | % = inv(L_K * L_K') 17 | % = logdet_K 18 | % 19 | % If Covx==[], then the term is not calculated. 20 | % This is useful when X is, e.g., observations. 21 | 22 | 23 | c = 0; 24 | d = length(x); 25 | 26 | % Cost from q-posterior 27 | if ~isempty(L) 28 | c = mnorm_entropy(L); 29 | else 30 | L = 0; 31 | end 32 | 33 | % Use Cholesky of the prior covariance 34 | z = solve_tril(L_K, x-mu); 35 | 36 | % Cost from prior 37 | 38 | % Below: (x-mu)' * Cov^(-1) * (x-mu) + trace(Cov^(-1)*(Covx+Covmu)) 39 | V = linsolve_chol(L_K, L*L'+L_mu*L_mu') 40 | err2 = z'*z + trace(V, V); % TODO: Optimize! 41 | % $$$ err2 = z'*z + trace(solve_triu(pCholCov',solve_tril(pCholCov,Covx+Covmu))); 42 | c = c - 0.5*logdet_K - 0.5*err2 - 0.5*d*log(2*pi); 43 | -------------------------------------------------------------------------------- /deprecated/mvnentropy.m: -------------------------------------------------------------------------------- 1 | function H = mvnentropy(Cov, ischol) 2 | warning('This function is deprecated') 3 | 4 | % H = entropy_gaussian(Cov) 5 | % 6 | % Cov is covariance matrix, or optionally, an upper triangular matrix U such 7 | % that U'*U = Cov (Cholesky decomposition) which is marked ischol=true. The 8 | % method calculates the Cholesky decomposition if it is not given. 9 | 10 | % $$$ warning(['Function mvnentropy deprecated, start using gaussian_entropy (it uses' ... 11 | % $$$ ' different parameters)']) 12 | 13 | if nargin < 2 || ~ischol 14 | U = safechol(Cov, 1e-8); 15 | else 16 | U = Cov; 17 | end 18 | 19 | d = size(Cov,1); 20 | 21 | H = d/2*log(2*pi) + 0.5*logdetchol(U) + d/2; 22 | -------------------------------------------------------------------------------- /deprecated/mymvnrnd.m: -------------------------------------------------------------------------------- 1 | 2 | function x = mymvnrnd(mu, Cov) 3 | warning('This function is deprecated') 4 | 5 | 6 | if false %issparse(Cov) 7 | [L,p,S] = chol(Cov, 'lower'); 8 | x = S' * L * randn(rows(Cov),1) + mu; 9 | else 10 | L = chol(Cov, 'lower'); 11 | x = L * randn(rows(Cov),1) + mu; 12 | end -------------------------------------------------------------------------------- /deprecated/rows.m: -------------------------------------------------------------------------------- 1 | function d = rows(X) 2 | warning('This function is deprecated') 3 | 4 | 5 | d = size(X,1); -------------------------------------------------------------------------------- /deprecated/solve_tril.m: -------------------------------------------------------------------------------- 1 | function X = solve_tril(L, Y) 2 | warning('This function is deprecated') 3 | 4 | % function X = solve_tril(L, Y) 5 | % 6 | % Solves X = L \ Y, or L*X = Y, when L is lower triangular. 7 | 8 | if issparse(L) 9 | % $$$ if ~issparse(L) 10 | % $$$ error('hei') 11 | % $$$ end 12 | X = L \ Y; 13 | else 14 | opts.LT = true; 15 | X = linsolve(L, full(Y), opts); 16 | end 17 | -------------------------------------------------------------------------------- /deprecated/solve_triu.m: -------------------------------------------------------------------------------- 1 | function X = solve_tril(U, Y) 2 | warning('This function is deprecated') 3 | 4 | % function X = solve_tril(L, Y) 5 | % 6 | % Solves X = L \ Y, or L*X = Y, when L is lower triangular. 7 | 8 | if issparse(U) 9 | X = U \ Y; 10 | else 11 | opts.UT = true; 12 | X = linsolve(U, full(Y), opts); 13 | end 14 | -------------------------------------------------------------------------------- /deprecated/spinv_jaakko.m: -------------------------------------------------------------------------------- 1 | function Z = spinv(X) 2 | warning('This function is deprecated') 3 | 4 | % Calculates the sparse inverse of X, that is, only those elements (i,j) 5 | % of the inverse that are non-zero in X. Note, that the real inverse can 6 | % be full! ALSO, X should be symmetric!! What about positive definite? 7 | 8 | warning('Use Vanhatalo''s sinv.m'); 9 | 10 | if ~issparse(X) 11 | error('X should be sparse'); 12 | end 13 | 14 | [n,n] = size(X); 15 | %Z = spones(X);%spalloc(M,N, nnz(X)); 16 | Z = X; % copy sparsity 17 | 18 | L = chol(X, 'lower'); 19 | D = diag(L); 20 | %I = spones(L); 21 | %N = 22 | 23 | [J,I] = find(tril(X)); 24 | 25 | % $$$ for i=N:-1:1 26 | % $$$ for j=N:-1:i 27 | for k=length(I):-1:1 28 | i = I(k); 29 | j = J(k); 30 | Z(i,j) = (i==j)/D(i)^2 - 1/D(i) * L((i+1):n,i)'*Z((i+1):n,j); 31 | Z(j,i) = Z(i,j); 32 | end 33 | % $$$ end 34 | % $$$ end 35 | -------------------------------------------------------------------------------- /deprecated/split_data.m: -------------------------------------------------------------------------------- 1 | 2 | function [Y1, Y2] = split_data(Y, I) 3 | warning('This function is deprecated') 4 | 5 | 6 | Y1 = Y; 7 | Y2 = Y; 8 | 9 | Y1(~I) = nan; 10 | Y2(I) = nan; 11 | 12 | 13 | -------------------------------------------------------------------------------- /deprecated/studpdf.m: -------------------------------------------------------------------------------- 1 | function p = studpdf(x,mu,s2,nu) 2 | warning('This function is deprecated') 3 | 4 | % function p = studpdf(x,mu,s2,nu) 5 | 6 | p = 1; 7 | p = p .* gamma((nu+1)/2); 8 | p = p ./ gamma(nu/2); 9 | p = p ./ sqrt(nu.*pi.*s2); 10 | p = p .* (1 + 1./nu .* ((x-mu).^2) ./ s2) .^ (-(nu+1)/2); 11 | -------------------------------------------------------------------------------- /deprecated/t2_dlpdf.m: -------------------------------------------------------------------------------- 1 | function dy = t2_dlpdf(x2, nu) 2 | warning('This function is deprecated') 3 | 4 | % Derivative of the Student-t log-probability density function (lpdf) with 5 | % degrees of freedom NU. 6 | % 7 | % DY = T2_LPDF(X2, NU) 8 | % 9 | % DY is the derivative with respect to the degrees of freedom NU. 10 | % 11 | % X2 = ((T-MU)/SIGMA)^2 is the normalized squared error. 12 | 13 | b = (nu+1)/2; 14 | z = log(1 + 1./nu.*x2); 15 | 16 | dy = 0.5 * ( psi(b) - psi(nu/2) - 1./nu - z + (nu+1).*x2./ (nu.*(nu+x2)) ); 17 | 18 | 19 | -------------------------------------------------------------------------------- /deprecated/t2_lpdf.m: -------------------------------------------------------------------------------- 1 | % Student-t log-probability density function (lpdf) with degrees of 2 | % freedom NU. 3 | % 4 | % Y = T2_LPDF(X2, NU, D) 5 | % [Y, DY] = T2_LPDF(X2, NU, D) 6 | % 7 | % DY is the derivative with respect to the degrees of freedom NU. 8 | % 9 | % X2 is the squared values of a Student-t random variable with zero mean 10 | % and unit scale. 11 | % 12 | % or in the multivariate case: 13 | % 14 | % ... = T2_LPDF(..., D) 15 | % 16 | % where D is the dimensionality, and X2 is the squared norm of a 17 | % Student-t random variable with zero mean and unit scale. 18 | 19 | % Last modified 2010-06-03 20 | % Copyright (c) Jaakko Luttinen 21 | 22 | % X2 = ((T-MU)/SIGMA)^2 is the normalized squared error. 23 | % X2 = (T-MU)' * inv(COV) * (T-MU) 24 | 25 | function [y, dy] = t2_lpdf(x2, nu, D) 26 | warning('This function is deprecated') 27 | 28 | 29 | warning('Deprecated. Use T_LOGPDF instead.') 30 | 31 | if nargin < 3 32 | D = 1; 33 | end 34 | 35 | b = (nu+D)/2; 36 | z = log(1 + 1./nu.*x2); 37 | 38 | y = gammaln(b) - gammaln(nu/2) - D./2.*log(nu*pi) - b .* z; 39 | 40 | if nargout >= 2 41 | dy = 0.5*psi(b) ... 42 | - 0.5*psi(nu/2) ... 43 | - 0.5*D./nu ... 44 | - 0.5*z ... 45 | + b.*x2./(nu.*(nu+x2)); 46 | % $$$ dy = 0.5 * ( psi(b) - psi(nu/2) - D./nu - z + (nu+1).*x2./ (nu.*(nu+x2)) ); 47 | end 48 | 49 | -------------------------------------------------------------------------------- /deprecated/test_argparse.m: -------------------------------------------------------------------------------- 1 | 2 | function test_argparse 3 | warning('This function is deprecated') 4 | 5 | 6 | options = struct('Eka', 1, 'Toka', 2); 7 | 8 | % Simple test 9 | [opts, errmsg] = argparse(options, 'Toka', 3) 10 | 11 | % Test case sensitivity 12 | [opts, errmsg] = argparse(options, 'toka', 4) 13 | 14 | % Test unknown option with remainder 15 | [opts, errmsg, rem] = argparse(options, 'Uusi', 10) 16 | 17 | % Test unknown option without remainder 18 | [opts, errmsg] = argparse(options, 'Uusi', 10) 19 | 20 | myplot(1:10, (1:10).^2, 'Color', 'r', 'Title', 'Parabola'); 21 | 22 | myfun('Age', 99); 23 | 24 | function myplot(x, y, varargin) 25 | opts = struct('title', 'My magnificent plot'); 26 | [opts, errmsg, remopts] = argparse(opts, varargin{:}); 27 | error(errmsg); 28 | plot(x, y, remopts{:}); % pass the remaining parameters to 'plot' 29 | title(opts.title); 30 | 31 | function myfun(varargin) 32 | opts = struct('name', 'Foo', 'age', 42); % default values 33 | [opts, errmsg] = argparse(opts, varargin{:}); % parse custom values 34 | error(errmsg); % print errors 35 | fprintf('%s is %d years old.\n', opts.name, opts.age); 36 | -------------------------------------------------------------------------------- /deprecated/test_dist_earth.m: -------------------------------------------------------------------------------- 1 | function test_dist_earth 2 | warning('This function is deprecated') 3 | 4 | 5 | % Grid 6 | % $$$ lon = linspace(22,28,100); 7 | % $$$ lat = linspace(55,65,60); 8 | lon = linspace(-180,180,100); 9 | lat = linspace(-90,90,60); 10 | [LAT,LON] = meshgrid(lat,lon); 11 | 12 | % Distances from.. 13 | coord0 = [25; 60]; 14 | D = dist_earth([LON(:), LAT(:)]', coord0); 15 | D = reshape(D, [length(lon), length(lat)]); 16 | 17 | % Plot 18 | figure 19 | mapproj('global-ellipse') 20 | % $$$ mapproj('testbed') 21 | mapcolor(lon, lat, D); 22 | mapcoast; -------------------------------------------------------------------------------- /deprecated/test_nested_functions.m: -------------------------------------------------------------------------------- 1 | 2 | function [addk,getk,gets] = test_nested_functions() 3 | warning('This function is deprecated') 4 | 5 | 6 | k = 0; 7 | 8 | addk = @add_k; 9 | getk = @get_k; 10 | gets = @get_setter; 11 | 12 | function add_k() 13 | k = k + 1; 14 | end 15 | 16 | function y = get_k() 17 | y = k; 18 | end 19 | 20 | function f = get_setter(x) 21 | f = @set_k; 22 | function set_k() 23 | k = x; 24 | end 25 | end 26 | 27 | end -------------------------------------------------------------------------------- /deprecated/test_sample_kron.m: -------------------------------------------------------------------------------- 1 | function test_sample_kron 2 | warning('This function is deprecated') 3 | 4 | 5 | N1 = 200; 6 | x1 = 1:N1; 7 | K1 = gp_cov_pp(log(30), x1, x1); 8 | I1 = 3e-1*speye(N1); 9 | 10 | N2 = 100; 11 | x2 = 1:N2; 12 | K2 = gp_cov_pp(log(10), x2, x2); 13 | I2 = speye(N2); 14 | 15 | % 16 | % Simulate 1-D data 17 | % 18 | 19 | C1 = K1 + I1; 20 | L_C1 = chol(C1, 'lower'); 21 | y1 = L_C1 * randn(N1,1); 22 | 23 | clf 24 | subplot(2,1,1) 25 | 26 | %figure 27 | plot(x1,y1,'r+') 28 | 29 | % 30 | % Draw samples from the posterior 31 | % 32 | 33 | M = 1000; % number of samples 34 | 35 | z = randn(2*N1,M); 36 | O = zeros(N1,N1); 37 | L1 = chol(K1, 'lower'); 38 | f1 = [L1 O]*z - K1 * (C1 \ bsxfun(@minus, [L1 I1.^0.5]*z, y1)); 39 | 40 | hold on 41 | plot(x1, f1, 'b') 42 | 43 | mu1 = mean(f1,2); 44 | std1 = std(f1,1,2); 45 | 46 | % 47 | % Draw samples from the brute force posterior 48 | % 49 | 50 | mu = K1*inv(C1)*y1; 51 | V = K1 - K1*inv(C1)*K1; 52 | f2 = bsxfun(@plus, mu, chol(V,'lower')*randn(N1,M)); 53 | 54 | hold on 55 | plot(x1,f2, 'r') 56 | 57 | mu2 = mean(f2,2); 58 | std2 = std(f2,1,2); 59 | 60 | subplot(2,1,2) 61 | 62 | plot(x1, [mu1, mu1-2*std1, mu1+2*std1], 'b'); 63 | hold on 64 | plot(x1, [mu2, mu2-2*std2, mu2+2*std2], 'r'); -------------------------------------------------------------------------------- /deprecated/test_sample_kron2.m: -------------------------------------------------------------------------------- 1 | function test_sample_kron2 2 | warning('This function is deprecated') 3 | 4 | 5 | N1 = 1000; 6 | x1 = 1:N1; 7 | K1 = gp_cov_pp(log(30), x1, x1); 8 | I1 = 3e-1*speye(N1); 9 | 10 | N2 = 100; 11 | x2 = 1:N2; 12 | K2 = gp_cov_pp(log(10), x2, x2); 13 | I2 = speye(N2); 14 | 15 | % 16 | % Simulate 1-D data 17 | % 18 | 19 | C1 = K1 + I1; 20 | L_C1 = chol(C1, 'lower'); 21 | y1 = L_C1 * randn(N1,1); 22 | 23 | clf 24 | subplot(2,1,1) 25 | 26 | %figure 27 | plot(x1,y1,'r+') 28 | 29 | % 30 | % Draw samples from the posterior using C-G 31 | % 32 | 33 | M = 100; % number of samples 34 | 35 | % afun = @(x) reshape(kronprod(K1+I1,K2+I2,reshape(x,N1,N2)), N1*N2, 1); 36 | 37 | O = zeros(N1,N1); 38 | L1 = chol(K1, 'lower'); 39 | f1 = zeros(N1,M); 40 | tic 41 | for m=1:M 42 | z = randn(2*N1,1); 43 | f1(:,m) = [L1 O]*z - K1 * pcg(C1, [L1 I1.^0.5]*z - y1); 44 | end 45 | toc 46 | 47 | hold on 48 | plot(x1, f1, 'b') 49 | 50 | mu1 = mean(f1,2); 51 | std1 = std(f1,1,2); 52 | 53 | % 54 | % Draw samples from the brute force posterior 55 | % 56 | 57 | tic 58 | mu = K1*inv(C1)*y1; 59 | V = K1 - K1*inv(C1)*K1; 60 | f2 = bsxfun(@plus, mu, chol(V,'lower')*randn(N1,M)); 61 | toc 62 | 63 | hold on 64 | plot(x1,f2, 'r') 65 | 66 | mu2 = mean(f2,2); 67 | std2 = std(f2,1,2); 68 | 69 | subplot(2,1,2) 70 | 71 | plot(x1, [mu1, mu1-2*std1, mu1+2*std1], 'b'); 72 | hold on 73 | plot(x1, [mu2, mu2-2*std2, mu2+2*std2], 'r'); -------------------------------------------------------------------------------- /deprecated/test_solve_kron.m: -------------------------------------------------------------------------------- 1 | 2 | function test_solve_kron 3 | warning('This function is deprecated') 4 | 5 | 6 | N1 = 150; 7 | x1 = 1:N1; 8 | K1 = gp_cov_pp(log(10), x1, x1); 9 | I1 = speye(N1); 10 | 11 | N2 = 100; 12 | x2 = 1:N2; 13 | K2 = gp_cov_pp(log(10), x2, x2); 14 | I2 = speye(N2); 15 | 16 | % Full matrix approach 17 | %K = kron(K1, K2); 18 | %K_noise = kron(K1+I1, K2+I2); 19 | %K_posterior = K - K * (K_noise \ K); 20 | 21 | % 22 | % Solve: kron(K1+I1,K2+I2) \ kron(K1,K2) 23 | % 24 | 25 | %K = kron(K2,K1); 26 | %X_full = K * (kron(K2+I2,K1+I1) \ K); 27 | 28 | % Kronecker approach 29 | tic 30 | C1 = K1 * ((K1 + I1) \ K1); 31 | C2 = K2 * ((K2 + I2) \ K2); 32 | X_kron = diag(C1) * diag(C2)'; 33 | %X_kron = dot(K1,C1,1)' * dot(K2,C2,1); 34 | toc 35 | 36 | % Conjugate gradient approach 37 | % 38 | % Computing the variances is veeery expensive... 39 | tic 40 | afun = @(x) reshape(kronprod(K1+I1,K2+I2,reshape(x,N1,N2)), N1*N2, 1); 41 | X_cg = zeros(N1,N2); 42 | for n1=1:N1 43 | for n2=1:N2 44 | fprintf('Progress: %d %%\n', floor(100*n1/N1)); 45 | x = pcg(afun, kron(K2(:,n2),K1(:,n1)), [], 2); 46 | X_cg(n1,n2) = K1(n1,:) * reshape(x,N1,N2) * K2(:,n2); 47 | end 48 | end 49 | toc 50 | 51 | full([X_kron(:), X_cg(:)]) 52 | %full([X_kron(:), X_cg(:), diag(X_full)]) 53 | 54 | function Y = kronprod(A, B, X) 55 | % kron(B', A) 56 | Y = A*X*B; -------------------------------------------------------------------------------- /deprecated/test_t_ml.m: -------------------------------------------------------------------------------- 1 | function test_t_tml 2 | warning('This function is deprecated') 3 | 4 | 5 | n = 100; 6 | t2 = trnd(10,n,1).^2; 7 | 8 | nu = 0.1:0.1:50; 9 | 10 | y = zeros(size(nu)); 11 | for i=1:length(nu) 12 | y(i) = sum(t2_lpdf(t2,nu(i))); 13 | end 14 | 15 | nu_ml = t_ml(t2,0.1) 16 | 17 | figure 18 | plot(nu,y) 19 | -------------------------------------------------------------------------------- /discrete/ngroupsk.m: -------------------------------------------------------------------------------- 1 | % X = NGROUPSK(N, K) 2 | % 3 | % The number of ways N*K objects can be divided into N groups of equal size 4 | % (K objects per group). 5 | % 6 | % N = number of groups 7 | % K = size of the groups 8 | % 9 | % X = number of different group division combinations 10 | 11 | % Last modified 2011-01-28 12 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 13 | 14 | function x = groupsk(n, k) 15 | 16 | x = factorial(n.*k) ./ ( factorial(n) .* factorial(k).^n ); -------------------------------------------------------------------------------- /discrete/npairsk.m: -------------------------------------------------------------------------------- 1 | % X = NPAIRSK(N, K) 2 | % 3 | % Number of pair combinations for N objects to K pairs. 4 | % 5 | % For instance, 6 | % 7 | % npairsk(2,1) = 1 8 | % 9 | % because two elements can be divided into a pair in one way. 10 | % 11 | % npairsk(4,2) = 3 12 | % 13 | % because four elements (A,B,C,D) can be divided into two pairs in three 14 | % ways: [(A,B);(C,D)], [(A,C);(B,D)], [(A,D);(B,C)]. 15 | % 16 | % npairsk(3,1) = 3 17 | % 18 | % because [(A,B)], [(A,C)], [(B,C)]. 19 | 20 | % Last modified 2011-01-28 21 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 22 | 23 | function x = npairsk(n, k) 24 | 25 | x = factorial(n) ./ (factorial(2*k) .* factorial(n-2*k)) .* ngroupsk(k,2); -------------------------------------------------------------------------------- /discrete/ntuples.m: -------------------------------------------------------------------------------- 1 | % Y = NTUPLES(X) 2 | % 3 | % Forms tuples for vectors 0:X(1), 0:X(2), ..., 0:X(END). 4 | % 5 | % For instance, 6 | % 7 | % >> ntuples([1 0 2]) 8 | % 9 | % ans = 10 | % 11 | % 0 0 0 12 | % 0 0 1 13 | % 0 0 2 14 | % 1 0 0 15 | % 1 0 1 16 | % 1 0 2 17 | % 18 | % See TUPLES for more information. 19 | 20 | % Last modified 2011-01-28 21 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 22 | 23 | function Y = ntuples(x) 24 | 25 | if ~isvector(x) 26 | error('Input should be a vector'); 27 | end 28 | 29 | D = length(x); 30 | C = cell(length(x),1); 31 | 32 | for d=1:D 33 | C{d} = 0:x(d); 34 | end 35 | 36 | Y = tuples(C{:}); 37 | 38 | % $$$ % Total number of combinations 39 | % $$$ N = prod(x+1); 40 | % $$$ 41 | % $$$ Y = zeros(N,D); 42 | % $$$ 43 | % $$$ Y(:,D) = mod(0:(N-1),x(D)+1); 44 | % $$$ 45 | % $$$ for d=(D-1):-1:1 46 | % $$$ Y(:,d) = mod( floor((0:(N-1))/prod(x((d+1):end)+1)), x(d)+1 ); 47 | % $$$ end 48 | -------------------------------------------------------------------------------- /discrete/tuples.m: -------------------------------------------------------------------------------- 1 | % Y = TUPLES(X1, X2, X3, ...) 2 | % 3 | % Forms the complete set of tuples from the given vectors. 4 | % 5 | % For instance, 6 | % 7 | % >> tuples([1 2], 3, [4 5 6]) 8 | % 9 | % ans = 10 | % 11 | % 1 3 4 12 | % 1 3 5 13 | % 1 3 6 14 | % 2 3 4 15 | % 2 3 5 16 | % 2 3 6 17 | % 18 | % The number of input vectors can be arbitrary. 19 | 20 | % Copyright (c) 2010 Jaakko Luttinen 21 | 22 | function Y = tuples(varargin) 23 | 24 | % Number of lists 25 | D = length(varargin); 26 | 27 | N = 1; 28 | for d=1:D 29 | N = N * length(varargin{d}); 30 | end 31 | 32 | Y = zeros(N,D); 33 | 34 | N_d = 1; 35 | for d=D:-1:1 36 | l = length(varargin{d}); 37 | ind = mod( floor((0:(N-1))/N_d), l ) + 1; 38 | N_d = N_d * l; 39 | Y(:,d) = varargin{d}(ind); 40 | end -------------------------------------------------------------------------------- /distributions/cov2corr.m: -------------------------------------------------------------------------------- 1 | % Transforms a covariance matrix into a correlation matrix 2 | 3 | function C = cov2corr(C) 4 | 5 | S = diag(diag(C).^(-0.5)); 6 | C = S*C*S; 7 | -------------------------------------------------------------------------------- /distributions/gamma_dlogpdf.m: -------------------------------------------------------------------------------- 1 | % GAMMA_DLOGPDF - The derivative of the log pdf of a Gamma distribution. 2 | % 3 | % Computes the derivative of the logarithm of the Gamma probability density 4 | % function G(ALPHA|A,B), where ALPHA is the random variable, A is the shape 5 | % and B is the inverse scale (note the difference to MATLAB's built-in 6 | % functions). 7 | % 8 | % The function is called as 9 | % 10 | % D = GAMMA_DLOGPDF(ALPHA, A, B) 11 | % D = GAMMA_DLOGPDF(DALPHA, A, B, DLOG_ALPHA) 12 | 13 | % Last modified 2011-02-09 14 | % Copyright (c) Jaakko Luttinen 15 | 16 | function dlp = gamma_dlogpdf(alpha, a, b, dlog_alpha) 17 | %function dlp = gamma_dlogpdf(alpha, a, b) 18 | 19 | if nargin < 4 20 | inv_alpha = 1./alpha; 21 | dlp = (a-1).*inv_alpha - b; 22 | else 23 | dlp = (a-1).*dlog_alpha - b.*alpha; 24 | end 25 | -------------------------------------------------------------------------------- /distributions/gamma_entropy.m: -------------------------------------------------------------------------------- 1 | % GAMMA_ENTROPY - Computes the entropy of a Gamma distribution. 2 | % 3 | % H = GAMMA_ENTROPY(A, B) 4 | % 5 | % where A is the shape and B is the inverse scale (note the difference to 6 | % MATLAB's built-in functions). 7 | 8 | % Last modified 2010-06-07 9 | % Copyright (c) Jaakko Luttinen 10 | 11 | function H = gamma_entropy(a,b) 12 | 13 | H = a - log(b) + gammaln(a) - (a-1).*psi(a); 14 | 15 | -------------------------------------------------------------------------------- /distributions/gamma_logpdf.m: -------------------------------------------------------------------------------- 1 | % GAMMA_LOGPDF - The log pdf of a Gamma distribution. 2 | % 3 | % Computes the logarithm of the Gamma probability density function 4 | % G(ALPHA|A,B), where ALPHA is the random variable, A is the shape and B is 5 | % the inverse scale (note the difference to MATLAB's built-in functions). 6 | % 7 | % The function is called as 8 | % 9 | % L = GAMMA_LOGPDF(ALPHA, A, B) 10 | % L = GAMMA_LOGPDF(ALPHA, A, B, LOG_ALPHA) 11 | % 12 | % where LOG_ALPHA is LOG(ALPHA). Letting the user to evaluate this logarithm 13 | % allows greater flexibility (e.g., for variational Bayesian cost function 14 | % one can give the expectation of the logarithm). However, A and B are 15 | % assumed to be fixed parameters in order to keep the function simple. 16 | 17 | % Last modified 2010-06-07 18 | % Copyright (c) Jaakko Luttinen 19 | 20 | function [lp, dlp] = gamma_logpdf(alpha, a, b, log_alpha) 21 | 22 | if nargin < 4 23 | log_alpha = log(alpha); 24 | % $$$ if nargin == 4 25 | % $$$ error(['Check the order of your input variables - the function has been ' ... 26 | % $$$ 'changed!']) 27 | % $$$ end 28 | % $$$ else 29 | % $$$ error('Improper number of inputs') 30 | end 31 | 32 | lp = (a-1).*log_alpha - b.*alpha - gammaln(a) + a.*log(b); 33 | 34 | if nargout >= 2 35 | inv_alpha = 1./alpha; 36 | dlp = (a-1).*inv_alpha - b; 37 | end 38 | 39 | %lp(alpha<=0) = -inf; 40 | %dlp(alpha<=0) = nan; -------------------------------------------------------------------------------- /distributions/gaussian_dlogpdf_cov.m: -------------------------------------------------------------------------------- 1 | % GAUSSIAN_DLOGPDF_COV - The gradient of the log pdf of a multivariate 2 | % normal distribution with respect to the covariance 3 | % matrix. 4 | % 5 | % Computes the gradient of the logarithm of the multivariate normal 6 | % probability density function N(Y|MU,COV), where Y is the random variable, 7 | % MU is the mean and COV is the covariance. The gradient is evaluated with 8 | % respect to the covariance matrix COV and returned in matrix form. 9 | % 10 | % d(LOG(N(Y|MU,COV))) / dCOV 11 | % 12 | % Usage: 13 | % 14 | % DX = GAUSSIAN_DLOGPDF_COV(INVCOV_Y, INVCOV_MU, INVCOV) 15 | % 16 | % where 17 | % 18 | % INVCOV_Y : INV(COV)*Y 19 | % INVCOV_MU : INV(COV)*MU 20 | % INVCOV : INV(COV) 21 | % 22 | % Letting the user to compute the terms allows greater efficiency and 23 | % flexibility. 24 | 25 | % Last modified 2010-11-19 26 | % Copyright (c) Jaakko Luttinen 27 | 28 | function dx = gaussian_dlogpdf_cov(invCov_y, invCov_mu, invCov) 29 | 30 | % $$$ if nargin == 3 31 | % $$$ y = varargin{1}; 32 | % $$$ mu = varargin{2}; 33 | % $$$ L = varargin{3}; 34 | % $$$ invCov_y = linsolve_chol(L, y, 'lower'); 35 | % $$$ invCov_mu = linsolve_chol(L, mu, 'lower'); 36 | % $$$ invCov = linsolve_chol(L, eye(size(L)), 'lower'); 37 | % $$$ end 38 | 39 | dx = - 0.5 * invCov ... 40 | + 0.5 * (invCov_y * invCov_y' - ... 41 | invCov_y * invCov_mu' - ... 42 | invCov_mu * invCov_y' + ... 43 | invCov_mu * invCov_mu'); 44 | 45 | 46 | -------------------------------------------------------------------------------- /distributions/gaussian_entropy.m: -------------------------------------------------------------------------------- 1 | % GAUSSIAN_ENTROPY - Computes the entropy of a multivariate normal 2 | % distribution with covariance SIGMA. 3 | % 4 | % H = GAUSSIAN_ENTROPY(Y, D) 5 | % 6 | % Y is the log-determinant of the covariance matrix, i.e., LOG(DET(SIGMA)). 7 | % However, it should be computed more efficiently using, e.g., LOGDET_COV. 8 | % 9 | % D is the dimensionality of the distribution. 10 | % 11 | % Also note that the function is linear with respect to its arguments. Thus, 12 | % some efficiency may be obtained for a large set of distributions by 13 | % summing the terms and calling this function only once with scalar 14 | % arguments. 15 | % 16 | % See also LOGDET_COV. 17 | 18 | % Last modified 2010-06-09 19 | % Copyright (c) Jaakko Luttinen 20 | 21 | function H = gaussian_entropy(logdet_Cov, D) 22 | 23 | H = 0.5 * (D*log(2*pi) + logdet_Cov + D); -------------------------------------------------------------------------------- /distributions/gaussian_rand.m: -------------------------------------------------------------------------------- 1 | 2 | function X = gaussian_rand(mu, Cov, N, varargin) 3 | 4 | options = struct('type', 'cov'); 5 | [options, errmsg] = argparse(options, varargin{:}); 6 | error(errmsg); 7 | 8 | if nargin < 3 9 | N = size(mu,2); 10 | end 11 | 12 | D = size(mu,1); 13 | 14 | Z = randn(D,N); 15 | switch options.type 16 | case 'cov' 17 | U = chol(Cov); 18 | Z = U' * Z; 19 | case 'invcov' 20 | U = chol(Cov); 21 | Z = linsolve_triu(U, Z); 22 | case 'cov-lchol' 23 | L = Cov; 24 | Z = L * Z; 25 | case 'invcov-lchol' 26 | Z = linsolve_triu(Cov, Z, true); 27 | otherwise 28 | error('Unknown type') 29 | end 30 | X = bsxfun(@plus, Z, mu); -------------------------------------------------------------------------------- /distributions/gaussian_rand_kron.m: -------------------------------------------------------------------------------- 1 | 2 | function y = gaussian_rand_kron(L1,L2) 3 | n1 = size(L1,2); 4 | n2 = size(L2,2); 5 | y = kronprod(L1,L2,randn(n2,n1)); -------------------------------------------------------------------------------- /distributions/l1_rand.m: -------------------------------------------------------------------------------- 1 | 2 | function x = l1_rand(mu,sigma,M) 3 | error('this function was just for testing') 4 | nu = 1; 5 | u = gamrnd(nu*1,2/nu,M); 6 | mean_invu = mean(1./u) 7 | x = normrnd(mu,sigma.*u,M); 8 | -------------------------------------------------------------------------------- /distributions/laplace_logpdf.m: -------------------------------------------------------------------------------- 1 | % LAPLACE_LOGPDF - The log pdf of Laplace distribution. 2 | % 3 | % pdf = lambda/2 * exp(-lambda*abs(x-mu)) 4 | % 5 | % x = laplace_logpdf(x, mu, lambda) 6 | % x = laplace_logpdf(x_lambda_mu, log_lambda) 7 | 8 | % The function is called as: 9 | % 10 | % L = GAUSSIAN_LOGPDF(Y_INVCOV_Y, Y_INVCOV_MU, MU_INVCOV_MU, LOGDET_COV, D) 11 | % 12 | % where 13 | % 14 | % Y_INVCOV_Y : Y'*INV(COV)*Y 15 | % Y_INVCOV_MU : Y'*INV(COV)*MU 16 | % MU_INVCOV_MU : MU'*INV(COV)*MU 17 | % LOGDET_COV : LOG(DET(COV)) 18 | % D : the dimensionality of the distribution 19 | % 20 | % However, these terms should be computed more efficiently than using INV 21 | % and LOG(DET(..)). 22 | % 23 | % Letting the user to compute the terms allows greater efficiency and 24 | % flexibility. 25 | % 26 | % Also note that the function is linear with respect to its arguments. Thus, 27 | % some efficiency may be obtained for a large set of pdfs by summing the 28 | % terms and calling this function only once with scalar arguments. 29 | % 30 | % Usage: 31 | % 32 | % X = GAUSSIAN_LOGPDF(Y, MU, L) 33 | % 34 | % X = GAUSSIAN_LOGPDF(Y_INVCOV_Y, Y_INVCOV_MU, MU_INVCOV_MU, LOGDET_COV, D) 35 | 36 | % Last modified 2010-06-04 37 | % Copyright (c) Jaakko Luttinen 38 | 39 | function lp = laplace_logpdf(x, mu, lambda) 40 | 41 | if nargin == 3 42 | x_lambda_mu = lambda * abs(x-mu); 43 | log_lambda = log(lambda); 44 | else 45 | x_lambda_mu = x; 46 | log_lambda = mu; 47 | end 48 | 49 | lp = log_lambda - log(2) - x_lambda_mu; 50 | 51 | 52 | -------------------------------------------------------------------------------- /distributions/lognormal_logpdf.m: -------------------------------------------------------------------------------- 1 | % LOGNORMAL_LOGPDF 2 | % 3 | % Y = LOGNORMAL_LOGPDF(X) 4 | % Y = LOGNORMAL_LOGPDF(X, MU, SIGMA) 5 | % 6 | % MU is the location parameter in (-INF, INF), default 0 7 | % SIGMA is the scale parameter in (0, INF) 8 | 9 | % Last modified 2010-11-12 10 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 11 | 12 | function y = lognormal_logpdf(x, varargin) 13 | 14 | if nargin <= 3 15 | if nargin == 1 16 | mu = 0; 17 | sigma = 1; 18 | else 19 | mu = varargin{1}; 20 | sigma = varargin{2}; 21 | end 22 | logx = log(x); 23 | y = -0.5*log(2*pi) ... 24 | -logx ... 25 | -log(sigma) ... 26 | -0.5*((logx-mu)./sigma).^2; 27 | end 28 | -------------------------------------------------------------------------------- /distributions/lognormal_rand.m: -------------------------------------------------------------------------------- 1 | % LOGNORMAL_RAND 2 | % 3 | % X = LOGNORMAL_RAND(MU, SIGMA) 4 | % 5 | % MU is the location parameter in (-INF, INF) 6 | % SIGMA is the scale parameter in (0, INF) 7 | % 8 | % Function calls 9 | % X = LOGNORMAL_RAND(MU,SIGMA,M,N,...) 10 | % X = LOGNORMAL_RAND(MU,SIGMA,[M,N,...]) 11 | % return an M-by-N-by-... array. 12 | % 13 | % See also LOGNORMAL_LOGPDF, NORMRND. 14 | 15 | % Last modified 2010-11-12 16 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 17 | 18 | function x = lognormal_rand(varargin) 19 | 20 | x = exp(normrnd(varargin{:})); -------------------------------------------------------------------------------- /distributions/mstud_lpdf.m: -------------------------------------------------------------------------------- 1 | function p = mstud_lpdf(x, mu, Cov, nu) 2 | % p = mstud_lpdf(x, mu, Cov, nu) 3 | 4 | D = length(x); 5 | 6 | x = x(:); 7 | mu = mu(:); 8 | 9 | invCov = inv(Cov); 10 | 11 | p = gammaln((D+nu)/2); 12 | p = p - gammaln(nu/2); 13 | p = p - D/2 * log(nu * pi); 14 | p = p - 0.5 * log(det(Cov)); 15 | p = p + -(D+nu)/2 * log(1 + 1/nu*(x-mu)'*invCov*(x-mu)); -------------------------------------------------------------------------------- /distributions/normal_logpdf.m: -------------------------------------------------------------------------------- 1 | % NORMAL_LOGPDF 2 | % 3 | % Y = NORMAL_LOGPDF(X) 4 | % Y = NORMAL_LOGPDF(X, MU, SIGMA) 5 | % 6 | % MU is the location parameter in (-INF, INF), default 0 7 | % SIGMA is the scale parameter in (0, INF) 8 | 9 | % Last modified 2010-11-12 10 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 11 | 12 | function y = lognormal_logpdf(x, varargin) 13 | 14 | if nargin <= 3 15 | if nargin == 1 16 | mu = 0; 17 | sigma = 1; 18 | else 19 | mu = varargin{1}; 20 | sigma = varargin{2}; 21 | end 22 | y = -0.5*log(2*pi) ... 23 | -log(sigma) ... 24 | -0.5*((x-mu)./sigma).^2; 25 | end 26 | -------------------------------------------------------------------------------- /distributions/t_logpdf.m: -------------------------------------------------------------------------------- 1 | % T_LOGPDF - The log pdf of a multivariate Student-t distribution. 2 | % 3 | % Computes the logarithm of the (multivariate) Student-t probability density 4 | % function STUDENT-T(Y|MU,COV,NU), where Y is the random variable, MU is the 5 | % location, COV is the scale matrix and NU is the degrees of freedom. 6 | % 7 | % The function is called as: 8 | % 9 | % L = T_LOGPDF(X2, LOGDET_COV, NU, D) 10 | % [L, DL] = T_LOGPDF(X2, LOGDET_COV, NU, D) 11 | % 12 | % where 13 | % 14 | % X2 : (Y-MU)'*INV(COV)*(Y-MU) 15 | % LOGDET_COV : LOG(DET(COV)) 16 | % NU : the degrees of freedom 17 | % D : the dimensionality of the distribution (default is 1) 18 | % 19 | % L : LOG(STUDENT-T(Y|MU,COV,NU)) 20 | % DL : the derivative of L with respect to NU (optional) 21 | 22 | % Last modified 2010-06-08 23 | % Copyright (c) Jaakko Luttinen 24 | 25 | function [l, dl] = t_logpdf(x2, logdet_Cov, nu, D) 26 | 27 | if nargin < 3 28 | D = 1; 29 | end 30 | 31 | lognu = log(nu); 32 | logx2 = log(x2); 33 | 34 | b = (nu+D)/2; 35 | z = log(1 + exp(-lognu+logx2)); 36 | % z = log(1 + 1./nu.*x2); 37 | 38 | l = gammaln(b) - gammaln(nu/2) - 0.5*logdet_Cov - D./2.*(lognu+log(pi)) - b .* z; 39 | 40 | if nargout >= 2 41 | dl = 0.5*psi(b) ... 42 | - 0.5*psi(nu/2) ... 43 | - 0.5*D./nu ... 44 | - 0.5*z ... 45 | + exp( log(b) + logx2 - (lognu + log(nu+x2)) ); 46 | % + b.*x2./(nu.*(nu+x2)); 47 | end 48 | 49 | -------------------------------------------------------------------------------- /distributions/t_lpdf.m: -------------------------------------------------------------------------------- 1 | function [y, dy] = t2_lpdf(x2, nu) 2 | % Student-t log-probability density function (lpdf) with degrees of 3 | % freedom NU. 4 | % 5 | % Y = T2_LPDF(X2, NU) 6 | % [Y, DY] = T2_LPDF(X2, NU) 7 | % 8 | % DY is the derivative with respect to the degrees of freedom NU. 9 | % 10 | % X2 = ((T-MU)/SIGMA)^2 is the normalized squared error. 11 | 12 | error('Not in use?') 13 | 14 | b = (nu+1)/2; 15 | z = log(1 + 1./nu.*x2); 16 | 17 | y = gammaln(b) - gammaln(nu/2) - 0.5*log(nu*pi) - (nu+1)/2 .* z; 18 | 19 | disp('debuggin too') 20 | if nargout >= 2 21 | disp('debuggin') 22 | dy = 0.5 * ( psi(b) - psi(nu/2) - 1./nu - z + (nu+1).*x2./ (nu.*(nu+x2)) ); 23 | end 24 | 25 | -------------------------------------------------------------------------------- /distributions/t_ml.m: -------------------------------------------------------------------------------- 1 | % T_ML - ML estimation of the degrees of freedom of a multivariate Student-t 2 | % distribution. 3 | % 4 | % Finds the maximum likelihood estimate for the degrees of freedom parameter 5 | % of a (multivariate) Student-t probability density function 6 | % STUDENT-T(Y|MU,COV,NU), where Y is the random variable, MU is the 7 | % location, COV is the scale matrix and NU is the degrees of freedom. 8 | % 9 | % The function is called as: 10 | % 11 | % NU = T_ML(X2, NU_INIT, D) 12 | % 13 | % where 14 | % 15 | % X2 : (Y-MU)'*INV(COV)*(Y-MU) 16 | % NU_INIT : the initial guess for NU 17 | % D : the dimensionality of the distribution 18 | % 19 | % NU : the maximum likelihood estimate 20 | 21 | % Last modified 2010-06-08 22 | % Copyright (c) Jaakko Luttinen 23 | 24 | function nu = t_ml(x2, nu, D) 25 | 26 | if nargin < 3 27 | D = 1; 28 | end 29 | 30 | %mycheckgrad(@cost, log(nu), 1e-4); 31 | 32 | options = optimset('GradObj','on','Display','off','MaxIter',10); 33 | nu = exp( fminunc(@cost, log(nu), options) ); 34 | 35 | % $$$ % DEBUG STUFF 36 | % $$$ lognuh = -2:0.1:4; 37 | % $$$ c = zeros(size(lognuh)); 38 | % $$$ dc = zeros(size(lognuh)); 39 | % $$$ for i=1:length(c) 40 | % $$$ [c(i),dc(i)] = cost(lognuh(i)); 41 | % $$$ end 42 | % $$$ figure(100) 43 | % $$$ plot(lognuh, c) 44 | % $$$ hold on 45 | % $$$ plot(lognuh, dc, 'r') 46 | % $$$ hold off 47 | % $$$ 48 | 49 | function [y, dy] = cost(lognu) 50 | nu = exp(lognu); 51 | [y,dy] = t_logpdf(x2, 0, nu, D); 52 | y = -sum(y); 53 | dy = -sum(dy)*nu; 54 | end 55 | 56 | end -------------------------------------------------------------------------------- /fa/noise_module_fixed.m: -------------------------------------------------------------------------------- 1 | function module = noise_module_fixed(M, N, tau) 2 | 3 | %Tau = bsxfun(@times, tau, ones(M,N)); 4 | 5 | module.initialize = @initialize; 6 | module.update = @update; 7 | module.get_struct = @get_struct; 8 | 9 | function S = get_struct() 10 | S.module = 'noise_module_fixed'; 11 | S.tau = tau; 12 | S.Tau = bsxfun(@times, tau, ones(M,N)); 13 | S.prior = []; 14 | S.init = []; 15 | S.options = []; 16 | end 17 | 18 | function [Tau, LogTau, KL_Tau] = update(iter, E2, Obs) 19 | 20 | I = ones(M,N); 21 | Tau = bsxfun(@times, tau, I); 22 | LogTau = bsxfun(@times, log(tau), I); 23 | KL_Tau = 0; 24 | 25 | end 26 | 27 | end 28 | 29 | -------------------------------------------------------------------------------- /fa/noise_module_product.m: -------------------------------------------------------------------------------- 1 | function module = noise_module_product(noise1, noise2) 2 | 3 | % module.initialize = @initialize; 4 | module.update = @update; 5 | module.get_struct = @get_struct; 6 | 7 | noise1_struct = noise1.get_struct(); 8 | noise2_struct = noise2.get_struct(); 9 | Tau1 = noise1_struct.Tau; 10 | Tau2 = noise2_struct.Tau; 11 | 12 | % $$$ Tau1 = []; 13 | % $$$ Tau2 = []; 14 | 15 | % $$$ function [Tau] = initialize(M, N) 16 | % $$$ Tau1 = noise1.initialize(M,N); 17 | % $$$ Tau2 = noise2.initialize(M,N); 18 | % $$$ Tau = Tau1 .* Tau2; 19 | % $$$ end 20 | 21 | function S = get_struct() 22 | S.module = 'noise_module_product'; 23 | S.noise1_struct = noise1.get_struct(); 24 | S.noise2_struct = noise2.get_struct(); 25 | S.Tau = S.noise1_struct.Tau .* S.noise2_struct.Tau; 26 | end 27 | 28 | function [Tau, LogTau, KL_Tau] = update(iter, E2, Obs) 29 | 30 | [Tau1, LogTau1, KL_Tau1] = noise1.update(iter, Tau2 .* E2, Obs); 31 | [Tau2, LogTau2, KL_Tau2] = noise2.update(iter, Tau1 .* E2, Obs); 32 | 33 | Tau = Tau1 .* Tau2; 34 | LogTau = LogTau1 + LogTau2; 35 | KL_Tau = KL_Tau1 + KL_Tau2; 36 | 37 | end 38 | 39 | end 40 | 41 | -------------------------------------------------------------------------------- /fa/old_noise_module_fixed.m: -------------------------------------------------------------------------------- 1 | function module = noise_module_fixed(tau) 2 | 3 | module.initialize = @initialize; 4 | module.update = @update; 5 | module.get_struct = @get_struct; 6 | 7 | function [Tau] = initialize(M,N) 8 | Tau = bsxfun(@times, tau, ones(M,N)); 9 | end 10 | 11 | function S = get_struct() 12 | S.module = 'noise_module_fixed'; 13 | S.tau = tau; 14 | end 15 | 16 | function [Tau, LogTau, KL_Tau] = update(iter, E2, Obs) 17 | 18 | I = ones(size(Obs)); 19 | Tau = bsxfun(@times, tau, I); 20 | LogTau = bsxfun(@times, log(tau), I); 21 | KL_Tau = 0; 22 | 23 | end 24 | 25 | end 26 | 27 | -------------------------------------------------------------------------------- /fa/old_noise_module_product.m: -------------------------------------------------------------------------------- 1 | function module = noise_module_product(noise1, noise2) 2 | 3 | module.initialize = @initialize; 4 | module.update = @update; 5 | module.get_struct = @get_struct; 6 | 7 | Tau1 = []; 8 | Tau2 = []; 9 | 10 | function [Tau] = initialize(M, N) 11 | Tau1 = noise1.initialize(M,N); 12 | Tau2 = noise2.initialize(M,N); 13 | Tau = Tau1 .* Tau2; 14 | end 15 | 16 | function S = get_struct() 17 | S.module = 'noise_module_product'; 18 | S.noise1 = noise1.get_struct(); 19 | S.noise2 = noise2.get_struct(); 20 | end 21 | 22 | function [Tau, LogTau, KL_Tau] = update(iter, E2, Obs) 23 | 24 | [Tau1, LogTau1, KL_Tau1] = noise1.update(iter, Tau2 .* E2, Obs); 25 | [Tau2, LogTau2, KL_Tau2] = noise2.update(iter, Tau1 .* E2, Obs); 26 | 27 | Tau = Tau1 .* Tau2; 28 | LogTau = LogTau1 + LogTau2; 29 | KL_Tau = KL_Tau1 + KL_Tau2; 30 | 31 | end 32 | 33 | end 34 | 35 | -------------------------------------------------------------------------------- /fa/test_comparison_to_scipy.m: -------------------------------------------------------------------------------- 1 | function test_comparison_to_scipy 2 | 3 | rand('state', 10) 4 | randn('state', 10) 5 | 6 | %M = 100 7 | %N = 200 8 | %D = 5 9 | M = 200 10 | N = 2000 11 | D = 20 12 | 13 | W = randn(D,M); 14 | X = randn(D,N); 15 | F = W'*X; 16 | Y = F + 1*randn(M,N); 17 | 18 | save('/home/jluttine/matlab/fa/data_pca_01_f.txt', 'F', '-ascii') 19 | save('/home/jluttine/matlab/fa/data_pca_01_y.txt', 'Y', '-ascii') 20 | save('/home/jluttine/matlab/fa/data_pca_01_d.txt', 'D', '-ascii') 21 | 22 | Y(:,21:40) = NaN; 23 | 24 | W_module = factor_module_iid(D,M); 25 | X_module = factor_module_iid(D,N); 26 | noise_module = noise_module_isotropic(M,N, ... 27 | 'prior', struct('a_tau', 1e-5, ... 28 | 'b_tau', 1e-5)); 29 | 30 | Q = vbfa(D, Y, W_module, X_module, noise_module, ... 31 | 'rotate', false, ... 32 | 'maxiter', 200); 33 | 34 | wx = Q.W'*Q.X; 35 | WW = bsxfun(@times, reshape(Q.W,[D,1,M]), reshape(Q.W,[1,D,M])) + Q.CovW; 36 | XX = bsxfun(@times, reshape(Q.X,[D,1,N]), reshape(Q.X,[1,D,N])) + Q.CovX; 37 | wwxx = reshape(WW, [D*D,M])' * reshape(XX, [D*D,N]); 38 | err_wx = 2*sqrt(wwxx - wx.^2); 39 | 40 | tserrorplot(wx', err_wx') 41 | addtsplot(W'*X, 'g') 42 | 43 | %Q.Tau 44 | 45 | %Q.CovX 46 | %Q.CovW -------------------------------------------------------------------------------- /fa/test_vbpca_old.m: -------------------------------------------------------------------------------- 1 | function Q = test_vbpca(seed) 2 | 3 | if nargin >= 1 4 | rand('state', seed); 5 | randn('state', seed); 6 | end 7 | 8 | D = 4; 9 | M = 100; 10 | N = 200; 11 | 12 | X = randn(D,N); 13 | W = diag(linspace(1,0.1,D))*randn(D,M); 14 | 15 | Y = W'*X + 4; 16 | Yn = Y + 0.1*randn(M,N); 17 | 18 | % Estimate D+1 components 19 | 20 | % PCA module (one constant component for bias modelling) 21 | prior.mu = [zeros(D,1); 1]; 22 | prior.CovX = diag([ones(D,1); 1e-8]); 23 | X_module = factor_module_iid(D+1,N,'prior',prior); % PCA module 24 | 25 | % ARD module 26 | W_module = factor_module_ard(D+1,M); 27 | 28 | % Isotropic noise 29 | noise_module = noise_module_isotropic(M, N, 1e-3, 1e-3, 'init', 100); 30 | 31 | Q = vbfa(D+1, Yn, W_module, X_module, noise_module, ... 32 | 'maxiter', 50, ... 33 | 'rotate', true, ... 34 | 'rotation_checkgrad', false, ... 35 | 'rotation_maxiter', 30); 36 | 37 | noise_std = Q.Tau(1)^(-0.5) 38 | 39 | %tsplot(Q.X) 40 | %tsplot(Q.W) 41 | 42 | figure 43 | plot(Q.loglikelihood) 44 | 45 | recon_error = rmse(Y, Q.W'*Q.X) 46 | 47 | tsplot(Q.X); 48 | tsplot(Q.W); 49 | -------------------------------------------------------------------------------- /fa/vbfa_probtest.m: -------------------------------------------------------------------------------- 1 | % Compute the probability of test data for a trained VBFA model 2 | % Usage: 3 | % [ prob, rms, Yrec, Ystd ] = vbfa_testprob( Q, Ytest ) 4 | % [ prob, rms, Yrec, Ystd ] = vbfa_testprob( 'filename', Ytest ) 5 | function [ prob, rms, Yrec, Ystd ] = vbfa_probtest( Q, Ytest ) 6 | 7 | % Check that the test data coverage is different from the training 8 | % data coverage 9 | ObsTest = ~isnan(Ytest); 10 | 11 | % Compute the reconstructions for the test data 12 | Yrec = W'*X; 13 | 14 | % Compute RMS 15 | 16 | % Compute the probability of test data given the training data 17 | 18 | % Compute the error bars 19 | -------------------------------------------------------------------------------- /geometry/dist.m: -------------------------------------------------------------------------------- 1 | % D = DIST(X1, X2) 2 | % 3 | % Returns a matrix D of pair-wise distances between X1 and X2. 4 | % 5 | % X1 is K x M matrix 6 | % X2 is K x N matrix 7 | % 8 | % K is the dimensionality, M and N are the number of samples. The resulting 9 | % matrix D is M x N matrix. 10 | % 11 | % Optional arguments: 12 | % 13 | % 'distfunc' : uses the given distance measure between the vectors 14 | % (default is the second norm). 15 | 16 | % Copyright (c) 2010 Jaakko Luttinen 17 | 18 | function D = dist(X1, X2, varargin) 19 | 20 | % Default parameters 21 | options = struct( ... 22 | 'distfunc', @(z1,z2) norm(z1-z2)); % distance measure 23 | 24 | % Check arguments 25 | options = argget( options, varargin{:} ); 26 | 27 | % Evaluate pair-wise distances 28 | n1 = size(X1,2); 29 | n2 = size(X2,2); 30 | D = zeros(n1,n2); 31 | for i=1:n1 32 | for j=1:n2 33 | D(i,j) = options.distfunc(X1(:,i),X2(:,j)); 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /geometry/geographic_to_euclidean.m: -------------------------------------------------------------------------------- 1 | % GEOGRAPHIC_TO_EUCLIDEAN - Transforms geographic coordinates to Euclidean 2 | % 3 | % X = GEOGRAPHIC_TO_EUCLIDEAN(L, R) 4 | % 5 | % L is a 2xN matrix of geographic coordinates (longitude,latitude). 6 | % R is the radius (default: spherical Earth radius 6371 kilometers) 7 | % X is a 3xN matrix of Euclidean coordinates (x,y,z) 8 | 9 | % Last modified 2010-12-17 10 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 11 | 12 | function X = geographic_to_euclidean(L, R) 13 | 14 | if nargin < 2 15 | % Earth radius 16 | R = 6371; 17 | end 18 | 19 | X = zeros(3, size(L,2)); 20 | 21 | X(1,:) = R .* cosd(L(2,:)) .* cosd(L(1,:)); 22 | X(2,:) = R .* cosd(L(2,:)) .* sind(L(1,:)); 23 | X(3,:) = R .* sind(L(2,:)); 24 | 25 | -------------------------------------------------------------------------------- /geometry/interpolate.m: -------------------------------------------------------------------------------- 1 | % YH = INTERPOLATE(D, Y, DH) 2 | % 3 | % Interpolates the values of Y based on given distances. 4 | % 5 | % D : N x N matrix of distances between the given targets Y. 6 | % Y : N x 1 vector of function values. 7 | % DH : M x N matrix of distances between the interpolated and given targets. 8 | % 9 | % Returns: 10 | % 11 | % YH : M x 1 vector of interpolated targets. 12 | % 13 | % Optional arguments: 14 | % 15 | % 'method' : 'nn' means nearest neighbour interpolation (default). 16 | % 'rbf' means linear radial basis function interpolation. 17 | 18 | % Copyright (c) 2010 Jaakko Luttinen 19 | 20 | function yh = interpolate(D, y, Dh, varargin) 21 | 22 | options = struct( ... 23 | 'method', 'nn'); 24 | 25 | % Get optional arguments 26 | [options, errmsg] = argparse(options, varargin{:}); 27 | error(errmsg); 28 | 29 | if strcmpi(options.method, 'nn') 30 | 31 | %% Nearest neighbour interpolation 32 | 33 | [tmp,ind] = min(Dh,[],2); 34 | yh = y(ind); 35 | 36 | elseif strcmpi(options.method, 'rbf') 37 | 38 | %% Linear radial basis functions interpolation 39 | 40 | mu = mean(y); 41 | y = y - mu; 42 | 43 | yh = Dh * (D\y) + mu; 44 | 45 | else 46 | 47 | error('Unknown interpolation method requested.'); 48 | 49 | end 50 | -------------------------------------------------------------------------------- /geometry/lonlat2standard.m: -------------------------------------------------------------------------------- 1 | % 2 | % [lon, lat] = lonlat2standard(lon, lat) 3 | % [coord] = lonlat2standard(coord) 4 | function [lon, lat] = lonlat2standard(lon, lat) 5 | 6 | if nargin == 1 7 | lat = lon(2,:); 8 | lon = lon(1,:); 9 | end 10 | 11 | lon = mod(lon, 360); 12 | lat = mod(lat, 360); 13 | 14 | %% Latitude to interval [-90,90] 15 | 16 | % first to interval [-180, 180] 17 | ind = lat>180; 18 | lat(ind) = lat(ind) - 360; 19 | % then to interval [-90, 180] 20 | ind = lat<-90; 21 | lon(ind) = mod(lon(ind)+180, 360); 22 | lat(ind) = -180 - lat(ind); 23 | % and finally to interval [-90,90] 24 | ind = lat>90; 25 | lon(ind) = mod(lon(ind)+180, 360); 26 | lat(ind) = 180 - lat(ind); 27 | 28 | %% Longitude to interval [-180, 180] 29 | 30 | lon(lon>180) = lon(lon>180) - 360; 31 | 32 | if nargin == 1 && nargout == 1 33 | lon = [lon(:)'; lat(:)']; 34 | end -------------------------------------------------------------------------------- /gp/gp_cov_delta.m: -------------------------------------------------------------------------------- 1 | 2 | function covfunc = gp_cov_delta(N) 3 | 4 | covfunc = @get_covariance; 5 | 6 | function varargout = get_covariance(theta) 7 | 8 | varargout = cell(nargout,1); 9 | 10 | if nargin == 0 11 | % Return the number of parameters and the size of the covariance matrix 12 | if nargout >= 1 13 | varargout{1} = 0; % number of parameters 14 | if nargout >= 2 15 | varargout{2} = N; % number of rows 16 | if nargout == 3 17 | varargout{3} = N; % number of columns 18 | else 19 | error('Too many outputs'); 20 | end 21 | end 22 | end 23 | return 24 | end 25 | 26 | K = speye(N); 27 | varargout{1} = K; 28 | if nargout >= 2 29 | dK = cell(0,1); 30 | varargout{2} = dK; 31 | end 32 | end 33 | 34 | end -------------------------------------------------------------------------------- /gp/gp_cov_jitter.m: -------------------------------------------------------------------------------- 1 | % GP_COV_JITTER - Adds a small constant to the diagonal of a covariance 2 | % matrix in order to make it numerically positive definite. 3 | % 4 | % Usage: 5 | % 6 | % COVFUNC_JITTERED = GP_COV_JITTER(COVFUNC) 7 | % COVFUNC_JITTERED = GP_COV_JITTER(COVFUNC, JITTER) 8 | % 9 | % COVFUNC is a covariance function to which the constant diagonal is 10 | % added. The covariance matrix by COVFUNC must be square. 11 | % 12 | % The default value for JITTER is 1E-6. 13 | % 14 | % See also GP_COV. 15 | 16 | % Last modified 2010-01-25 17 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 18 | 19 | function covfunc = gp_cov_jitter(covfunc, jitter) 20 | 21 | [~, N1, N2] = covfunc(); 22 | 23 | if nargin < 2 24 | jitter = 1e-6; 25 | end 26 | 27 | if N1 ~= N2 28 | error('Jittered matrix must be square'); 29 | else 30 | N = N1; 31 | end 32 | 33 | covfunc = gp_cov_sum(covfunc, gp_cov_scale(gp_cov_delta(N), 'scale', jitter)); 34 | -------------------------------------------------------------------------------- /gp/gp_cov_remove.m: -------------------------------------------------------------------------------- 1 | function func = gp_cov_remove(covfunc, ind1, ind2) 2 | if nargin < 3 3 | ind2 = ind1; 4 | end 5 | func = @cov_remove; 6 | function varargout = cov_remove(theta) 7 | ind1 8 | out = cell(nargout,1); 9 | varargout = cell(nargout,1); 10 | if nargin == 0 11 | [out{:}] = covfunc(); 12 | varargout{1} = out{1}; 13 | varargout(2:end) = out(2:end); 14 | return 15 | end 16 | [out{:}] = covfunc(theta); 17 | out{1}(ind1,ind2) = []; 18 | varargout{1} = out{1}; 19 | if nargout >= 2 20 | varargout{2} = cell(numel(theta),1); 21 | for n=1:numel(theta) 22 | % Scale the gradients 23 | out{2}{n}(ind1,ind2) = []; 24 | varargout{2}{n} = out{2}{n}; 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /gp/gp_cov_select.m: -------------------------------------------------------------------------------- 1 | function func = gp_cov_select(covfunc, ind1, ind2) 2 | if nargin < 3 3 | ind2 = ind1; 4 | end 5 | func = @cov_select; 6 | function varargout = cov_select(theta) 7 | out = cell(nargout,1); 8 | varargout = cell(nargout,1); 9 | if nargin == 0 10 | [out{:}] = covfunc(); 11 | varargout{1} = out{1}; 12 | if islogical(ind1) 13 | varargout{2} = sum(ind1); 14 | elseif isvector(ind1) 15 | varargout{2} = numel(ind1); 16 | else 17 | error('ind1 should be a vector') 18 | end 19 | if islogical(ind2) 20 | varargout{3} = sum(ind2); 21 | elseif isvector(ind2) 22 | varargout{3} = numel(ind2); 23 | else 24 | error('ind2 should be a vector') 25 | end 26 | varargout(4:end) = out(4:end); 27 | return 28 | end 29 | [out{:}] = covfunc(theta); 30 | varargout{1} = out{1}(ind1,ind2); 31 | if nargout >= 2 32 | varargout{2} = cell(numel(theta),1); 33 | for n=1:numel(theta) 34 | % Scale the gradients 35 | varargout{2}{n} = out{2}{n}(ind1,ind2); 36 | end 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /gp/gp_cov_toeplitz.m: -------------------------------------------------------------------------------- 1 | % Uses SPTOEPLITZ for sparse matrices 2 | function covfunc_toeplitz = gp_cov_toeplitz(covfunc) 3 | covfunc_toeplitz = @cov_toeplitz; 4 | function varargout = cov_toeplitz(theta) 5 | varargout = cell(nargout,1); 6 | if nargin == 0 7 | if nargout == 1 8 | varargout{1} = covfunc(); 9 | else 10 | out = cell(max(nargout,3),1); 11 | [out{:}] = covfunc(); 12 | out{2} = max(out{2}, out{3}); 13 | out{3} = out{2}; 14 | varargout(:) = out(1:nargout); 15 | end 16 | return 17 | end 18 | if nargout == 1 19 | k = covfunc(theta); 20 | if issparse(k) 21 | varargout{1} = sptoeplitz(k); 22 | else 23 | varargout{1} = toeplitz(k); 24 | end 25 | elseif nargout == 2 26 | [k, dk] = covfunc(theta); 27 | if issparse(k) 28 | varargout{1} = sptoeplitz(k); 29 | else 30 | varargout{1} = toeplitz(k); 31 | end 32 | varargout{2} = cell(numel(dk),1); 33 | for i=1:numel(dk) 34 | if issparse(k) 35 | varargout{2}{i} = sptoeplitz(dk{i}); 36 | else 37 | varargout{2}{i} = toeplitz(dk{i}); 38 | end 39 | end 40 | end 41 | end 42 | end 43 | 44 | -------------------------------------------------------------------------------- /gp/gp_cov_toeplitz_block.m: -------------------------------------------------------------------------------- 1 | % Uses SPTOEPLITZ for sparse matrices 2 | function covfunc_toeplitz = gp_cov_toeplitz_block(covfunc) 3 | covfunc_toeplitz = @cov_toeplitz_block; 4 | function varargout = cov_toeplitz_block(theta) 5 | varargout = cell(nargout,1); 6 | if nargin == 0 7 | if nargout == 1 8 | varargout{1} = covfunc(); 9 | else 10 | out = cell(max(nargout,3),1); 11 | [out{:}] = covfunc(); 12 | out{2} = max(out{2}, out{3}); 13 | out{3} = out{2}; 14 | varargout(:) = out(1:nargout); 15 | end 16 | return 17 | end 18 | if nargout == 1 19 | k = covfunc(theta); 20 | varargout{1} = toeplitz_block(k); 21 | elseif nargout == 2 22 | [k, dk] = covfunc(theta); 23 | varargout{1} = toeplitz_block(k); 24 | varargout{2} = cell(numel(dk),1); 25 | for i=1:numel(dk) 26 | varargout{2}{i} = toeplitz_block(dk{i}); 27 | end 28 | end 29 | end 30 | end 31 | 32 | -------------------------------------------------------------------------------- /gp/gp_cov_wrap.m: -------------------------------------------------------------------------------- 1 | % GP_COV_WRAP - A covariance function which wraps a given covariance 2 | % matrix as a covariance function. 3 | % 4 | % The function makes it easy to create a dummy covariance function wrapper 5 | % for a covariance matrix. The covariance function simply returns the given 6 | % covariance matrix. 7 | % 8 | % COVFUNC = GP_COV_WRAP(C) 9 | % 10 | % [K, DK] = COVFUNC(THETA) 11 | % 12 | % The covariance function COVFUNC has no parameters (THETA is empty) and it 13 | % returns K=C. The derivative DK is an empty cell. 14 | % 15 | % [N_THETA, N1, N2] = COVFUNC() 16 | 17 | % Last modified 2010-01-21 18 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 19 | 20 | function covfunc = gp_cov_wrap(C) 21 | 22 | covfunc = @get_covariance; 23 | 24 | function varargout = get_covariance(theta) 25 | 26 | varargout = cell(nargout,1); 27 | 28 | if nargin == 0 29 | % Return the number of parameters and the size of the covariance matrix 30 | if nargout >= 1 31 | varargout{1} = 0; % number of parameters 32 | if nargout >= 2 33 | varargout{2} = size(C,1); % number of rows 34 | if nargout == 3 35 | varargout{3} = size(C,2); % number of columns 36 | else 37 | error('Too many outputs'); 38 | end 39 | end 40 | end 41 | return 42 | end 43 | 44 | % Covariance matrix 45 | varargout{1} = C; 46 | 47 | % Gradient for hyperparameters 48 | if nargout >= 2 49 | varargout{2} = cell(0,1); 50 | end 51 | 52 | end 53 | 54 | end -------------------------------------------------------------------------------- /gp/gp_dlogpdf.m: -------------------------------------------------------------------------------- 1 | 2 | % dl = gp_dlogpdf(y_invK_dK_invK_y, trace_invK_dK) 3 | 4 | % dl = gp_dlogpdf(dK, invK, invK_y) 5 | 6 | function dl = gp_dlogpdf(y_invK_dK_invK_y, trace_invK_dK) 7 | dl = 0.5*y_invK_dK_invK_y - 0.5*trace_invK_dK; 8 | end 9 | % $$$ function dl = gp_dlogpdf(dK, invK, invK_y) 10 | % $$$ dl = 0.5*(invK_y'*dK*invK_y) - 0.5*traceprod(invK,dK); 11 | % $$$ end 12 | 13 | -------------------------------------------------------------------------------- /gp/gp_initialize.m: -------------------------------------------------------------------------------- 1 | 2 | function gpstruct = gp_initialize(covstruct) 3 | 4 | % Initialize regular GP and use different function for kron and sparse-approx? 5 | 6 | covstruct; 7 | gpstruct.get_posterior_sampler = @get_posterior_sampler; 8 | 9 | function sampler = get_posterior_sampler(logtheta) 10 | 11 | end 12 | 13 | function [mu,s2] = evaluate_posterior_mean(y) 14 | % should multiply with noiseless covstruct.. :/ 15 | mu = covstruct.multiply( covstruct.linsolve(y) ); 16 | if nargout >= 2 17 | % 18 | end 19 | end 20 | 21 | end 22 | -------------------------------------------------------------------------------- /gp/gp_learn.m: -------------------------------------------------------------------------------- 1 | 2 | % theta = gp_learn(y, covfunc, theta, varargin) 3 | 4 | function [theta, loglikelihood] = gp_learn(y, covfunc, theta, varargin) 5 | 6 | options = struct('maxiter', 100, ... 7 | 'checkgrad', false); 8 | [options, errmsg] = argparse(options, varargin{:}); 9 | error(errmsg); 10 | 11 | if options.checkgrad 12 | mycheckgrad(@cost, log(theta), 1e-3); 13 | end 14 | 15 | [logtheta, logpdfs] = minimize(log(theta), @cost, options.maxiter); 16 | %options = optimset('GradObj', 'on'); 17 | %[logtheta, logpdfs] = fminunc(@cost, log(theta), options); 18 | 19 | loglikelihood = -min(logpdfs); 20 | theta = exp(logtheta); 21 | 22 | function [c, dc] = cost(logtheta) 23 | theta = exp(logtheta); 24 | %fprintf( '%f %f %f %f\n', theta(1), theta(2), theta(3), theta(4) ) 25 | 26 | [c, dc] = gp_loglikelihood(y, covfunc, theta); 27 | c = -c; 28 | dc = -dc.*theta; 29 | 30 | if isinf(c) 31 | c = 1e0; 32 | end 33 | % c 34 | 35 | end 36 | 37 | end 38 | -------------------------------------------------------------------------------- /gp/gp_logpdf.m: -------------------------------------------------------------------------------- 1 | 2 | % l = gp_logpdf(y_invK_y, logdetK, N) 3 | 4 | function l = gp_logpdf(y_invK_y, logdetK, N) 5 | l = gaussian_logpdf(y_invK_y, 0, 0, logdetK, N); 6 | end 7 | -------------------------------------------------------------------------------- /gp/test_gp_geographic.m: -------------------------------------------------------------------------------- 1 | 2 | function test_gp_geographic 3 | 4 | lat = -87.5:5:87.5; 5 | lon = -177.5:5:177.5; 6 | 7 | 8 | %[LAT,LON] = meshgrid(lat,lon); 9 | [LON,LAT] = meshgrid(lon,lat); 10 | X = geographic_to_euclidean([LON(:)';LAT(:)']); 11 | 12 | 13 | switch 1 14 | case 1 15 | % Use block-Toeplitz structure 16 | [lat,lon0] = meshgrid(lat,lon(1)); 17 | X0 = geographic_to_euclidean([lon0(:)';lat(:)']); 18 | D = sqrt(sq_dist(X0,X)); 19 | covfunc = gp_cov_toeplitz_block(gp_cov_pp(D,3)); 20 | case 2 21 | % Brute force 22 | D = sqrt(sq_dist(X,X)); 23 | covfunc = gp_cov_pp(D,3); 24 | end 25 | 26 | % covfunc1 = gp_cov_scale(covfunc1); 27 | %covfunc1 = gp_cov_toeplitz(covfunc1); 28 | 29 | theta = 4000; 30 | 31 | t = cputime(); 32 | K = covfunc(theta); 33 | time_covfunc = cputime() - t 34 | 35 | figure 36 | %spy(K) 37 | 38 | fullness_K = nnz(K) / numel(K) 39 | %return 40 | 41 | t = cputime(); 42 | [L,p,q] = lchol(K); 43 | time_lchol = cputime() - t 44 | 45 | t = cputime(); 46 | y(q) = L*randn(size(K,1),1); 47 | time_rand = cputime() - t 48 | 49 | N = numel(y) 50 | 51 | %figure 52 | map_projection() 53 | %map_pcolor(lon, lat, reshape(K(415,:), [length(lon), length(lat)])) 54 | map_pcolor(LON, LAT, reshape(y, size(LON))) 55 | %map_pcolor(lon, lat, reshape(y, [length(lon), length(lat)])) 56 | map_colormap() 57 | map_grid() 58 | map_coast() 59 | -------------------------------------------------------------------------------- /gp/test_gp_kron_eigs.m: -------------------------------------------------------------------------------- 1 | % 2 | % DATA 3 | % 4 | 5 | %randn('state', 10) 6 | %rand('state', 10) 7 | function test_gp_kron_eigs() 8 | 9 | if nargin == 0 10 | N1 = 200; 11 | N2 = 300; 12 | end 13 | if nargin < 3 14 | N_samples = 4; 15 | end 16 | if nargin >= 4 17 | randn('state', seed); 18 | rand('state', seed); 19 | else 20 | randn('state', 10); 21 | rand('state', 10); 22 | end 23 | 24 | 25 | % Inputs 26 | x1 = 1:N1; 27 | x2 = 1:N2; 28 | 29 | % Covariance models 30 | 31 | covfuncs = cell(2,3); 32 | thetas = cell(2,3); 33 | K = cell(2,3); 34 | 35 | % Distances on the two domains 36 | d1 = sqrt(sq_dist(x1(1), x1)); 37 | d2 = sqrt(sq_dist(x2(1), x2)); 38 | 39 | % "Spatial" covariance functions 40 | covfunc = gp_cov_pp(d1,1); 41 | covfunc = gp_cov_toeplitz(covfunc); 42 | covfuncs(1,:) = {covfunc}; 43 | thetas(1,:) = {5; 10; 20}; 44 | 45 | % "Temporal" covariance functions 46 | covfunc = gp_cov_pp(d2,1); 47 | covfunc = gp_cov_toeplitz(covfunc); 48 | covfuncs(2,:) = {covfunc}; 49 | thetas(2,:) = {5; 10; 20}; 50 | 51 | % Compute covariance matrices 52 | for i=1:2 53 | for j=1:3 54 | K{i,j} = feval(covfuncs{i,j}, thetas{i,j}); 55 | end 56 | end 57 | 58 | 59 | A = @(x) vec(kronprod(K{1,1},K{2,1},reshape(x,N2,N1)) + ... 60 | kronprod(K{1,2},K{2,2},reshape(x,N2,N1)) + ... 61 | kronprod(K{1,3},K{2,3},reshape(x,N2,N1))) + 0.3*x; 62 | 63 | e = eigs(A,N1*N2, [], 1000, 'LM', struct('issym', true)) -------------------------------------------------------------------------------- /gp/test_gp_kron_iteration.m: -------------------------------------------------------------------------------- 1 | 2 | function test_gp_kron_iteration 3 | 4 | % 5 | % DATA 6 | % 7 | 8 | logtheta1 = log(5); 9 | N1 = 40; 10 | x1 = 1:N1; 11 | K1 = gp_cov_pp(logtheta1, x1, x1); 12 | 13 | logtheta2 = log(5); 14 | N2 = 45; 15 | x2 = 1:N2; 16 | K2 = gp_cov_pp(logtheta2, x2, x2); 17 | 18 | s = 1e-0; 19 | [X2,X1] = meshgrid(x2,x1); 20 | freq1 = 0.03*2*pi; 21 | freq2 = 0.07*2*pi; 22 | Y_noiseless = lchol(K2) * randn(N2,N1) * lchol(K1)'; 23 | Y = Y_noiseless + s*randn(N2,N1); 24 | 25 | % 26 | % Comparison 27 | % 28 | 29 | K = kron(K1,K2) + s^2*speye(N1*N2); 30 | tic 31 | LD = ldlchol(K); 32 | X_real = reshape(linsolve_ldlchol(LD, Y(:)), [N2,N1]); 33 | toc 34 | 35 | e = norm(Y); 36 | V = Y/e; 37 | for i=2:10 38 | W = K2*V*K1 + V; 39 | for j=1:k-1 40 | H(j,k-1) = traceprod(V,W); 41 | W = W - V*H(j,k-1); 42 | end 43 | H(k,k-1) = norm(W); 44 | V = W / H(k,k-1); 45 | end 46 | 47 | 48 | % $$$ tic 49 | % $$$ LD1 = ldlchol(K1); 50 | % $$$ LD2 = ldlchol(K2); 51 | % $$$ X = zeros(N2,N1); 52 | % $$$ for i=1:10 53 | % $$$ Y1 = Y - s^2*X; 54 | % $$$ Y2 = Y - kronprod(K1,K2,X); 55 | % $$$ 56 | % $$$ X1 = linsolve_kron_ldlchol(LD1,LD2,Y1); 57 | % $$$ X2 = s^(-2)*Y2; 58 | % $$$ 59 | % $$$ X = 0.5*(X1+X2); 60 | % $$$ end 61 | % $$$ toc 62 | 63 | norm(X-X_real) -------------------------------------------------------------------------------- /gp/test_localglobal_gp.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | N = 100; 4 | Np = 10; 5 | 6 | x = 1:N; % data inputs 7 | xp = linspace(1,N,Np); % pseudo inputs 8 | xh = 1:(N+10); % predictive inputs 9 | 10 | logtheta_se = [log(1), log(N/10)]; 11 | logtheta_cs = log(3); 12 | 13 | Kxpxp_se = gp_cov_se(xp,xp,logtheta_se -------------------------------------------------------------------------------- /gppca/argschk.m: -------------------------------------------------------------------------------- 1 | function [ opts, errmsg, wrnmsg ] = argschk( defopts, varargin ) 2 | 3 | errmsg = ''; 4 | wrnmsg = ''; 5 | opts = defopts; 6 | 7 | if mod( length(varargin), 2 ) 8 | if isstruct(varargin{1}) 9 | opts_in = varargin{1}; 10 | else 11 | errmsg = [ 'The optional parameters must be given in',... 12 | 'parameter/value pairs or using a structure argument.' ]; 13 | return 14 | end 15 | else 16 | opts_in = struct( varargin{:} ); 17 | end 18 | 19 | flds = fieldnames( opts_in ); 20 | for i = 1:length(flds) 21 | if ~strcmp( flds{i}, lower(flds{i}) ) 22 | opts_in.(lower(flds{i})) = opts_in.(flds{i}); 23 | opts_in = rmfield( opts_in, flds{i} ); 24 | flds{i} = lower(flds{i}); 25 | end 26 | end 27 | for i = 1:length(flds) 28 | if ~isfield( opts, flds{i} ) 29 | wrnmsg = [ 'Unknown parameter ''' flds{i} '''' ]; 30 | end 31 | opts.(flds{i}) = opts_in.(flds{i}); 32 | end 33 | -------------------------------------------------------------------------------- /gppca/cholsolve.m: -------------------------------------------------------------------------------- 1 | 2 | function X = cholsolve(A,B) 3 | % Solves (A'*A)*X=B with respect to X. 4 | % A should be an upper triangular matrix (Cholesky decomposition). 5 | 6 | opts.UT = true; 7 | opts.TRANSA = true; 8 | Z = linsolve(A, B, opts); 9 | opts.TRANSA = false; 10 | X = linsolve(A, Z, opts); 11 | 12 | -------------------------------------------------------------------------------- /gppca/gamvbcost.m: -------------------------------------------------------------------------------- 1 | 2 | function c = gamvbcost(x,logx,apost,bpost,aprior,bprior) 3 | % function c = gamvbcost(x,logx,apost,bpost,aprior,bprior) 4 | % 5 | % Calculates c = -KL(q||p) = - 6 | % where the expectation <.> is taken over q(X). This is used for 7 | % calculating the lower bound of the marginal loglikelihood in VB 8 | % models. 9 | % 10 | % Let X be a Gamma distributed variable: 11 | % p(X) = G(aprior,bprior) 12 | % q(X) = G(apost,bpost) 13 | % = x 14 | % = logx 15 | 16 | % Cost from prior 17 | c = aprior*log(bprior) - gammaln(aprior) + (aprior-1)*logx - bprior*x; 18 | % Cost from q-posterior 19 | c = c + gamentropy(apost,bpost); 20 | -------------------------------------------------------------------------------- /gppca/gpcovConstScale.m: -------------------------------------------------------------------------------- 1 | 2 | % K = gpcovConstScale(s, covfunc, ...) 3 | % [K, dK_logtheta] = gpcovConstScale(s, covfunc, ...) 4 | % [K, dK_logtheta, dK_x2] = gpcovConstScale(s, covfunc, ...) 5 | % 6 | % For instance, 7 | % covfunc = {@gpcovConstScale, 0.5, {@gpcovSEiso}} 8 | % and then 9 | % K = feval(covfunc{:}, logtheta, x1, x2) 10 | 11 | function varargout = gpcovConstScale(s,covfunc,varargin) 12 | % $$$ function [K, dK_logtheta, dK_x2] = gpcovConstScale(s,covfunc,varargin) 13 | 14 | if ~iscell(covfunc), covfunc = {covfunc}; end 15 | 16 | varargout = cell(nargout,1); 17 | [varargout{:}] = feval(covfunc{:}, varargin{:}); 18 | 19 | for i=1:nargout 20 | varargout{i} = s*varargout{i}; 21 | end 22 | 23 | % $$$ % Use constant scale 24 | % $$$ if nargout == 1 25 | % $$$ K = feval(covfunc{:}, varargin{:}); 26 | % $$$ elseif nargout == 2 27 | % $$$ [K, dK_logtheta] = feval(covfunc{:}, varargin{:}); 28 | % $$$ dK_logtheta = s*dK_logtheta; 29 | % $$$ elseif nargout == 3 30 | % $$$ [K, dK_logtheta, dK_x2] = feval(covfunc{:}, varargin{:}); 31 | % $$$ dK_logtheta = s*dK_logtheta; 32 | % $$$ dK_x2 = s*dK_x2; 33 | % $$$ end 34 | % $$$ 35 | % $$$ K = s * K; 36 | -------------------------------------------------------------------------------- /gppca/gpcovDot.m: -------------------------------------------------------------------------------- 1 | 2 | % [K, dK_logtheta, dK_x2] = gpcov(x1, x2, logtheta) 3 | % 4 | function [K, dK_logtheta, dK_x2] = gpcovDot(logtheta, x1, x2) 5 | 6 | warning('Under development, not ready') 7 | 8 | % Covariance matrix 9 | K = x1' * x2; 10 | 11 | % Gradient for hyperparameters 12 | if nargout >= 2 13 | dK_logtheta = nan; 14 | end 15 | 16 | % Gradients for inputs x2 17 | if nargout >= 3 18 | if isempty(x2) 19 | error('Can''t calculate gradient: x2 not given'); 20 | end 21 | d = rows(x2); % dimensionality of inputs 22 | m = cols(x1); % number of other inputs 23 | n = cols(x2); % number of inputs 24 | dK_x2 = zeros([d,m,n]); 25 | for j=1:n 26 | dK_x2(:,:,j) = x1; 27 | end 28 | end 29 | 30 | -------------------------------------------------------------------------------- /gppca/gpcovNoise.m: -------------------------------------------------------------------------------- 1 | 2 | % K = gpcovConstScale(s, covfunc, ...) 3 | % [K, dK_logtheta] = gpcovConstScale(s, covfunc, ...) 4 | % [K, dK_logtheta, dK_x2] = gpcovConstScale(s, covfunc, ...) 5 | % 6 | % For instance, 7 | % covfunc = {@gpcovConstScale, 0.5, {@gpcovSEiso}} 8 | % and then 9 | % K = feval(covfunc{:}, logtheta, x1, x2) 10 | 11 | function [K, dK_logtheta, dK_x2] = gpcovNoise(logtheta,x1,x2) 12 | 13 | warning('Not well tested / debugged yet'); 14 | 15 | n1 = cols(x1); 16 | if nargin < 3 17 | % Asking only vector of variances 18 | n2 = 1; 19 | s2 = exp(logtheta(1)); 20 | K = s2 * ones(n1,1); 21 | else 22 | % Asking covariance matrix 23 | n2 = cols(x2); 24 | if all(size(x1) == size(x2)) && all(x1 == x2) 25 | % Noise variance only.. 26 | s2 = exp(logtheta(1)); 27 | else 28 | % .. no correlations between different inputs 29 | s2 = 0; 30 | end 31 | K = s2 * eye(n1,n2); 32 | end 33 | 34 | 35 | % Gradient for hyperparameters 36 | if nargout >= 2 37 | dK_logtheta = zeros([n1, n2, 1]); 38 | dK_logtheta(:,:,1) = K; 39 | end 40 | 41 | % Gradients for inputs x2 42 | if nargout >= 3 43 | if isempty(x2) 44 | error('Can''t calculate gradient: x2 not given'); 45 | end 46 | d = rows(x2); % dimensionality of inputs 47 | dK_x2 = zeros([d,n1,n2]); 48 | end 49 | 50 | -------------------------------------------------------------------------------- /gppca/gpcovScale.m: -------------------------------------------------------------------------------- 1 | 2 | % K = gpcovConstScale(s, covfunc, ...) 3 | % [K, dK_logtheta] = gpcovConstScale(s, covfunc, ...) 4 | % [K, dK_logtheta, dK_x2] = gpcovConstScale(s, covfunc, ...) 5 | % 6 | % For instance, 7 | % covfunc = {@gpcovConstScale, 0.5, {@gpcovSEiso}} 8 | % and then 9 | % K = feval(covfunc{:}, logtheta, x1, x2) 10 | 11 | function varargout = gpcovScale(covfunc,logtheta,varargin) 12 | % $$$ function [K, dK_logtheta, dK_x2] = gpcovConstScale(s,covfunc,varargin) 13 | 14 | if ~iscell(covfunc), covfunc = {covfunc}; end 15 | 16 | varargout = cell(nargout,1); 17 | s = exp(logtheta(1)); 18 | logtheta(1) = []; % Don't use 2:end, because it might be empty? 19 | [varargout{:}] = feval(covfunc{:}, logtheta, varargin{:}); 20 | 21 | for i=1:nargout 22 | varargout{i} = s * varargout{i}; 23 | end 24 | 25 | % Add the partial derivative w.r.t. the scale 26 | if nargout >= 2 27 | varargout{2} = cat(3, full(varargout{1}), varargout{2}); 28 | end 29 | 30 | % $$$ figure 31 | % $$$ in_gpcovScale = true 32 | % $$$ imagesc(varargout{1}) 33 | 34 | % $$$ % Use constant scale 35 | % $$$ if nargout == 1 36 | % $$$ K = feval(covfunc{:}, varargin{:}); 37 | % $$$ elseif nargout == 2 38 | % $$$ [K, dK_logtheta] = feval(covfunc{:}, varargin{:}); 39 | % $$$ dK_logtheta = s*dK_logtheta; 40 | % $$$ elseif nargout == 3 41 | % $$$ [K, dK_logtheta, dK_x2] = feval(covfunc{:}, varargin{:}); 42 | % $$$ dK_logtheta = s*dK_logtheta; 43 | % $$$ dK_x2 = s*dK_x2; 44 | % $$$ end 45 | % $$$ 46 | % $$$ K = s * K; 47 | -------------------------------------------------------------------------------- /gppca/gpcovSum.m: -------------------------------------------------------------------------------- 1 | function varargout = gpcovSum(covfunc1,covfunc2,logtheta,varargin) 2 | % $$$ function [K, dK_logtheta, dK_x2] = gpcovConstScale(s,covfunc,varargin) 3 | 4 | error('NOT IMPLEMENTED YET!!') 5 | 6 | if ~iscell(covfunc1), covfunc1 = {covfunc1}; end 7 | if ~iscell(covfunc2), covfunc2 = {covfunc2}; end 8 | 9 | params1 = feval(covfunc1{:}); 10 | params2 = feval(covfunc2{:}); 11 | 12 | logtheta1 = logtheta(1:params1); 13 | logtheta2 = logtheta((params1+1):end); 14 | 15 | varargout1 = cell(nargout,1); 16 | [varargout1{:}] = feval(covfunc1{:}, logtheta1, varargin{:}); 17 | varargout2 = cell(nargout,1); 18 | [varargout2{:}] = feval(covfunc2{:}, logtheta2, varargin{:}); 19 | 20 | varargout = cell(nargout,1); 21 | if nargout >= 1 22 | varargout{1} = varargout1{1}.*varargout2{1}; 23 | end 24 | if nargout >= 2 25 | p = params1 + params2; 26 | [m,n] = size(varargout{1}); 27 | varargout{2} = zeros([m,n,p]); 28 | varargout{2}(:,:,1:params1) = bsxfun(@times,varargout1{2},varargout2{1}); 29 | varargout{2}(:,:,(params1+1):end) = bsxfun(@times,varargout1{1},varargout2{2}); 30 | end 31 | if nargout >= 3 32 | [m,n] = size(varargout{1}); 33 | varargout{3} = bsxfun(@times,reshape(varargout1{1},[1,m,n]),varargout2{3}) + ... 34 | bsxfun(@times,varargout1{3},reshape(varargout2{1},[1,m,n])); 35 | end 36 | -------------------------------------------------------------------------------- /gppca/gpcov_decper.m: -------------------------------------------------------------------------------- 1 | % 2 | % [K, dK] = gpK_decper(D, p1, p2, p3, p4) 3 | % 4 | % Decaying periodic covariance function for GP. 5 | % 6 | % p1 is the scale 7 | % p2 is the decaying length 8 | % p3 is the frequency of the periodicity 9 | % p4 is the smoothness of the periodic signal 10 | % 11 | function [K, dK] = gpK_decper(D, p1, p2, p3, p4) 12 | K = p1^2*exp(-0.5*(D.^2)/(p2^2)-2*sin(pi*D*p3).^2/(p4^2)); 13 | 14 | if nargout >= 2 15 | dK = zeros([size(D),4]); 16 | dK(:,:,1) = K .* 2 / p1; 17 | dK(:,:,2) = K .* (-0.5*D.^2) .* (-2*p2^(-3)); 18 | dK(:,:,3) = K .* (-2*sin(pi*D*p3)*2) .* cos(pi*D*p3) .* (pi*D); 19 | dK(:,:,4) = K .* (-2*sin(pi*D*p3).^2) * (-2)*p4^(-3); 20 | end 21 | -------------------------------------------------------------------------------- /gppca/gpcov_old.m: -------------------------------------------------------------------------------- 1 | function [K, dK] = gpcov(varargin) 2 | 3 | K = 0; 4 | dK = []; 5 | for i=1:nargin 6 | if nargout == 1 7 | Knew = varargin{i}(); 8 | else 9 | [Knew,dKnew] = varargin{i}(); 10 | n = size(dKnew,3); 11 | if isempty(dK) 12 | dK = dKnew; 13 | else 14 | dK(:,:,end+(1:n)) = dKnew; 15 | end 16 | end 17 | K = K + Knew; 18 | end 19 | -------------------------------------------------------------------------------- /gppca/gpcov_ratquad.m: -------------------------------------------------------------------------------- 1 | 2 | function [K,dK] = gpcov_ratquad(D, p1, p2, p3) 3 | % f = 1 + D.^2/(2*p3^2*p2^2); 4 | % K = p1^2*f.^(-p3^2); 5 | 6 | f = 1 + D.^2/(2*p3^2*p2^2); 7 | K = p1^2*f.^(-p3^2); 8 | 9 | if nargout >= 2 10 | dK = zeros([size(D),3]); 11 | dK(:,:,1) = K .* 2 / p1; 12 | dK(:,:,2) = K .* (-p3^2).*f.^(-1) .* (-2).*D.^2/(2*p3^2)*p2^(-3); 13 | dK(:,:,3) = K .* (-2*p3*log(f) + (-p3^2)./f.*D.^2/(2*p2^2) * (-2) * p3^(-3)); 14 | end 15 | -------------------------------------------------------------------------------- /gppca/gpdist.m: -------------------------------------------------------------------------------- 1 | % 2 | % D = gpdist(x1, x2, funcDist) 3 | % 4 | % Returns matrix of pair-wise distances between x1 and x2 as a matrix. 5 | % x1 is K x M matrix 6 | % x2 is K x N matrix 7 | % where K is the dimensionality, M and N are the number of samples. 8 | % The resulting matrix D is M x N matrix. 9 | % If no function is given for distance calculation, 2-norm is used. 10 | function D = gpdist(x1, x2, funcDist) 11 | 12 | if nargin < 2 13 | x2 = x1; 14 | end 15 | if isvector(x1) 16 | x1 = x1(:)'; 17 | x2 = x2(:)'; 18 | end 19 | if nargin < 3 20 | funcDist = @(z1,z2) norm(z1-z2); 21 | % funcDist = @(z1,z2) abs(z1-z2); 22 | end 23 | 24 | n1 = size(x1,2); 25 | n2 = size(x2,2); 26 | D = zeros(n1,n2); 27 | for i=1:n1 28 | for j=1:n2 29 | D(i,j) = funcDist(x1(:,i),x2(:,j)); 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /gppca/gpeval.m: -------------------------------------------------------------------------------- 1 | function gpeval(func, varargin) 2 | 3 | 4 | if isa(func, 'function_handler'); 5 | f = func2str(func); 6 | end 7 | 8 | eval(sprintf('feval(%s, varargin{:})', f)); 9 | -------------------------------------------------------------------------------- /gppca/gpfa.m: -------------------------------------------------------------------------------- 1 | function Q = gpfa(D, Y, W_module, X_module, noise_module, varargin) 2 | 3 | % OK, a dummy wrapper at the moment, but I could make this method a bit 4 | % easier to use. For instance, this function would form the modules given 5 | % some covariance functions or something... 6 | 7 | Q = vbfa(D, Y, W_module, X_module, noise_module, varargin{:}); 8 | -------------------------------------------------------------------------------- /gppca/gpmapcolor.m: -------------------------------------------------------------------------------- 1 | 2 | function gpmapcolor(coord, f, Covf, lon, lat, logtheta, covfunc) 3 | 4 | if iscell(coord) 5 | D = numel(coord); 6 | end 7 | 8 | %[lon, lat] = lonlat2standard(lon, lat); 9 | [LAT,LON] = meshgrid(lat,lon); 10 | Xh = [LON(:)'; LAT(:)']; 11 | 12 | for d=1:D 13 | subplot(D,2,2*d-1); 14 | %mapproj('global-ellipse'); 15 | [fh, varfh] = gppred(coord{d}, f{d}, Covf{d}, Xh, logtheta{d}, covfunc{d}); 16 | m_pcolor(LON,LAT,reshape(fh,size(LON))); 17 | shading('flat'); 18 | mapcoast 19 | %colormap('hsv'); 20 | 21 | subplot(D,2,2*d); 22 | %mapproj('global-ellipse'); 23 | [fh, varfh] = gppred(coord{d}, f{d}, Covf{d}, Xh, logtheta{d}, covfunc{d}); 24 | m_pcolor(LON,LAT,reshape(varfh,size(LON))); 25 | shading('flat'); 26 | mapcoast 27 | %colormap('gray'); 28 | end 29 | 30 | -------------------------------------------------------------------------------- /gppca/gprnd.m: -------------------------------------------------------------------------------- 1 | function y = gprnd(X, logtheta, covfunc) 2 | 3 | if ~iscell(covfunc) 4 | covfunc = {covfunc}; 5 | end 6 | 7 | K = regularize(feval(covfunc{:}, logtheta, X, X));% + 1e-5*diag(sparse(ones(cols(X),1))); 8 | 9 | y = mymvnrnd(0, K); -------------------------------------------------------------------------------- /gppca/index_selected.m: -------------------------------------------------------------------------------- 1 | function bool = index_selected(iter, indeces) 2 | 3 | if islogical(indeces) 4 | if isscalar(indeces) 5 | bool = indeces; 6 | else 7 | error('Logical values must not be matrices'); 8 | end 9 | elseif isscalar(indeces) 10 | if indeces > 0 11 | bool = iter >= indeces; 12 | else 13 | error('Index must be positive integer.'); 14 | end 15 | else 16 | bool = any(iter==indeces); 17 | end 18 | -------------------------------------------------------------------------------- /gppca/logdetchol.m: -------------------------------------------------------------------------------- 1 | function logdetx = logdetchol(U) 2 | % function logdetx = chollogdet(U) 3 | % 4 | % Returns log(det(X)) where X=U'*U and U is upper triangular matrix, that 5 | % is, U is Cholesky decomposition of X. 6 | 7 | logdetx = 2 * sum(log(diag(U))); -------------------------------------------------------------------------------- /gppca/logdettri.m: -------------------------------------------------------------------------------- 1 | function y = logdettri(X) 2 | 3 | y = sum(log(diag(X))); -------------------------------------------------------------------------------- /gppca/mvnvbcost.m: -------------------------------------------------------------------------------- 1 | function c = mvnvbcost(x,Covx,mu,Covmu,pCholCov,pLogDetCov) 2 | % c = mvnvbcost(x,Covx,mu,Covmu,pCholCov,pLogDetCov) 3 | % 4 | % Calculates c = -KL(q||p) = - 5 | % where the expectation <.> is taken over q(X). This is used for 6 | % calculating the lower bound of the marginal loglikelihood in VB 7 | % models. 8 | % 9 | % p(X) = N(M,T) 10 | % q(X) = N(x,Covx) 11 | % 12 | % Rest of the parameters are defined as: 13 | % q(M) = N(mu,Covmu) 14 | % = inv(pCholCov * pCholCov') 15 | % = pLogDetCov 16 | % 17 | % If Covx==[], then the term is not calculated. 18 | % This is useful when X is, e.g., observations. 19 | 20 | 21 | c = 0; 22 | d = length(x); 23 | 24 | % Cost from q-posterior 25 | if ~isempty(Covx) 26 | % TODO: 27 | % if ~isempty(pCholCov), entropy = mvnentropy(pCholCov,true); else.... end 28 | % entropy = mvnentropy(pCholCov,true); 29 | entropy = mvnentropy(Covx); 30 | c = entropy; 31 | else 32 | Covx = 0; 33 | end 34 | 35 | % Use Cholesky of the prior covariance 36 | % $$$ opts.UT = true; 37 | % $$$ opts.TRANSA = true; 38 | z = solve_tril(pCholCov, x-mu); 39 | 40 | % Cost from prior 41 | 42 | % Below: (x-mu)' * Cov^(-1) * (x-mu) + trace(Cov^(-1)*(Covx+Covmu)) 43 | err2 = z'*z + trace(solve_triu(pCholCov',solve_tril(pCholCov,Covx+Covmu))); 44 | %err2 = (x-mu)'*pInvCov*(x-mu) + (Covx(:)+Covmu(:))'*pInvCov(:); 45 | %((x-mu).^2 + diag(Covx) + diag(Covmu)); 46 | c = c - 0.5*pLogDetCov - 0.5*err2 - 0.5*d*log(2*pi); 47 | -------------------------------------------------------------------------------- /gppca/myldl.m: -------------------------------------------------------------------------------- 1 | function [L,D] = myldl(A) 2 | 3 | % TODO: Check squareness and symmetricity!! 4 | 5 | 6 | n = rows(A); 7 | 8 | L = eye(n); 9 | D = zeros(n); 10 | 11 | for i=1:n 12 | m = i - 1; 13 | for j=1:m 14 | r = j - 1; 15 | L(i,j) = 1/D(j,j) * (A(i,j) - L(i,1:r)*D(1:r,1:r)*L(j,1:r)'); 16 | end 17 | D(i,i) = A(i,i) - L(i,1:m)*D(1:m,1:m)*L(i,1:m)'; 18 | end 19 | -------------------------------------------------------------------------------- /gppca/regularize.m: -------------------------------------------------------------------------------- 1 | function X = regularize(X, reg) 2 | 3 | %error('Do not use this function'); 4 | 5 | % $$$ normX = normest(X, 1e-1); 6 | 7 | n = length(X); 8 | I = diag(sparse(ones(n,1))); 9 | 10 | if nargin < 2 11 | reg = 1; 12 | end 13 | normX = reg; 14 | coeff = 1e-6; 15 | X = X + coeff*normX*I; 16 | 17 | % $$$ while ~success 18 | % $$$ try 19 | % $$$ U = chol(X + 1e-8*normX*I, varargin{:}); 20 | % $$$ success = true; 21 | % $$$ catch 22 | % $$$ times = times + 1; 23 | % $$$ warning(sprintf(['Cholesky decomposition failed %d-th time, increasing ' ... 24 | % $$$ 'regularization'], times)) 25 | % $$$ normX = normX*100; 26 | % $$$ end 27 | % $$$ end 28 | -------------------------------------------------------------------------------- /gppca/safechol.m: -------------------------------------------------------------------------------- 1 | function [U, Xreg] = safechol(X, coeff, varargin) 2 | % function [U,X] = safechol(X,coeff,...) 3 | % 4 | % Returns the Cholesky decomposition such that U'*U = X. 5 | % X must be symmetric positive definite matrix. Tries to solve numerical 6 | % issues by adding a small value to the diagonal. 7 | % 8 | % coeff is relative regularization w.r.t. the norm, e.g., 1e-6. 9 | 10 | normX = normest(X, 1e-4); 11 | n = length(X); 12 | I = diag(sparse(ones(n,1))); 13 | 14 | success = false; 15 | times = 0; 16 | %coeff = 1e-10; 17 | reg = coeff*normX; 18 | while ~success 19 | try 20 | Xreg = X + reg*I; 21 | U = chol(Xreg, varargin{:}); 22 | success = true; 23 | catch 24 | reg = 100 * reg; 25 | % $$$ if times == 0 26 | % $$$ reg = 1e-16*normX; 27 | % $$$ end 28 | times = times + 1; 29 | % $$$ warning(sprintf(['Cholesky decomposition failed %d-th time, increasing' ... 30 | % $$$ ' regularization'], times)) 31 | warning(sprintf(['Cholesky decomposition failed %d-th time, increasing ' ... 32 | 'regularization, coeff=%.1e, norm=%.1e'], times, ... 33 | coeff, normX)) 34 | end 35 | end 36 | 37 | %U = cholmod(sparse(X)); 38 | %U = U*S'; % re-permutate 39 | 40 | % $$$ [L,D] = ldl(X); 41 | % $$$ D = diag(D); 42 | % $$$ D(D<0) = 0; 43 | % $$$ U = diag(sqrt(D)) * L'; 44 | 45 | % $$$ choltol = 1e-6; % TODO: How to choose this?? 46 | % $$$ U = chol(X + diag(choltol*ones(length(X),1))); 47 | -------------------------------------------------------------------------------- /gppca/solvegpmvn.m: -------------------------------------------------------------------------------- 1 | 2 | function [sqrtCov, Cov, mu] = solvegpmvn(K, A, B) 3 | % sqrtCov = solvegpmvn(K, A) 4 | % [sqrtCov, Cov] = solvegpmvn(K, A) 5 | % [sqrtCov, Cov, mu] = solvegpmvn(K, A, B) 6 | % 7 | % Solves the set of equations 8 | % Cov^(-1) = K^(-1) * A * K^(-1) 9 | % mu = Cov * K^(-1) * B 10 | % or equivalently for Cov, 11 | % mu = K * A^(-1) * B 12 | % Cov = K * A^(-1) * K 13 | % using Cholesky decomposition. K and A should be symmetric positive 14 | % definite matrices of the same size. B should be a column vector. 15 | % 16 | % sqrtCov is such that Cov = sqrtCov'*sqrtCov 17 | 18 | U = safechol(A); % A=U'*U, i.e., A^(-1) = U^(-1) * U^(-1)' 19 | opts.UT = true; 20 | opts.TRANSA = true; 21 | sqrtCov = linsolve(U,K,opts); % sqrtCov = U^(-1)' * K 22 | if nargout >= 2 23 | Cov = sqrtCov'*sqrtCov; 24 | end 25 | if nargout >= 3 26 | mu = sqrtCov'*linsolve(U,B,opts); % mu = K * U^(-1) * U^(-1)' * B 27 | end 28 | -------------------------------------------------------------------------------- /gppca/spgpr.m: -------------------------------------------------------------------------------- 1 | % 2 | % [fh, varfh] = gppred(X, f, Covf, Xh, kX, logtheta, ...) 3 | % 4 | % p(y|X) = N(y; f(X), inv(V)) 5 | function [f, varf] = spgpr(logtheta, covfunc, X, y, V, varargin) 6 | %function [fh, varfh] = gppred(X, f, Covf, Xh, kX, logtheta, varargin) 7 | 8 | % If you need more than just variances for PCA, you could give some sparse 9 | % matrix to indicate which covariances should be evaluated. But again, this 10 | % covariances should be zero for the latent function values fh a 11 | % priori.. Things would be much easier if I just factored the principal 12 | % components too.. :) 13 | 14 | opts = struct('cholkx', [], 'khx', [], 'kh', []); 15 | %opts = struct('cholkx', [], 'khx', [], 'kh', []); 16 | 17 | [ opts, errmsg, wrnmsg ] = argschk( opts, varargin{:} ); 18 | if ~isempty(errmsg), error( errmsg ), end 19 | if ~isempty(wrnmsg), warning( wrnmsg ), end 20 | 21 | nh = cols(Xh); 22 | 23 | if isempty(opts.cholkx) 24 | Kpp = feval(kX, X, X, logtheta); 25 | Kpp = Kpp + 1e-6*eye(size(Kpp)); 26 | Lp = chol(Kpp, 'lower'); 27 | else 28 | Lp = opts.cholkx; 29 | end 30 | 31 | if isempty(opts.khx) 32 | Kxp = feval(kX, Xh, X, logtheta); 33 | else 34 | Kxp = opts.khx; 35 | end 36 | 37 | if isempty(opts.kh) 38 | Kx = feval(kX, Xh, [], logtheta); 39 | else 40 | Kx = opts.kh; 41 | end 42 | 43 | fh = Kxp * solve_triu(Lp', solve_tril(Lp, f)); 44 | 45 | if nargout >= 2 46 | varfh = zeros(nh,1); 47 | for i=1:nh 48 | r = solve_tril(Lp, Kxp(i,:)'); 49 | s = solve_triu(Lp', r); 50 | varfh(i) = Kx(i) - r'*r + s'*Covf*s; 51 | end 52 | end 53 | 54 | -------------------------------------------------------------------------------- /gppca/sqdistEuclidean.m: -------------------------------------------------------------------------------- 1 | % 2 | % [D2, dD1] = sqdistEuclidean(x1, x2) 3 | % 4 | function [D, dD_dx2] = sqdistEuclidean(x1, x2, squared) 5 | 6 | if nargin < 3 7 | squared = true; 8 | end 9 | 10 | D = sq_dist(x1,x2); 11 | 12 | if nargout >= 2 13 | [d,n1] = size(x1); 14 | n2 = cols(x2); 15 | dD_dx2 = 2 * bsxfun(@minus, x1, reshape(x2,[d,1,n2])) * (-1); 16 | end 17 | 18 | % dD_dx2(:,:,i) = 2 * bsxfun(@minus, x1, x2(:,i)) * (-1); 19 | 20 | warning off MATLAB:divideByZero 21 | if squared == false 22 | D = sqrt(D); 23 | if nargout >= 2 24 | %[i,j] = find(D>0); 25 | dD_dx2 = 0.5 * bsxfun(@rdivide, dD_dx2, reshape(D,[1,n1,n2])); 26 | dD_dx2(isnan(dD_dx2)) = 0; % TODO, correct? 27 | end 28 | 29 | % $$$ for i=1:n2 30 | % $$$ % $$$ % dD_dx2 = 2 * bsxfun(@minus, x1, reshape(x2,[d,1,n2])) * (-1); 31 | % $$$ % $$$ dD_dx2(:,:,i) = 2 * bsxfun(@minus, x1, x2(:,i)) * (-1); 32 | % $$$ % $$$ %error('he') 33 | % $$$ % $$$ if squared == false 34 | % $$$ % $$$ D = sqrt(D); 35 | % $$$ % $$$ error('not yet') 36 | % $$$ % $$$ end 37 | % $$$ end 38 | end 39 | 40 | -------------------------------------------------------------------------------- /gppca/test_gradient_Kxx.m: -------------------------------------------------------------------------------- 1 | 2 | function test_gradient_Kxx 3 | 4 | mycheckgrad(@func, randn(10,1), 1e-8) 5 | 6 | 7 | function [f, df] = func(x) 8 | 9 | [K,tmp,dK_x] = gpcovDot(1,x',x'); 10 | 11 | f = sum(K(:)); 12 | 13 | n = length(x); 14 | df = zeros(n,1); 15 | 16 | Dpp = zeros(n,n); 17 | for i=1:n 18 | %error('jou') 19 | Dpp(:) = 0; 20 | Dpp(i,:) = dK_x(1,:,i); 21 | % Dpp(:,i) = dK_x(1,:,i)'; 22 | Dpp(:,i) = Dpp(:,i) + dK_x(1,:,i)'; 23 | % Dpp(i,i) = 0; % DEBUGGING 24 | Dpp; 25 | % $$$ Dpp(:,i) = dKpp_pseudo(j,:,i)'; 26 | 27 | df(i) = sum(Dpp(:)); 28 | % $$$ traceprod(Dpp, Tpp, true) ... 29 | % $$$ - 0.5 * b' * Dpp * b ... 30 | % $$$ + dKxp_pseudo(j,:,i) * Txp(:,i) ... 31 | % $$$ + dif' * dKxp_pseudo(j,:,i)' * b(i); 32 | % $$$ end 33 | end 34 | -------------------------------------------------------------------------------- /gppca/tmpsin.m: -------------------------------------------------------------------------------- 1 | function [f,df] = tmpsin(x) 2 | f = sum(sin(x)); 3 | df = cos(x); -------------------------------------------------------------------------------- /gppca/tsgpplot.m: -------------------------------------------------------------------------------- 1 | 2 | function hax = tsgpplot(x, Y, E, varargin) 3 | % function tsgpplot(x, Mu, E) 4 | % 5 | % Plots d signals. 6 | % x = inputs (n x 1) 7 | % Y = expected outputs (n x d) 8 | % E = errors of the outputs (n x d) 9 | 10 | opts = struct('pseudoinputs', []); 11 | [ opts, errmsg, wrnmsg ] = argschk( opts, varargin{:} ); 12 | if ~isempty(errmsg), error( errmsg ), end 13 | if ~isempty(wrnmsg), warning( wrnmsg ), end 14 | 15 | figure 16 | 17 | n = size(Y,2); 18 | for i=1:n 19 | params = {}; 20 | if ~isempty(opts.pseudoinputs) 21 | params = [params(:); {'pseudoinputs'}; {opts.pseudoinputs{i}}]; 22 | end 23 | hax(i) = subplot(n,1,i); 24 | gpplot(x(:),Y(:,i),E(:,i), params{:}); 25 | 26 | set( gca, 'xlim', [ min(x) max(x) ] ) 27 | 28 | pos_ax = get( gca, 'Position' ); 29 | hgt_ax = (0.95 - 0.1) / ( n + (n-1)*0.1 ); 30 | hgt_sp = hgt_ax * 0.1; 31 | pos_ax(1) = 0.1; 32 | pos_ax(2) = 0.95 - (i-1)*( hgt_ax + hgt_sp ) - hgt_ax; 33 | pos_ax(3) = 0.84; 34 | pos_ax(4) = hgt_ax; 35 | set( gca, 'Position', pos_ax ) 36 | if i ~= n 37 | disp('removing x tick labels') 38 | set( gca, 'XTickLabel', [] ) 39 | end 40 | axis tight 41 | 42 | end 43 | 44 | if nargout < 1 45 | clear hax; 46 | end 47 | -------------------------------------------------------------------------------- /gppde/test_gppde_1d.m: -------------------------------------------------------------------------------- 1 | 2 | function test_gppde_1d 3 | 4 | X_g = (-5:0.5:10)'; 5 | X_y = []; %(10*rand(1,2) - 5)'; 6 | X_f = (-5:0.1:10)'; 7 | 8 | % Data 9 | theta = [10 1]; 10 | s2_y = theta(1)^2 * 1e-1; 11 | y = []; %theta(1)*sin(2*pi*X_y(:)/theta(2)) + sqrt(s2_y)*randn(size(X_y,2),1); 12 | 13 | % Define the partial differential equation 14 | N_g = size(X_g, 1); 15 | switch 2 16 | case 2 % f'' = -a*f 17 | D = [0; 2]; 18 | alpha = [1; 0.5]; 19 | end 20 | g = zeros(N_g,1); 21 | 22 | % Inference: Get posterior predictive distribution 23 | [m_f, V_f] = gppde(X_y, y, s2_y, theta, X_g, g, alpha, D, X_f); 24 | figure 25 | imagesc(V_f); 26 | 27 | % Draw posterior predictive samples 28 | I_f = speye(length(V_f)); 29 | s2_f = 1e-8; % numerical reasons 30 | L_f = chol(V_f + s2_f*I_f, 'lower'); 31 | f = bsxfun(@plus, L_f * randn(length(L_f),10), m_f); 32 | figure 33 | plot(X_f, f); 34 | -------------------------------------------------------------------------------- /gppde/test_gppde_3d.m: -------------------------------------------------------------------------------- 1 | 2 | function test_gppde_3d 3 | 4 | error('Not 3d yet') 5 | 6 | % $$$ x_g = -5:0.5:5; 7 | % $$$ [x1_g, x2_g] = meshgrid( 8 | T = 10; 9 | L = 10; 10 | [x1_g, x2_g] = meshgrid(linspace(-0.5*T,1.5*T,40), linspace(-0.5*L,1.5*L,40)); 11 | X_g = [x1_g(:), x2_g(:)]; 12 | %X_g = [unifrnd(-0.5*T,1.5*T,[2000,1]), unifrnd(-0.5*L,1.5*L,[2000,1])]; 13 | X_y = [unifrnd(0,T, [0,1]), unifrnd(0,L, [0,1])]; 14 | [x1_f, x2_f] = meshgrid(linspace(0,T,20), linspace(0,L,20)); 15 | X_f = [x1_f(:), x2_f(:)]; 16 | 17 | % Gaussian process 18 | theta = [10 1]; 19 | 20 | % Data 21 | s2_y = theta(1)^2 * 1e-5; 22 | y = + sqrt(s2_y)*randn(size(X_y,1),1); 23 | 24 | % Define the partial differential equation 25 | N_g = size(X_g, 1); 26 | D = [1 0 27 | 0 2]; 28 | alpha = [1 29 | -1]; 30 | g = zeros(N_g,1); 31 | 32 | % Inference: Get posterior predictive distribution 33 | [m_f, V_f] = gppde(X_y, y, s2_y, theta, X_g, g, alpha, D, X_f); 34 | % $$$ figure 35 | % $$$ imagesc(V_f); 36 | 37 | % Draw posterior predictive samples 38 | samples = 9; 39 | I_f = speye(length(V_f)); 40 | s2_f = 1e-8; % numerical reasons 41 | L_f = chol(V_f + s2_f*I_f, 'lower'); 42 | f = bsxfun(@plus, L_f * randn(length(L_f),samples), m_f); 43 | figure 44 | for i = 1:samples 45 | subplot(ceil(sqrt(samples)), ceil(sqrt(samples)), i); 46 | % pcolor(reshape(f(:,i), size(x1_f))); 47 | contourf(x1_f, x2_f, reshape(f(:,i), size(x1_f)), 100); 48 | shading('flat') 49 | end 50 | -------------------------------------------------------------------------------- /matrix_computations/adddiag.m: -------------------------------------------------------------------------------- 1 | function A = adddiag(A,v,k) 2 | %ADDDIAG Modify the diagonal(s) of a matrix. 3 | % ADDDIAG(A,V) 4 | % ADDDIAG(A,V,K) 5 | % 6 | % See also DIAG, FINDDIAG. 7 | 8 | if nargin < 3 9 | k = 0; 10 | end 11 | i = finddiag(A,k); 12 | A(i) = A(i) + v; 13 | -------------------------------------------------------------------------------- /matrix_computations/diagprod.m: -------------------------------------------------------------------------------- 1 | % DIAGPROD - Computes the diagonal elements of a matrix product. 2 | % 3 | % D = DIAGPROD(A,B) 4 | % 5 | % Evaluates diag(A*B') efficiently. A and B must have the same size. 6 | % 7 | % D = DIAGPROD(A) 8 | % 9 | % Evaluates diag(A*A') efficiently. 10 | 11 | % Last modified 2010-06-24 12 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 13 | 14 | function d = diagprod(A,B) 15 | 16 | if nargin < 2 17 | d = dot(A,A,2); 18 | else 19 | d = dot(A,B,2); 20 | end 21 | -------------------------------------------------------------------------------- /matrix_computations/finddiag.m: -------------------------------------------------------------------------------- 1 | function i = finddiag(A,k) 2 | %FINDDIAG Index elements on the diagonal. 3 | % FINDDIAG(A) returns the indices of the diagonal of A. 4 | % FINDDIAG(A,K) returns the indices of the K-th diagonal of A. 5 | % 6 | % See also DIAG, SETDIAG. 7 | 8 | % Written by Tom Minka 9 | % (c) Microsoft Corporation. All rights reserved. 10 | 11 | if nargin < 2 12 | k = 0; 13 | end 14 | n = length(A); 15 | if k >= 0 16 | i = (k*n+1):(n+1):(n^2); 17 | else 18 | i = (1-k):(n+1):(n*(n+k)); 19 | end 20 | -------------------------------------------------------------------------------- /matrix_computations/get_linsolve_cov.m: -------------------------------------------------------------------------------- 1 | function func = get_linsolve_cov(K) 2 | if issparse(K) 3 | [LD,p,q] = ldlchol(K); 4 | if p ~= 0 5 | figure 6 | imagesc(K); 7 | error('Matrix not positive definite') 8 | end 9 | func = @(x) linsolve_ldlchol(LD, x, q); 10 | else 11 | [L,p] = chol(K, 'lower'); 12 | if p ~= 0 13 | figure 14 | imagesc(K); 15 | error('Matrix not positive definite') 16 | end 17 | func = @(x) linsolve_lchol(L, x); 18 | end 19 | -------------------------------------------------------------------------------- /matrix_computations/inv_chol.m: -------------------------------------------------------------------------------- 1 | 2 | function U = inv_chol(U, type) 3 | 4 | if nargin < 2 5 | U = linsolve_chol(U,speye(size(U))); 6 | else 7 | U = linsolve_chol(U,speye(size(U)),type); 8 | end 9 | 10 | % $$$ if nargin >= 2 && strcmpi(type, 'lower') 11 | % $$$ else 12 | % $$$ X = linsolve_triu(U,speye(size(U))); 13 | % $$$ X = linsolve_triu(U,speye(size(U))); 14 | % $$$ % X = A*A'; 15 | % $$$ end 16 | 17 | -------------------------------------------------------------------------------- /matrix_computations/kronprod.m: -------------------------------------------------------------------------------- 1 | % KRONPROD - Evaluates a matrix-vector product when the matrix is a 2 | % Kronecker product of two matrices. 3 | % 4 | % Y = KRONPROD(A,B,X) 5 | % 6 | % Evaluates KRON(A,B)*VEC(X), where VEC(X)=X(:), as Y=B*X*A'. 7 | 8 | % 9 | % Y = KRONPROD(...,FORM) 10 | % 11 | % FORM must be either 'vector' or 'matrix'. It specifies in which form the 12 | % resulting Y is given. The default is 'matrix'. 13 | 14 | % Last modified 2010-11-11 15 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 16 | 17 | function Y = kronprod(A, B, X) 18 | % $$$ function Y = kronprod(A, B, X, form) 19 | 20 | %X(:,:) = B*X; 21 | %X(:,:) = X*A'; 22 | 23 | %X(:,:) = B*X*A'; 24 | Y = B*X*A'; 25 | 26 | return 27 | 28 | 29 | % $$$ if isnumeric(B) 30 | % $$$ Y = B*X; 31 | % $$$ else 32 | % $$$ Y = B(X); 33 | % $$$ end 34 | % $$$ if isnumeric(A) 35 | % $$$ Y = Y*A'; 36 | % $$$ else 37 | % $$$ Y = A(Y')'; 38 | % $$$ end 39 | % $$$ 40 | % $$$ % Y = B*X*A'; % original 41 | % $$$ 42 | % $$$ if nargin >= 4 && ~isempty(form) 43 | % $$$ if strcmpi(form,'vector') 44 | % $$$ Y = Y(:); 45 | % $$$ elseif ~strcmpi(form,'matrix') 46 | % $$$ error(['Requested the result in unknown form. Must be either ''vector'' ' ... 47 | % $$$ 'or ''matrix''']); 48 | % $$$ end 49 | % $$$ end 50 | % $$$ 51 | % $$$ 52 | % $$$ 53 | -------------------------------------------------------------------------------- /matrix_computations/ldlchol2lchol.m: -------------------------------------------------------------------------------- 1 | 2 | function L = ldlchol2lchol(LD) 3 | [L,D] = ldlsplit(LD); 4 | L = L * sqrt(D); 5 | -------------------------------------------------------------------------------- /matrix_computations/linsolve_chol.m: -------------------------------------------------------------------------------- 1 | % LINSOLVE_CHOL - Solves C*X=Y using the Cholesky decomposition of C. 2 | % 3 | % A function call 4 | % 5 | % X = LINSOLVE_CHOL(R,Y) 6 | % 7 | % assumes C=R'R. 8 | % 9 | % A function call 10 | % 11 | % X = LINSOLVE_CHOL(L,Y,'lower') 12 | % 13 | % assumes C=L*L'. 14 | % 15 | % R or L can be found using, e.g., the Cholesky decomposition CHOL. 16 | 17 | % Last modified 2010-10-18 18 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 19 | 20 | function Y = linsolve_chol(R,Y,type) 21 | 22 | if nargin >= 3 && strcmpi(type, 'lower') 23 | if issparse(R) 24 | Y = R' \ (R \ Y); 25 | else 26 | opts.LT = true; 27 | Y = linsolve(R, full(Y), opts); 28 | opts.TRANSA = true; 29 | Y = linsolve(R, Y, opts); 30 | end 31 | else 32 | if issparse(R) 33 | Y = R \ (R' \ Y); 34 | else 35 | opts.UT = true; 36 | opts.TRANSA = true; 37 | Y = linsolve(R, full(Y), opts); 38 | opts.TRANSA = false; 39 | Y = linsolve(R, Y, opts); 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /matrix_computations/linsolve_cov.m: -------------------------------------------------------------------------------- 1 | function Y = linsolve_cov(C,Y,type) 2 | % X = linsolve_cov(C,Y) 3 | % 4 | % Solves C*X = Y, where C is symmetric positive definite matrix. 5 | 6 | Y = linsolve_chol(chol(C),Y); 7 | -------------------------------------------------------------------------------- /matrix_computations/linsolve_kron.m: -------------------------------------------------------------------------------- 1 | % LINSOLVE_KRON - Solves a matrix-vector equation when the matrix is a 2 | % Kronecker product of two matrices. 3 | % 4 | % Solves KRON(K1,K2)*VEC(Y) = VEC(X). Usage: 5 | % 6 | % Y = LINSOLVE_KRON(K1,K2,X) 7 | % 8 | % K1 and/or K2 can be handles to functions which return the solutions K1\Z 9 | % and K2\Z for given matrix inputs Z. By default, MLDIVIDE is used. 10 | % 11 | % Y = LINSOLVE_KRON(...,FORM) 12 | % 13 | % FORM must be either 'vector' or 'matrix'. It specifies in which form the 14 | % resulting Y is given. The default is 'matrix'. 15 | % 16 | % Y must be in matrix form. 17 | % 18 | % See also KRON. 19 | 20 | % Last modified 2010-10-28 21 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 22 | 23 | function x = linsolve_kron(K1, K2, x, form); 24 | 25 | % x = reshape(x,size(K2,1),size(K1,1)); 26 | 27 | if isnumeric(K2) 28 | x = K2\x; 29 | else 30 | x = K2(x); 31 | end 32 | if isnumeric(K1) 33 | x = (K1\(x'))'; 34 | else 35 | x = K1(x')'; 36 | end 37 | 38 | if nargin >= 4 && ~isempty(form) 39 | if strcmpi(form,'vector') 40 | x = x(:); 41 | elseif ~strcmpi(form,'matrix') 42 | error(['Requested the result in unknown form. Must be either ''vector'' ' ... 43 | 'or ''matrix''']); 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /matrix_computations/linsolve_kron_ldlchol.m: -------------------------------------------------------------------------------- 1 | % LINSOLVE_KRON_LDLCHOL - Solves a matrix-vector equation when the matrix is 2 | % a Kronecker product of two sparse symmetric 3 | % positive definite matrices. 4 | % 5 | % Solves KRON(K1,K2)*VEC(Y) = VEC(X). Usage: 6 | % 7 | % Y = LINSOLVE_KRON_LDLCHOL(LD1,LD2,X) 8 | % 9 | % where LD1 = LDLCHOL(K1) and LD2 = LDLCHOL(K2). 10 | % 11 | % Y = LINSOLVE_KRON_LDLCHOL(...,FORM) 12 | % 13 | % FORM must be either 'vector' or 'matrix'. It specifies in which form the 14 | % resulting Y is given. The default is 'matrix'. 15 | % 16 | % See also LDLCHOL, KRON. 17 | 18 | % Last modified 2010-10-28 19 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 20 | 21 | function x = linsolve_kron_ldlchol(LD1, LD2, x, form); 22 | 23 | K2 = @(x) ldlsolve(LD2,x); 24 | K1 = @(x) ldlsolve(LD1,x); 25 | 26 | if nargin < 4 27 | x = linsolve_kron(K1,K2,x); 28 | else 29 | x = linsolve_kron(K1,K2,x,form); 30 | end 31 | -------------------------------------------------------------------------------- /matrix_computations/linsolve_lchol.m: -------------------------------------------------------------------------------- 1 | % LINSOLVE_CHOL - Solves C*X=Y using the Cholesky decomposition of C. 2 | % 3 | % A function call 4 | % 5 | % X = LINSOLVE_CHOL(R,Y) 6 | % 7 | % assumes C=R'R. 8 | % 9 | % A function call 10 | % 11 | % X = LINSOLVE_CHOL(L,Y,'lower') 12 | % 13 | % assumes C=L*L'. 14 | % 15 | % R or L can be found using, e.g., the Cholesky decomposition CHOL. 16 | 17 | % Last modified 2010-10-18 18 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 19 | 20 | function Y = linsolve_lchol(L,Y,q) 21 | 22 | % $$$ if nargin < 3 23 | % $$$ x = ldlsolve(LD,x); 24 | % $$$ else 25 | % $$$ x(q(:),:) = ldlsolve(LD,x(q(:),:)); 26 | % $$$ end 27 | 28 | if issparse(L) 29 | if nargin < 3 30 | Y = L' \ (L \ Y); 31 | else 32 | Y(q(:),:) = L' \ (L \ Y(q(:),:)); 33 | end 34 | else 35 | if nargin < 3 36 | opts.LT = true; 37 | Y = linsolve(L, full(Y), opts); 38 | opts.TRANSA = true; 39 | Y = linsolve(L, Y, opts); 40 | else 41 | error('Not yet implemented'); 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /matrix_computations/linsolve_ldlchol.m: -------------------------------------------------------------------------------- 1 | % LINSOLVE_LDLCHOL - Solves a matrix-vector equation when the matrix is a 2 | % sparse symmetric positive definite matrix. 3 | % 4 | % Solves K*Y = X. Usage: 5 | % 6 | % Y = LINSOLVE_LDLCHOL(LD,X) 7 | % 8 | % where LD = LDLCHOL(K). 9 | % 10 | % The function is just a simple wrapper in accordance with the naming 11 | % conventions. 12 | % 13 | % See also LDLCHOL, LDLSOLVE. 14 | 15 | % Last modified 2011-01-31 16 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 17 | 18 | function x = linsolve_ldlchol(LD,x,q) 19 | 20 | %fprintf('Sparsity in linsolve_ldlchol: %f\n', sparsity(LD)); 21 | 22 | if nargin < 3 23 | x = ldlsolve(LD,x); 24 | else 25 | x(q(:),:) = ldlsolve(LD,x(q(:),:)); 26 | end 27 | -------------------------------------------------------------------------------- /matrix_computations/linsolve_neumann.m: -------------------------------------------------------------------------------- 1 | % Approximates X = INV(A)*Y by a truncated series. 2 | function x = linsolve_neumann(A,y,D,n) 3 | 4 | if nargin < 3 && isnumeric(A) 5 | tol = 1e-6; 6 | D = (1+tol)*normest(A,tol); % this is probably veeeery bad... 7 | end 8 | if nargin < 4 9 | n = 100; 10 | end 11 | 12 | if isnumeric(D) 13 | if numel(D) == 1 14 | D = D*speye(length(y)); 15 | end 16 | D = @(x) D\x; 17 | end 18 | 19 | if isnumeric(A) 20 | A = @(x) A*x; 21 | end 22 | 23 | z = D(y); 24 | x = z; 25 | for i=1:n 26 | z = z - D(A(z)); 27 | x = x + z; 28 | end -------------------------------------------------------------------------------- /matrix_computations/linsolve_tril.m: -------------------------------------------------------------------------------- 1 | function X = linsolve_tril(L,Y) 2 | % function X = LINSOLVE_TRIL(L, Y) 3 | % 4 | % Solves X = L \ Y, or L*X = Y, when L is upper triangular. 5 | 6 | % (c) 2010 Jaakko Luttinen 7 | 8 | if issparse(L) 9 | X = L \ Y; 10 | else 11 | opts.LT = true; 12 | X = linsolve(L, full(Y), opts); 13 | end 14 | -------------------------------------------------------------------------------- /matrix_computations/linsolve_triu.m: -------------------------------------------------------------------------------- 1 | function X = linsolve_triu(R, Y, transpose) 2 | % function X = LINSOLVE_TRIU(R, Y) 3 | % 4 | % Solves X = R \ Y, or R*X = Y, when R is upper triangular. 5 | 6 | % (c) 2010 Jaakko Luttinen 7 | 8 | if issparse(R) 9 | X = R \ Y; 10 | else 11 | opts.UT = true; 12 | if nargin >= 3 && transpose 13 | opts.UT = false; 14 | opts.LT = true; 15 | opts.TRANSA = true; 16 | end 17 | X = linsolve(R, full(Y), opts); 18 | end 19 | -------------------------------------------------------------------------------- /matrix_computations/logdet.m: -------------------------------------------------------------------------------- 1 | % LOGDET - Computes the log-determinant of a square matrix X. 2 | % 3 | % Y = LOGDET(X) 4 | % 5 | % More precisely, the function takes the logarithm with respect to the 6 | % absolute value of the determinant, i.e., Y = LOG(ABS(DET(X))). However, 7 | % the function uses QR decomposition for stable and efficient performance. 8 | 9 | % Last modified 2010-06-04 10 | % Copyright (c) Jaakko Luttinen 11 | 12 | function y = logdet(X) 13 | 14 | [Q,R] = qr(X); 15 | y = sum(log(abs(diag(R)))); -------------------------------------------------------------------------------- /matrix_computations/logdet_chol.m: -------------------------------------------------------------------------------- 1 | % LOGDET_CHOL - Computes the log-determinant of a matrix using its 2 | % Cholesky factor. 3 | % 4 | % Y = LOGDET_CHOL(U) 5 | % 6 | % Computes the log-determinant of the matrix X whose Cholesky factor is 7 | % the triangular matrix U. U can be lower or upper triangular. 8 | % 9 | % More generally, X could be a product of two instances of U and/or U'. For 10 | % instance, X=U'*U' or X=U'*U. 11 | 12 | % Last modified 2010-06-04 13 | % Copyright (c) Jaakko Luttinen 14 | 15 | function y = logdet_chol(U) 16 | 17 | y = 2 * logdet_tri(U); -------------------------------------------------------------------------------- /matrix_computations/logdet_cov.m: -------------------------------------------------------------------------------- 1 | % LOGDET_COV - Computes the log-determinant of a positive-definite, symmetric 2 | % matrix SIGMA. 3 | % 4 | % X = LOGDET_COV(SIGMA) 5 | % 6 | % The function uses the Cholesky decomposition of SIGMA for stable and 7 | % efficient computations. 8 | 9 | % Last modified 2010-06-04 10 | % Copyright (c) Jaakko Luttinen 11 | 12 | function x = logdet_cov(Sigma) 13 | 14 | x = logdet_chol(chol(Sigma)); -------------------------------------------------------------------------------- /matrix_computations/logdet_diag.m: -------------------------------------------------------------------------------- 1 | % LOGDET_DIAG - Computes the log-determinant of a diagonal matrix. 2 | % 3 | % Y = LOGDET_DIAG(D) 4 | % 5 | % D must be a diagonal matrix. 6 | 7 | % Last modified 2011-01-24 8 | % Copyright (c) Jaakko Luttinen 9 | 10 | function y = logdet_diag(D) 11 | 12 | y = sum(log(diag(D))); -------------------------------------------------------------------------------- /matrix_computations/logdet_kron_ldlchol.m: -------------------------------------------------------------------------------- 1 | % LOGDET_KRON_LDLCHOL - Computes the log-determinant of a matrix using its 2 | % Cholesky factor from SuiteSparse. 3 | % 4 | % Y = LOGDET_LDLCHOL(LD1,LD2) 5 | % 6 | % Computes the log-determinant of the matrix X whose Cholesky factor is 7 | % the triangular matrix U. U can be lower or upper triangular. 8 | % 9 | % More generally, X could be a product of two instances of U and/or U'. For 10 | % instance, X=U'*U' or X=U'*U. 11 | 12 | % Last modified 2010-11-09 13 | % Copyright (c) Jaakko Luttinen 14 | 15 | function y = logdet_ldlchol(LD1,LD2) 16 | 17 | N1 = size(LD1,1); 18 | N2 = size(LD2,1); 19 | 20 | y = N2*logdet_tri(LD1) + N1*logdet_tri(LD2); -------------------------------------------------------------------------------- /matrix_computations/logdet_ldlchol.m: -------------------------------------------------------------------------------- 1 | % LOGDET_LDLCHOL - Computes the log-determinant of a matrix using its 2 | % Cholesky factor from SuiteSparse. 3 | % 4 | % Y = LOGDET_LDLCHOL(LD) 5 | % 6 | % Computes the log-determinant of the matrix X whose Cholesky factor is 7 | % the triangular matrix U. U can be lower or upper triangular. 8 | % 9 | % More generally, X could be a product of two instances of U and/or U'. For 10 | % instance, X=U'*U' or X=U'*U. 11 | 12 | % Last modified 2010-06-04 13 | % Copyright (c) Jaakko Luttinen 14 | 15 | function y = logdet_ldlchol(LD) 16 | 17 | y = logdet_tri(LD); -------------------------------------------------------------------------------- /matrix_computations/logdet_tri.m: -------------------------------------------------------------------------------- 1 | % LOGDET_TRI - Computes the log-determinant of a triangular matrix. 2 | % 3 | % Y = LOGDET_TRI(U) 4 | % 5 | % U must be either lower or upper triangular square matrix. 6 | 7 | % Last modified 2010-06-04 8 | % Copyright (c) Jaakko Luttinen 9 | 10 | function y = logdet_tri(U) 11 | 12 | y = sum(log(diag(U))); -------------------------------------------------------------------------------- /matrix_computations/mdiag.m: -------------------------------------------------------------------------------- 1 | % D = MDIAG(X) 2 | % 3 | % Returns the diagonals of the matrices in X. 4 | % 5 | % X is M x N x K matrix. 6 | % 7 | % D is min(M,N) x K matrix having the diagonals of X(:,:,k) k=1,...,K as 8 | % column vectors. 9 | 10 | % Copyright (c) 2010 Jaakko Luttinen 11 | 12 | function D = mdiag(X) 13 | 14 | [M,N,K] = size(X); 15 | 16 | D = zeros(min(M,N),K); 17 | for k=1:K 18 | D(:,k) = diag(X(:,:,k)); 19 | end 20 | -------------------------------------------------------------------------------- /matrix_computations/mtraceprod.m: -------------------------------------------------------------------------------- 1 | function X = mtraceprod(A,B) 2 | % X = MTRACEPROD(A, B) 3 | % 4 | % Evaluates trace(A*B') efficiently. 5 | % 6 | % X = MTRACEPROD(A) 7 | % 8 | % Evaluates trace(A*A') efficiently. 9 | % 10 | % If A and/or B has third dimension, the third dimension of A results in 11 | % rows to X and the third dimension of B columns to X: 12 | % 13 | % X(I,J) = trace(A(:,:,I)*B(:,:,J)') 14 | 15 | %warning('Deprecated.'); 16 | 17 | if nargin < 2 18 | [M,N,D] = size(A); 19 | X = reshape(A,[M*N,D])' * reshape(A,[M*N,D]); 20 | else 21 | [M,N,Da] = size(A); 22 | [M,N,Db] = size(B); 23 | X = reshape(A,[M*N,Da])' * reshape(B,[M*N,Db]); 24 | end -------------------------------------------------------------------------------- /matrix_computations/setdiag.m: -------------------------------------------------------------------------------- 1 | function A = setdiag(A,v,k) 2 | %SETDIAG Modify the diagonal(s) of a matrix. 3 | % SETDIAG(A,V) returns a copy of A where the main diagonal is set to V. 4 | % V can be a scalar or vector. 5 | % SETDIAG(A,V,K) sets the K-th diagonal to V. The K-th diagonal has length 6 | % N-ABS(K). 7 | % 8 | % See also DIAG, FINDDIAG. 9 | 10 | if nargin < 3 11 | k = 0; 12 | end 13 | A(finddiag(A,k)) = v; 14 | -------------------------------------------------------------------------------- /matrix_computations/sparsity.m: -------------------------------------------------------------------------------- 1 | % SPARSITY - Evaluates the sparsity of a matrix. 2 | % 3 | % D = SPARSITY(X) 4 | % 5 | % D is the relative number of non-zero elements in the matrix X. For 6 | % instance, D=0 for a matrix of zeros and D=1 for a matrix of no zeros. 7 | 8 | function d = sparsity(X) 9 | 10 | if issparse(X) 11 | d = nnz(X) / numel(X); 12 | else 13 | d = sum(~isnan(X(:))) / numel(X); 14 | end -------------------------------------------------------------------------------- /matrix_computations/spdiag.m: -------------------------------------------------------------------------------- 1 | % SPDIAG - Creates a sparse diagonal matrix from a given vector. 2 | % 3 | % D = SPDIAG(X) 4 | % 5 | % D is a sparse matrix that has the vector X on its main diagonal. 6 | % 7 | % D = SPDIAG(X,I) 8 | % 9 | % D is a sparse matrix that has the vector X on its I-th diagonal. 10 | 11 | % Last modified 2010-06-18 12 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 13 | 14 | function D = spdiag(d,i) 15 | 16 | if nargin < 2 || (isscalar(i) && i==0) 17 | m = numel(d); 18 | D = sparse(1:m, 1:m, d, m, m); 19 | else 20 | m = numel(d) + abs(i); 21 | if i >= 0 22 | D = sparse(1:numel(d),(1:numel(d))+i,d,m,m); 23 | else 24 | D = sparse((1:numel(d))-i,(1:numel(d)),d,m,m); 25 | end 26 | end 27 | 28 | -------------------------------------------------------------------------------- /matrix_computations/sptoeplitz.m: -------------------------------------------------------------------------------- 1 | function T = sptoeplitz(col,row) 2 | % SPTOEPLITZ Sparse Toeplitz matrix. 3 | % SPTOEPLITZ(C,R) produces a sparse nonsymmetric Toeplitz matrix having 4 | % C as its first column and R as its first row. Neither C nor R needs to 5 | % be sparse. No full-size dense matrices are formed. 6 | % 7 | % SPTOEPLITZ(R) is a sparse symmetric/Hermitian Toeplitz matrix. 8 | % 9 | % Examples: 10 | % sptoeplitz( real( (1i).^(0:8) ) ) % 9x9, 41 nonzeros 11 | % sptoeplitz( [-2 1 zeros(1,9998)] ); % classic 2nd difference 12 | % 13 | % See also TOEPLITZ, SPDIAGS. 14 | 15 | % Copyright (c) 2006 by Tobin Driscoll (tobin.driscoll@gmail.com). 16 | % First version, 11 December 2006. 17 | 18 | % This part is borrowed from built-in Toeplitz. 19 | if nargin < 2 % symmetric case 20 | col(1) = conj(col(1)); row = col; col = conj(col); 21 | else 22 | if col(1)~=row(1) 23 | warning('MATLAB:sptoeplitz:DiagonalConflict',['First element of ' ... 24 | 'input column does not match first element of input row. ' ... 25 | '\n Column wins diagonal conflict.']) 26 | end 27 | end 28 | 29 | % Size of result. 30 | m = length(col(:)); n = length(row(:)); 31 | 32 | % Locate the nonzero diagonals. 33 | [ic,jc,sc] = find(col(:)); 34 | row(1) = 0; % not used 35 | [ir,jr,sr] = find(row(:)); 36 | 37 | % Use spdiags for construction. 38 | d = [ ir-1; 1-ic ]; 39 | B = repmat( [ sr; sc ].', min(m,n),1 ); 40 | T = spdiags( B,d,m,n ); 41 | -------------------------------------------------------------------------------- /matrix_computations/sqrt_cov.m: -------------------------------------------------------------------------------- 1 | % SQRT_COV - Factorizes a positive semi-definite symmetric matrix. 2 | % 3 | % V = SQRT_COV(A) 4 | % 5 | % The matrix A must be positive semi-definite and symmetric. The resulting 6 | % matrix V is such that V*V'=A. 7 | 8 | % Last modified 2011-10-20 9 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@aalto.fi) 10 | 11 | function V = sqrt_cov(A) 12 | 13 | [L,D] = ldl(A); 14 | D(D<0) = 0; 15 | V = L * sqrt(D); 16 | -------------------------------------------------------------------------------- /matrix_computations/takagi.m: -------------------------------------------------------------------------------- 1 | % TAKAGI - Factorizes a positive semi-definite symmetric matrix. 2 | % 3 | % V = TAKAGI(A) 4 | % 5 | % The matrix A must be positive semi-definite and symmetric. The resulting 6 | % matrix V is such that V*V'=A. 7 | % 8 | % Note: This is not really Takagi decomposition but I couldn't think of a 9 | % better name.. :( 10 | 11 | % Last modified 2010-06-04 12 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 13 | 14 | function V = takagi(A) 15 | 16 | [L,D] = ldl(A); 17 | V = L * sqrt(D); 18 | -------------------------------------------------------------------------------- /matrix_computations/traceinv_chol.m: -------------------------------------------------------------------------------- 1 | function y = traceinv_chol(U, type) 2 | 3 | if nargin >= 2 && strcmpi(type, 'lower') 4 | A = linsolve_tril(U,speye(size(U))); 5 | else 6 | A = linsolve_triu(U,speye(size(U))); 7 | end 8 | 9 | y = traceprod(A,A); -------------------------------------------------------------------------------- /matrix_computations/traceinv_qr.m: -------------------------------------------------------------------------------- 1 | function y = traceinv_qr(Q,R) 2 | 3 | A = linsolve_triu(R,speye(size(R))); 4 | y = traceprod(A,Q); -------------------------------------------------------------------------------- /matrix_computations/traceprod.m: -------------------------------------------------------------------------------- 1 | % X = TRACEPROD(A, B) 2 | % 3 | % Evaluates trace(A*B') efficiently. 4 | 5 | % Last modified 2010-06-08 6 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 7 | 8 | function x = traceprod(A,B) 9 | 10 | 11 | if any(size(A)~=size(B)) 12 | error('The input matrices must have the same number of rows and columns.'); 13 | end 14 | 15 | if nargin < 2 16 | warning('Do not use like this.'); 17 | x = A(:)'*A(:); 18 | else 19 | if issparse(A) || issparse(B) 20 | x = full(sum(sum(A.*B))); 21 | else 22 | x = A(:)'*B(:); 23 | end 24 | end -------------------------------------------------------------------------------- /matrix_computations/traceprodm.m: -------------------------------------------------------------------------------- 1 | % X = TRACEPRODM(A,B) 2 | % 3 | % Evaluates TRACE(A*B') efficiently. 4 | % 5 | % If A and/or B have a third dimension, the function returns a vector of 6 | % traces as: 7 | % 8 | % X(I) = TRACE(A(:,:,I)*B(:,:,I)') 9 | % 10 | % A or B is kept fixed if it does not have a third dimension. 11 | 12 | % Last modified 2010-06-08 13 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 14 | 15 | function x = traceprodm(A,B) 16 | 17 | warning('Deprecated?'); 18 | 19 | [rows_A, cols_A, pages_A] = size(A); 20 | [rows_B, cols_B, pages_B] = size(B); 21 | 22 | if rows_A~=rows_B || cols_A~=cols_B 23 | error('The input matrices must have the same number of rows and columns.'); 24 | end 25 | 26 | if pages_A~=pages_B && pages_A>1 && pages_B>1 27 | error('Non-singular third dimensions must agree.'); 28 | end 29 | 30 | x = sum( bsxfun(@times, ... 31 | reshape(A,[rows_A*cols_A,pages_A]), ... 32 | reshape(B,[rows_B*cols_A,pages_B])), 1 )'; 33 | -------------------------------------------------------------------------------- /matrix_computations/vec.m: -------------------------------------------------------------------------------- 1 | 2 | function x = vec(X) 3 | x = X(:); -------------------------------------------------------------------------------- /mcmc/mcmc_ess_acorr.m: -------------------------------------------------------------------------------- 1 | 2 | % Effective sample size (ESS) based on autocorrelation 3 | % 4 | % Kass et al (1998), no, not this one 5 | 6 | function Nh = mcmc_ess_acorr(x) 7 | 8 | x = x(:); 9 | 10 | N = length(x); 11 | xh = mean(x); 12 | 13 | g = zeros(size(x)); 14 | for k=0:(N-1) 15 | % g(k+1) = 1/(N-k) * (x(1:(N-k))-xh)'*(x((1+k):end)); 16 | g(k+1) = 1/N * (x(1:(N-k))-xh)'*(x((1+k):end)-xh); 17 | end 18 | 19 | 20 | % $$$ % Cut-off 21 | % $$$ rho = g/g(1); 22 | % $$$ d = find(rho<0.01,1); 23 | % $$$ tau = sum(rho(1:d)); 24 | % $$$ Nh = N / tau; 25 | % $$$ figure 26 | % $$$ plot(rho) 27 | 28 | % $$$ figure 29 | % $$$ plot(g/g(1)) 30 | 31 | %g_even = g(1:2:(end-1)); 32 | %g_odd = g(2:2:end); 33 | %d = find(g_even+g_odd<=0,1) 34 | 35 | d = find((g/g(1))<0.01, 1); 36 | if isempty(d) 37 | d = length(g); 38 | end 39 | 40 | % $$$ g_sum = g(1:(end-1)) + g(2:end); 41 | % $$$ d = find(g_sum<=0, 1); 42 | % $$$ if isempty(d) 43 | % $$$ d = length(g); 44 | % $$$ end 45 | 46 | %d 47 | %v = 1/N * (g(1) + 2*sum(g(2:(2*d+1)))); 48 | v = 1/N * (g(1) + 2*sum(g(2:(d-1)))); 49 | Nh = g(1) / v; 50 | %[~,d] = find( 51 | 52 | 53 | % error('not implemented') -------------------------------------------------------------------------------- /mcmc/mcmc_init_metropolishastings.m: -------------------------------------------------------------------------------- 1 | function [func, fx] = mcmc_init_metropolishastings(x_init, get_logpdf, q, ... 2 | logpdf_q, func_x) 3 | 4 | if nargin < 5 5 | func_x = @(x) x; 6 | end 7 | 8 | x_current = x_init; 9 | fx_current = func_x(x_current); 10 | logpdf_current = get_logpdf(fx_current); 11 | 12 | func = @metropolishastings; 13 | if nargout >= 2 14 | fx = fx_current; 15 | end 16 | 17 | function [x, fx] = metropolishastings(varargin) 18 | 19 | % Draw proposal 20 | x_proposal = q(x_current); 21 | fx_proposal = func_x(x_proposal); 22 | logpdf_proposal = get_logpdf(fx_proposal); 23 | lp_proposal = logpdf_proposal(varargin{:}); 24 | 25 | % Compare to the current value 26 | lp_current = logpdf_current(varargin{:}); 27 | r = (lp_proposal - logpdf_q(x_proposal, x_current)) - ... 28 | (lp_current - logpdf_q(x_current, x_proposal)); 29 | if log(rand) < r 30 | % Accept 31 | %disp('accept') 32 | x_current = x_proposal; 33 | fx_current = fx_proposal; 34 | logpdf_current = logpdf_proposal; 35 | else 36 | % Reject (do nothing to the current values) 37 | %disp('reject') 38 | end 39 | x = x_current; 40 | fx = fx_current; 41 | end 42 | end 43 | 44 | -------------------------------------------------------------------------------- /mcmc/mcmc_init_metropolishastings_backup.m: -------------------------------------------------------------------------------- 1 | function func = mcmc_init_metropolishastings(x_init, get_logpdf, q, logpdf_q, ... 2 | func_x) 3 | 4 | if nargin < 6 5 | samplefunc = []; 6 | end 7 | x_current = x_init; 8 | fx_current = func_x(x_current); 9 | logpdf_current = get_logpdf(x_current, fx_current); 10 | 11 | func = @metropolishastings; 12 | 13 | function [x, fx] = metropolishastings(varargin) 14 | 15 | %figure 16 | %rmse = sqrt(mean(mean((varargin{1}-varargin{2}).^2))) 17 | %imagesc(varargin{1}) 18 | %map_colormap(); 19 | %error('jou') 20 | 21 | % Draw proposal 22 | x_proposal = q(x_current); 23 | fx_proposal = func_x(x_proposal); 24 | logpdf_proposal = get_logpdf(x_proposal, fx_proposal); 25 | lp_proposal = logpdf_proposal(varargin{:}); 26 | 27 | % Compare to the current value 28 | lp_current = logpdf_current(varargin{:}); 29 | r = (lp_proposal - logpdf_q(x_proposal, x_current)) - ... 30 | (lp_current - logpdf_q(x_current, x_proposal)); 31 | if log(rand) < r 32 | % Accept 33 | disp('accept') 34 | fx_current = fx_proposal; 35 | x_current = x_proposal; 36 | logpdf_current = logpdf_proposal; 37 | else 38 | % Reject (do nothing to the current values) 39 | disp('reject') 40 | end 41 | x = x_current; 42 | fx = fx_current; 43 | end 44 | end 45 | 46 | -------------------------------------------------------------------------------- /optimization/conjgrad.m: -------------------------------------------------------------------------------- 1 | % x = conjgrad(A,b,x,I) 2 | % A must be symmetric positive definite 3 | 4 | function x = conjgrad(A,b,x,I,varargin) 5 | 6 | options = struct( ... 7 | 'maxiter', 100, ... 8 | 'tol', 1e-6); 9 | 10 | % Parse arguments 11 | [options, errmsg] = argparse( options, varargin{:} ); 12 | error(errmsg); 13 | 14 | if nargin < 4 || isempty(x) 15 | x = zeros(size(b)); 16 | end 17 | 18 | if nargin < 5 || isempty(I) 19 | I = true(size(b)); 20 | end 21 | 22 | if isnumeric(A) 23 | A = @(x) A*x; 24 | end 25 | 26 | r = b-A(x); 27 | r(~I) = 0; 28 | 29 | p = r; 30 | rsold=r(I)'*r(I); 31 | 32 | %Ap = zeros(size(x)); 33 | 34 | for i=1:options.maxiter 35 | Ap = A(p); 36 | Ap(~I) = 0; 37 | 38 | alpha = rsold/(p(:)'*Ap(:)); 39 | x(:) = x(:) + alpha*p(:); 40 | r(:) = r(:) - alpha*Ap(:); 41 | rsnew = r(:)'*r(:); 42 | if sqrt(rsnew) < options.tol 43 | break; 44 | end 45 | p(:) = r(:) + (rsnew/rsold)*p(:); 46 | rsold = rsnew; 47 | end 48 | -------------------------------------------------------------------------------- /optimization/conjgrad_kron.m: -------------------------------------------------------------------------------- 1 | % x = conjgrad_kron(A1,A2,b,x,I) 2 | % A1 and A2 must be symmetric positive definite 3 | 4 | function x = conjgrad_kron(A1,A2,b,x,I,opts) 5 | 6 | options = struct( ... 7 | 'maxiter', 100, ... 8 | 'tol', 1e-6); 9 | 10 | % Parse arguments 11 | [options, errmsg] = argparse( options, varargin{:} ); 12 | error(errmsg); 13 | 14 | if nargin < 4 || isempty(x) 15 | x = zeros(size(b)); 16 | end 17 | 18 | if nargin < 5 || isempty(I) 19 | I = true(size(b)); 20 | end 21 | 22 | r=b-kronprod(A1,A2,x); 23 | r(~I) = 0; 24 | 25 | p=r; 26 | rsold=r(I)'*r(I); 27 | 28 | Ap = zeros(size(x)); 29 | 30 | for i=1:opts.maxiter 31 | Ap(:,:) = A2*p; 32 | Ap(:,:) = Ap*A1'; 33 | 34 | alpha=rsold/(p(I)'*Ap(I)); 35 | x(I)=x(I)+alpha*p(I); 36 | r(I)=r(I)-alpha*Ap(I); 37 | rsnew=r(I)'*r(I); 38 | if sqrt(rsnew) < opts.tol 39 | break; 40 | end 41 | p(I)=r(I)+(rsnew/rsold)*p(I); 42 | rsold=rsnew; 43 | end 44 | -------------------------------------------------------------------------------- /optimization/minimize_binary.m: -------------------------------------------------------------------------------- 1 | function x = minimize_binary(f,df,x0) 2 | % Binary search for minimizing a unimodal univariate function with a 3 | % strictly monotonically increasing derivative. 4 | 5 | step = 1; 6 | dy0 = feval(df,x0); 7 | s0 = sign(dy0); 8 | s1 = s0; 9 | 10 | % Find boundary [x0,x1] (or [x1,x0]) inside which the optimum lies. 11 | while s1 ~= -s0 12 | x1 = x0; 13 | x0 = x0 - s0 * step; 14 | dy1 = dy0; 15 | dy0 = feval(df,x0); 16 | s0 = sign(dy0); 17 | step = 2*step; 18 | end 19 | [x0, x1] 20 | 21 | x = fzero( 22 | 23 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 24 | 25 | % Binary search within the boundary 26 | for m=1:10 27 | x = 0.5*(x0+x1); 28 | dy = feval(df,x); 29 | if sign(dy) == s0 30 | x0 = x; 31 | else 32 | x1 = x; 33 | end 34 | end 35 | y1 = feval(f,x1); 36 | y0 = feval(f,x0); 37 | 38 | [x0, x1] 39 | 40 | % Finally, try linear interpolation between the boundary points 41 | x = (abs(dy1)*x0+abs(dy0)*x1)/(abs(dy1)+abs(dy0)); 42 | y = feval(f,x); 43 | 44 | [x0, x1, x] 45 | 46 | % Select the optimum 47 | if y0 < y 48 | x = x0; 49 | elseif y1 < y 50 | x = x1; 51 | end 52 | 53 | -------------------------------------------------------------------------------- /optimization/mycheckgrad.m: -------------------------------------------------------------------------------- 1 | function d = mycheckgrad(f, x, e, varargin); 2 | % d = mycheckgrad(f, x, e); 3 | 4 | [y, dy] = feval(f, x, varargin{:}); % get the partial derivatives dy 5 | n = length(x); 6 | 7 | dh = zeros(n,1) ; 8 | for j = 1:n 9 | dx = zeros(n,1); 10 | dx(j) = dx(j) + e; % perturb a single dimension 11 | y2 = feval(f, x+dx, varargin{:}); 12 | y1 = feval(f, x-dx, varargin{:}); 13 | dh(j) = (y2 - y1)/(2*e); 14 | fprintf('%+.4e \t %+.4e \t %+.4e \t (function, numerical, x)\n', dy(j), dh(j), x(j)); 15 | end 16 | 17 | %disp([dy dh]) % print the two vectors 18 | %[dy dh] 19 | d = norm(dh-dy)/norm(dh+dy); % return norm of diff divided by norm of sum 20 | -------------------------------------------------------------------------------- /optimization/test_conjgradmv.m: -------------------------------------------------------------------------------- 1 | function test_conjgradmv() 2 | 3 | N = 100; 4 | x = rand(N,1); 5 | A = sparse(double(rand(N) < 0.1)); 6 | K = A*A' + speye(N); 7 | 8 | ind = 1:2:N; 9 | 10 | xmv = x(ind); 11 | Kmv = K(ind,ind); 12 | ymv = Kmv*xmv; 13 | y = zeros(N,1); 14 | y(ind) = ymv; 15 | I = false(N,1); 16 | I(ind) = true; 17 | 18 | x_pcg = pcg(Kmv,ymv,1e-6,100); 19 | x_cg = conjgradmv(K,y,I,'tol',1e-6,'maxiter',100,'verbose',true); 20 | 21 | norm(x_pcg-x_cg(ind)) 22 | -------------------------------------------------------------------------------- /pca/alex2jaakko.m: -------------------------------------------------------------------------------- 1 | function Q = alex2jaakko(A) 2 | Q.W = A.A; 3 | Q.X = A.S; 4 | Q.mu = A.Mu; 5 | 6 | if ~isempty(A.Av) 7 | D = rows(A.Av{1}); 8 | M = length(A.Av); 9 | Q.CovW = zeros([D,D,M]); 10 | for m=1:M 11 | Q.CovW(:,:,m) = A.Av{m}; 12 | end 13 | end 14 | if ~isempty(A.Sv) 15 | D = rows(A.Sv{1}); 16 | N = length(A.Sv); 17 | Q.CovX = zeros([D,D,N]); 18 | for n=1:N 19 | Q.CovW(:,:,n) = A.Sv{n}; 20 | end 21 | end 22 | 23 | if isempty(A.Muv) 24 | Q.v_mu = 0; 25 | else 26 | Q.v_mu = A.Muv; 27 | end 28 | -------------------------------------------------------------------------------- /pca/covarray_to_covcell.m: -------------------------------------------------------------------------------- 1 | function Covcell = covarray_to_covcell(Covarray) 2 | 3 | if isnumeric(Covarray) 4 | [M,N,D] = size(Covarray); 5 | Covcell = cell(D,1); 6 | for d=1:D 7 | Covcell{d} = Covarray(:,:,d); 8 | end 9 | end -------------------------------------------------------------------------------- /pca/covcell_to_covarray.m: -------------------------------------------------------------------------------- 1 | function Covarray = covcell_to_covarray(Covcell) 2 | 3 | if iscell(Covcell) 4 | sz = size(Covcell{1}); 5 | N = length(Covcell); 6 | Covarray = zeros([sz, N]); 7 | for n=1:N 8 | Covarray(:,:,n) = Covcell{n}; 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /pca/rppcamv_loglikelihood.m: -------------------------------------------------------------------------------- 1 | function loglike = rppcamv_loglikelihood(Y, W, X, invB, mu, tau, a_u, b_u) 2 | 3 | if nargin == 2 4 | Q = W; 5 | W = Q.W; 6 | X = Q.X; 7 | mu = Q.mu; 8 | tau = Q.tau; 9 | a_u = Q.a_u; 10 | b_u = Q.b_u; 11 | invB = Q.invB; 12 | end 13 | 14 | [m,n] = size(Y); 15 | d = size(W,2); 16 | 17 | N = 1:n; 18 | loglike = 0; 19 | 20 | if isempty(X) 21 | X = zeros(d,n); 22 | end 23 | if isempty(invB) 24 | invB = repmat(eye(d), [1,1,n]); 25 | end 26 | 27 | for j=1:n 28 | imv = ~isnan(Y(:,j)); 29 | if any(imv) 30 | mean_y = W(imv,:)*X(:,j) + mu(imv); 31 | Cov_y = b_u(j)/a_u(j) * (W(imv,:)*invB(:,:,j)*W(imv,:)'+eye(sum(imv))/tau); 32 | nu_y = 2*a_u(j); 33 | loglike = loglike + mstud_lpdf(Y(imv,j), mean_y, Cov_y, nu_y); 34 | end 35 | end 36 | 37 | -------------------------------------------------------------------------------- /pca/vb_rotationcost_gamma.m: -------------------------------------------------------------------------------- 1 | % 2 | % P(ALPHA) = GAMMA(A0,B0) 3 | % 4 | % Q(ALPHA) = GAMMA(A,B), 5 | % 6 | % where B = B(THETA). 7 | % 8 | % KL(Q||P) w.r.t. THETA up to a constant and the gradient. 9 | 10 | function [KL,dKL] = vb_rotationcost_gamma(A0_logB, ... 11 | A_B0_invB, ... 12 | grad_A0_logB, ... 13 | grad_A_B0_invB) 14 | 15 | KL = A0_logB + A_B0_invB; 16 | dKL = grad_A0_logB + grad_A_B0_invB; 17 | -------------------------------------------------------------------------------- /pca/vbpca_rotate.m: -------------------------------------------------------------------------------- 1 | function [W,CovW,X,CovX] = vbpca_rotate(R, W, CovW, X, CovX) 2 | % Rotate W*R and R\X 3 | 4 | % Rotate W 5 | W = W * R; 6 | if ~isempty(CovW) 7 | if iscell(CovW) 8 | for i=1:length(CovW) 9 | CovW{i} = R'*CovW{i}*R; 10 | end 11 | else 12 | for i=1:size(CovW,3) 13 | CovW(:,:,i) = R'*CovW(:,:,i)*R; 14 | end 15 | end 16 | end 17 | 18 | % Rotate X 19 | X = R \ X; 20 | if ~isempty(CovX) 21 | if iscell(CovX) 22 | for j = 1:length(CovX) 23 | CovX{j} = (R \ (R\CovX{j})')'; 24 | end 25 | else 26 | for j = 1:size(CovX,3) 27 | CovX(:,:,j) = (R \ (R\CovX(:,:,j))')'; 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /plotting/addtsplot.m: -------------------------------------------------------------------------------- 1 | % ADDTSPLOT - Add time series to current figure 2 | % 3 | % ADDTSPLOT(Y) adds time series in the rows of Y to the current 4 | % figure which should be created by TSPLOT. Y can be followed by 5 | % parameter/value pairs to specify additional properties of the 6 | % lines. 7 | % 8 | % See also TSPLOT 9 | 10 | % This software is provided "as is", without warranty of any kind. 11 | % Alexander Ilin, Tapani Raiko 12 | 13 | function hax = addtsplot( y, varargin ) 14 | 15 | if nargin >= 2 & isnumeric(varargin{1}) 16 | x = y; 17 | y = varargin{1}; 18 | varargin = {varargin{2:end}}; 19 | else 20 | x = 1:size(y,2); 21 | end 22 | 23 | nts = size(y,1); 24 | 25 | hax = findobj( gcf, 'type', 'axes' ); 26 | if length(hax) ~= nts 27 | tsplot( y, varargin{:} ); 28 | return 29 | end 30 | 31 | pos = zeros( length(hax), 4 ); 32 | for i = 1:length(hax) 33 | pos(i,:) = get( hax(i), 'position' ); 34 | end 35 | [tmp,I] = sort( -pos(:,2) ); 36 | hax = hax(I); 37 | 38 | for i = 1:nts 39 | axes(hax(i)) 40 | 41 | hold on 42 | plot( x, y(i,:), varargin{:} ) 43 | 44 | end 45 | 46 | if nargout == 0 47 | clear hax 48 | end -------------------------------------------------------------------------------- /plotting/clim_centered.m: -------------------------------------------------------------------------------- 1 | 2 | function clim_centered(hax) 3 | 4 | if nargin < 1 5 | hax = gca; 6 | end 7 | 8 | cl = get(hax, 'clim'); 9 | cl = max(abs(cl)); 10 | set(hax, 'clim', [-cl cl]); 11 | -------------------------------------------------------------------------------- /plotting/clim_positive.m: -------------------------------------------------------------------------------- 1 | 2 | function clim_positive(hax) 3 | 4 | if nargin < 1 5 | hax = gca; 6 | end 7 | 8 | cl = get(hax, 'clim'); 9 | cl = max(cl); 10 | set(hax, 'clim', [0 cl]); 11 | -------------------------------------------------------------------------------- /plotting/climcm_2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jluttine/matlab/63406c7782b0869948f06e1dbc594460c165d24e/plotting/climcm_2.mat -------------------------------------------------------------------------------- /plotting/colormap_centered.m: -------------------------------------------------------------------------------- 1 | function colormap_centered(hax) 2 | 3 | if nargin < 1 4 | hax = gca(); 5 | end 6 | 7 | for n=1:length(hax) 8 | cl = get(hax(n), 'clim'); 9 | l = max(abs(cl)); 10 | cl = [-l l]; 11 | set(hax(n), 'clim', cl); 12 | end -------------------------------------------------------------------------------- /plotting/colormap_redblue.m: -------------------------------------------------------------------------------- 1 | function colormap_redblue() 2 | 3 | load climcm_2 4 | colormap(map); 5 | -------------------------------------------------------------------------------- /plotting/colormap_scale.m: -------------------------------------------------------------------------------- 1 | function colormap_scale() 2 | 3 | cm = hot(); 4 | cm = cm(end:-1:1,:); 5 | colormap(cm); 6 | 7 | -------------------------------------------------------------------------------- /plotting/dateticks.m: -------------------------------------------------------------------------------- 1 | 2 | % DATETICKS(...) 3 | % 4 | % DATETICKS(AXES, ...) 5 | % 6 | % Simple wrapper to run DATETICK on several plots. 7 | % 8 | % See DATETICK for details on the optional arguments. 9 | 10 | % Copyright (c) 2010 Jaakko Luttinen 11 | 12 | function dateticks(varargin) 13 | 14 | if nargin >= 1 && isnumeric(varargin{1}) 15 | hax = varargin{1}; 16 | params = varargin(2:end); 17 | else 18 | hax = gca; 19 | params = varargin; 20 | end 21 | 22 | N = length(hax); 23 | for n=1:N 24 | datetick(hax(n), params{:}); 25 | end -------------------------------------------------------------------------------- /plotting/decades_equal.m: -------------------------------------------------------------------------------- 1 | function decades_equal(hAxes,xLimits,yLimits) 2 | 3 | if (nargin < 2) || isempty(xLimits) 4 | xLimits = get(hAxes,'XLim'); 5 | end 6 | if (nargin < 3) || isempty(yLimits) 7 | yLimits = get(hAxes,'YLim'); 8 | end 9 | 10 | logScale = diff(yLimits)/diff(xLimits); 11 | powerScale = diff(log10(yLimits))/diff(log10(xLimits)); 12 | 13 | set(hAxes,'Xlim',xLimits,... 14 | 'YLim',yLimits,... 15 | 'DataAspectRatio',[1 logScale/powerScale 1]); 16 | 17 | end 18 | -------------------------------------------------------------------------------- /plotting/draw_gaussian.m: -------------------------------------------------------------------------------- 1 | function draw_gaussian(mu,Cov,std,varargin) 2 | if nargin < 3 || isempty(std) 3 | std = 1:3; 4 | end 5 | 6 | [U,S,V] = svd(Cov); 7 | a = linspace(0,2*pi,100);%0:0.1:(2*pi); 8 | X = [cos(a);sin(a)]; 9 | 10 | for ind=1:length(std) 11 | Y = std(ind) * U*sqrt(S)*X; 12 | Z1(:,ind) = mu(1) + Y(1,:); 13 | Z2(:,ind) = mu(2) + Y(2,:); 14 | end 15 | 16 | plot(Z1,Z2,varargin{:}); 17 | -------------------------------------------------------------------------------- /plotting/gpplot.m: -------------------------------------------------------------------------------- 1 | 2 | function gpplot(x, y, e, varargin) 3 | % function gpplot(x, y, e, flipaxis) 4 | % 5 | % x = inputs (e.g., time) 6 | % y = expectated values of the outputs 7 | % e = errors of the outputs 8 | 9 | warning('Deprecated, use errorplot') 10 | 11 | opts = struct('pseudoinputs', []); 12 | [ opts, errmsg, wrnmsg ] = argschk( opts, varargin{:} ); 13 | if ~isempty(errmsg), error( errmsg ), end 14 | if ~isempty(wrnmsg), warning( wrnmsg ), end 15 | 16 | 17 | 18 | % $$$ if nargin < 4 19 | % $$$ flipaxis = false; 20 | % $$$ end 21 | x = x(:); 22 | y = y(:); 23 | %e = sqrt(diag(Cov)); 24 | X = [x(1:end); x(end:-1:1)]; 25 | Y = [y(1:end)+e; y(end:-1:1)-e(end:-1:1)]; 26 | C = [.75,.75,.75]; 27 | % $$$ if flipaxis 28 | % $$$ fill(Y,X,C,'EdgeColor',C); 29 | % $$$ hold on 30 | % $$$ plot(y,x,'k'); 31 | % $$$ else 32 | fill(X,Y,C,'EdgeColor',C); 33 | hold on 34 | plot(x,y,'k'); 35 | if ~isempty(opts.pseudoinputs) 36 | yl = ylim; 37 | py = yl(1) + 0.8*(yl(2)-yl(1)); 38 | plot(opts.pseudoinputs, py, 'k+') 39 | end 40 | % $$$ end -------------------------------------------------------------------------------- /plotting/m_scatter.m: -------------------------------------------------------------------------------- 1 | function han = m_scatter(lon, lat, varargin) 2 | % function m_scatter(lon, lat, S, C) 3 | 4 | m_proj('miller','lat',82); 5 | %m_grid('linestyle','none','box','on','tickdir','out'); 6 | [x,y] = m_ll2xy(lon, lat); 7 | %drawnow 8 | h = scatter(x, y, varargin{:}); 9 | line(x(1:10), y(1:10)) 10 | %plot(x,y,'.') 11 | %get(gca, 12 | z = findobj(gca, 'type', 'scattergroup') 13 | m_grid('linestyle','none','box','on','tickdir','out') 14 | %h = scatter(x, y, varargin{:}); 15 | 16 | if nargout == 1 17 | han = h; 18 | end 19 | -------------------------------------------------------------------------------- /plotting/map_coast.m: -------------------------------------------------------------------------------- 1 | % MAP_COAST - Draws the coast line using M_MAP package. 2 | % 3 | % MAP_COAST(...) 4 | % MAP_COAST(HAX,...) 5 | % 6 | % HAX : vector of axes handles (default: GCA) 7 | % 8 | % Optional arguments: 9 | % 10 | % 'color' : color of the coast line (default: [0.3 0.3 0.3]) 11 | % 'usercoast' : custom file for loading the coast line data (default: []) 12 | % 13 | % See M_COAST and M_USERCOAST for more optional arguments. 14 | 15 | % Last modified 2010-06-11 16 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 17 | 18 | function map_coast(varargin) 19 | 20 | options = struct( ... 21 | 'color', [0.3 0.3 0.3], ... 22 | 'usercoast', []); 23 | 24 | % Read user's parameters 25 | if nargin >= 1 && isnumeric(varargin{1}) 26 | hax = varargin{1}; 27 | [options, errmsg, remopts] = argparse(options, varargin{2:end}); 28 | error(errmsg); 29 | else 30 | hax = gca(); 31 | [options, errmsg, remopts] = argparse(options, varargin{:}); 32 | error(errmsg); 33 | end 34 | 35 | % Draw coast 36 | old_axes = gca(); 37 | for n=1:numel(hax) 38 | axes(hax(n)); 39 | if ~isempty(options.usercoast) 40 | m_usercoast(options.usercoast, 'color', options.color, remopts{:}); 41 | else 42 | m_coast('color', options.color, remopts{:}); 43 | end 44 | end 45 | axes(old_axes); 46 | 47 | -------------------------------------------------------------------------------- /plotting/map_colormap.m: -------------------------------------------------------------------------------- 1 | function mapcolormap 2 | 3 | load climcm_2 4 | colormap(map); 5 | -------------------------------------------------------------------------------- /plotting/map_grid.m: -------------------------------------------------------------------------------- 1 | 2 | % MAP_GRID 3 | % 4 | % Draws the map grid and box using M_MAP package. 5 | % 6 | % See M_COAST and M_USERCOAST for optional arguments. 7 | % 8 | % By default, 'box' is set to 'on'. If 'hammer-aitoff' projection is 9 | % used, 'xtick' is set to [] by default. 10 | 11 | % Last modified 2010-12-17 12 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 13 | 14 | function map_grid(varargin) 15 | 16 | global MAP_PROJECTION 17 | 18 | %opts = struct('xtick', [], 'xticklabels', []); 19 | 20 | % Some default parameters 21 | opts.box = 'on'; 22 | if strcmpi(MAP_PROJECTION.name, 'hammer-aitoff') 23 | opts.xtick = []; 24 | %opts.xtick = 'xtick', []; 25 | end 26 | 27 | % Read user's parameters 28 | if nargin >= 1 && isnumeric(varargin{1}) 29 | hax = varargin{1}; 30 | [opts, errmsg, remopts] = argparse(opts, varargin{2:end}); 31 | else 32 | hax = NaN; 33 | [opts, errmsg, remopts] = argparse(opts, varargin{:}); 34 | end 35 | opts = struct2list(opts); 36 | params = {opts{:}, remopts{:}}; 37 | 38 | % Draw grid 39 | if ~isnan(hax) 40 | old_axes = gca(); 41 | for n=1:numel(hax) 42 | axes(hax(n)); 43 | m_grid(params{:}); 44 | end 45 | axes(old_axes); 46 | else 47 | m_grid(params{:}); 48 | end 49 | -------------------------------------------------------------------------------- /plotting/map_pcolor.m: -------------------------------------------------------------------------------- 1 | % MAP_PCOLOR - Plots a spatial function colored on a grid 2 | % 3 | % MAP_PCOLOR(LON, LAT, Z, ...) 4 | 5 | function hax = map_pcolor(lon, lat, Z, varargin) 6 | 7 | if isvector(lon) && isvector(lat) && numel(Z)==length(lon)*length(lat) 8 | [lon, lat] = meshgrid(lon,lat); 9 | end 10 | 11 | hax = m_pcolor(lon,lat,reshape(Z,size(lon)),varargin{:}); 12 | shading('flat'); 13 | %clim_centered(); 14 | %map_colormap(); 15 | 16 | if nargout < 1 17 | clear hax; 18 | end 19 | 20 | %shading('interp'); 21 | -------------------------------------------------------------------------------- /plotting/map_plot.m: -------------------------------------------------------------------------------- 1 | % MAP_PLOT - Plots on a map. 2 | % 3 | % [...] = MAP_PLOT(C, ...) 4 | % 5 | % where C(1,:) is the longitudes, C(2,:) is the latitudes. For more 6 | % information, see M_PLOT. 7 | 8 | % Last modified 2010-06-11 9 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 10 | 11 | function varargout = map_plot(C, varargin) 12 | 13 | varargout{:} = m_plot(C(1,:), C(2,:), varargin{:}); 14 | -------------------------------------------------------------------------------- /plotting/map_projection.m: -------------------------------------------------------------------------------- 1 | % MAP_PROJECTION - Set the projection 2 | % 3 | % 'GLOBAL-ELLIPSE' 4 | % 'GLOBAL-RECT' 5 | % 'TESTBED' 6 | 7 | function map_projection(projection) 8 | 9 | if nargin < 1 10 | projection = 'global-ellipse'; 11 | end 12 | 13 | switch lower(projection) 14 | case 'global-ellipse' 15 | m_proj('mollweide', 'clongitude', 0); % straight latitudes (equal area) 16 | %m_proj('hammer-aitoff', 'clon', 0); % curved latitudes (equal area) 17 | case 'global-rect' 18 | m_proj('miller'); 19 | case 'testbed' 20 | m_proj('Mercator', 'lon',[22.5,26.8], 'lat',[59.74,61.0]) 21 | otherwise 22 | error('Unknown projection') 23 | end 24 | 25 | -------------------------------------------------------------------------------- /plotting/map_text.m: -------------------------------------------------------------------------------- 1 | % MAPTEXT Text annotations on maps. 2 | % 3 | % MAPTEXT(C, 'string') adds the text in the quotes to location 4 | % specified by the coordinates C. C(1,:) specifies the longitudes and 5 | % C(2,:) the latitudes. 6 | % 7 | % MAPTEXT(C, S) adds the texts in the cell array S to locations specified 8 | % by the coordinates C. C(1,:) specifies the longitudes and C(2,:) the 9 | % latitudes. 10 | % 11 | % MAPTEXT(..., property/value pairs) can be used to change, e.g., 12 | % fontsize, weight, and color using the standard TEXT properties. 13 | % 14 | % See also TEXT and M_TEXT. 15 | 16 | % Copyright (c) 2010 Jaakko Luttinen 17 | 18 | function maptext(C, str, varargin) 19 | 20 | if iscell(str) 21 | for i=1:numel(str) 22 | m_text(C(1,i), C(2,i), str{i}, varargin{:}); 23 | end 24 | else 25 | m_text(C(1,:), C(2,:), str, varargin{:}); 26 | end 27 | -------------------------------------------------------------------------------- /plotting/map_topography.m: -------------------------------------------------------------------------------- 1 | % MAP_TOPOGRAPHY - Plots 5-minute resolution topography map. 2 | 3 | % Last modified 2010-06-11 4 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 5 | 6 | function map_topography() 7 | 8 | % Draw high resolution topography 9 | values = m_etopo2('contourf', 100, 'EdgeColor', 'None'); 10 | 11 | % Set colormap 12 | map_colormap(); 13 | 14 | % Set the range of the coloring (sea level is white). 15 | max_value = max(values, [], 2); 16 | set( gca, 'clim', [-max_value(1), max_value(1)]); 17 | -------------------------------------------------------------------------------- /plotting/mapproj.m: -------------------------------------------------------------------------------- 1 | 2 | function mapproj(projection) 3 | 4 | if nargin < 1 5 | projection = 'global'; 6 | end 7 | 8 | switch lower(projection) 9 | case 'global-ellipse' 10 | m_proj('hammer-aitoff', 'clon', 0); 11 | case 'global-rect' 12 | m_proj('miller'); 13 | case 'testbed' 14 | m_proj('Mercator', 'lon',[22.5,26.8], 'lat',[59.74,61.0]) 15 | end 16 | 17 | -------------------------------------------------------------------------------- /plotting/plot_acorr.m: -------------------------------------------------------------------------------- 1 | 2 | function plot_acorr(x, lag) 3 | 4 | error('not ready'); 5 | if nargin < 2 6 | lag = size(x,2); 7 | end -------------------------------------------------------------------------------- /plotting/plot_scatterhist.m: -------------------------------------------------------------------------------- 1 | 2 | function plot_scatterhist(x) 3 | 4 | N = size(x,2); 5 | 6 | for n=1:N 7 | for m=1:N 8 | subplot(N,N,n+(m-1)*N) 9 | if n ~= m 10 | plot(x(:,n),x(:,m),'k.') 11 | else 12 | hist(x(:,n)); 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /plotting/print2latex.m: -------------------------------------------------------------------------------- 1 | function print2latex(filename, figsize, varargin) 2 | 3 | set(0, 'defaulttextinterpreter', 'none'); 4 | 5 | printcmd = sprintf('print(gcf, ''-depsc2'', ''%s'')', filename) 6 | laprint(gcf, filename, 'width', figsize, varargin{:}, 'printcmd', printcmd); -------------------------------------------------------------------------------- /plotting/set_colorbar_position.m: -------------------------------------------------------------------------------- 1 | % SET_COLORBAR_POSITION 2 | % 3 | % SET_COLORBAR_POSITION(HCB, HAX, CB_SIZE, CB_SEP) 4 | % 5 | % HCB is the handle to the colorbar object 6 | % 7 | % HAX is the handle to the axes 8 | % 9 | % CB_SIZE is the size of the colorbar 10 | % 11 | % CB_SEP is the separation of the colorbar from the axes 12 | % 13 | % At the moment, this only works for horizontal colorbars placed under the 14 | % axes. 15 | 16 | function set_colorbar_position(hcb, hax, cb_size, cb_sep) 17 | 18 | for d=1:numel(hax) 19 | pos_ax = get( hax(d), 'Position'); 20 | cb_pos = pos_ax; 21 | cb_pos(2) = pos_ax(2) - cb_size - cb_sep; 22 | cb_pos(4) = cb_size; 23 | set(hcb(d), 'Position', cb_pos); 24 | end 25 | -------------------------------------------------------------------------------- /plotting/set_figure_fontsize.m: -------------------------------------------------------------------------------- 1 | function set_figure_fontsize(fig, size) 2 | 3 | set(findobj(fig,'Type','text'),'FontSize',size) -------------------------------------------------------------------------------- /plotting/set_figure_size.m: -------------------------------------------------------------------------------- 1 | % SET_FIGURE_SIZE - Set the size of a figure. 2 | % 3 | % SET_FIGURE_SIZE(WIDTH,HEIGHT,FIG,UNITS) 4 | % 5 | % If FIG is not given, GCF is used. The default for UNITS is 'centimeters'. 6 | 7 | % Last modified 2010-06-11 8 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 9 | 10 | function set_figure_size(width,height,fig,units) 11 | 12 | if nargin < 3 13 | fig = gcf; 14 | end 15 | 16 | if nargin < 4 17 | units = 'centimeters'; 18 | end 19 | 20 | old_units = get(fig, 'Units'); 21 | set(fig, 'Units', units); 22 | set(fig, 'PaperUnits', units); 23 | pos = get(fig, 'Position'); 24 | set(fig, 'Position', [pos(1) pos(2) width height]); 25 | set(fig, 'PaperPosition', [0 0 width height]); 26 | set(fig, 'PaperSize', [width height]); 27 | %set(fig, 'PaperPositionMode', 'auto', 'PaperSize', [width height]); 28 | set(fig, 'Units', old_units); 29 | -------------------------------------------------------------------------------- /plotting/set_label_fontsize.m: -------------------------------------------------------------------------------- 1 | function set_label_fontsize(hax, size) 2 | 3 | for i=1:length(hax) 4 | h_xlabel = get(hax(i),'XLabel'); 5 | set(h_xlabel,'FontSize',size); 6 | h_ylabel = get(hax(i),'YLabel'); 7 | set(h_ylabel,'FontSize',size); 8 | end 9 | -------------------------------------------------------------------------------- /plotting/set_legend_fontsize.m: -------------------------------------------------------------------------------- 1 | function set_legend_fontsize(h_leg, fontsize); 2 | set(h_leg, 'FontSize', fontsize); -------------------------------------------------------------------------------- /plotting/set_plot_style.m: -------------------------------------------------------------------------------- 1 | function set_plot_style(hax) 2 | if nargin < 1 3 | hax = gca; 4 | end 5 | set(hax, 'Box', 'off' ); 6 | set(hax, 'TickDir', 'out'); -------------------------------------------------------------------------------- /plotting/set_subplot_positions.m: -------------------------------------------------------------------------------- 1 | 2 | % SET_SUBPLOT_POSITIONS(HAX, ROW, COL, MAR, SEP) 3 | % 4 | % Places subplots to a nice layout. 5 | % 6 | % HAX : handles to axes 7 | % ROW : number of rows in the layout 8 | % COL : number of columns in the layout 9 | % MAR : margins [left top right bottom] 10 | % SEP : separating space between subplots [horizontal vertical] 11 | % 12 | % All the units are normalized. 13 | 14 | % TODO: Maybe should take handle to figure instead of axes? 15 | % TODO: Let the user change 'Units' to something else than 'normalized' 16 | 17 | % Copyright (c) 2010 Jaakko Luttinen 18 | 19 | function set_subplot_positions(hax, rows, columns, margins, separators) 20 | 21 | % Dimensions of the area for plots 22 | area_width = 1 - margins(1) - margins(3); 23 | area_height = 1 - margins(2) - margins(4); 24 | 25 | plot_width = (area_width - (columns-1) * separators(1)) / columns; 26 | plot_height = (area_height - (rows-1) * separators(2)) / rows; 27 | 28 | ind = 0; 29 | for i=1:rows 30 | for j=1:columns 31 | ind = ind + 1; 32 | if ind <= numel(hax) 33 | pos(1) = margins(1) + (j-1)*(plot_width+separators(1)); 34 | pos(3) = plot_width; 35 | pos(2) = 1 - (margins(2) + plot_height + (i-1)* ... 36 | (plot_height+separators(2))); 37 | pos(4) = plot_height; 38 | units = get(hax(ind), 'Units'); 39 | set(hax(ind), 'Units', 'normalized'); 40 | set(hax(ind), 'Position', pos); 41 | set(hax(ind), 'Units', units); 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /plotting/set_ticks_fontsize.m: -------------------------------------------------------------------------------- 1 | % SET_TICKS_FONTSIZE - Set the font size of the given axes. 2 | % 3 | % SET_TICKS_FONTSIZE(HAX,FONTSIZE) 4 | 5 | % Last modified 2010-06-11 6 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 7 | 8 | function set_ticks_fontsize(hax, size) 9 | 10 | set(hax, 'FontSize', size); 11 | -------------------------------------------------------------------------------- /plotting/tsplot.m: -------------------------------------------------------------------------------- 1 | % TSPLOT - Plot time series 2 | % 3 | % TSPLOT(Y) plots time series in the rows of Y on a separate 4 | % subplot. Y can be followed by parameter/value pairs to specify 5 | % additional properties of the lines. 6 | % 7 | % See also ADDTSPLOT 8 | 9 | % This software is provided "as is", without warranty of any kind. 10 | % Alexander Ilin, Tapani Raiko 11 | 12 | function hax = tsplot( y, varargin ) 13 | 14 | if nargin >= 2 & isnumeric(varargin{1}) 15 | x = y; 16 | y = varargin{1}; 17 | varargin = {varargin{2:end}}; 18 | else 19 | x = 1:size(y,2); 20 | end 21 | 22 | nts = size(y,1); 23 | 24 | figure 25 | for i = 1:nts 26 | hax(i) = subplot( nts, 1, i ); 27 | end 28 | 29 | for i = 1:nts 30 | axes(hax(i)) 31 | 32 | plot( x, y(i,:), varargin{:} ) 33 | set( gca, 'xlim', [ min(x) max(x) ] ) 34 | 35 | pos_ax = get( gca, 'Position' ); 36 | 37 | hgt_ax = (0.95 - 0.1) / ( nts + (nts-1)*0.1 ); 38 | hgt_sp = hgt_ax * 0.1; 39 | pos_ax(1) = 0.1; 40 | pos_ax(2) = 0.95 - (i-1)*( hgt_ax + hgt_sp ) - hgt_ax; 41 | pos_ax(3) = 0.84; 42 | pos_ax(4) = hgt_ax; 43 | set( gca, 'Position', pos_ax ) 44 | if i ~= nts 45 | set( gca, 'XTickLabel', [] ) 46 | end 47 | end 48 | 49 | if nargout == 0 50 | clear hax 51 | end -------------------------------------------------------------------------------- /plotting/xlabels.m: -------------------------------------------------------------------------------- 1 | % H = XLABELS(TEXT,...) 2 | % H = XLABELS(HAX,TEXT,...) 3 | 4 | % Last modified 2010-06-11 5 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 6 | 7 | function h = xlabels(text,varargin) 8 | 9 | options = struct('Separation', []); 10 | 11 | if isnumeric(text) 12 | hax = text; 13 | text = varargin{1}; 14 | [options, errmsg, remopts] = argparse(options, varargin{2:end}); 15 | error(errmsg); 16 | else 17 | hax = gca; 18 | [options, errmsg, remopts] = argparse(options, varargin{:}); 19 | error(errmsg); 20 | end 21 | 22 | h = zeros(size(hax)); 23 | for n=1:numel(hax) 24 | 25 | % Get axes position 26 | pos_ax = get(hax(n), 'Position'); 27 | 28 | % Add a colorbar 29 | if iscell(text) 30 | h(n) = xlabel(hax(n), text{n}, remopts{:}); 31 | else 32 | h(n) = xlabel(hax(n), text, remopts{:}); 33 | end 34 | 35 | % Use the same units 36 | units = get(hax(n), 'Units'); 37 | set(h(n), 'Units', units); 38 | 39 | pos = get(h(n), 'Position'); 40 | pos(2) = -options.Separation; 41 | set(h(n), 'Position', pos); 42 | 43 | end 44 | 45 | if nargout < 1 46 | clear hcb; 47 | end -------------------------------------------------------------------------------- /plotting/ylim_centered.m: -------------------------------------------------------------------------------- 1 | function ylim_centered(hax) 2 | 3 | for n=1:numel(hax) 4 | ylim = get(hax(n), 'ylim'); 5 | ylim = max(abs(ylim)); 6 | set(hax(n), 'ylim', [-ylim ylim]); 7 | end -------------------------------------------------------------------------------- /plotting/ylim_scale.m: -------------------------------------------------------------------------------- 1 | function ylim_scale(hax, scale) 2 | 3 | for n=1:numel(hax) 4 | ylim = get(hax(n), 'ylim'); 5 | set(hax(n), 'ylim', scale*ylim); 6 | end -------------------------------------------------------------------------------- /publications/aistats2012/aistats2012_mohsst5_rmse.m: -------------------------------------------------------------------------------- 1 | function [rmse_train, rmse_test] = aistats2012_mohsst5_rmse(F, testset, seed) 2 | 3 | if nargin < 3 4 | [Ytrain, Ytest, Itrain, Itest, data, sea] = aistats2012_mohsst5_sets(testset); 5 | else 6 | [Ytrain, Ytest, Itrain, Itest, data, sea] = aistats2012_mohsst5_sets(testset, ... 7 | seed); 8 | end 9 | 10 | [M,N] = size(F); 11 | 12 | % Error measures 13 | [LON,LAT] = meshgrid(data.longitude, data.latitude); 14 | weights = cosd(LAT(:)); 15 | weights = weights(sea); 16 | weights = repmat(weights, [1, N]); 17 | rmse_train = rmsew(Ytrain(Itrain)-F(Itrain),weights(Itrain)); 18 | rmse_test = rmsew(Ytest(Itest)-F(Itest),weights(Itest)); 19 | rmse_zero = rmsew(Ytest(Itest),weights(Itest)); 20 | 21 | -------------------------------------------------------------------------------- /publications/aistats2012/aistats2012_mohsst5_sets.m: -------------------------------------------------------------------------------- 1 | function [Ytrain, Ytest, Itrain, Itest, data, sea] = aistats2012_mohsst5_sets(testset, seed) 2 | 3 | % Seed for the random number generator 4 | if nargin < 2 5 | seed = 10; 6 | end 7 | 8 | rand('state', seed); 9 | randn('state', seed); 10 | 11 | % Load the data 12 | data = mohsst5_loaddata(); 13 | sea = sum(~isnan(data.observations),2) > 0; 14 | Y = data.observations(sea,:); 15 | 16 | % 17 | % FORM TRAIN AND TEST SETS 18 | % 19 | 20 | switch testset 21 | case 1 % randomly (uniformly) selected 22 | %disp('Test set: Uniform'); 23 | 24 | I = rand(size(Y)) < 0.2; 25 | Itest = I & ~isnan(Y); 26 | Itrain = ~I & ~isnan(Y); 27 | Ytest = Y; 28 | Ytrain = Y; 29 | Ytest(~Itest) = nan; 30 | Ytrain(~Itrain) = nan; 31 | 32 | test_percent = sum(Itest(:)) / sum(~isnan(Y(:))); 33 | %fprintf('Size of the test set: %.1f percent\n', 100*test_percent); 34 | 35 | case 2 % pattern from earliest years used for latest years 36 | %disp('Test set: Pattern'); 37 | 38 | Imv = isnan(Y); 39 | Imv(:,(20*12+1):end) = false; 40 | Itest = Imv(:,end:-1:1) & ~isnan(Y); 41 | Itrain = ~Imv(:,end:-1:1) & ~isnan(Y); 42 | 43 | Ytest = Y; 44 | Ytrain = Y; 45 | Ytest(~Itest) = nan; 46 | Ytrain(~Itrain) = nan; 47 | 48 | test_percent = sum(Itest(:)) / sum(~isnan(Y(:))); 49 | %fprintf('Size of the test set: %.1f percent\n', 100*test_percent); 50 | 51 | otherwise 52 | error('Unknown test set requested'); 53 | 54 | end 55 | -------------------------------------------------------------------------------- /publications/icml2011/icml2011_mohsst5_parse_results.m: -------------------------------------------------------------------------------- 1 | function [F,FF] = icml2011_mohsst5_parse_results(filename, N, burnin) 2 | 3 | F_mean = 0; 4 | FF = 0; 5 | 6 | for n=(burnin+1):N 7 | fprintf('%d/%d\n', n, N); 8 | name = sprintf('%s_F%d',filename,n); 9 | load(name); 10 | F_mean = (F + (n-burnin-1)*F_mean) / (n-burnin); 11 | FF = (F.*F + (n-burnin-1)*FF) / (n-burnin); 12 | end 13 | 14 | F = F_mean; 15 | 16 | -------------------------------------------------------------------------------- /publications/neurocomputing2010/nc2010_publish_compexperiment.m: -------------------------------------------------------------------------------- 1 | function nc2010_publish_compexperiment 2 | 3 | threshold = 1e-3 4 | 5 | 6 | for flatW = [true false] 7 | for datatype = {'no', 'weak', 'strong'} 8 | 9 | if flatW 10 | stringW = 'flatW'; 11 | else 12 | stringW = 'hierW'; 13 | end 14 | 15 | filename = sprintf(['/home/jluttine/papers/neurocomputing2010/' ... 16 | 'fig_compexperiment_severalruns_%s_subspace=%s'], ... 17 | stringW, datatype{:}) 18 | 19 | nc2010_plot_compexperiment(flatW, datatype{:}, threshold); 20 | 21 | set(gcf, 'units', 'centimeters', 'paperunits', 'centimeters'); 22 | pos = get(gcf, 'position'); 23 | set(gcf, 'position', [pos(1:2), 8,6]); 24 | pos = get(gcf, 'paperposition'); 25 | set(gcf, 'paperposition', [pos(1:2),8,6]) 26 | 27 | print(gcf, '-depsc2', filename); 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /publications/neurocomputing2010/nc2010_publish_datasets.m: -------------------------------------------------------------------------------- 1 | 2 | % Test whether it is necessary to rotate VB PCA 3 | function nc2010_publish_datasets 4 | 5 | n = 200; 6 | m = 50; 7 | d = 10; 8 | 9 | ncomps = 30; 10 | 11 | randn('state', 1); 12 | rand('state', 1); 13 | 14 | % Generate multivariate normal data 15 | disp('Using weak subspace') 16 | eigs = (1 + [d:-1:1 zeros(1,m-d)]) .^ 2 17 | datastring = 'weak'; 18 | plot_eigenvalues(eigs, datastring); 19 | 20 | disp('Using strong subspace') 21 | eigs = ([5*ones(1,d) 1*ones(1,m-d)]) .^ 2 22 | datastring = 'strong'; 23 | plot_eigenvalues(eigs, datastring); 24 | 25 | disp('Using no subspace') 26 | eigs = (m:-1:1) .^ 2 27 | datastring = 'no'; 28 | plot_eigenvalues(eigs, datastring); 29 | 30 | 31 | function plot_eigenvalues(eigs, datastring) 32 | 33 | filename = sprintf(['/home/jluttine/papers/neurocomputing2010/' ... 34 | 'fig_eigenvalues_dataset=%s'], datastring); 35 | 36 | figure 37 | plot(sqrt(eigs), 'k-'); 38 | set(gcf, 'units', 'centimeters', 'paperunits', 'centimeters'); 39 | pos = get(gcf, 'position'); 40 | set(gcf, 'position', [pos(1:2), 6,4]); 41 | pos = get(gcf, 'paperposition'); 42 | set(gcf, 'paperposition', [pos(1:2),6,4]) 43 | 44 | ylim([0, max(sqrt(eigs))+1]); 45 | xlim([1, length(eigs)]); 46 | 47 | %ylabel('standard deviation') 48 | 49 | print(gcf, '-depsc2', filename); 50 | 51 | -------------------------------------------------------------------------------- /publications/neurocomputing2010/nc2010_publish_toyexperiment.m: -------------------------------------------------------------------------------- 1 | function nc2010_publish_toyexperiment 2 | 3 | datatype = 'weak'; 4 | 5 | for flatW = [true false] 6 | 7 | if flatW 8 | stringW = 'flatW'; 9 | costlim = []; 10 | rmselim = []; 11 | rmsetestlim = [1.1 1.7]; 12 | xlim = [1e0 1e3]; 13 | else 14 | stringW = 'hierW'; 15 | costlim = []; 16 | rmselim = []; 17 | rmsetestlim = [1.1 1.7]; 18 | xlim = [1e0 1e3]; 19 | end 20 | 21 | filename = sprintf(['/home/jluttine/papers/neurocomputing2010/' ... 22 | 'fig_toyexperiment_%s_subspace=%s'], stringW, datatype); 23 | 24 | figh = nc2010_plot_toyexperiment(200,50,10,30, flatW, datatype, 'xlim', ... 25 | xlim, 'costlim', costlim, 'rmselim', ... 26 | rmselim, 'rmsetestlim', rmsetestlim); 27 | 28 | for ind = 1:3 29 | set(figh(ind), 'units', 'centimeters', 'paperunits', 'centimeters'); 30 | pos = get(figh(ind), 'position'); 31 | set(figh(ind), 'position', [pos(1:2), 8,6]); 32 | pos = get(figh(ind), 'paperposition'); 33 | set(figh(ind), 'paperposition', [pos(1:2),8,6]) 34 | 35 | switch ind 36 | case 1 37 | print(figh(ind), '-depsc2', [filename, '_cost']); 38 | case 2 39 | print(figh(ind), '-depsc2', [filename, '_rmse']); 40 | case 3 41 | print(figh(ind), '-depsc2', [filename, '_rmsetest']); 42 | end 43 | 44 | end 45 | 46 | end 47 | -------------------------------------------------------------------------------- /publications/neurocomputing2010/nc2010_vbpca_mlexperiment.m: -------------------------------------------------------------------------------- 1 | 2 | % Test whether it is necessary to rotate VB PCA 3 | function nc2010_vbpca_mlexperiment(flatW, rotate, ncomps) 4 | 5 | if nargin < 3 6 | ncomps = 100; 7 | end 8 | 9 | randn('state', 1); 10 | rand('state', 1); 11 | 12 | % Movie lens data with 100.000 ratings 13 | U = load('/share/bayes/data/jaakko/movielens/u.data'); 14 | %Y = sparse(U(:,1), U(:,2), U(:,3)); 15 | 16 | % Divide into training (90%) and test sets (10%) 17 | m = max(U(:,1)); 18 | n = max(U(:,2)); 19 | testset = (rand(rows(U),1) < 0.1); 20 | Ytrain = full(sparse(U(~testset,1), U(~testset,2), U(~testset,3),m,n)); 21 | Ytest = full(sparse(U(testset,1), U(testset,2), U(testset,3),m,n)); 22 | Ytrain(Ytrain==0) = nan; 23 | Ytest(Ytest==0) = nan; 24 | 25 | %size(Ytrain) 26 | 27 | if flatW 28 | disp('Using non-hierarchical flat prior for W'); 29 | stringW = 'flatW'; 30 | else 31 | disp('Using hierarchical prior for W'); 32 | stringW = 'hierW'; 33 | end 34 | 35 | filename = sprintf(['/home/jluttine/matlab/neurocomputing2010/' ... 36 | 'nc2010_vbpca_mlexperiment_%s_ncomps=%d_rotate=%d'], ... 37 | stringW, ncomps, rotate); 38 | 39 | 40 | results = vbpcamv(Ytrain, ncomps, 'testset', Ytest, 'maxiters', 1e7, ... 41 | 'rotate', rotate, 'startupdatehyper', 1, ... 42 | 'startrotate', 1, 'autosavetime', 1, ... 43 | 'autosavefile', filename, 'fixw', flatW); 44 | 45 | -------------------------------------------------------------------------------- /publications/neurocomputing2010/nc2010_vbpca_mnistexperiment.m: -------------------------------------------------------------------------------- 1 | function nc2010_vbpca_mnistexperiment(flatW, rotate, ncomps) 2 | 3 | M = load('mnist_all'); 4 | 5 | n = 100; 6 | 7 | Y = double(M.train5'); 8 | inds = randperm(cols(Y)); 9 | Y = Y(:,inds(1:n)); 10 | size(Y) 11 | 12 | m = rows(Y); 13 | 14 | startupdatehyper = 1 15 | maxiters = 1e4; 16 | 17 | randn('state', 1); 18 | rand('state', 1); 19 | 20 | % Divide into training (90%) and test sets (10%) 21 | testset = (rand(size(Y)) < 0.1); 22 | Ytrain = Y; 23 | Ytest = Y; 24 | Ytrain(testset) = nan; 25 | Ytest(~testset) = nan; 26 | 27 | %size(Ytrain) 28 | 29 | if flatW 30 | error('Using flat W might be problematic because m>n ???'); 31 | disp('Using non-hierarchical flat prior for W'); 32 | stringW = 'flatW'; 33 | else 34 | disp('Using hierarchical prior for W'); 35 | stringW = 'hierW'; 36 | end 37 | 38 | filename = sprintf(['/home/jluttine/matlab/neurocomputing2010/' ... 39 | 'nc2010_vbpca_mnistexperiment_%s_ncomps=%d_rotate=%d'], ... 40 | stringW, ncomps, rotate); 41 | 42 | 43 | results = vbpcamv(Ytrain, ncomps, 'testset', Ytest, 'maxiters', 1e7, ... 44 | 'rotate', rotate, 'startupdatehyper', startupdatehyper, ... 45 | 'startrotate', 1, 'autosavetime', 1, ... 46 | 'autosavefile', filename, 'fixw', flatW); 47 | 48 | -------------------------------------------------------------------------------- /publications/nips2011/nips2011_run_comparison.m: -------------------------------------------------------------------------------- 1 | 2 | function aistats2012_run_comparison 3 | 4 | % 5 | % Theta 6 | % 7 | 8 | % Cholesky 9 | 10 | nips2011_comparison(3, 1, true, false); 11 | nips2011_comparison(3.5, 1, true, false); 12 | nips2011_comparison(4, 1, true, false); 13 | nips2011_comparison(4.5, 1, true, false); 14 | nips2011_comparison(5, 1, true, false); 15 | 16 | % Kronecker 17 | 18 | % $$$ nips2011_comparison(3, 1, true, true); 19 | % $$$ nips2011_comparison(3.5, 1, true, true); 20 | % $$$ nips2011_comparison(4, 1, true, true); 21 | % $$$ nips2011_comparison(4.5, 1, true, true); 22 | % $$$ nips2011_comparison(5, 1, true, true); 23 | % $$$ nips2011_comparison(6, 1, true, true); 24 | nips2011_comparison(7, 1, true, true); 25 | 26 | % 27 | % F 28 | % 29 | 30 | % Kronecker 31 | 32 | % $$$ nips2011_comparison(3, 1, false, true); 33 | % $$$ nips2011_comparison(3.5, 1, false, true); 34 | % $$$ nips2011_comparison(4, 1, false, true); 35 | % $$$ nips2011_comparison(4.5, 1, false, true); 36 | % $$$ nips2011_comparison(5, 1, false, true); 37 | % $$$ nips2011_comparison(5.5, 1, false, true); 38 | % $$$ nips2011_comparison(6, 1, false, true); 39 | % $$$ nips2011_comparison(7, 1, false, true); 40 | % $$$ nips2011_comparison(8, 1, false, true); 41 | 42 | % Cholesky 43 | 44 | nips2011_comparison(3, 1, false, false); 45 | nips2011_comparison(3.5, 1, false, false); 46 | nips2011_comparison(4, 1, false, false); 47 | nips2011_comparison(4.5, 1, false, false); 48 | nips2011_comparison(5, 1, false, false); 49 | nips2011_comparison(5.5, 1, false, false); 50 | 51 | -------------------------------------------------------------------------------- /publications/vbrfa2010/vbrfa2010_testbed_experiment.m: -------------------------------------------------------------------------------- 1 | function vbrfa2010_testbed_experiment(D, common_nu, common_tau, maxiter) 2 | 3 | testbed_experiment_vbrfa(D, common_nu, common_tau, maxiter, 'robustness', ... 4 | 'independent-t'); 5 | testbed_experiment_vbrfa(D, common_nu, common_tau, maxiter, 'robustness', ... 6 | 'multivariate-t'); 7 | testbed_experiment_vbrfa(D, common_nu, common_tau, maxiter, 'robustness', ... 8 | 'none'); 9 | -------------------------------------------------------------------------------- /publications/vbrfa2011/vbrfa2011_artificial_results.m: -------------------------------------------------------------------------------- 1 | function vbrfa2011_artificial_results() 2 | 3 | % RMSE for noiseless function values that weren't corrupted 4 | rmse_test = [ 5 | 0.8456 0.8125 0.6698 0.8436 6 | 0.8614 0.7664 0.6331 0.8949 7 | 1.1214 0.9540 0.6635 0.8917 8 | 1.3117 0.9283 0.6910 1.0223 9 | 0.8336 0.7994 0.6859 0.8775 10 | 1.1250 0.7574 0.6650 0.9184 11 | 1.0032 0.9118 0.8627 0.9114 12 | 0.9997 0.8684 0.6879 1.0439 13 | 0.7788 0.7029 0.6270 0.6544 14 | 1.0748 0.7851 0.6843 0.8839 15 | 16 | ] 17 | 18 | % RMSE for noiseless function values that were corrupted 19 | rmse_outliers = [ 20 | 10.9814 2.3072 1.1215 1.3202 21 | 13.5295 1.2184 0.7501 1.0824 22 | 10.8686 2.0811 0.8717 1.4725 23 | 4.9825 1.8998 0.6136 1.1558 24 | 11.7699 2.3320 1.1557 1.2918 25 | 8.4381 1.8021 0.6251 1.0046 26 | 15.0804 2.0952 0.8323 1.0567 27 | 10.5611 2.4988 0.8107 1.5382 28 | 12.9707 2.0960 0.8681 1.2415 29 | 6.4134 1.3702 0.5044 0.8838 30 | 31 | ] 32 | 33 | mean(rmse_test,1) 34 | mean(rmse_outliers,1) 35 | %std(rmse_test,1,1) 36 | %std(rmse_outliers,1,1) 37 | -------------------------------------------------------------------------------- /publications/vbrfa2011/vbrfa2011_testbed_data.m: -------------------------------------------------------------------------------- 1 | function vbrfa2011_testbed_data() 2 | 3 | rand('state', 10); 4 | randn('state', 10); 5 | 6 | data = testbed_preprocess(testbed_loaddata()); 7 | 8 | [M,N] = size(data.observations); 9 | Itrain = rand(M,N) > 0.2; 10 | Itest = ~Itrain; 11 | 12 | data_train = data; 13 | data_train.observations(~Itrain) = NaN; 14 | 15 | data_test = data; 16 | data_test.observations(~Itest) = NaN; 17 | 18 | folder = '/share/climate/jluttine/testbed/'; 19 | file_train = [folder, 'testbed_vbrfa2011_traindata']; 20 | file_test = [folder, 'testbed_vbrfa2011_testdata']; 21 | save(file_train, '-struct', 'data_train'); 22 | save(file_test, '-struct', 'data_test'); 23 | 24 | fprintf('Saved train and test data into %s and %s\n', file_train, file_test); 25 | -------------------------------------------------------------------------------- /signal_processing/acorr.m: -------------------------------------------------------------------------------- 1 | function c = acorr(y, maxlag) 2 | 3 | if isvector(y) 4 | if nargin < 2 5 | maxlag = length(y) - 1; 6 | end 7 | y = y - mean(y); 8 | c = zeros(1+maxlag,1); 9 | c0 = acov(y,0) 10 | c(1) = 1; 11 | for l=1:maxlag 12 | c(l+1) = acov(y, l) / c0; 13 | end 14 | else 15 | if nargin < 2 16 | maxlag = size(y,1) - 1; 17 | end 18 | y = bsxfun(@minus, y, mean(y,1)); 19 | c = zeros(maxlag+1,size(y,2)); 20 | for n=1:size(y,2) 21 | c0 = acov(y(:,n),0); 22 | c(1,n) = 1; 23 | for l=1:maxlag 24 | c(l+1,n) = acov(y(:,n),l) / c0; 25 | end 26 | end 27 | end 28 | 29 | function c = acov(x0, L) 30 | N = length(x0); 31 | c = 1/(N-L) * ( dot(x0(1:(N-L)), x0((1+L):end)) ); 32 | -------------------------------------------------------------------------------- /signal_processing/acorr_backup.m: -------------------------------------------------------------------------------- 1 | function c = acorr(y, maxlag) 2 | 3 | 4 | if isvector(y) 5 | if nargin < 2 6 | maxlag = length(y); 7 | end 8 | y = y - mean(y); 9 | z = xcorr(y,maxlag,'coeff'); 10 | l0 = (length(z)+1)/2; 11 | c = z(l0:end); 12 | else 13 | if nargin < 2 14 | maxlag = size(y,1); 15 | end 16 | y = bsxfun(@minus, y, mean(y,1)); 17 | c = zeros(maxlag+1,size(y,2)); 18 | for n=1:size(y,2) 19 | z = xcorr(y(:,n),maxlag,'coeff'); 20 | l0 = (size(z,1)+1)/2; 21 | c(:,n) = z(l0:end); 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /signal_processing/moving_average.m: -------------------------------------------------------------------------------- 1 | % Y = MOVING_AVERAGE(X, L) 2 | % 3 | % X is the original signal (column vector). 4 | % L is the number of preceding/succeeding elements to take into account. 5 | % 6 | % Thus, the average is evaluated over 2xL+1 elements of X. 7 | % 8 | % NaNs are handled properly. If there are more than 2xL+1 consecutive NaNs, 9 | % no mean for the corresponding element can be evaluated, and NaN is the 10 | % result. To completely avoid NaNs in the result, use L such that 2xL+1 is 11 | % larger than the maximum number of consecutive NaNs. 12 | % 13 | % Jaakko Luttinen 2009 14 | function y = moving_average(x, l) 15 | 16 | % Width of the filter window 17 | d = 2*l + 1; 18 | w = ones(d,1); 19 | 20 | % Ignore nans 21 | b = ~isnan(x); 22 | x(~b) = 0; 23 | 24 | % Add stuff to avoid bad tail behaviour 25 | z = zeros(l,1); 26 | x = [x(:); z]; 27 | b = [b(:); z]; 28 | 29 | % Filter, and take only the relevant stuff 30 | y = filter(w,1,x) ./ filter(w,1,b); 31 | y = y((l+1):end); -------------------------------------------------------------------------------- /signal_processing/periodic.m: -------------------------------------------------------------------------------- 1 | function y = periodic(x, x0, x1) 2 | 3 | l = x1-x0; 4 | 5 | y = rem(x-x0, l); 6 | y(y<0) = y(y<0) + l; 7 | y = y + x0; -------------------------------------------------------------------------------- /signal_processing/rts_smoother_step.m: -------------------------------------------------------------------------------- 1 | % [x, Covx, Covx_x, entropy] = rts_smoother_step(x, Covx, x_s, Covx_s, A, Q) 2 | 3 | % Last modified 2011-10-19 4 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@aalto.fi) 5 | 6 | function [x, Covx, Covx_x, entropy] = rts_smoother_step(x, Covx, x_s, Covx_s, A, Q) 7 | % Perform the RTS smoothing step 8 | 9 | x_p = A*x; 10 | Covx_p = A*Covx*A' + Q; 11 | 12 | S = (Covx*A') / Covx_p; 13 | x = x + S*(x_s-x_p); 14 | if nargout >= 2 15 | Covx = Covx + S*(Covx_s-Covx_p)*S'; 16 | end 17 | if nargout >= 3 18 | Covx_x = S*Covx_s; 19 | end 20 | if nargout >= 4 21 | % Compute entropy term: 22 | % 23 | % INT[ p(x_n, x_(n+1) | Y) log p(x_n | Y, x_(n+1)) dx_n dx_(n+1) ] 24 | Cov_joint = [Covx Covx_x 25 | Covx_x' Covx_s]; 26 | entropy = gaussian_entropy(logdet_cov(Cov_joint), size(Cov_joint,1)) ... 27 | - gaussian_entropy(logdet_cov(Covx_s), size(Covx_s,1)); 28 | 29 | end -------------------------------------------------------------------------------- /signal_processing/test_kalman_filter.m: -------------------------------------------------------------------------------- 1 | 2 | function test_kalman_filter() 3 | 4 | % Dynamical model (noisy resonator) 5 | D = 2; 6 | w = 0.2; 7 | A = [cos(w) sin(w)/w 8 | -w*sin(w) cos(w)]; 9 | % $$$ A = [1 0 10 | % $$$ -0.5 0.5]; 11 | q = randn(D); 12 | Q = q*q'; 13 | 14 | % Observation model 15 | M = 4; 16 | C = randn(M,2); 17 | r = randn(M); 18 | R = 1e2*r*r'; 19 | 20 | % Initial state 21 | x0 = zeros(D,1); 22 | Covx0 = eye(D); 23 | x = mvnrnd(x0,Covx0)'; 24 | %Covx0 = Covx0 * 1e10; 25 | 26 | % Simulate data 27 | N = 100; 28 | Y = zeros(M,N); 29 | X = zeros(D,N); 30 | for n=1:N 31 | x = mvnrnd(A*x, Q)'; 32 | Y(:,n) = mvnrnd(C*x, R)'; 33 | X(:,n) = x; 34 | end 35 | 36 | %tsplot(Y) 37 | %tsplot(X) 38 | X_true = X; 39 | 40 | % Kalman filter (method 1) 41 | [X, CovX, logp] = kalman_filter(x0, Covx0, Y, A, Q, C, R, 1); 42 | logp 43 | 44 | varx = reshape(CovX, [D*D, N]); 45 | varx = varx(1:(D+1):end,:); 46 | tserrorplot(X', 2*sqrt(varx')) 47 | addtsplot(X_true, 'r'); 48 | 49 | % Kalman filter (method 2) 50 | [X, CovX, logp] = kalman_filter(x0, Covx0, Y, A, Q, C, R, 2); 51 | logp 52 | 53 | varx = reshape(CovX, [D*D, N]); 54 | varx = varx(1:(D+1):end,:); 55 | tserrorplot(X', 2*sqrt(varx')) 56 | addtsplot(X_true, 'r'); 57 | 58 | % RTS smoother 59 | [X, CovX, x0, Covx0, CovX_X, entropy] = rts_smoother(x0, Covx0, X, CovX, A, Q); 60 | 61 | varx = reshape(CovX, [D*D, N]); 62 | varx = varx(1:(D+1):end,:); 63 | tserrorplot(X', 2*sqrt(varx')) 64 | addtsplot(X_true, 'r'); 65 | 66 | entropy 67 | -------------------------------------------------------------------------------- /startup.m: -------------------------------------------------------------------------------- 1 | 2 | % Set paths 3 | addpath(genpath('.')); 4 | %addpath(genpath('/home/jluttine/matlab')); 5 | %addpath(genpath('/share/bayes/data/jaakko')); 6 | addpath(genpath('/share/climate/data')); 7 | %addpath(genpath('/share/climate/jluttine')); 8 | -------------------------------------------------------------------------------- /utilities/broadcast.m: -------------------------------------------------------------------------------- 1 | % Expands singleton axes of the matrix. 2 | % 3 | % X = BROADCAST(X, D) 4 | % 5 | % D is the desired size. For non-singleton dimensions of X must hold 6 | % SIZE(X,I) == D(I). 7 | 8 | function X = broadcast(X, d) 9 | X = bsxfun(@plus, zeros(d), X); 10 | -------------------------------------------------------------------------------- /utilities/columns_to_cells.m: -------------------------------------------------------------------------------- 1 | % COLUMNS_TO_CELLS - Puts the columns of a matrix to cells 2 | % 3 | % Y = COLUMNS_TO_CELLS(X) 4 | % 5 | % An MxN matrix is transformed to Nx1 cell array, where each cell 6 | % contains a column vector of X: Y{K} = X(:,K). 7 | 8 | % Last modified 2011-01-27 9 | % Copyright (c) Jaakko Luttinen (jaakko.luttinen@tkk.fi) 10 | 11 | function y = columns_to_cells(X) 12 | N = size(X,2); 13 | y = cell(N,1); 14 | for n=1:N 15 | y{n} = X(:,n); 16 | end 17 | -------------------------------------------------------------------------------- /utilities/mat2str.m: -------------------------------------------------------------------------------- 1 | % MAT2STR Convert numbers to strings. 2 | % 3 | % Y = MAT2STR(X) converts the elements of the matrix X to strings. The 4 | % strings are contained in a cell array with the same size as X. 5 | 6 | % Copyright (c) 2010 Jaakko Luttinen 7 | 8 | function Y = mat2str(X) 9 | 10 | Y = cell(size(X)); 11 | 12 | for i=1:numel(X) 13 | Y{i} = num2str(X(i)); 14 | end 15 | -------------------------------------------------------------------------------- /utilities/struct2list.m: -------------------------------------------------------------------------------- 1 | % L = STRUCT2LIST(S) 2 | % 3 | % Converts a struct into list of fieldname/value pairs. 4 | % 5 | % For instance, 6 | % 7 | % s = struct('name', 'Foo Bar', 'age', 42); 8 | % 9 | % Then 10 | % 11 | % l = struct2list(s); 12 | % 13 | % is equivalent to 14 | % 15 | % l = {'name', 'Foo Bar', 'age', 42}; 16 | % 17 | % Thus, 18 | % 19 | % struct(struct2list(s)) 20 | % 21 | % is identity operator. 22 | 23 | function l = struct2list(s) 24 | 25 | names = fieldnames(s); 26 | values = struct2cell(s); 27 | 28 | N = length(names); 29 | l = cell(2*N,1); 30 | 31 | for n=1:N 32 | i = 2*n-1; 33 | l(i:(i+1)) = {names{n}, values{n}}; 34 | end --------------------------------------------------------------------------------