├── GP ├── .gitignore ├── Copyright ├── README.md ├── any2vec.m ├── cov │ ├── apx.m │ ├── apxGrid.m │ ├── apxSparse.m │ ├── apxState.m │ ├── covADD.m │ ├── covConst.m │ ├── covCos.m │ ├── covDiscrete.m │ ├── covDot.m │ ├── covEye.m │ ├── covFBM.m │ ├── covGE.m │ ├── covGabor.m │ ├── covGaborard.m │ ├── covGaboriso.m │ ├── covLIN.m │ ├── covLINard.m │ ├── covLINiso.m │ ├── covLINone.m │ ├── covMaha.m │ ├── covMask.m │ ├── covMatern.m │ ├── covMaternard.m │ ├── covMaterniso.m │ ├── covNNone.m │ ├── covNoise.m │ ├── covOU.m │ ├── covOne.m │ ├── covPER.m │ ├── covPERard.m │ ├── covPERiso.m │ ├── covPP.m │ ├── covPPard.m │ ├── covPPiso.m │ ├── covPeriodic.m │ ├── covPeriodicNoDC.m │ ├── covPoly.m │ ├── covPref.m │ ├── covProd.m │ ├── covRQ.m │ ├── covRQard.m │ ├── covRQiso.m │ ├── covSE.m │ ├── covSEard.m │ ├── covSEiso.m │ ├── covSEisoU.m │ ├── covSEproj.m │ ├── covSEvlen.m │ ├── covSM.m │ ├── covScale.m │ ├── covSum.m │ ├── covULL.m │ ├── covW.m │ ├── covWarp.m │ └── covZero.m ├── doc │ ├── README.md │ ├── changelog │ ├── checkmark.png │ ├── demoClassification.m │ ├── demoCrossCovariances.m │ ├── demoGrid1d.m │ ├── demoGrid2d.m │ ├── demoMinimize.m │ ├── demoRegression.m │ ├── demoSparse.m │ ├── demoState.m │ ├── f0.gif │ ├── f1.gif │ ├── f2.gif │ ├── f3.gif │ ├── f4.gif │ ├── f5.gif │ ├── f6.gif │ ├── f7.gif │ ├── f8.gif │ ├── f9.png │ ├── gpml_randn.m │ ├── index.html │ ├── manual.pdf │ ├── style.css │ ├── usageClassification.m │ ├── usageCov.m │ ├── usageLik.m │ ├── usageMean.m │ ├── usagePrior.m │ ├── usageRegression.m │ └── usageSampling.m ├── fitGP.m ├── getopts.m ├── gp.m ├── inf │ ├── infEP.m │ ├── infGaussLik.m │ ├── infGrid.m │ ├── infKL.m │ ├── infLOO.m │ ├── infLaplace.m │ ├── infMCMC.m │ ├── infPrior.m │ ├── infState.m │ └── infVB.m ├── lik │ ├── likBeta.m │ ├── likErf.m │ ├── likExp.m │ ├── likGamma.m │ ├── likGauss.m │ ├── likGaussWarp.m │ ├── likGumbel.m │ ├── likInvGauss.m │ ├── likLaplace.m │ ├── likLogGauss.m │ ├── likLogistic.m │ ├── likMix.m │ ├── likNegBinom.m │ ├── likPoisson.m │ ├── likSech2.m │ ├── likT.m │ ├── likUni.m │ └── likWeibull.m ├── mean │ ├── meanConst.m │ ├── meanDiscrete.m │ ├── meanGPexact.m │ ├── meanLinear.m │ ├── meanMask.m │ ├── meanNN.m │ ├── meanOne.m │ ├── meanPoly.m │ ├── meanPow.m │ ├── meanPref.m │ ├── meanProd.m │ ├── meanScale.m │ ├── meanSum.m │ ├── meanWSPC.m │ └── meanWarp.m ├── prior │ ├── priorClamped.m │ ├── priorClampedMulti.m │ ├── priorDelta.m │ ├── priorDeltaMulti.m │ ├── priorEqualMulti.m │ ├── priorGamma.m │ ├── priorGauss.m │ ├── priorGaussMulti.m │ ├── priorInvGauss.m │ ├── priorLaplace.m │ ├── priorLaplaceMulti.m │ ├── priorLogNormal.m │ ├── priorMix.m │ ├── priorSameMulti.m │ ├── priorSmoothBox1.m │ ├── priorSmoothBox2.m │ ├── priorT.m │ ├── priorTMulti.m │ ├── priorTransform.m │ └── priorWeibull.m ├── priorDistributions.m ├── startup.m ├── util │ ├── any2vec.m │ ├── binaryEPGP.m │ ├── binaryGP.m │ ├── binaryLaplaceGP.m │ ├── brentmin.m │ ├── covFITC.m │ ├── covGrid.m │ ├── digamma.m │ ├── elsympol.m │ ├── gauher.m │ ├── glm_invlink_exp.m │ ├── glm_invlink_expexp.m │ ├── glm_invlink_logistic.m │ ├── glm_invlink_logistic2.m │ ├── glm_invlink_logit.m │ ├── glm_invlink_square.m │ ├── gpr.m │ ├── infExact.m │ ├── infFITC.m │ ├── infFITC_EP.m │ ├── infFITC_Laplace.m │ ├── lbfgsb.m │ ├── lbfgsb │ │ ├── LICENSE │ │ ├── Makefile │ │ ├── README │ │ ├── array.h │ │ ├── arrayofmatrices.cpp │ │ ├── arrayofmatrices.h │ │ ├── blas.f │ │ ├── lbfgsb.cpp │ │ ├── linpack.f │ │ ├── matlabexception.cpp │ │ ├── matlabexception.h │ │ ├── matlabmatrix.cpp │ │ ├── matlabmatrix.h │ │ ├── matlabprogram.cpp │ │ ├── matlabprogram.h │ │ ├── matlabscalar.cpp │ │ ├── matlabscalar.h │ │ ├── matlabstring.cpp │ │ ├── matlabstring.h │ │ ├── program.cpp │ │ ├── program.h │ │ ├── solver_2_4.f │ │ ├── solver_3_0.f │ │ └── timer.f │ ├── lik_epquad.m │ ├── logphi.m │ ├── logsumexp2.m │ ├── make.m │ ├── minfunc │ │ ├── ArmijoBacktrack.m │ │ ├── License │ │ ├── WolfeLineSearch.m │ │ ├── compiled │ │ │ ├── lbfgsAddC.mex │ │ │ ├── lbfgsAddC.mexa64 │ │ │ ├── lbfgsAddC.mexglx │ │ │ ├── lbfgsAddC.mexmaci64 │ │ │ ├── lbfgsAddC.mexw64 │ │ │ ├── lbfgsC.mex │ │ │ ├── lbfgsC.mexa64 │ │ │ ├── lbfgsC.mexglx │ │ │ ├── lbfgsC.mexmac │ │ │ ├── lbfgsC.mexmaci │ │ │ ├── lbfgsC.mexmaci64 │ │ │ ├── lbfgsC.mexw32 │ │ │ ├── lbfgsC.mexw64 │ │ │ ├── lbfgsProdC.mex │ │ │ ├── lbfgsProdC.mexa64 │ │ │ ├── lbfgsProdC.mexglx │ │ │ ├── lbfgsProdC.mexmaci64 │ │ │ ├── lbfgsProdC.mexw64 │ │ │ ├── mcholC.mex │ │ │ ├── mcholC.mexa64 │ │ │ ├── mcholC.mexglx │ │ │ ├── mcholC.mexmac │ │ │ ├── mcholC.mexmaci64 │ │ │ ├── mcholC.mexw32 │ │ │ └── mcholC.mexw64 │ │ ├── conjGrad.m │ │ ├── dampedUpdate.m │ │ ├── isLegal.m │ │ ├── lbfgs.m │ │ ├── lbfgsAdd.m │ │ ├── lbfgsProd.m │ │ ├── lbfgsUpdate.m │ │ ├── mchol.m │ │ ├── mcholinc.m │ │ ├── mex │ │ │ ├── lbfgsAddC.c │ │ │ ├── lbfgsC.c │ │ │ ├── lbfgsProdC.c │ │ │ ├── mcholC.c │ │ │ ├── mexAll_matlab.m │ │ │ └── mexAll_octave.m │ │ ├── minFunc.m │ │ ├── minFunc_processInputOptions.m │ │ ├── polyinterp.m │ │ ├── precondDiag.m │ │ ├── precondTriu.m │ │ ├── precondTriuDiag.m │ │ └── taylorModel.m │ ├── minimize.m │ ├── minimize_lbfgsb.m │ ├── minimize_lbfgsb_gradfun.m │ ├── minimize_lbfgsb_objfun.m │ ├── minimize_minfunc.m │ ├── solve_chol.c │ ├── solve_chol.m │ ├── sparseinv │ │ ├── license.txt │ │ ├── sparseinv.c │ │ ├── sparseinv.h │ │ ├── sparseinv.m │ │ ├── sparseinv_mex.c │ │ └── sparseinv_mex.m │ ├── sq_dist.m │ └── vfe_xu_opt.m └── vec2any.m ├── Moment_Matching_Approximation ├── Gp_transition_change.m ├── KernalCov.m └── MM_test.m ├── Presentation_final_harshith.pdf ├── README.md ├── Thesis_harshith final.pdf ├── examples ├── Dc motor plant │ ├── Dc_motor_final_GP_mpc.fig │ ├── Dc_motor_final_f_mpc.fig │ ├── Gp_mpc.m │ ├── Gp_mpc_MS_test.m │ ├── Gp_transition_change.m │ ├── KernalCov.m │ ├── Results │ │ ├── GP_MPC_with_uncertainity │ │ │ ├── data_points_dc_noise.svg │ │ │ ├── f_mpc_noise.svg │ │ │ ├── f_mpc_noise_1.svg │ │ │ ├── mpc_dc_noise.svg │ │ │ └── mpc_dc_noise_1.svg │ │ ├── Gp_MPC_without_uncertanity │ │ │ ├── data_points_wo_dc.svg │ │ │ ├── f_mpc_wo_dc.svg │ │ │ ├── gp_mpc_wo.svg │ │ │ └── gp_mpc_wo_1.svg │ │ └── Validation │ │ │ ├── data_points_test_dc.svg │ │ │ ├── fig_1_data_points.svg │ │ │ ├── fig_2_gp_l.svg │ │ │ ├── fig_3_gp_test.svg │ │ │ ├── gp_test_dc.svg │ │ │ └── long_term_test_dc.svg │ ├── cost.m │ ├── costf.m │ ├── data │ │ ├── data.mat │ │ ├── data_dc_Gp_test_1.mat │ │ ├── data_dc_Gp_test_2.mat │ │ ├── data_dc_Gp_test_final.mat │ │ ├── data_dc_motor.mat │ │ ├── data_dc_motor_2.mat │ │ ├── data_dc_motor_3.mat │ │ ├── data_dc_motor_5.mat │ │ ├── data_dc_motor_6.mat │ │ ├── data_dc_motor_7.mat │ │ ├── data_dc_motor_8.mat │ │ ├── data_dc_motor_9.mat │ │ ├── data_dc_motor_noise.mat │ │ └── data_dc_motor_wo_noise.mat │ ├── f_mpc.m │ ├── fitGP.m │ └── getopts.m └── Van_der_pol_oscillator │ ├── Gp_mpc.m │ ├── Gp_mpc_MS_test.m │ ├── Gp_transition_change.m │ ├── KernalCov.m │ ├── VanDerPol_dynamics.m │ ├── VanDerPol_dynamics_test.m │ ├── VanDerPol_uncertanity_dynamics.m │ ├── cost.m │ ├── costf.m │ ├── data │ ├── Van_pol_1.mat │ ├── Van_pol_2.mat │ ├── Van_pol_3.mat │ ├── Van_pol_4.mat │ ├── Van_pol_5.mat │ ├── Van_pol_6.mat │ ├── Van_pol_uncertainty.mat │ ├── Van_pol_uncertainty1.mat │ ├── Van_pol_uncertainty3.mat │ ├── Van_pol_uncertainty4.mat │ ├── Van_pol_uncertainty5.mat │ └── Van_pol_uncertainty6.mat │ ├── f_mpc.m │ ├── plot_reults_vdp.m │ └── results │ ├── GP_MPC_with_uncertanity │ ├── 2_Gp_mpc_uncert_vdp.svg │ ├── 2_data_points_uncert_vdp.svg │ ├── 2_f_mpc_uncert_vdp.svg │ ├── 2_fmpc_setpont.svg │ ├── 2_setpoint_gp_mpc.svg │ ├── 3_gp_mpc_set_1_0.svg │ ├── 3_gp_mpc_set_1_0_1.svg │ ├── 4_f_mpc_set_1_0.svg │ ├── 4_gp_mpc_set_1_0.svg │ ├── 5_f_mpc_set_1_0.svg │ ├── 5_gp_mpc_set_1_0.svg │ ├── Gp_mpc_uncert_vdp.svg │ ├── data_points_uncert_vdp.svg │ └── f_mpc_uncert_vdp.svg │ ├── GP_MPC_without_uncertanity │ ├── GP_mpc_better.svg │ ├── data_set_1.svg │ └── f_mpc_better.svg │ ├── Phase_portrait │ ├── phase_portrait_1.pdf │ ├── phase_portrait_12.jpg │ ├── phase_portrait_13.jpg │ ├── phase_portrait_initial_pts.svg │ ├── phase_portrait_vector.pdf │ ├── phase_portrait_vector_pts.svg │ ├── pp.fig │ └── pp_1.fig │ ├── phase_portrait.svg │ └── validation │ ├── gp_valdation_w_uncert.png │ ├── gp_valdation_w_uncert.svg │ ├── gp_valdation_wo_n.svg │ ├── long_w_uncert.png │ ├── long_w_uncert.svg │ └── long_wo_noise.svg ├── multi_states_GP_MPC ├── Gp_mpc.m ├── Gp_mpc_MS_test.m ├── Gp_transition_change.m ├── KernalCov.m ├── cost.m ├── untitled.fig └── untitled2.fig ├── papers ├── Data-Efficient Reinforcement Learning with Probabilistic MPC.pdf ├── DeiHubHan09.pdf ├── Gp_for data-effecient_leaning_in_robotics and control.pdf ├── NIPS-2002-gaussian-process-priors-with-uncertain-inputs-application-to-multiple-step-ahead-time-series-forecasting-Paper.pdf ├── PILCO.pdf └── blockdia.png └── single_state_GP_MPC ├── Gp_mpc.m ├── Gp_mpc_test.m ├── Gp_transition_change.m ├── KernalCov.m └── cost.m /GP/.gitignore: -------------------------------------------------------------------------------- 1 | # Backup files 2 | *.*~ 3 | 4 | # Matlab data files 5 | *.mat 6 | -------------------------------------------------------------------------------- /GP/Copyright: -------------------------------------------------------------------------------- 1 | GAUSSIAN PROCESS REGRESSION AND CLASSIFICATION Toolbox version 4.2 2 | for GNU Octave 3.2.x and Matlab 7.x 3 | 4 | The code is released under the FreeBSD License. 5 | 6 | Copyright (c) 2005-2018 Carl Edward Rasmussen & Hannes Nickisch. All rights reserved. 7 | 8 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY CARL EDWARD RASMUSSEN & HANNES NICKISCH ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARL EDWARD RASMUSSEN & HANNES NICKISCH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 15 | 16 | The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of Carl Edward Rasmussen & Hannes Nickisch. 17 | 18 | The code and associated documentation is available from http://gaussianprocess.org/gpml/code. -------------------------------------------------------------------------------- /GP/any2vec.m: -------------------------------------------------------------------------------- 1 | % Extract the numerical values from "s" into the column vector "v". The 2 | % variable "s" can be of any type, including struct and cell array. 3 | % Non-numerical elements are ignored. See also the reverse vec2any.m. 4 | 5 | function v = any2vec(s) 6 | v = []; 7 | if isnumeric(s) 8 | v = s(:); % numeric values are recast to column vector 9 | elseif isstruct(s) 10 | v = any2vec(struct2cell(orderfields(s)));% alphabetize, conv to cell, recurse 11 | elseif iscell(s) 12 | for i = 1:numel(s) % cell array elements are handled sequentially 13 | v = [v; any2vec(s{i})]; 14 | end 15 | end % other types are ignored 16 | -------------------------------------------------------------------------------- /GP/cov/covConst.m: -------------------------------------------------------------------------------- 1 | function varargout = covConst(varargin) 2 | 3 | % Wrapper for unit constant covariance function covOne.m. 4 | % 5 | % Covariance function for a constant function. The covariance function is 6 | % parameterized as: 7 | % 8 | % k(x,z) = sf^2 9 | % 10 | % The scalar hyperparameter is: 11 | % 12 | % hyp = [ log(sf) ] 13 | % 14 | % For more help on design of covariance functions, try "help covFunctions". 15 | % 16 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-19. 17 | % 18 | % See also covFunctions.m, covOne.m. 19 | 20 | varargout = cell(max(nargout,1),1); 21 | [varargout{:}] = covScale({'covOne'},varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covCos.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covCos(hyp, x, z) 2 | 3 | % Stationary covariance function for a sinusoid with period p in 1d: 4 | % 5 | % k(x,z) = sf^2*cos(2*pi*(x-z)/p) 6 | % 7 | % where the hyperparameters are: 8 | % 9 | % hyp = [ log(p) 10 | % log(sf) ] 11 | % 12 | % Note that covPeriodicNoDC converges to covCos as ell goes to infinity. 13 | % 14 | % Copyright (c) by James Robert Lloyd and Hannes Nickisch, 2016-11-05. 15 | % 16 | % See also covFunctions.m, cov/covPeriodicNoDC.m. 17 | 18 | if nargin<2, K = '2'; return; end % report number of parameters 19 | if nargin<3, z = []; end % make sure, z exists 20 | xeqz = isempty(z); dg = strcmp(z,'diag'); % determine mode 21 | 22 | [n,D] = size(x); 23 | if D~=1, error('Covariance is defined for 1d data only.'), end 24 | p = exp(hyp(1)); 25 | sf2 = exp(2*hyp(2)); 26 | 27 | % precompute deviations and exploit symmetry of cos 28 | if dg % vector txx 29 | T = zeros(size(x,1),1); 30 | else 31 | if xeqz % symmetric matrix Txx 32 | T = 2*pi/p*bsxfun(@plus,x,-x'); 33 | else % cross covariances Txz 34 | T = 2*pi/p*bsxfun(@plus,x,-z'); 35 | end 36 | end 37 | 38 | K = sf2*cos(T); % covariances 39 | if nargout > 1 40 | dK = @(Q) dirder(Q,K,T,x,p,sf2,dg,xeqz); 41 | end 42 | 43 | function [dhyp,dx] = dirder(Q,K,T,x,p,sf2,dg,xeqz) 44 | dhyp = [sf2*(sin(T(:)).*T(:))'*Q(:); 2*Q(:)'*K(:)]; 45 | if nargout > 1 46 | R = -sf2*pi/p * Q .* sin(T); 47 | if dg 48 | dx = zeros(size(x)); 49 | else 50 | if xeqz 51 | dx = 2*(sum(R,2)-sum(R,1)'); 52 | else 53 | dx = 2*sum(R,2); 54 | end 55 | end 56 | end -------------------------------------------------------------------------------- /GP/cov/covEye.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covEye(hyp, x, z) 2 | 3 | % Independent covariance function, i.e. "white noise", with unit variance. 4 | % The covariance function is specified as: 5 | % 6 | % k(x^p,x^q) = \delta(p,q) 7 | % 8 | % \delta(p,q) is a Kronecker delta function which is 1 iff p=q and zero 9 | % otherwise in mode 1). 10 | % In cross covariance mode 2) two data points x_p and z_q are considered equal 11 | % if their difference norm |x_p-z_q| is less than eps, the machine precision. 12 | % The hyperparameters are: 13 | % 14 | % hyp = [ ] 15 | % 16 | % For more help on design of covariance functions, try "help covFunctions". 17 | % 18 | % Copyright (c) by Hannes Nickisch, 2016-04-18. 19 | % 20 | % See also covFunctions.m. 21 | 22 | tol = eps; % threshold on the norm when two vectors are considered to be equal 23 | if nargin<2, K = '0'; return; end % report number of parameters 24 | if nargin<3, z = []; end % make sure, z exists 25 | dg = strcmp(z,'diag'); % determine mode 26 | 27 | n = size(x,1); 28 | 29 | if dg % vector kxx 30 | K = ones(n,1); 31 | else 32 | if isempty(z) % symmetric matrix Kxx 33 | K = eye(n); 34 | else % cross covariances Kxz 35 | K = double(sq_dist(x',z') 1 40 | dK = @(Q) dirder(Q,x); % directional hyper derivative 41 | end 42 | 43 | function [dhyp,dx] = dirder(Q,x) 44 | dhyp = zeros(0,1); if nargout > 1, dx = zeros(size(x)); end -------------------------------------------------------------------------------- /GP/cov/covGE.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covGE(mode, par, hyp, varargin) 2 | 3 | % Gamma Exponential covariance function. 4 | % The covariance function is parameterized as: 5 | % 6 | % k(x,z) = exp(-r^gamma), r = maha(x,z) 7 | % 8 | % where maha(x,z) is a Mahalanobis distance and gamma is the shape parameter 9 | % for the GE covariance. The hyperparameters are: 10 | % 11 | % hyp = [ hyp_maha 12 | % log(gamma/(2-gamma)) ] 13 | % 14 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-27. 15 | % 16 | % See also cov/covFunctions.m. 17 | 18 | if nargin<1, mode = 'eye'; end, if nargin <2, par = []; end % default values 19 | if nargin<4, K = [covMaha(mode,par),'+1']; return, end 20 | 21 | gamma = 2/(1+exp(-hyp(end))); 22 | k = @(d2) exp(-d2.^(gamma/2)); 23 | dk = @(d2,k) -gamma/2*set_zero(k.*d2.^(gamma/2-1),d2==0); 24 | 25 | if nargout==2 26 | [K,dKmaha,D2] = covMaha(mode,par,k,dk,hyp(1:end-1),varargin{:}); 27 | dK = @(Q) dirder(Q,K,D2,dKmaha,gamma); 28 | else 29 | K = covMaha(mode,par,k,dk,hyp(1:end-1),varargin{:}); 30 | end 31 | 32 | function [dhyp,dx] = dirder(Q,K,D2,dKmaha,gamma) 33 | if nargout==1 34 | dhyp = dKmaha(Q); 35 | else 36 | [dhyp,dx] = dKmaha(Q); 37 | end 38 | Q = Q.*K; B = (gamma-2)*gamma/4 * D2.^(gamma/2) .* set_zero(log(D2),D2==0); 39 | dhyp = [dhyp; Q(:)'*B(:)]; 40 | 41 | function A = set_zero(A,I), A(I) = 0; -------------------------------------------------------------------------------- /GP/cov/covGaborard.m: -------------------------------------------------------------------------------- 1 | function varargout = covGaborard(varargin) 2 | 3 | % Wrapper for Gabor covariance function covGabor.m. 4 | % 5 | % Gabor covariance function with length scales ell and periods p. The covariance 6 | % function is parameterized as: 7 | % 8 | % k(x,z) = h(x-z), h(t) = exp(-sum(t.^2./(2*ell.^2)))*cos(2*pi*sum(t./p)). 9 | % 10 | % The hyperparameters are: 11 | % 12 | % hyp = [ log(ell_1) 13 | % log(ell_2) 14 | % .. 15 | % log(ell_D) 16 | % log(p_1) 17 | % log(p_2) 18 | % .. 19 | % log(p_D) ] 20 | % 21 | % For more help on design of covariance functions, try "help covFunctions". 22 | % 23 | % Note that covSM implements a weighted sum of Gabor covariance functions, but 24 | % using an alternative (spectral) parameterization. 25 | % 26 | % Copyright (c) by Hannes Nickisch, 2016-04-25. 27 | % 28 | % See also covFunctions.m, covGabor.m, covSM.m. 29 | 30 | varargout = cell(max(1,nargout),1); 31 | [varargout{:}] = covGabor('ard',varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covGaboriso.m: -------------------------------------------------------------------------------- 1 | function varargout = covGaboriso(varargin) 2 | 3 | % Wrapper for Gabor covariance function covGabor.m. 4 | % 5 | % Gabor covariance function with length scale ell and period p. The 6 | % covariance function is parameterized as: 7 | % 8 | % k(x,z) = h(x-z) with h(t) = exp(-t'*t/(2*ell^2))*cos(2*pi*sum(t)/p). 9 | % 10 | % The hyperparameters are: 11 | % 12 | % hyp = [ log(ell) 13 | % log(p) ] 14 | % 15 | % Note that covSM implements a weighted sum of Gabor covariance functions, but 16 | % using an alternative (spectral) parameterization. 17 | % 18 | % For more help on design of covariance functions, try "help covFunctions". 19 | % 20 | % Copyright (c) by Hannes Nickisch, 2016-04-25. 21 | % 22 | % See also covFunctions.m, cov/covGabor.m, covSM.m. 23 | 24 | varargout = cell(max(1,nargout),1); 25 | [varargout{:}] = covGabor('iso',varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covLIN.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covLIN(mode,par,hyp,x,z) 2 | 3 | % Linear covariance function. 4 | % The covariance function is parameterized as: 5 | % 6 | % k(x,z) = dot(x,z) 7 | % 8 | % where dot(x,z) is a dot product. The hyperparameters are: 9 | % 10 | % hyp = [ hyp_dot ] 11 | % 12 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2017-09-26. 13 | % 14 | % See also cov/covDot.m. 15 | 16 | if nargin<2, mode = 'eye'; par = []; end, narg = nargin; % default mode 17 | if ~ischar(mode) % make compatible to old interface version 18 | if nargin>2, z = hyp; end 19 | if nargin>1, x = par; end 20 | if nargin>0, hyp = mode; end 21 | mode = 'eye'; narg = narg+2; 22 | end 23 | if narg<4, K = covDot(mode,par); return, end 24 | if narg<5, z = []; end % make sure, z exists 25 | 26 | k = @(s) s; dk = @(s) ones(size(s)); 27 | 28 | if nargout > 1 29 | [K,dK] = covDot(mode,par,k,dk,hyp,x,z); 30 | else 31 | K = covDot(mode,par,k,dk,hyp,x,z); 32 | end -------------------------------------------------------------------------------- /GP/cov/covLINard.m: -------------------------------------------------------------------------------- 1 | function varargout = covLINard(varargin) 2 | 3 | % Wrapper for Linear covariance function covLin.m. 4 | % 5 | % Linear covariance function with Automatic Relevance Determination (ARD). The 6 | % covariance function is parameterized as: 7 | % 8 | % k(x,z) = x'*inv(P)*z 9 | % 10 | % where the P matrix is diagonal with ARD parameters ell_1^2,...,ell_D^2, where 11 | % D is the dimension of the input space. The hyperparameters are: 12 | % 13 | % hyp = [ log(ell_1) 14 | % log(ell_2) 15 | % .. 16 | % log(ell_D) ] 17 | % 18 | % Note that there is no bias term; use covConst to add a bias. 19 | % 20 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-26. 21 | % 22 | % See also cov/covLin.m. 23 | 24 | varargout = cell(max(1,nargout),1); 25 | [varargout{:}] = covLIN('ard',[],varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covLINiso.m: -------------------------------------------------------------------------------- 1 | function varargout = covLINiso(varargin) 2 | 3 | % Wrapper for Linear covariance function covLin.m. 4 | % 5 | % Linear covariance function with Automatic Relevance Determination (ARD). The 6 | % covariance function is parameterized as: 7 | % 8 | % k(x,z) = x'*inv(P)*z 9 | % 10 | % where the P matrix is ell^2 times the unit matrix. The hyperparameters are: 11 | % 12 | % hyp = [ log(ell) ] 13 | % 14 | % Note that there is no bias term; use covConst to add a bias. 15 | % 16 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2017-09-26. 17 | % 18 | % See also cov/covLin.m. 19 | 20 | varargout = cell(max(1,nargout),1); 21 | [varargout{:}] = covLIN('iso',[],varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covLINone.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covLINone(hyp, x, z) 2 | 3 | % Linear covariance function with a single hyperparameter. The covariance 4 | % function is parameterized as: 5 | % 6 | % k(x,z) = (x'*z + 1)/t^2; 7 | % 8 | % where the P matrix is t2 times the unit matrix. The second term plays the 9 | % role of the bias. The hyperparameter is: 10 | % 11 | % hyp = [ log(t) ] 12 | % 13 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-23. 14 | % 15 | % See also covFunctions.m. 16 | 17 | if nargin<2, K = '1'; return; end % report number of parameters 18 | if nargin<3, z = []; end % make sure, z exists 19 | xeqz = isempty(z); dg = strcmp(z,'diag'); % determine mode 20 | 21 | it2 = exp(-2*hyp); % t2 inverse 22 | 23 | % precompute inner products 24 | if dg % vector kxx 25 | K = sum(x.*x,2); 26 | else 27 | if xeqz % symmetric matrix Kxx 28 | K = x*x'; 29 | else % cross covariances Kxz 30 | K = x*z'; 31 | end 32 | end 33 | K = it2*(K+1); % covariances 34 | if nargout > 1, dK = @(Q) dirder(Q,K,x,z,it2,xeqz,dg); end % dir derivative 35 | 36 | function [dhyp,dx] = dirder(Q,K,x,z,it2,xeqz,dg) 37 | dhyp = -2*Q(:)'*K(:); 38 | if nargout>1 39 | if dg 40 | dx = zeros(size(x)); 41 | else 42 | if xeqz 43 | dx = it2*(Q*x+Q'*x); 44 | else 45 | dx = it2*Q*z; 46 | end 47 | end 48 | end -------------------------------------------------------------------------------- /GP/cov/covMask.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covMask(mask, cov, hyp, x, z) 2 | 3 | % Apply a covariance function to a subset of the dimensions only. The subset can 4 | % either be specified by a 0/1 mask by a boolean mask or by an index set. 5 | % 6 | % This function doesn't actually compute very much on its own, it merely does 7 | % some bookkeeping, and calls another covariance function to do the actual work. 8 | % 9 | % The function computes: 10 | % k(x,z) = k0(x(mask),z(mask)) 11 | % Example: 12 | % k0 = {@covSEiso}; 13 | % msk = [1,3,7]; 14 | % k = {@covMask,msk,k0{:}}; 15 | % 16 | % The function was suggested by Iain Murray, 2010-02-18 and is based on an 17 | % earlier implementation of his dating back to 2009-06-16. 18 | % 19 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2018-11-14. 20 | % 21 | % See also covFunctions.m. 22 | 23 | narg = nargin; % make a copy to become independent of actual number of params 24 | if iscell(mask) && numel(mask)==2 % => [K,dK] = covMask({mask, cov}, hyp, x, z) 25 | if narg>3, z = x; end % shift parameters by 1 26 | if narg>2, x = hyp; end 27 | if narg>1, hyp = cov; end 28 | narg = narg+1; 29 | cov = mask{2}; mask = mask{1}; % split {mask, cov} into constituents 30 | end 31 | 32 | if ~iscell(cov), cov = {cov}; end % properly wrap into cell array 33 | nh_string = feval(cov{:}); % number of hyperparameters of the full covariance 34 | 35 | if max(mask)<2 && length(mask)>1, mask = find(mask); end % convert 1/0->index 36 | D = length(mask); % masked dimension 37 | if narg<4, K = num2str(eval(nh_string)); return, end % number of parameters 38 | if narg<5, z = []; end % make sure, z exists 39 | xeqz = isempty(z); dg = strcmp(z,'diag'); % determine mode 40 | 41 | if eval(nh_string)~=length(hyp) % check hyperparameters 42 | error('number of hyperparameters does not match size of masked data') 43 | end 44 | 45 | xm = x(:,mask); if ~dg && ~xeqz, zm = z(:,mask); else zm = z; end 46 | if nargout>1 47 | [K,dK] = feval(cov{:}, hyp, xm, zm); 48 | dK = @(Q) dirder(Q,dK,x,mask); 49 | else 50 | K = feval(cov{:}, hyp, xm, zm); 51 | end 52 | 53 | function [dhyp,dx] = dirder(Q,dK,x,mask) 54 | if nargout>1 55 | [dhyp,dxm] = dK(Q); n = size(x,1); 56 | subs = [repmat((1:n)',length(mask),1), reshape(repmat(mask(:)',n,1),[],1)]; 57 | dx = accumarray(subs,dxm(:),size(x)); 58 | else 59 | dhyp = dK(Q); 60 | end -------------------------------------------------------------------------------- /GP/cov/covMaternard.m: -------------------------------------------------------------------------------- 1 | function varargout = covMaternard(d,varargin) 2 | 3 | % Wrapper for Matern covariance function covMatern.m. 4 | % 5 | % Matern covariance function with nu = d/2 and with Automatic Relevance 6 | % Determination (ARD) distance measure. For d=1 the function is also known as 7 | % the exponential covariance function or the Ornstein-Uhlenbeck covariance 8 | % in 1d. The covariance function is: 9 | % 10 | % k(x,z) = f( sqrt(d)*r ) * exp(-sqrt(d)*r) 11 | % 12 | % with f(t)=1 for d=1, f(t)=1+t for d=3 and f(t)=1+t+t^2/3 for d=5. 13 | % Here r is the distance sqrt((x-z)'*inv(P)*(x-z)), where the P matrix 14 | % is diagonal with ARD parameters ell_1^2,...,ell_D^2, where D is the dimension 15 | % of the input space and sf2 is the signal variance. The hyperparameters are: 16 | % 17 | % hyp = [ log(ell_1) 18 | % log(ell_2) 19 | % .. 20 | % log(ell_D) 21 | % log(sf) ] 22 | % 23 | % Copyright (c) by Hannes Nickisch, 2016-04-17. 24 | % 25 | % See also covFunctions.m. 26 | 27 | varargout = cell(max(1,nargout),1); 28 | [varargout{:}] = covScale({'covMatern','ard',[],d},varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covMaterniso.m: -------------------------------------------------------------------------------- 1 | function varargout = covMaterniso(d,varargin) 2 | 3 | % Wrapper for Matern covariance function covMatern.m. 4 | % 5 | % Matern covariance function with nu = d/2 and isotropic distance measure. For 6 | % d=1 the function is also known as the exponential covariance function or the 7 | % Ornstein-Uhlenbeck covariance in 1d. The covariance function is: 8 | % 9 | % k(x,z) = sf^2 * f( sqrt(d)*r ) * exp(-sqrt(d)*r) 10 | % 11 | % with f(t)=1 for d=1, f(t)=1+t for d=3 and f(t)=1+t+t^2/3 for d=5. 12 | % Here r is the distance sqrt((x-z)'*inv(P)*(x-z)), P is ell times 13 | % the unit matrix and sf2 is the signal variance. The hyperparameters are: 14 | % 15 | % hyp = [ log(ell) 16 | % log(sf) ] 17 | % 18 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-27. 19 | % 20 | % See also covFunctions.m. 21 | 22 | varargout = cell(max(1,nargout),1); 23 | [varargout{:}] = covScale({'covMatern','iso',[],d},varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covNNone.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covNNone(hyp, x, z) 2 | 3 | % Neural network covariance function with a single parameter for the distance 4 | % measure. The covariance function is parameterized as: 5 | % 6 | % k(x,z) = sf2 * asin(x'*P*z / sqrt[(1+x'*P*x)*(1+z'*P*z)]) 7 | % 8 | % where the x and z vectors on the right hand side have an added extra bias 9 | % entry with unit value. P is ell^-2 times the unit matrix and sf2 controls the 10 | % signal variance. The hyperparameters are: 11 | % 12 | % hyp = [ log(ell) 13 | % log(sqrt(sf2) ] 14 | % 15 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-23. 16 | % 17 | % See also covFunctions.m. 18 | 19 | if nargin<2, K = '2'; return; end % report number of parameters 20 | if nargin<3, z = []; end % make sure, z exists 21 | xeqz = isempty(z); dg = strcmp(z,'diag'); % determine mode 22 | 23 | ell2 = exp(2*hyp(1)); 24 | sf2 = exp(2*hyp(2)); 25 | 26 | sx = 1 + sum(x.*x,2); 27 | if dg % vector kxx 28 | A = sx./(sx+ell2); sz = sx; 29 | else 30 | if xeqz % symmetric matrix Kxx 31 | S = 1 + x*x'; sz = sx; 32 | A = S./(sqrt(ell2+sx)*sqrt(ell2+sx)'); 33 | else % cross covariances Kxz 34 | S = 1 + x*z'; sz = 1 + sum(z.*z,2); 35 | A = S./(sqrt(ell2+sx)*sqrt(ell2+sz)'); 36 | end 37 | end 38 | 39 | K = sf2*asin(A); % covariances 40 | if nargout > 1 41 | dK = @(Q) dirder(Q,K,A,sx,sz,ell2,sf2,x,z,dg,xeqz); % dir hyper derivative 42 | end 43 | 44 | function [dhyp,dx] = dirder(Q,K,A,sx,sz,ell2,sf2,x,z,dg,xeqz) 45 | n = size(x,1); 46 | if dg 47 | V = A; 48 | else 49 | vx = sx./(ell2+sx); 50 | if xeqz 51 | V = repmat(vx/2,1,n) + repmat(vx'/2,n,1); 52 | else 53 | vz = sz./(ell2+sz); nz = size(z,1); 54 | V = repmat(vx/2,1,nz) + repmat(vz'/2,n,1); 55 | end 56 | end 57 | P = Q./sqrt(1-A.*A); 58 | dhyp = [-2*sf2*sum(sum((A-A.*V).*P)); 2*Q(:)'*K(:)]; 59 | if nargout > 1 60 | if dg 61 | dx = zeros(size(x)); 62 | else 63 | W = P./(sqrt(ell2+sx)*sqrt(ell2+sz)'); ssx = sqrt(ell2+sx); 64 | if xeqz, W = W+W'; z = x; ssz = ssx; else ssz = sqrt(ell2+sz); end 65 | dx = sf2*(W*z - bsxfun(@times,x,((W.*A)*ssz)./ssx)); 66 | end 67 | end -------------------------------------------------------------------------------- /GP/cov/covNoise.m: -------------------------------------------------------------------------------- 1 | function varargout = covNoise(varargin) 2 | 3 | % Wrapper for unit "white noise" covariance function covEye.m. 4 | % 5 | % Independent covariance function, i.e. "white noise". 6 | % The covariance function is specified as: 7 | % 8 | % k(x^p,x^q) = sf^2 * \delta(p,q) 9 | % 10 | % \delta(p,q) is a Kronecker delta function which is 1 iff p=q and zero 11 | % otherwise in mode 1). 12 | % In cross covariance mode 2) two data points x_p and z_q are considered equal 13 | % if their difference norm |x_p-z_q| is less than eps, the machine precision. 14 | % The hyperparameters are: 15 | % 16 | % hyp = [ log(sf) ] 17 | % 18 | % For more help on design of covariance functions, try "help covFunctions". 19 | % 20 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-19. 21 | % 22 | % See also cov/covEye.m. 23 | 24 | varargout = cell(max(nargout,1),1); 25 | [varargout{:}] = covScale({'covEye'},varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covOne.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covOne(hyp, x, z) 2 | 3 | % Constant (degenerate) covariance function, with unit variance. 4 | % The covariance function is specified as: 5 | % 6 | % k(x,z) = 1 7 | % 8 | % hyp = [ ] 9 | % 10 | % For more help on design of covariance functions, try "help covFunctions". 11 | % 12 | % Copyright (c) by Hannes Nickisch, 2016-04-17. 13 | % 14 | % See also covFunctions.m. 15 | 16 | if nargin<2, K = '0'; return; end % report number of parameters 17 | if nargin<3, z = []; end % make sure, z exists 18 | dg = strcmp(z,'diag'); % determine mode 19 | 20 | n = size(x,1); 21 | 22 | if dg % vector kxx 23 | K = ones(n,1); 24 | else 25 | if isempty(z) % symmetric matrix Kxx 26 | K = ones(n,n); 27 | else % cross covariances Kxz 28 | K = ones(n,size(z,1)); 29 | end 30 | end 31 | 32 | if nargout > 1 33 | dK = @(Q) dirder(Q,K,x); % directional hyper derivative 34 | end 35 | 36 | function [dhyp,dx] = dirder(Q,K,x) 37 | dhyp = zeros(0,1); if nargout > 1, dx = zeros(size(x)); end 38 | -------------------------------------------------------------------------------- /GP/cov/covPERard.m: -------------------------------------------------------------------------------- 1 | function varargout = covPERard(varargin) 2 | 3 | % Wrapper for PERiodic covariance function covPER.m. 4 | % 5 | % Periodic covariance function from an arbitrary covariance function k0 via 6 | % embedding IR^D into IC^D. 7 | % The covariance function is parameterized as: 8 | % 9 | % k(x,z) = k0(u(x),u(z)), u(x) = [sin(pi*x./p); cos(pi*x./p)] 10 | % 11 | % where the period p belongs to covPERiso and hyp0 belong to k0: 12 | % 13 | % hyp = [ log(p_1) 14 | % log(p_2) 15 | % . 16 | % log(p_D) 17 | % hyp0 ] 18 | % 19 | % Note that for k0 = covSEard and D = 1, a faster alternative is covPeriodic. 20 | % 21 | % Copyright (c) by Hannes Nickisch, 2016-04-25. 22 | % 23 | % See also covFunctions.m. 24 | 25 | varargout = cell(max(1,nargout),1); 26 | [varargout{:}] = covPER('ard',varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covPERiso.m: -------------------------------------------------------------------------------- 1 | function varargout = covPERiso(varargin) 2 | 3 | % Wrapper for PERiodic covariance function covPER.m. 4 | % 5 | % Periodic covariance function from an arbitrary covariance function k0 via 6 | % embedding IR^D into IC^D. 7 | % The covariance function is parameterized as: 8 | % 9 | % k(x,z) = k0(u(x),u(z)), u(x) = [sin(pi*x/p); cos(pi*x/p)] 10 | % 11 | % where the period p belongs to covPERiso and hyp0 belongs to k0: 12 | % 13 | % hyp = [ log(p) 14 | % hyp0 ] 15 | % 16 | % Note that for k0 = covSEiso and D = 1, a faster alternative is covPeriodic. 17 | % 18 | % Copyright (c) by Hannes Nickisch, 2016-04-25. 19 | % 20 | % See also covFunctions.m. 21 | 22 | varargout = cell(max(1,nargout),1); 23 | [varargout{:}] = covPER('iso',varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covPP.m: -------------------------------------------------------------------------------- 1 | function varargout = covPP(mode, par, v, hyp, x, varargin) 2 | 3 | % Piecewise Polynomial covariance function with compact support, v = 0,1,2,3. 4 | % The covariance functions are 2v times contin. diff'ble and the corresponding 5 | % processes are hence v times mean-square diffble. The covariance function is: 6 | % 7 | % k(x,z) = max(1-r,0)^(j+v) * f(r,j) with j = floor(D/2)+v+1 8 | % 9 | % where r is the Mahalanobis distance sqrt(maha(x,z)). The hyperparameters are: 10 | % 11 | % hyp = [ hyp_maha ] 12 | % 13 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-05-23. 14 | % 15 | % See also cov/covMaha.m. 16 | 17 | if nargin < 1, error('Mode cannot be empty.'); end % no default 18 | if nargin < 2, par = []; end % default 19 | varargout = cell(max(1, nargout), 1); % allocate mem for output 20 | if nargin<5, varargout{1} = covMaha(mode,par); return, end 21 | 22 | [n,D] = size(x); 23 | if all(v~=[0,1,2,3]), error('only 0,1,2 and 3 allowed for v'), end % degree 24 | j = floor(D/2)+v+1; % exponent 25 | 26 | switch v 27 | case 0, f = @(r,j) 1; 28 | df = @(r,j) 0; 29 | case 1, f = @(r,j) 1 + (j+1)*r; 30 | df = @(r,j) (j+1); 31 | case 2, f = @(r,j) 1 + (j+2)*r + ( j^2+ 4*j+ 3)/ 3*r.^2; 32 | df = @(r,j) (j+2) + 2*( j^2+ 4*j+ 3)/ 3*r; 33 | case 3, f = @(r,j) 1 + (j+3)*r + (6*j^2+36*j+45)/15*r.^2 ... 34 | + (j^3+9*j^2+23*j+15)/15*r.^3; 35 | df = @(r,j) (j+3) + 2*(6*j^2+36*j+45)/15*r ... 36 | + (j^3+9*j^2+23*j+15)/ 5*r.^2; 37 | end 38 | cs = @(r,e) (r<1).*max(1-r,0).^e; 39 | pp = @(r,j,v,f) cs(r,j+v ).* f(r,j); 40 | dpp = @(r,j,v,f) r.*cs(r,j+v-1).*( f(r,j)*(j+v) - max(1-r,0).*df(r,j) ); 41 | 42 | k = @(d2) pp( sqrt(d2), j, v, f ); 43 | dk = @(d2,k) set_zero( -(1/2)*dpp( sqrt(d2), j, v, f )./d2 , d2==0); 44 | 45 | [varargout{:}] = covMaha(mode, par, k, dk, hyp, x, varargin{:}); 46 | function A = set_zero(A,I), A(I) = 0; -------------------------------------------------------------------------------- /GP/cov/covPPard.m: -------------------------------------------------------------------------------- 1 | function varargout = covPPard(v,varargin) 2 | 3 | % Wrapper for Piecewise Polyonomial covariance function covPP.m. 4 | % 5 | % Piecewise Polynomial covariance function with compact support, v = 0,1,2,3. 6 | % The covariance functions are 2v times contin. diff'ble and the corresponding 7 | % processes are hence v times mean-square diffble. The covariance function is: 8 | % 9 | % k(x,z) = s2f * max(1-r,0)^(j+v) * f(r,j) with j = floor(D/2)+v+1 10 | % 11 | % where r is the distance sqrt((x-z)'*inv(P)*(x-z)), and the P matrix 12 | % is diagonal with ARD parameters ell_1^2,...,ell_D^2, where D is the dimension 13 | % of the input space and sf2 is the signal variance. The hyperparameters are: 14 | % 15 | % hyp = [ log(ell_1) 16 | % log(ell_2) 17 | % .. 18 | % log(ell_D) 19 | % log(sf) ] 20 | % 21 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-27. 22 | % 23 | % See also cov/covPP.m. 24 | 25 | varargout = cell(max(1,nargout),1); 26 | [varargout{:}] = covScale({'covPP','ard',[],v},varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covPPiso.m: -------------------------------------------------------------------------------- 1 | function varargout = covPPiso(v,varargin) 2 | 3 | % Wrapper for Piecewise Polyonomial covariance function covPP.m. 4 | % 5 | % Piecewise polynomial covariance function with compact support, v = 0,1,2,3. 6 | % The covariance functions are 2v times contin. diff'ble and the corresponding 7 | % processes are hence v times mean-square diffble. The covariance function is: 8 | % 9 | % k(x,z) = sf^2 * max(1-r,0)^(j+v) * f(r,j) with j = floor(D/2)+v+1 10 | % 11 | % where r is the distance sqrt((x-z)'*inv(P)*(x-z)), P is ell^2 times 12 | % the unit matrix and sf2 is the signal variance. The hyperparameters are: 13 | % 14 | % hyp = [ log(ell) 15 | % log(sf) ] 16 | % 17 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-27. 18 | % 19 | % See also covPP.m. 20 | 21 | varargout = cell(max(1,nargout),1); 22 | [varargout{:}] = covScale({'covPP','iso',[],v},varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covPeriodic.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covPeriodic(hyp, x, z) 2 | 3 | % Stationary covariance function for a smooth periodic function, with period p 4 | % in 1d (see covPERiso and covPERard for multivariate data): 5 | % 6 | % k(x,z) = sf^2 * exp( -2*sin^2( pi*(x-z)/p )/ell^2 ) 7 | % 8 | % where the hyperparameters are: 9 | % 10 | % hyp = [ log(ell) 11 | % log(p) 12 | % log(sf) ] 13 | % 14 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-24. 15 | % 16 | % See also covFunctions.m. 17 | 18 | if nargin<2, K = '3'; return; end % report number of parameters 19 | if nargin<3, z = []; end % make sure, z exists 20 | xeqz = isempty(z); dg = strcmp(z,'diag'); % determine mode 21 | 22 | [n,D] = size(x); 23 | if D>1, error('Covariance is defined for 1d data only.'), end 24 | ell = exp(hyp(1)); p = exp(hyp(2)); sf2 = exp(2*hyp(3)); % extract hyperparams 25 | 26 | % precompute deviations and exploit symmetry of sin^2 27 | if dg % vector txx 28 | T = zeros(size(x,1),1); 29 | else 30 | if xeqz % symmetric matrix Txx 31 | T = pi/p*bsxfun(@plus,x,-x'); 32 | else % cross covariances Txz 33 | T = pi/p*bsxfun(@plus,x,-z'); 34 | end 35 | end 36 | 37 | S2 = (sin(T)/ell).^2; 38 | K = sf2*exp( -2*S2 ); % covariances 39 | 40 | if nargout>1 41 | dK = @(Q) dirder(Q,K,S2,T,ell,xeqz,x,z,p); % directional hyper derivative 42 | end 43 | 44 | function [dhyp,dx] = dirder(Q,K,S2,T,ell,xeqz,x,z,p) 45 | Q = K.*Q; P = sin(2*T).*Q; 46 | dhyp = [4*(S2(:)'*Q(:)); 2/ell^2*(P(:)'*T(:)); 2*sum(Q(:))]; 47 | if nargout > 1 48 | R = P./T; R(T==0) = 0; 49 | q2 = sum(R,2); q1 = sum(R,1)'; 50 | if xeqz 51 | y = bsxfun(@times,q1+q2,x) - (R+R')*x; 52 | else 53 | Rz = R*z; y = bsxfun(@times,q2,x) - Rz; 54 | end 55 | dx = -2*pi^2/(ell*p)^2 * y; 56 | end -------------------------------------------------------------------------------- /GP/cov/covPoly.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covPoly(mode,par,d,hyp,x,z) 2 | 3 | % Polynomial covariance function. The covariance function is parameterized as: 4 | % 5 | % k(x,z) = sf^2 * ( c + s )^d , where s = x*inv(P)*z is the dot product 6 | % 7 | % The hyperparameters are: 8 | % 9 | % hyp = [ hyp_dot 10 | % log(c) 11 | % log(sf) ] 12 | % 13 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2017-09-26. 14 | % 15 | % See also cov/covDot.m. 16 | 17 | if nargin<2, mode = 'eye'; par = []; end % default mode 18 | if ~ischar(mode) % make compatible to old interface version 19 | if nargin>3, z = hyp; end 20 | if nargin>2, x = d; end 21 | if nargin>1, hyp = par; end 22 | if nargin>0, d = mode; end 23 | mode = 'eye'; par = []; narg = nargin+2; 24 | else 25 | narg = nargin; 26 | end 27 | 28 | if narg<5, K = [covDot(mode,par),'+2']; return, end % report nr of parameters 29 | if narg<6, z = []; end % make sure, z exists 30 | [n,D] = size(x); % dimensions 31 | ne = eval(covDot(mode,par)); 32 | c = exp(hyp(ne+1)); % inhomogeneous offset 33 | sf2 = exp(2*hyp(ne+2)); % signal variance 34 | if d~=max(1,fix(d)), error('only nonzero integers allowed for d'), end % degree 35 | 36 | k = @(s) (c+s).^d; dk = @(s) d*(c+s).^(d-1); 37 | 38 | if nargout > 1 39 | [K,dK0] = covScale({'covDot',mode,par,k,dk},hyp([1:ne,ne+2]),x,z); 40 | S = covDot(mode,par,@(s)s,[],hyp(1:ne),x,z); 41 | dK = @(Q) dirder(Q,S,dK0,c,d,sf2,ne); 42 | else 43 | K = covScale({'covDot',mode,par,k,dk},hyp([1:ne,ne+2]),x,z); 44 | end 45 | 46 | function [dhyp,dx] = dirder(Q,S,dK0,c,d,sf2,ne) 47 | if nargout > 1, [dhyp,dx] = dK0(Q); else dhyp = dK0(Q); end 48 | dhyp = [dhyp(1:ne); c*d*sf2*(Q(:)'*(c+S(:)).^(d-1)); dhyp(ne+1)]; % insert c -------------------------------------------------------------------------------- /GP/cov/covPref.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covPref(cov, hyp, x, z) 2 | 3 | % covPref - covariance function for preference learning. The covariance 4 | % function corresponds to a prior on f(x1) - f(x2). 5 | % 6 | % k(x,z) = k_0(x1,z1) + k_0(x2,z2) - k_0(x1,z2) - k_0(x2,z1). 7 | % 8 | % The hyperparameters are: 9 | % 10 | % hyp = [ hyp_k0 ] 11 | % 12 | % For more help on design of covariance functions, try "help covFunctions". 13 | % 14 | % See Collaborative Gaussian Processes for Preference Learning, NIPS 2014. 15 | % 16 | % Copyright (c) by Hannes Nickisch and Roman Garnett, 2016-04-17. 17 | % 18 | % See also covFunctions.m. 19 | 20 | if nargin<3, K = strrep(feval(cov{:}),'D','D/2'); return; end % no of params 21 | if nargin<4, z = []; end % make sure, z exists 22 | xeqz = isempty(z); dg = strcmp(z,'diag'); % determine mode 23 | 24 | x1 = x(:,1:end/2); x2 = x(:,1+end/2:end); 25 | if dg || xeqz 26 | z1 = x1; z2 = x2; 27 | else 28 | z1 = z(:,1:end/2); z2 = z(:,1+end/2:end); 29 | end 30 | if xeqz 31 | [K11,dK11] = feval(cov{:},hyp,x1); 32 | [K22,dK22] = feval(cov{:},hyp,x2); 33 | else 34 | if dg 35 | [K11,dK11] = feval(cov{:},hyp,x1,'diag'); 36 | [K22,dK22] = feval(cov{:},hyp,x2,'diag'); 37 | else 38 | [K11,dK11] = feval(cov{:},hyp,x1,z1); 39 | [K22,dK22] = feval(cov{:},hyp,x2,z2); 40 | end 41 | end 42 | [K12,dK12] = feval(cov{:},hyp,x1,z2); 43 | [K21,dK21] = feval(cov{:},hyp,x2,z1); 44 | 45 | if dg, K12 = diag(K12); K21 = diag(K21); end 46 | K = K11 + K22 - K12 - K21; 47 | dK = @(Q) dirder(Q,dK11,dK22,dK12,dK21,dg,xeqz); 48 | 49 | function [dhyp,dx] = dirder(Q,dK11,dK22,dK12,dK21,dg,xeqz) 50 | if nargout > 1 51 | [dhyp11,dx11] = dK11(Q); [dhyp22,dx22] = dK22(Q); 52 | if dg 53 | [dhyp12,dx12] = dK12(diag(Q)); [dhyp21,dx21] = dK21(diag(Q)); 54 | else 55 | [dhyp12,dx12] = dK12(Q); [dhyp21,dx21] = dK21(Q); 56 | end 57 | if xeqz 58 | [junk,dx21t] = dK12(Q'); [junk,dx12t] = dK21(Q'); 59 | dx = [dx11-dx12-dx21t, dx22-dx21-dx12t]; 60 | else 61 | dx = [dx11-dx12, dx22-dx21]; 62 | end 63 | else 64 | dhyp11 = dK11(Q); dhyp22 = dK22(Q); 65 | if dg 66 | dhyp12 = dK12(diag(Q)); dhyp21 = dK21(diag(Q)); 67 | else 68 | dhyp12 = dK12(Q); dhyp21 = dK21(Q); 69 | end 70 | end 71 | dhyp = dhyp11 + dhyp22 - dhyp12 - dhyp21; -------------------------------------------------------------------------------- /GP/cov/covRQ.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covRQ(mode, par, hyp, varargin) 2 | 3 | % Rational Quadratic covariance function. 4 | % The covariance function is parameterized as: 5 | % 6 | % k(x,z) = [1 + maha(x,z)/(2*alpha)]^(-alpha) 7 | % 8 | % where maha(x,z) is a Mahalanobis distance and alpha is the shape parameter 9 | % for the RQ covariance. The hyperparameters are: 10 | % 11 | % hyp = [ hyp_maha 12 | % log(alpha) ] 13 | % 14 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-05-23. 15 | % 16 | % See also covFunctions.m. 17 | 18 | if nargin < 1, error('Mode cannot be empty.'); end % no default 19 | if nargin < 2, par = []; end % default 20 | if nargin<4, K = [covMaha(mode,par),'+1']; return, end 21 | 22 | alpha = exp(hyp(end)); 23 | k = @(d2) (1+0.5*d2/alpha).^(-alpha); dk = @(d2,k) -k./(2+d2/alpha); 24 | 25 | if nargout==2 26 | [K,dKmaha,D2] = covMaha(mode,par,k,dk,hyp(1:end-1),varargin{:}); 27 | dK = @(Q) dirder(Q,K,D2,dKmaha,alpha); 28 | else 29 | K = covMaha(mode,par,k,dk,hyp(1:end-1),varargin{:}); 30 | end 31 | 32 | function [dhyp,dx] = dirder(Q,K,D2,dKmaha,alpha) 33 | if nargout==1 34 | dhyp = dKmaha(Q); 35 | else 36 | [dhyp,dx] = dKmaha(Q); 37 | end 38 | Q = Q.*K; B = 1+0.5*D2/alpha; 39 | dhyp = [dhyp; sum(sum( Q.*(0.5*D2./B-alpha*log(B)) ))]; -------------------------------------------------------------------------------- /GP/cov/covRQard.m: -------------------------------------------------------------------------------- 1 | function varargout = covRQard(varargin) 2 | 3 | % Wrapper for Rational Quadratic covariance function covRQ.m. 4 | % 5 | % Rational Quadratic covariance function with Automatic Relevance Determination 6 | % (ARD) distance measure. The covariance function is parameterized as: 7 | % 8 | % k(x,z) = sf^2 * [1 + (x-z)'*inv(P)*(x-z)/(2*alpha)]^(-alpha) 9 | % 10 | % where the P matrix is diagonal with ARD parameters ell_1^2,...,ell_D^2, where 11 | % D is the dimension of the input space, sf2 is the signal variance and alpha 12 | % is the shape parameter for the RQ covariance. The hyperparameters are: 13 | % 14 | % hyp = [ log(ell_1) 15 | % log(ell_2) 16 | % .. 17 | % log(ell_D) 18 | % log(sf) 19 | % log(alpha) ] 20 | % 21 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-10-01. 22 | % 23 | % See also cov/covRQ.m. 24 | 25 | varargout = cell(max(1,nargout),1); 26 | if nargin>0 % restore old hyper parameter order 27 | hyp = varargin{1}; 28 | if numel(hyp)>2, varargin{1} = hyp([1:end-2,end,end-1]); end 29 | end 30 | [varargout{:}] = covScale({'covRQ','ard',[]},varargin{:}); 31 | if nargout>1 % restore old hyper parameter order 32 | o2 = varargout{2}; varargout{2} = @(Q) dirder(Q,o2); 33 | end 34 | 35 | function [dKdhyp,dKdx] = dirder(Q,dK) 36 | if nargout>1, [dKdhyp,dKdx] = dK(Q); else dKdhyp = dK(Q); end 37 | dKdhyp = dKdhyp([1:end-2,end,end-1]); -------------------------------------------------------------------------------- /GP/cov/covRQiso.m: -------------------------------------------------------------------------------- 1 | function varargout = covRQiso(varargin) 2 | 3 | % Wrapper for Rational Quadratic covariance function covRQ.m. 4 | % 5 | % Rational Quadratic covariance function with isotropic distance measure. The 6 | % covariance function is parameterized as: 7 | % 8 | % k(x,z) = sf^2 * [1 + (x-z)'*inv(P)*(x-z)/(2*alpha)]^(-alpha) 9 | % 10 | % where the P matrix is ell^2 times the unit matrix, sf2 is the signal 11 | % variance and alpha is the shape parameter for the RQ covariance. The 12 | % hyperparameters are: 13 | % 14 | % hyp = [ log(ell) 15 | % log(sf) 16 | % log(alpha) ] 17 | % 18 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-10-01. 19 | % 20 | % See also covRQ.m. 21 | 22 | varargout = cell(max(1,nargout),1); 23 | if nargin>0 % restore old hyper parameter order 24 | hyp = varargin{1}; 25 | if numel(hyp)>2, varargin{1} = hyp([1:end-2,end,end-1]); end 26 | end 27 | [varargout{:}] = covScale({'covRQ','iso',[]},varargin{:}); 28 | if nargout>1 % restore old hyper parameter order 29 | o2 = varargout{2}; varargout{2} = @(Q) dirder(Q,o2); 30 | end 31 | 32 | function [dKdhyp,dKdx] = dirder(Q,dK) 33 | if nargout>1, [dKdhyp,dKdx] = dK(Q); else dKdhyp = dK(Q); end 34 | dKdhyp = dKdhyp([1:end-2,end,end-1]); -------------------------------------------------------------------------------- /GP/cov/covSE.m: -------------------------------------------------------------------------------- 1 | function varargout = covSE(mode, par, varargin) 2 | % COVSE Squared Exponential covariance function with unit amplitude. 3 | % 4 | % Report number of hyperparameters 5 | % s = COVSE (mode) 6 | % s = COVSE (mode, par) 7 | % s = COVSE (mode, par, hyp) 8 | % 9 | % Evaluation of k(x,x) 10 | % k = COVSE (mode, par, hyp, x) 11 | % k = COVSE (mode, par, hyp, x, []) 12 | % 13 | % Evaluation of diag(k(x,x)) 14 | % k = COVSE (mode, par, hyp, x, 'diag') 15 | % 16 | % Evaluation of k(x,z) 17 | % k = COVSE (mode, par, hyp, x, z) 18 | % 19 | % Evaluation of function derivatives dk (w.r.t. hyp and x) 20 | % [k, dk] = COVSE (mode, par, hyp, x) 21 | % [k, dk] = COVSE (mode, par, hyp, x, []) 22 | % [k, dk] = COVSE (mode, par, hyp, x, 'diag') 23 | % [k, dk] = COVSE (mode, par, hyp, x, z) 24 | % 25 | % Call covFunctions to get an explanation of outputs in each mode. 26 | % 27 | % The covariance function is: 28 | % 29 | % k(x,z) = exp(-maha(x,z)/2) 30 | % 31 | % where maha(x,z) is a squared Mahalanobis distance. The function takes a "mode" 32 | % parameter, which specifies precisely the Mahalanobis distance used, see 33 | % covMaha. The function returns either the number of hyperparameters (with less 34 | % than 3 input arguments) or it returns a covariance matrix and (optionally) a 35 | % derivative function. 36 | % 37 | % See also COVMAHA, COVFUNCTIONS 38 | 39 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-05-23. 40 | 41 | if nargin < 1, error('Mode cannot be empty.'); end % no default 42 | if nargin < 2, par = []; end % default 43 | varargout = cell(max(1, nargout), 1); % allocate mem for output 44 | if nargin < 4, varargout{1} = covMaha(mode,par); return, end 45 | 46 | k = @(d2) exp(-d2/2); dk = @(d2,k) (-1/2)*k; % covariance and derivative 47 | [varargout{:}] = covMaha(mode, par, k, dk, varargin{:}); 48 | -------------------------------------------------------------------------------- /GP/cov/covSEard.m: -------------------------------------------------------------------------------- 1 | function varargout = covSEard(varargin) 2 | 3 | % Wrapper for Squared Exponential covariance function covSE.m. 4 | % 5 | % Squared Exponential covariance function with Automatic Relevance Detemination 6 | % (ARD) distance measure. The covariance function is parameterized as: 7 | % 8 | % k(x,z) = sf^2 * exp(-(x-z)'*inv(P)*(x-z)/2) 9 | % 10 | % where the P matrix is diagonal with ARD parameters ell_1^2,...,ell_D^2, where 11 | % D is the dimension of the input space and sf2 is the signal variance. The 12 | % hyperparameters are: 13 | % 14 | % hyp = [ log(ell_1) 15 | % log(ell_2) 16 | % . 17 | % log(ell_D) 18 | % log(sf) ] 19 | % 20 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-27. 21 | % 22 | % See also covSE.m. 23 | 24 | varargout = cell(max(1,nargout),1); 25 | [varargout{:}] = covScale({'covSE','ard',[]},varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covSEiso.m: -------------------------------------------------------------------------------- 1 | function varargout = covSEiso(varargin) 2 | 3 | % Wrapper for Squared Exponential covariance function covSE.m. 4 | % 5 | % Squared Exponential covariance function with isotropic distance measure. The 6 | % covariance function is parameterized as: 7 | % 8 | % k(x,z) = sf^2 * exp(-(x-z)'*inv(P)*(x-z)/2) 9 | % 10 | % where the P matrix is ell^2 times the unit matrix and sf^2 is the signal 11 | % variance. The hyperparameters are: 12 | % 13 | % hyp = [ log(ell) 14 | % log(sf) ] 15 | % 16 | % For more help on design of covariance functions, try "help covFunctions". 17 | % 18 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-27. 19 | % 20 | % See also cov/covSE.m. 21 | 22 | varargout = cell(max(1,nargout),1); 23 | [varargout{:}] = covScale({'covSE','iso',[]},varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covSEisoU.m: -------------------------------------------------------------------------------- 1 | function varargout = covSEisoU(varargin) 2 | 3 | % Wrapper for Squared Exponential covariance function covSE.m. 4 | % 5 | % Squared Exponential covariance function with isotropic distance measure with 6 | % unit magnitude. The covariance function is parameterized as: 7 | % 8 | % k(x,z) = exp(-(x-z)'*inv(P)*(x-z)/2) 9 | % 10 | % where the P matrix is ell^2 times the unit matrix and sf2 is the signal 11 | % variance. The hyperparameters are: 12 | % 13 | % hyp = [ log(ell) ] 14 | % 15 | % For more help on design of covariance functions, try "help covFunctions". 16 | % 17 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-27. 18 | % 19 | % See also cov/covSE.m. 20 | 21 | varargout = cell(max(1,nargout),1); 22 | [varargout{:}] = covSE('iso',[],varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covSEproj.m: -------------------------------------------------------------------------------- 1 | function varargout = covSEproj(d,varargin) 2 | 3 | % Wrapper for Squared Exponential covariance function covSE.m. 4 | % 5 | % Projected squared exponential covariance. The covariance function is 6 | % parameterized as: 7 | % 8 | % k(x,z) = sf^2 * exp(-(x-z)'*inv(P)*(x-z)/2) 9 | % 10 | % where the inv(P) matrix is L'*L with L of size (d,D) and sf^2 is the signal 11 | % variance. 12 | % 13 | % The hyperparameters are: 14 | % hyp = [ L_11; 15 | % L_21; 16 | % .. 17 | % L_d1; 18 | % .. 19 | % L_dD; 20 | % log(sf) ], 21 | % 22 | % Copyright (c) by Roman Garnett & Hannes Nickisch, 2016-04-27. 23 | % 24 | % See also covFunctions.m. 25 | 26 | varargout = cell(max(1,nargout),1); 27 | [varargout{:}] = covScale({'covSE','proj',d},varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covSEvlen.m: -------------------------------------------------------------------------------- 1 | function varargout = covSEvlen(llen,varargin) 2 | 3 | % Wrapper for Squared Exponential covariance function covSE.m. 4 | % 5 | % Squared Exponential covariance function with spatially varying lengthscale. 6 | % The covariance function is parameterized as: 7 | % 8 | % k(x,z) = sf^2 * sqrt(a/b)^D * exp(-(x-z)'*(x-z)/b) where 9 | % a = 2*len(x)*len(z) 10 | % b = len(x)^2 + len(z)^2 11 | % 12 | % where len is the spatially varying lengthscale (here specified in the log 13 | % domain), D is the dimension of the input data and sf^2 is the signal variance. 14 | % The log-lengthscale function llen is supposed to be a valid GPML mean function 15 | % with hyperparameters hyp_len. 16 | % 17 | % The hyperparameters of covSEvlen are: 18 | % 19 | % hyp = [ hyp_len 20 | % log(sf) ] 21 | % 22 | % The covariance function has been introduced by Mark N. Gibbs in his 1997 PhD 23 | % thesis and was later generalised by Paciorek&Schervish at NIPS 2004. 24 | % 25 | % Note that by setting len(x)=len(z)=ell for every input x and z, we 26 | % recover covSEiso. 27 | % 28 | % For more help on design of covariance functions, try "help covFunctions". 29 | % 30 | % Copyright (c) by Hannes Nickisch, 2016-05-04. 31 | % 32 | % See also cov/covSEiso.m, covFunctions.m. 33 | 34 | varargout = cell(max(1,nargout),1); 35 | [varargout{:}] = covScale({'covSE','vlen',llen},varargin{:}); -------------------------------------------------------------------------------- /GP/cov/covULL.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covULL(hyp, x, z) 2 | % covULL Stationary covariance function for underdamped linear Langevin process 3 | % 4 | % [K,dK] = covULL(hyp, x, z) 5 | % 6 | % where the hyperparameters are: 7 | % 8 | % hyp = [ log(mu) 9 | % log(omega) 10 | % log(a) ] 11 | % 12 | % The covariance is obtained by filtering white noise through an underdamped 13 | % 2nd order system 14 | % 15 | % m * f''(x) + c * f'(x) + k * f(x) = N(0,sf^2). 16 | % 17 | % k(t) = a^2*exp(-mu*t) * ( sin(omega*t)/omega + cos(omega*t)/mu ), 18 | % 19 | % where t = abs(x-z), and 20 | % 21 | % mu = c/(2*m), omega = sqrt(k/m-mu^2), a = sf/(2*sqrt(m*k)) 22 | % 23 | % Reference: 24 | % See https://en.wikipedia.org/wiki/Harmonic_oscillator 25 | % 26 | % Copyright (c) by Robert MacKay, 2016-11-15. 27 | % 28 | % See also covFunctions.m. 29 | 30 | if nargin<2, K = '3'; return; end % report number of parameters 31 | if nargin<3, z = []; end % make sure, z exists 32 | xeqz = isempty(z); dg = strcmp(z,'diag'); % determine mode 33 | 34 | [n,D] = size(x); 35 | if D~=1, error('Covariance is defined for 1d data only.'), end 36 | 37 | mu = exp(hyp(1)); 38 | omega = exp(hyp(2)); 39 | a2 = exp(2*hyp(3)); 40 | 41 | % precompute deviations 42 | if dg % vector txx 43 | T = zeros(n,1); 44 | else 45 | if xeqz % symmetric matrix Txx 46 | T = bsxfun(@minus,x,x'); 47 | else % cross covariances Txz 48 | T = bsxfun(@minus,x,z'); 49 | end 50 | end 51 | 52 | K = a2*exp(-mu*abs(T)) .* ( sin(omega*abs(T))/omega + cos(omega*T)/mu ); % cov 53 | if nargout > 1 54 | dK = @(Q) dirder(Q,K,T,mu,omega,a2,dg,xeqz); 55 | end 56 | 57 | function [dhyp,dx] = dirder(Q,K,T,mu,omega,a2,dg,xeqz) 58 | A = mu*abs(T).*K + a2/mu*exp(-mu*abs(T)) .* cos(omega*T); 59 | B = abs(T).*cos(omega*T) - sin(omega*abs(T))/omega - omega*T.*sin(omega*T)/mu; 60 | B = a2*exp(-mu*abs(T)) .* B; 61 | dhyp = [-A(:)'*Q(:); B(:)'*Q(:); 2*(K(:)'*Q(:))]; 62 | if nargout > 1 63 | R = -a2*(mu/omega+omega/mu)*exp(-mu*abs(T)) .* sin(omega*T) .* Q; 64 | if dg 65 | dx = zeros(size(Q,1),1); 66 | else 67 | if xeqz 68 | dx = sum(R,2)-sum(R,1)'; 69 | else 70 | dx = sum(R,2); 71 | end 72 | end 73 | end 74 | -------------------------------------------------------------------------------- /GP/cov/covWarp.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covWarp(cov, p, dp, Dp, hyp, x, z) 2 | 3 | % Apply a covariance function to p(x) rather than x i.e. warp the inputs. 4 | % 5 | % This function doesn't actually compute very much on its own, it merely does 6 | % some bookkeeping, and calls another covariance function to do the actual work. 7 | % 8 | % The function computes: 9 | % k(x,z) = k0(p(x),p(z)) 10 | % Example: 11 | % k0 = {@covSEiso}; 12 | % p = @(x) sum(2*x,2); 13 | % dp = @(x) 2*x; 14 | % Dp = 1; 15 | % k = {@covWarp,k0,p,dp,Dp}; 16 | % 17 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-11-14. 18 | % 19 | % See also covFunctions.m, covMask.m. 20 | 21 | nh_string = feval(cov{:}); % number of hyperparameters of the full covariance 22 | D = Dp; % make variable available 23 | if nargin<6, K = num2str(eval(nh_string)); return, end % number of parameters 24 | if nargin<7, z = []; end % make sure, z exists 25 | xeqz = isempty(z); dg = strcmp(z,'diag'); % determine mode 26 | if numel(p) ==0, p = @(x) x; end % default is identity 27 | if numel(dp)==0, dp = @(x) 1; end % default is one 28 | 29 | if eval(nh_string)~=length(hyp) % check hyperparameters 30 | error('number of hyperparameters does not match size of warped data') 31 | end 32 | 33 | px = p(x); if ~dg && ~xeqz, pz = p(z); else pz = z; end 34 | if nargout>1 35 | [K,dK] = feval(cov{:}, hyp, px, pz); 36 | dK = @(Q) dirder(Q,dK,x,dp); 37 | else 38 | K = feval(cov{:}, hyp, px, pz); 39 | end 40 | 41 | function [dhyp,dx] = dirder(Q,dK,x,dp) 42 | if nargout>1 43 | [dhyp,dx] = dK(Q); Dp = size(dx,2); dpx = dp(x); % size(dx)=[n,Dp] 44 | if ndims(dpx)<=2 % apply chain rule 45 | dx = bsxfun(@times,dx,dpx); % size(dpx)=[1,1] or [n,1] or [n,D] 46 | else 47 | dx = sum(bsxfun(@times,reshape(dx,[],1,Dp),dpx),3); % size(dpx)=[n,D,Dp] 48 | end % size(dx)=[n,D] 49 | else 50 | dhyp = dK(Q); 51 | end -------------------------------------------------------------------------------- /GP/cov/covZero.m: -------------------------------------------------------------------------------- 1 | function [K,dK] = covZero(hyp, x, z) 2 | 3 | % Constant (degenerate) covariance function, with zero variance. 4 | % The covariance function is specified as: 5 | % 6 | % k(x,z) = 0 7 | % 8 | % hyp = [ ] 9 | % 10 | % For more help on design of covariance functions, try "help covFunctions". 11 | % 12 | % Copyright (c) by Hannes Nickisch, 2016-04-17. 13 | % 14 | % See also covFunctions.m. 15 | 16 | if nargin<2, K = '0'; return; end % report number of parameters 17 | if nargin<3, z = []; end % make sure, z exists 18 | dg = strcmp(z,'diag'); % determine mode 19 | 20 | n = size(x,1); 21 | 22 | if dg % vector kxx 23 | K = zeros(n,1); 24 | else 25 | if isempty(z) % symmetric matrix Kxx 26 | K = zeros(n,n); 27 | else % cross covariances Kxz 28 | K = zeros(n,size(z,1)); 29 | end 30 | end 31 | 32 | if nargout > 1 33 | dK = @(Q) dirder(Q,K,x); % directional hyper derivative 34 | end 35 | 36 | function [dhyp,dx] = dirder(Q,K,x) 37 | dhyp = zeros(0,1); if nargout > 1, dx = zeros(size(x)); end 38 | -------------------------------------------------------------------------------- /GP/doc/changelog: -------------------------------------------------------------------------------- 1 | GAUSSIAN PROCESS REGRESSION AND CLASSIFICATION Toolbox version 4.2 2 | for GNU Octave 3.2.x and Matlab 7.x 3 | 4 | Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2018-06-11. 5 | 6 | Changes and small incremental fixes applied since the last major release. 7 | -------------------------------------------------------------------------------- /GP/doc/checkmark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/doc/checkmark.png -------------------------------------------------------------------------------- /GP/doc/demoCrossCovariances.m: -------------------------------------------------------------------------------- 1 | clear all, close all 2 | 3 | % GP spec 4 | mean = {@meanSum, {@meanLinear, @meanConst}}; hyp.mean = [0.5; 1]; 5 | cov = {@covMaterniso, 3}; ell = 1/4; sf = 1; hyp.cov = log([ell; sf]); 6 | lik = @likGauss; sn = 0.1; hyp.lik = log(sn); 7 | inf = @infGaussLik; 8 | 9 | % synthetic data 10 | n = 20; x = gpml_randn(0.3, n, 1); 11 | K = feval(cov{:}, hyp.cov, x); 12 | m = feval(mean{:}, hyp.mean, x); 13 | y = chol(K)'*gpml_randn(0.15, n, 1) + m + exp(hyp.lik)*gpml_randn(0.2, n, 1); 14 | 15 | % inference 16 | [nlZ,dnlZ,post] = gp(hyp, inf, mean, cov, lik, x, y); 17 | 18 | % prediction 19 | ns = 101; xs = linspace(-1.9, 1.9, ns)'; 20 | [ymu,ys2,fmu,fs2] = gp(hyp, inf, mean, cov, lik, x, post, xs); 21 | 22 | % compute cross-covariances 23 | Lchol = isnumeric(post.L) && all(all(tril(post.L,-1)==0)&diag(post.L)'>0&isreal(diag(post.L))'); 24 | if Lchol 25 | Kss = feval(cov{:}, hyp.cov, xs); 26 | Ks = feval(cov{:}, hyp.cov, x, xs); 27 | V = post.L'\(repmat(post.sW,1,ns).*Ks); 28 | S2 = Kss - V'*V; 29 | 30 | assert( norm(diag(S2)-fs2)<1e-10 ) 31 | end -------------------------------------------------------------------------------- /GP/doc/demoMinimize.m: -------------------------------------------------------------------------------- 1 | clear all, close all, nit = 50; % tabular rasa, max number of fun/grad evals 2 | n = 300; x = 10*sort(rand(n,1)); % sample data 3 | sn = 0.2; y = sin(x)+sqrt(x) + randn(n,1)*sn; 4 | cov = {@covSEiso}; sf = 1; ell = 0.4; hyp0.cov = log([ell;sf]); % covariance 5 | mean = {@meanSum,{@meanLinear,@meanConst}}; hyp0.mean = [0.2;1]; % mean 6 | lik = 'likGauss'; hyp0.lik = log(sn); inf = 'infExact'; % likelihood/inference 7 | par = {inf, mean, cov, lik, x, y}; 8 | 9 | 10 | [x1 fx1 c1] = minimize( hyp0,@gp,-nit,par{:}); 11 | [x2 fx2 c2] = minimize_minfunc(hyp0,@gp,-nit,par{:}); 12 | try 13 | [x3 fx3 c3] = minimize_lbfgsb(hyp0,@gp,-nit,par{:}); 14 | catch 15 | x3 = hyp0; fx3 = gp(hyp0,par{:}); c3 = 0; 16 | end 17 | fprintf('nit=%02d: minimize: %1.2f\n',c1,fx1(end)) 18 | fprintf('nit=%02d: minimize_minfunc: %1.2f\n',c2,fx2(end)) 19 | fprintf('nit=%02d: minimize_lbfgsb: %1.2f\n',c3,fx3(end)) -------------------------------------------------------------------------------- /GP/doc/f0.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/doc/f0.gif -------------------------------------------------------------------------------- /GP/doc/f1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/doc/f1.gif -------------------------------------------------------------------------------- /GP/doc/f2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/doc/f2.gif -------------------------------------------------------------------------------- /GP/doc/f3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/doc/f3.gif -------------------------------------------------------------------------------- /GP/doc/f4.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/doc/f4.gif -------------------------------------------------------------------------------- /GP/doc/f5.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/doc/f5.gif -------------------------------------------------------------------------------- /GP/doc/f6.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/doc/f6.gif -------------------------------------------------------------------------------- /GP/doc/f7.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/doc/f7.gif -------------------------------------------------------------------------------- /GP/doc/f8.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/doc/f8.gif -------------------------------------------------------------------------------- /GP/doc/f9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/doc/f9.png -------------------------------------------------------------------------------- /GP/doc/gpml_randn.m: -------------------------------------------------------------------------------- 1 | function x = gpml_randn(seed,varargin) 2 | 3 | % Generate pseudo-random numbers in a quick and dirty way. 4 | % The function makes sure, we obtain the same random numbers using Octave and 5 | % Matlab for the demo scripts. 6 | % 7 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch 2010-07-07. 8 | 9 | if nargin==2 10 | sz = varargin{1}; 11 | else 12 | sz = zeros(1,nargin-1); 13 | for i=1:nargin-1 14 | sz(i) = varargin{i}; 15 | end 16 | end 17 | 18 | n = prod(sz); N = ceil(n/2)*2; 19 | 20 | % minimal uniform random number generator for uniform deviates from [0,1] 21 | % by Park and Miller 22 | a = 7^5; m = 2^31-1; 23 | % using Schrage's algorithm 24 | q = fix(m/a); r = mod(m,a); % m = a*q+r 25 | u = zeros(N+1,1); u(1) = fix(seed*2^31); 26 | for i=2:N+1 27 | % Schrage's algorithm for mod(a*u(i),m) 28 | u(i) = a*mod(u(i-1),q) - r*fix(u(i-1)/q); 29 | if u(i)<0, u(i) = u(i)+m; end 30 | end 31 | u = u(2:N+1)/2^31; 32 | 33 | % Box-Muller transform: Numerical Recipies, 2nd Edition, $7.2.8 34 | % http://en.wikipedia.org/wiki/Box-Muller_transform 35 | w = sqrt(- 2*log(u(1:N/2))); % split into two groups 36 | x = [w.*cos(2*pi*u(N/2+1:N)); w.*sin(2*pi*u(N/2+1:N))]; 37 | x = reshape(x(1:n),sz); 38 | -------------------------------------------------------------------------------- /GP/doc/manual.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/doc/manual.pdf -------------------------------------------------------------------------------- /GP/doc/style.css: -------------------------------------------------------------------------------- 1 | body {font-family: sans-serif; font-size: 16px} 2 | table {font-size: inherit;} 3 | 4 | -------------------------------------------------------------------------------- /GP/doc/usageMean.m: -------------------------------------------------------------------------------- 1 | % demonstrate usage of mean functions 2 | % 3 | % See also meanFunctions.m. 4 | % 5 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2017-11-17. 6 | % File automatically generated using noweb. 7 | clear all, close all 8 | n = 5; D = 2; x = randn(n,D); % create a random data set 9 | 10 | % set up simple mean functions 11 | m0 = {'meanZero'}; hyp0 = []; % no hyperparameters are needed 12 | m1 = {'meanOne'}; hyp1 = []; % no hyperparameters are needed 13 | mc = {@meanConst}; hypc = 2; % also function handles are possible 14 | ml = {@meanLinear}; hypl = [2;3]; % m(x) = 2*x1 + 3*x2 15 | mp = {@meanPoly,2}; hypp = [1;1;2;3]; % m(x) = x1+x2+2*x1^2+3*x2^2 16 | mn = {@meanNN,[1,0; 0,1],[0.9,0.5]}; hypn = []; % nearest neighbor 17 | s = 12; hypd = randn(s,1); % discrete mean with 12 hypers 18 | md = {'meanDiscrete',s}; 19 | hyp.cov = [0;0]; hypg = []; % GP predictive mean 20 | xt = randn(2*n,D); yt = sign(xt(:,1)-xt(:,2)); % training data 21 | mg = {@meanGP,hyp,@infEP,@meanZero,@covSEiso,@likErf,xt,yt}; 22 | hype = [0;0; log(0.1)]; % regression GP predictive mean 23 | xt = randn(2*n,D); yt = xt(:,1).*xt(:,2); % training data 24 | me = {@meanGPexact,@meanZero,@covSEiso,xt,yt}; 25 | 26 | % set up composite mean functions 27 | msc = {'meanScale',{m1}}; hypsc = [3; hyp1]; % scale by 3 28 | msu = {'meanSum',{m0,mc,ml}}; hypsu = [hyp0; hypc; hypl]; % sum 29 | mpr = {@meanProd,{mc,ml}}; hyppr = [hypc; hypl]; % product 30 | mpo = {'meanPow',3,msu}; hyppo = hypsu; % third power 31 | mask = [false,true]; % mask excluding all but the 2nd component 32 | mma = {'meanMask',mask,ml}; hypma = hypl(mask); 33 | mpf = {@meanPref,ml}; hyppf = 2; % linear pref with slope 34 | mwp = {@meanWarp,ml,@sin,@cos};hypwp = 2; % sin of linear 35 | 36 | % 0) specify mean function 37 | % mean = md; hyp = hypd; x = randi([1,s],n,1); 38 | % mean = mn; hyp = hypn; 39 | % mean = mg; hyp = hypg; 40 | mean = me; hyp = hype; 41 | % mean = m0; hyp = hyp0; 42 | % mean = msu; hyp = hypsu; 43 | % mean = mpr; hyp = hyppr; 44 | % mean = mpo; hyp = hyppo; 45 | % mean = mpf; hyp = hyppf; 46 | 47 | % 1) query the number of parameters 48 | feval(mean{:}) 49 | 50 | % 2) evaluate the function on x 51 | feval(mean{:},hyp,x) 52 | 53 | % 3) evaluate and compute the derivatives 54 | mean = ml; hyp = hypl; 55 | [m,dm] = feval(mean{:},hyp,x) 56 | -------------------------------------------------------------------------------- /GP/getopts.m: -------------------------------------------------------------------------------- 1 | function opts=getopts(opts,newOpts) 2 | if isempty(newOpts); return; end 3 | ch1=fieldnames(newOpts); 4 | ch2=fieldnames(opts); 5 | for i=1:length(ch1) 6 | for j=1:length(ch2) 7 | if strcmp(ch1{i},ch2{j}) 8 | if isstruct(opts.(ch2{j})) && isstruct(newOpts.(ch1{i})) 9 | opts.(ch2{j})=getopts(opts.(ch2{j}),newOpts.(ch1{i})); 10 | else 11 | opts.(ch2{j})=newOpts.(ch1{i}); 12 | end 13 | break; 14 | end 15 | if j == length(ch2) 16 | opts.(ch1{i})=newOpts.(ch1{i}); 17 | end 18 | end 19 | end 20 | end 21 | 22 | -------------------------------------------------------------------------------- /GP/inf/infGaussLik.m: -------------------------------------------------------------------------------- 1 | function [post nlZ dnlZ] = infGaussLik(hyp, mean, cov, lik, x, y, opt) 2 | 3 | % Exact inference for a GP with Gaussian likelihood. 4 | % 5 | % Compute a parametrization of the posterior, the negative log marginal 6 | % likelihood and its derivatives w.r.t. the hyperparameters. The function takes 7 | % a specified covariance function (see covFunctions.m) and likelihood function 8 | % (see likFunctions.m), and is designed to be used with gp.m. 9 | % 10 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2019-03-24. 11 | % File automatically generated using noweb. 12 | % 13 | % See also infMethods.m, cov/apx.m. 14 | 15 | if nargin<7, opt = []; end % make sure parameter exists 16 | if iscell(lik), likstr = lik{1}; else likstr = lik; end 17 | if ~ischar(likstr), likstr = func2str(likstr); end 18 | if ~strcmp(likstr,'likGauss') % NOTE: no explicit call to likGauss 19 | error('Exact inference only possible with Gaussian likelihood'); 20 | end 21 | 22 | [n, D] = size(x); 23 | [m,dm] = feval(mean{:}, hyp.mean, x); % evaluate mean vector and deriv 24 | sn2 = exp(2*hyp.lik); W = ones(n,1)/sn2; % noise variance of likGauss 25 | K = apx(hyp,cov,x,opt); % set up covariance approximation 26 | [ldB2,solveKiW,dW,dhyp,post.L] = K.fun(W); % obtain functionality depending on W 27 | 28 | alpha = solveKiW(y-m); 29 | post.alpha = K.P(alpha); % return the posterior parameters 30 | post.sW = sqrt(W); % sqrt of noise precision vector 31 | if nargout>1 % do we want the marginal likelihood? 32 | nlZ = (y-m)'*alpha/2 + ldB2 + n*log(2*pi*sn2)/2; % -log marginal likelihood 33 | if nargout>2 % do we want derivatives? 34 | dnlZ = dhyp(alpha); dnlZ.mean = -dm(alpha); 35 | dnlZ.lik = -sn2*(alpha'*alpha) - 2*sum(dW)/sn2 + n; 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /GP/lik/likExp.m: -------------------------------------------------------------------------------- 1 | function [varargout] = likExp(link, hyp, varargin) 2 | 3 | % likExp Exponential likelihood function for strictly positive data y>0. 4 | % 5 | % Report number of hyperparameters 6 | % s = likExp () 7 | % s = likExp (link) 8 | % 9 | % Prediction mode 10 | % lp = likExp (link, hyp, y, mu) 11 | % [lp, ymu, ys2] = likExp (link, hyp, y, mu, s2) 12 | % 13 | % Inference mode 14 | % [varargout] = likExp (link, hyp, y, mu, s2, inf) 15 | % [varargout] = likExp (link, hyp, y, mu, s2, inf, i) 16 | % 17 | % Call likFunctions.m to get an explanation of outputs in each mode. 18 | % 19 | % The expression for the likelihood is 20 | % likExp(f) = exp(-y/mu) / mu with 21 | % mean=mu and variance=mu^2 where mu = g(f) is the intensity, f is a 22 | % Gaussian process, y is the strictly positive data. Hence, we have 23 | % llik(f) = log(likExp(f)) = -y/g(f) - log(g(f)). 24 | % 25 | % Internally, the function is in fact a wrapper around likGamma with hyper 26 | % parameter al=1. 27 | % 28 | % We provide two inverse link functions 'exp' and 'logistic': 29 | % g(f) = exp(f) and g(f) = log(1+exp(f))). 30 | % The link functions are located at util/glm_invlink_*.m. 31 | % Note that for the 'exp' intensity the likelihood lik(f) is log concave. 32 | % 33 | % The hyperparameters are: 34 | % 35 | % hyp = [ ] 36 | % 37 | % Several modes are provided, for computing likelihoods, derivatives and moments 38 | % respectively, see likFunctions.m for the details. In general, care is taken 39 | % to avoid numerical issues when the arguments are extreme. 40 | % 41 | % Copyright (c) by Hannes Nickisch, 2013-10-29. 42 | % 43 | % See also likFunctions.m, lik/likGamma.m. 44 | 45 | if nargin<4, varargout = {'0'}; return; end % report number of hyperparameters 46 | varargout = cell(nargout, 1); % allocate the right number of output arguments 47 | for j=1:nargout, varargout{j} = []; end % derivative output 48 | if nargin<=6, [varargout{:}] = likGamma(link,0,varargin{:}); end % log(al) = 0 -------------------------------------------------------------------------------- /GP/mean/meanConst.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanConst(hyp, x) 2 | 3 | % Constant mean function. The mean function is parameterized as: 4 | % 5 | % m(x) = c 6 | % 7 | % The hyperparameter is: 8 | % 9 | % hyp = [ c ] 10 | % 11 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-15. 12 | % 13 | % See also meanFunctions.m. 14 | 15 | if nargin<2, m = '1'; return; end % report number of hyperparameters 16 | if numel(hyp)~=1, error('Exactly one hyperparameter needed.'), end 17 | c = hyp; 18 | m = c*ones(size(x,1),1); % mean 19 | dm = @(q) sum(q); % directional derivative 20 | -------------------------------------------------------------------------------- /GP/mean/meanDiscrete.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanDiscrete(s, hyp, x) 2 | 3 | % Mean function for discrete inputs x. Given a function defined on the 4 | % integers 1,2,3,..,s, the mean function is parametrized as: 5 | % 6 | % m(x) = mu_x, 7 | % 8 | % where mu is a fixed vector of length s. 9 | % 10 | % This implementation assumes that the inputs x are given as integers 11 | % between 1 and s, which simply index the provided vector. 12 | % 13 | % The hyperparameters are: 14 | % 15 | % hyp = [ mu_1 16 | % mu_2 17 | % .. 18 | % mu_s ] 19 | % 20 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2016-04-16. 21 | % 22 | % See also covDiscrete.m, meanFunctions.m. 23 | 24 | if nargin==0, error('s must be specified.'), end % check for dimension 25 | if nargin<=2, m = num2str(s); return; end % report number of hyperparameters 26 | mu = hyp(:); m = mu(x(:)); % evaluate mean 27 | dm = @(q) dirder(q,s,x); 28 | 29 | function dmdhyp = dirder(q,s,x) 30 | dmdhyp = zeros(s,1); 31 | for i=1:s, dmdhyp(i) = sum(q(x==i)); end -------------------------------------------------------------------------------- /GP/mean/meanGPexact.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanGPexact(mean,cov,x,y, hypz,z) 2 | 3 | % Mean function being the predictive mean of a GP model: 4 | % 5 | % mu(z) = posterior mean of GP at location z as given by 6 | % mu(z) = gp(hyp,@infExact,mean,cov,@likGauss,x,y, z) where 7 | % hyp.mean = hyp_mean; hyp.lik = log(sn); hyp.cov = hyp.cov; 8 | % 9 | % The hyperparameters are: 10 | % 11 | % hypz = [ hyp_cov 12 | % log(sn) 13 | % hyp_mean ] 14 | % 15 | % where hyp_cov are the covariance function hyperparameters, sn is the 16 | % noise variance of the Gaussian likelihood and hyp_mean are the mean 17 | % function hyperparameters. 18 | % 19 | % Copyright (c) by Hannes Nickisch, 2016-04-16. 20 | % 21 | % See also meanFunctions.m and meanGP.m. 22 | 23 | if nargin<4, error('GP must be specified.'), end % check for dimension 24 | if isempty(mean), mean = @meanZero; end % set default and make cell 25 | if ~iscell(mean), mean = {mean}; end 26 | if isempty(cov), cov = @covSEiso; end 27 | if ~iscell(cov), cov = {cov}; end 28 | nms = feval(mean{:}); ncs = feval(cov{:}); % number of hyperparameter string 29 | if nargin<6, m = [ncs,'+1+',nms]; return, end % report number of hyperparameters 30 | 31 | [nz,D] = size(z); n = size(x,1); 32 | nc = eval(ncs); nm = eval(nms); 33 | hyp = vec2any(struct('cov',zeros(nc,1),'lik',0,'mean',zeros(nm,1)),hypz); 34 | 35 | [mu,dmu] = feval(mean{:},hyp.mean,x); 36 | [muz,dmuz] = feval(mean{:},hyp.mean,z); 37 | [K,dK] = feval(cov{:}, hyp.cov, x); 38 | [kz,dkz] = feval(cov{:}, hyp.cov, x,z); 39 | 40 | sn2 = exp(2*hyp.lik); % noise variance of likGauss 41 | if sn2<1e-6 % very tiny sn2 can lead to numerical trouble 42 | L = chol(K+sn2*eye(n)); sl = 1; % Cholesky factor of covariance with noise 43 | else 44 | L = chol(K/sn2+eye(n)); sl = sn2; % Cholesky factor of B 45 | end 46 | iKs = @(t) solve_chol(L,t)/sl; % iKs(t) = (K+sn2*eye(n))\t 47 | alpha = iKs(y-mu); 48 | m = muz+kz'*alpha; % eval posterior mean 49 | dm = @(q) dirder(q,alpha,dmu,dmuz,kz,dkz,iKs,dK,sn2); % directional derivative 50 | 51 | function dmdhyp = dirder(q,alpha,dmu,dmuz,kz,dkz,iKs,dK,sn2) 52 | v = iKs(kz*q); 53 | dmdhyp = [dkz(alpha*q')-dK(alpha*v'); -2*sn2*v'*alpha; dmuz(q)-dmu(v)]; -------------------------------------------------------------------------------- /GP/mean/meanLinear.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanLinear(hyp, x) 2 | 3 | % Linear mean function. The mean function is parameterized as: 4 | % 5 | % m(x) = sum_i c_i * x_i; 6 | % 7 | % The hyperparameter is: 8 | % 9 | % hyp = [ c_1 10 | % c_2 11 | % .. 12 | % c_D ] 13 | % 14 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-15. 15 | % 16 | % See also meanFunctions.m. 17 | 18 | if nargin<2, m = 'D'; return; end % report number of hyperparameters 19 | [n,D] = size(x); 20 | if any(size(hyp)~=[D,1]), error('Exactly D hyperparameters needed.'), end 21 | m = x*hyp(:); % evaluate mean 22 | dm = @(q) x'*q(:); % directional derivative 23 | -------------------------------------------------------------------------------- /GP/mean/meanMask.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanMask(mask, mean, hyp, x) 2 | % MEANMASK Apply a mean function to a subset of the dimensions only. 3 | % [m, dm] = MEANMASK (mask, mean, hyp, x) 4 | % 5 | % The subset of dimensions is specified by the input argument mask, and can 6 | % either be a 0/1 mask, by a boolean mask, or by an index set. The second input 7 | % argument, mean, should be a handle to a valid gpml mean function. The other 8 | % input arguments are the same as for regular mean functions. The size of 9 | % the hyperparamter vector, hyp, should match the number of hyperparameters of 10 | % the masked mean function. 11 | % 12 | % For example, if the input x is 3-dimensional and we want a linear mean on the 13 | % second component of the inputs we could write any of the following 14 | % 15 | % linear_on_x2 = {@meanMask, [0,1,0], @meanLinear} 16 | % linear_on_x2 = {@meanMask, [false,true,false], @meanLinear} 17 | % linear_on_x2 = {@meanMask, 2, @meanLinear} 18 | % 19 | % the function linear_on_x2 behaves like a regular mean function but the number 20 | % of hyperparameters is 1, instead of 3. 21 | % 22 | % This function doesn't actually compute very much on its own, it merely does 23 | % some bookkeeping, and calls another mean function to do the actual work. 24 | % 25 | % See also MEANFUNCTIONS 26 | 27 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-16. 28 | 29 | if ~iscell(mean), mean = {mean}; end % properly wrap into cell array 30 | nh_string = feval(mean{:}); % number of hyperparameters of the full mean 31 | 32 | if max(mask)<2 && length(mask)>1, mask = find(mask); end % convert 1/0->index 33 | D = length(mask); % masked dimension 34 | if nargin<4, m = num2str(eval(nh_string)); return; end % number of parameters 35 | 36 | if eval(nh_string)~=length(hyp) % check hyperparameters 37 | error('number of hyperparameters does not match size of masked data') 38 | end 39 | 40 | [m,dm] = feval(mean{:}, hyp, x(:,mask)); 41 | -------------------------------------------------------------------------------- /GP/mean/meanNN.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanNN(c,v, hyp, x) 2 | 3 | % Nearest neighbor mean function. The mean function is parameterized as: 4 | % 5 | % m(z) = v_j, j = arg min_i d(ci,x) where d is the Euclidean distance and ci is 6 | % the ith cluster center. 7 | % 8 | % The hyperparameters are: 9 | % 10 | % hyp = [ ] 11 | % 12 | % Copyright (c) by Hannes Nickisch, 2016-04-16. 13 | % 14 | % See also meanFunctions.m. 15 | 16 | if nargin<4, m = '0'; return; end % report number of hyperparameters 17 | if numel(hyp)~=0, error('No hyperparameters needed for this model.'), end 18 | 19 | [junk,j] = min(sq_dist(c',x')); m = v(j); m = m(:); % evaluate mean 20 | dm = @(q) zeros(0,1); % directional derivative 21 | -------------------------------------------------------------------------------- /GP/mean/meanOne.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanOne(hyp, x) 2 | 3 | % One mean function. The mean function does not have any parameters. 4 | % 5 | % m(x) = 1 6 | % 7 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2016-04-15. 8 | % 9 | % See also meanFunctions.m. 10 | 11 | if nargin<2, m = '0'; return; end % report number of hyperparameters 12 | m = ones(size(x,1),1); % mean 13 | dm = @(q) zeros(0,1); % directional derivative 14 | -------------------------------------------------------------------------------- /GP/mean/meanPoly.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanPoly(d, hyp, x) 2 | 3 | % meanPoly - compose a mean function as a polynomial. 4 | % 5 | % The degree d has to be a strictly positive integer. 6 | % 7 | % m(x) = sum_i=1..D sum_j=1..d a_ij * x_i^j 8 | % 9 | % The hyperparameter is: 10 | % 11 | % hyp = [ a_11 12 | % a_21 13 | % .. 14 | % a_D1 15 | % a_12 16 | % a_22 17 | % .. 18 | % a_Dd] 19 | % 20 | % This function doesn't actually compute very much on its own, it merely does 21 | % some bookkeeping, and calls other mean function to do the actual work. 22 | % 23 | % Copyright (c) by Hannes Nickisch 2016-04-15. 24 | % 25 | % See also meanFunctions.m. 26 | 27 | d = max(abs(floor(d)),1); % positive integer degree 28 | if nargin<3, m = ['D*',int2str(d)]; return; end % report number of hyperparams 29 | 30 | [n,D] = size(x); 31 | a = reshape(hyp,D,d); 32 | 33 | m = zeros(n,1); % allocate memory 34 | for j=1:d, m = m + (x.^j)*a(:,j); end % evaluate mean 35 | dm = @(q) dirder(q,x,a); % directional derivative 36 | 37 | function dhyp = dirder(q,x,a) 38 | [D,d] = size(a); 39 | dhyp = zeros(D*d,1); for j=1:d, dhyp((j-1)*D+(1:D)) = (x.^j)'*q(:); end -------------------------------------------------------------------------------- /GP/mean/meanPow.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanPow(d, mean, hyp, x) 2 | 3 | % meanPow - compose a mean function as the power of another mean function m0. 4 | % 5 | % m(x) = m_0(x) ^ d 6 | % 7 | % If the degree d is not a strictly positive integer, we use 8 | % m(x) = sign(m_0(x)) * abs(m_0(x)) ^ d 9 | % to stay within the reals 10 | % 11 | % The hyperparameter is: 12 | % 13 | % hyp = [ hyp_m0 ] 14 | % 15 | % This function doesn't actually compute very much on its own, it merely does 16 | % some bookkeeping, and calls other mean function to do the actual work. 17 | % 18 | % Copyright (c) by Carl Edward Rasmussen & Hannes Nickisch 2016-05-04. 19 | % 20 | % See also meanFunctions.m. 21 | 22 | if nargin<4 % report number of parameters 23 | m = feval(mean{:}); return 24 | end 25 | 26 | [m0,dm0] = feval(mean{:},hyp,x); % evaluate m0 27 | if d>0 && ceil(d)==d % strictly positive integer 28 | s = 1; 29 | else % general real number 30 | s = sign(m0); m0 = abs(m0); 31 | end 32 | m = s.*m0.^d; % mean 33 | dm = @(q) dm0( (d*m0.^(d-1)).*q ); % directional derivative -------------------------------------------------------------------------------- /GP/mean/meanPref.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanPref(mean, hyp, x) 2 | 3 | % meanPref - mean function for preference learning. 4 | % 5 | % m(x) = m_0(x1)-m_0(x2), where x1=x(:,1:D), x2=x(:,D+1:2*D), D = size(x,2)/2. 6 | % 7 | % The hyperparameters are: 8 | % 9 | % hyp = [ hyp_m0 ] 10 | % 11 | % This function doesn't actually compute very much on its own, it merely does 12 | % some bookkeeping, and calls another mean function to do the actual work. 13 | % 14 | % See Collaborative Gaussian Processes for Preference Learning, NIPS 2014. 15 | % 16 | % Copyright (c) by Hannes Nickisch and Roman Garnett, 2016-04-16. 17 | % 18 | % See also meanFunctions.m and cov/covPref.m. 19 | 20 | if nargin<3, m = strrep(feval(mean{:}),'D','D/2'); return; end % no of params 21 | 22 | [m1,dm1] = feval(mean{:}, hyp, x(:,1 :end/2)); 23 | [m2,dm2] = feval(mean{:}, hyp, x(:,1+end/2:end )); 24 | m = m1-m2; dm = @(q) dm1(q)-dm2(q); -------------------------------------------------------------------------------- /GP/mean/meanProd.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanProd(mean, hyp, x) 2 | 3 | % meanProd - compose a mean function as the product of other mean functions. 4 | % This function doesn't actually compute very much on its own, it merely does 5 | % some bookkeeping, and calls other mean functions to do the actual work. 6 | % 7 | % m(x) = \prod_i m_i(x) 8 | % 9 | % Copyright (c) by Carl Edward Rasmussen & Hannes Nickisch 2016-04-15. 10 | % 11 | % See also meanFunctions.m. 12 | 13 | nm = numel(mean); 14 | for ii = 1:nm % iterate over mean functions 15 | f = mean(ii); if iscell(f{:}), f = f{:}; end % expand cell array if necessary 16 | j(ii) = cellstr(feval(f{:})); % collect number hypers 17 | end 18 | 19 | if nargin<3 % report number of parameters 20 | m = char(j(1)); for ii=2:nm, m = [m, '+', char(j(ii))]; end; return 21 | end 22 | [n,D] = size(x); 23 | 24 | v = []; % v vector indicates to which mean parameters belong 25 | for ii = 1:nm, v = [v repmat(ii, 1, eval(char(j(ii))))]; end 26 | 27 | m = ones(n,1); mi = cell(nm,1); dmi = cell(nm,1); % allocate space 28 | for ii = 1:nm % iteration over factor functions 29 | f = mean(ii); if iscell(f{:}), f = f{:}; end % expand cell array if needed 30 | [mi{ii},dmi{ii}] = feval(f{:}, hyp(v==ii), x); 31 | m = m.*mi{ii}; % accumulate means 32 | end 33 | dm = @(q) dirder(q,mi,dmi,v,nm); % directional derivative 34 | 35 | function dhyp = dirder(q,mi,dmi,v,nm) 36 | dhyp = zeros(nm,1); 37 | for ii = 1:nm 38 | qi = q; for jj=1:nm, if ii~=jj, qi = qi .* mi{jj}; end, end % accumulate 39 | dhyp(v==ii,1) = dmi{ii}(qi); 40 | end -------------------------------------------------------------------------------- /GP/mean/meanScale.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanScale(mean, hyp, x) 2 | 3 | % meanScale - compose a mean function as a scaled version of another one. 4 | % 5 | % m(x) = a * m_0(x) 6 | % 7 | % The hyperparameters are: 8 | % 9 | % hyp = [ a; 10 | % hyp_m0 ] 11 | % 12 | % This function doesn't actually compute very much on its own, it merely does 13 | % some bookkeeping, and calls other mean functions to do the actual work. 14 | % 15 | % Copyright (c) by Carl Edward Rasmussen & Hannes Nickisch 2016-04-15. 16 | % 17 | % See also meanFunctions.m. 18 | 19 | if nargin<3 % report number of parameters 20 | m = [feval(mean{:}),'+1']; return 21 | end 22 | 23 | a = hyp(1); 24 | [m0,dm0] = feval(mean{:},hyp(2:end),x); % evaluate m0 25 | m = a*m0; % mean 26 | dm = @(q) [m0'*q(:); a*dm0(q)]; % directional derivative 27 | -------------------------------------------------------------------------------- /GP/mean/meanSum.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanSum(mean, hyp, x) 2 | 3 | % meanSum - compose a mean function as the sum of other mean functions. 4 | % This function doesn't actually compute very much on its own, it merely does 5 | % some bookkeeping, and calls other mean functions to do the actual work. 6 | % 7 | % m(x) = \sum_i m_i(x) 8 | % 9 | % Copyright (c) by Carl Edward Rasmussen & Hannes Nickisch 2016-04-15. 10 | % 11 | % See also meanFunctions.m. 12 | 13 | nm = numel(mean); 14 | for ii = 1:nm % iterate over mean functions 15 | f = mean(ii); if iscell(f{:}), f = f{:}; end % expand cell array if necessary 16 | j(ii) = cellstr(feval(f{:})); % collect number hypers 17 | end 18 | 19 | if nargin<3 % report number of parameters 20 | m = char(j(1)); for ii=2:nm, m = [m, '+', char(j(ii))]; end; return 21 | end 22 | [n,D] = size(x); 23 | 24 | v = []; % v vector indicates to which mean parameters belong 25 | for ii = 1:nm, v = [v repmat(ii, 1, eval(char(j(ii))))]; end 26 | 27 | m = zeros(n,1); dmi = cell(nm,1); % allocate space 28 | for ii = 1:nm % iteration over summand functions 29 | f = mean(ii); if iscell(f{:}), f = f{:}; end % expand cell array if needed 30 | [mi,dmi{ii}] = feval(f{:}, hyp(v==ii), x); 31 | m = m+mi; % accumulate means 32 | end 33 | dm = @(q) dirder(q,dmi,v,nm); % directional derivative 34 | 35 | function dhyp = dirder(q,dmi,v,nm) 36 | dhyp = zeros(nm,1); 37 | for ii = 1:nm, dhyp(v==ii,1) = dmi{ii}(q); end -------------------------------------------------------------------------------- /GP/mean/meanWSPC.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanWSPC(d, hyp, x) 2 | 3 | % Weighted Sum of Projected Cosines or Random Kitchen Sink features. 4 | % 5 | % This function represents the feature function of a zero mean GP with 6 | % stationary covariance function. See the paper "Sparse spectrum GP regression" 7 | % by Lazaro-Gredilla et al., JMLR, 2010 for details. 8 | % 9 | % m(x) = sqrt(2/d) sum_j=1..d a_j * cos(w_j'*x + b_j) 10 | % 11 | % The hyperparameter is: 12 | % hyp = [w_1; b_1; a_1; ..; w_d; b_d; a_d] 13 | % 14 | % Copyright (c) by William Herlands and Hannes Nickisch, 2016-04-15. 15 | % 16 | % See also meanFunctions.m. 17 | 18 | if nargin<3, m = sprintf('(D+2)*%d',d); return; end % report number of hypers 19 | [n,D] = size(x); 20 | if any(length(hyp)~=eval(sprintf('(D+2)*%d',d))) 21 | error('Incorrect number of hyperparameters for meanRKS.') 22 | end 23 | hyp = reshape(hyp,D+2,d); 24 | w = hyp(1:D,:); b = hyp(D+1,:); a = hyp(D+2,:)'; % separate hyps into w, b, a 25 | 26 | r = bsxfun(@plus,x*w,b); cr = cos(r); m = sqrt(2/d)*cr*a; % mean 27 | dm = @(q) dirder(q,x,a,r,cr,d); % directional derivative 28 | 29 | function dhyp = dirder(q,x,a,r,cr,d) 30 | msr = -sin(r); dhyp = [x'*(msr.*(q*a')); (q'*msr).*a'; q'*cr]; 31 | dhyp = sqrt(2/d)*dhyp(:); -------------------------------------------------------------------------------- /GP/mean/meanWarp.m: -------------------------------------------------------------------------------- 1 | function [m,dm] = meanWarp(mean, g, dg, hyp, x) 2 | 3 | % meanWarp - compose a mean function by warping another mean function m0. 4 | % 5 | % m(x) = g( m_0(x) ). 6 | % 7 | % The hyperparameter is: 8 | % 9 | % hyp = [ hyp_m0 ] 10 | % 11 | % This function doesn't actually compute very much on its own, it merely does 12 | % some bookkeeping, and calls other mean function to do the actual work. 13 | % 14 | % Copyright (c) by William Herlands & Hannes Nickisch, 2016-04-15. 15 | % 16 | % See also meanFunctions.m. 17 | 18 | if nargin<5, m = feval(mean{:}); return, end % report number of parameters 19 | 20 | [m0,dm0] = feval(mean{:},hyp,x); % evaluate m0 21 | m = g(m0); % mean 22 | dm = @(q) dm0(dg(m0).*q); % directional derivative -------------------------------------------------------------------------------- /GP/prior/priorClamped.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorClamped(x) 2 | % PRIORCLAMPED fix the value of a hyperparameter. 3 | % 4 | % This is a dummy hyperparameter prior distribution. 5 | % The function is not intended to be evaluated but exists merely to make 6 | % the user aware of the possibility to use it. The function is equivalent 7 | % to priorDelta. 8 | % 9 | % For more help on design of priors, try "help priorDistributions". 10 | % 11 | % See also PRIORDISTRIBUTIONS 12 | 13 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-12-06. 14 | 15 | error('The function is not intended to be called directly.') 16 | -------------------------------------------------------------------------------- /GP/prior/priorClampedMulti.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorClampedMulti(x) 2 | 3 | % Dummy hyperparameter prior distribution to fix the value of a hyperparameter. 4 | % The function is not intended to be evaluated but exists merely to make 5 | % the user aware of the possibility to use it. The function is equivalent 6 | % to priorDeltaMulti. 7 | % 8 | % For more help on design of priors, try "help priorDistributions". 9 | % 10 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-12-06. 11 | % 12 | % See also priorDistributions.m. 13 | 14 | error('The function is not intended to be called directly.') -------------------------------------------------------------------------------- /GP/prior/priorDelta.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorDelta(x) 2 | % PRIORDELTA fix the value of a hyperparameter. 3 | % 4 | % This is a summy hyperparameter prior distribution. 5 | % The function is not intended to be evaluated but exists merely to make 6 | % the user aware of the possibility to use it. The function is equivalent 7 | % to priorClamped. 8 | % 9 | % For more help on design of priors, try "help priorDistributions". 10 | % 11 | % See also PRIORDISTRIBUTIONS 12 | 13 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-12-06. 14 | 15 | error('The function is not intended to be called directly.') 16 | -------------------------------------------------------------------------------- /GP/prior/priorDeltaMulti.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorDeltaMulti(x) 2 | 3 | % Dummy hyperparameter prior distribution to fix the value of a hyperparameter. 4 | % The function is not intended to be evaluated but exists merely to make 5 | % the user aware of the possibility to use it. The function is equivalent 6 | % to priorClampedMulti. 7 | % 8 | % For more help on design of priors, try "help priorDistributions". 9 | % 10 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-12-06. 11 | % 12 | % See also priorDistributions.m. 13 | 14 | error('The function is not intended to be called directly.') -------------------------------------------------------------------------------- /GP/prior/priorEqualMulti.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorEqualMulti(x) 2 | 3 | % Dummy hyperparameter prior distribution to have a group of hyperparameter 4 | % share the same value. 5 | % The function is not intended to be evaluated but exists merely to make 6 | % the user aware of the possibility to use it. The function is equivalent 7 | % to priorSameMulti. 8 | % 9 | % For more help on design of priors, try "help priorDistributions". 10 | % 11 | % Copyright (c) by Hannes Nickisch, 2016-10-26. 12 | % 13 | % See also priorDistributions.m. 14 | 15 | error('The function is not intended to be called directly.') -------------------------------------------------------------------------------- /GP/prior/priorGamma.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorGamma(k,t,x) 2 | 3 | % Univariate Gamma hyperparameter prior distribution. 4 | % Compute log-likelihood and its derivative or draw a random sample. 5 | % The prior distribution is parameterized as: 6 | % 7 | % p(x) = exp(x/t)/gamma(k)*x^(k-1)/t^k 8 | % 9 | % where k(1x1) is the shape parameter, t(1x1) is the scale parameter 10 | % and x(1xN) contains query hyperparameters for prior evaluation. 11 | % 12 | % Sampling is done using the algorithm from p. 53 of the paper Generating gamma 13 | % variates by a modified rejection technique by J.H. Ahrens and U. Dieter, 1982, 14 | % ACM, 25, 47–54. 15 | % 16 | % For more help on design of priors, try "help priorDistributions". 17 | % 18 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-09-08. 19 | % 20 | % See also priorDistributions.m. 21 | 22 | if nargin<2, error('k and t parameters need to be provided'), end 23 | if ~(isscalar(k)&&isscalar(t)) 24 | error('k and t parameters need to be scalars'), end 25 | if nargin<3 26 | m = 1; 27 | d = k-floor(k); % fractional part 28 | v0 = exp(1)/(exp(1)+d); 29 | while true 30 | v = rand(3,1); 31 | if v(1)<=v0 32 | r = v(2)^(1/d); s = v(3)*r^(d-1); 33 | else 34 | r = 1-log(v(2)); s = v(3)*exp(-r); 35 | end 36 | if s<=r^(d-1)*exp(-r), break, end 37 | m = m+1; 38 | end 39 | u = rand(floor(k),1); 40 | lp = t*(r-sum(log(u))); 41 | return 42 | end 43 | 44 | lx = log(x); 45 | lp = -gammaln(k) - k*log(t) + (k-1)*lx - x/t; 46 | dlp = (k-1)./x - 1/t; 47 | lp(x<0) = -inf; dlp(x<0) = 0; -------------------------------------------------------------------------------- /GP/prior/priorGauss.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorGauss(mu,s2,x) 2 | 3 | % Univariate Gaussian hyperparameter prior distribution. 4 | % Compute log-likelihood and its derivative or draw a random sample. 5 | % The prior distribution is parameterized as: 6 | % 7 | % p(x) = exp(-(x-mu)^2/(2*s2)) / sqrt(2*pi*s2), where 8 | % 9 | % mu(1x1) is the mean parameter, s2(1x1) is the variance parameter and 10 | % x(1xN) contains query hyperparameters for prior evaluation. 11 | % 12 | % For more help on design of priors, try "help priorDistributions". 13 | % 14 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-09-09. 15 | % 16 | % See also priorDistributions.m. 17 | 18 | if nargin<2, error('mu and s2 parameters need to be provided'), end 19 | if ~(isscalar(mu)&&isscalar(s2)) 20 | error('mu and s2 parameters need to be scalars') 21 | end 22 | if nargin<3, lp = sqrt(s2)*randn+mu; return, end % return a sample 23 | lp = -(x-mu).^2/(2*s2) - log(2*pi*s2)/2; 24 | dlp = -(x-mu)/s2; -------------------------------------------------------------------------------- /GP/prior/priorGaussMulti.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorGaussMulti(mu,s2,x) 2 | 3 | % Multivariate Gaussian hyperparameter prior distribution. 4 | % Compute log-likelihood and its derivative or draw a random sample. 5 | % The prior distribution is parameterized as: 6 | % 7 | % p(x) = exp(-r2/2) / sqrt(det(2*pi*s2)) where r2(x) = (x-mu)'*inv(s2)*(x-mu), 8 | % 9 | % mu(Dx1) is the mean parameter, s2(Dx1) or s2(DxD) is the variance parameter 10 | % and x(DxN) contains query hyperparameters for prior evaluation. 11 | % 12 | % For more help on design of priors, try "help priorDistributions". 13 | % 14 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-09-12. 15 | % 16 | % See also priorDistributions.m, prior/priorGauss.m. 17 | 18 | if nargin<2, error('mu and s2 parameters need to be provided'), end 19 | if ndims(mu)~=2 || size(mu,2)~=1, error('mu needs to be (Dx1)'), end 20 | D = size(mu,1); 21 | s2_ok = ndims(s2)==2 && all(size(s2)==[D,1] | size(s2)==[D,D]); 22 | if ~s2_ok, error('s2 needs to be (DxD) or (Dx1)'), end 23 | if size(s2,2)==D % full multivariate case 24 | s = chol(s2)'; lds = sum(log(diag(s))); % lds = log(det(s2))/2 25 | else % diagonal covariance 26 | s = sqrt(s2); lds = sum(log(s)); 27 | end 28 | if nargin<3 % return a random sample 29 | lp = randn(D,1); % unit sample 30 | if size(s,2)==D, lp = s*lp+mu; else lp = s.*lp+mu; end % affine transformation 31 | return 32 | end 33 | if D==1 && size(x,1)>1 % both mu/s2 scalar => inflate 34 | D = size(x,1); mu = mu*ones(D,1); s = s*ones(D,1); lds = D*lds; 35 | end 36 | if ~(ndims(x)==2 && size(x,1)==D), error('x needs to be (Dxn)'), end 37 | 38 | oN = ones(1,size(x,2)); 39 | if size(s,2)==D 40 | xs = s\(x-mu*oN); dlp = -s'\xs; 41 | else 42 | xs = (x-mu*oN)./(s*oN); dlp = -xs./(s*oN); 43 | end 44 | lp = -sum(xs.^2,1)/2 - D*log(2*pi)/2 - lds; -------------------------------------------------------------------------------- /GP/prior/priorInvGauss.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorInvGauss(mu,lam,x) 2 | % PRIORINVGAUSS Univariate Inverse Gaussian hyperparameter prior distribution. 3 | % 4 | % Random sample from the distribution 5 | % r = PRIORINVGAUSS (mu, lam) 6 | % 7 | % Compute log density at location x and first derivative (w.r.t. x) 8 | % [lp, dlp] = PRIORINVGAUSS (mu, lam, x) 9 | % 10 | % Call priorDistributions to get an explanation of outputs in each mode. 11 | % 12 | % The prior distribution is parameterized as: 13 | % 14 | % p(x) = exp(-lam * (x-mu)^2 / (2*mu^2*x)) / sqrt(2*pi * x^3 / lam) 15 | % 16 | % where mu (1x1) is the mean parameter, lam (1x1) is the scale parameter 17 | % and x (1xN) contains query hyperparameters for prior evaluation. 18 | % 19 | % See also PRIORDISTRIBUTIONS 20 | 21 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-09-08. 22 | 23 | if nargin<2, error('mu and lam parameters need to be provided'), end 24 | if ~(isscalar(mu)&&isscalar(lam)) 25 | error('mu and lam parameters need to be scalars'),end 26 | if nargin<3 % return a sample 27 | n = randn; y = n*n; 28 | r = mu + mu*(mu*y-sqrt(4*mu*lam*y+mu^2*y^2))/(2*lam); 29 | z = rand; 30 | if z<=mu/(mu+r) 31 | lp = r; 32 | else 33 | lp = mu^2/r; 34 | end 35 | return 36 | end 37 | 38 | lp = -lam*(x-mu).^2./(2*mu^2*x) - log(2*pi*x.^3/lam)/2; 39 | q = (x-mu)./x; 40 | dlp = -lam*q.*(2-q)/(2*mu^2) - 3./(2*x); 41 | lp(x<0) = -inf; dlp(x<0) = 0; 42 | -------------------------------------------------------------------------------- /GP/prior/priorLaplace.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorLaplace(mu,s2,x) 2 | 3 | % Univariate Laplacian hyperparameter prior distribution. 4 | % Compute log-likelihood and its derivative or draw a random sample. 5 | % The prior distribution is parameterized as: 6 | % 7 | % p(x) = exp(-abs(x-mu)/b)/(2*b), where b = sqrt(s2/2), 8 | % 9 | % mu(1x1) is the mean parameter, s2(1x1) is the variance parameter and 10 | % x(1xN) contains query hyperparameters for prior evaluation. 11 | % 12 | % For more help on design of priors, try "help priorDistributions". 13 | % 14 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-09-09. 15 | % 16 | % See also priorDistributions.m. 17 | 18 | if nargin<3 % return a sample 19 | u = rand-1/2; lp = mu - sqrt(s2/2)*sign(u)*log(1-2*abs(u)); return 20 | end 21 | 22 | b = sqrt(s2/2); 23 | lp = -abs(x-mu)/b - log(2*b); 24 | dlp = -sign(x-mu)/b; -------------------------------------------------------------------------------- /GP/prior/priorLaplaceMulti.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorLaplaceMulti(mu,s2,x) 2 | 3 | % Multivariate Laplace hyperparameter prior distribution. 4 | % Compute log-likelihood and its derivative or draw a random sample. 5 | % The prior distribution is parameterized as: 6 | % 7 | % p(x) = exp(-sqrt(2)*sqrt(r2))/sqrt(det(2*s2)), 8 | % where r2(x) = (x-mu)'*inv(s2)*(x-mu), 9 | % 10 | % further mu(Dx1) is the mean parameter, s2(Dx1) or s2(DxD) is the variance 11 | % parameter and x(DxN) contains query hyperparameters for prior evaluation. 12 | % 13 | % For more help on design of priors, try "help priorDistributions". 14 | % 15 | % Copyright (c) by Hannes Nickisch, 2014-10-15. 16 | % 17 | % See also priorDistributions.m, prior/priorLaplace.m. 18 | 19 | if nargin<2, error('mu and s2 parameters need to be provided'), end 20 | if ndims(mu)~=2 || size(mu,2)~=1, error('mu needs to be (Dx1)'), end 21 | D = size(mu,1); 22 | s2_ok = ndims(s2)==2 && all(size(s2)==[D,1] | size(s2)==[D,D]); 23 | if ~s2_ok, error('s2 needs to be (DxD) or (Dx1)'), end 24 | if size(s2,2)==D % full multivariate case 25 | s = chol(s2)'; lds = sum(log(diag(s))); % lds = log(det(s2))/2 26 | else % diagonal covariance 27 | s = sqrt(s2); lds = sum(log(s)); 28 | end 29 | if nargin<3 % return a random sample 30 | u = rand(D,1)-1/2; lp = sign(u).*log(1-2*abs(u))/sqrt(2); % unit sample 31 | if size(s,2)==D, lp = s*lp+mu; else lp = s.*lp+mu; end % affine transformation 32 | return 33 | end 34 | if D==1 && size(x,1)>1 % both mu/s2 scalar => inflate 35 | D = size(x,1); mu = mu*ones(D,1); s = s*ones(D,1); lds = D*lds; 36 | end 37 | if ~(ndims(x)==2 && size(x,1)==D), error('x needs to be (Dxn)'), end 38 | 39 | oN = ones(1,size(x,2)); 40 | if size(s,2)==D 41 | xm = x-mu*oN; xs = s\xm; 42 | else 43 | xm = x-mu*oN; xs = xm./(s*oN); 44 | end 45 | 46 | dlp = -sqrt(2)*sign(xs); 47 | lp = -sqrt(2)*sum(abs(xs),1) - D*log(2)/2 - lds; 48 | if size(s,2)==D, dlp = s'\dlp; else dlp = dlp./(s*oN); end -------------------------------------------------------------------------------- /GP/prior/priorLogNormal.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp]=priorLogNormal(mu,s2,x) 2 | 3 | % Univariate Log-normal hyperparameter prior distribution. 4 | % Compute log-likelihood and its derivative or draw a random sample. 5 | % The prior distribution is parameterized as: 6 | % 7 | % p(x) = exp(-(log(x)-mu)^2/(2*s2)) / (sqrt(2*pi*s2)*x) 8 | % 9 | % where mu(1x1) is the log scale parameter, s2(1x1) is the shape parameter 10 | % and x(1xN) contains query hyperparameters for prior evaluation. 11 | % 12 | % For more help on design of priors, try "help priorDistributions". 13 | % 14 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-09-08. 15 | % 16 | % See also priorDistributions.m. 17 | 18 | if nargin<2, error('mu and s2 parameters need to be provided'), end 19 | if ~(isscalar(mu)&&isscalar(s2)) 20 | error('mu and s2 parameters need to be scalars'), end 21 | if nargin<3, lp = exp(sqrt(s2)*randn+mu); return, end % return a sample 22 | 23 | lx = log(x); 24 | lp = -(lx-mu).^2/(2*s2) - log(2*pi*s2)/2 - lx; 25 | dlp = -(lx-mu+s2)./(x*s2); 26 | lp(x<0) = -inf; dlp(x<0) = 0; -------------------------------------------------------------------------------- /GP/prior/priorMix.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorMix(w,priors,x) 2 | 3 | % Mixture of Univariate or Multivariate hyperparameter prior distributions. 4 | % Compute log-likelihood and its derivative or draw a random sample. 5 | % The prior distribution is parameterized as: 6 | % 7 | % p(x) = sum_i w_i p_i(x), 8 | % 9 | % w(bx1) is the mixture weight vector (nonnegative, normalised to add up to 1), 10 | % priors(bx1) is a cell array containing the priors to be mixed and x(1xN) 11 | % contains query hyperparameters for prior evaluation. 12 | % 13 | % For more help on design of priors, try "help priorDistributions". 14 | % 15 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-09-08. 16 | % 17 | % See also priorDistributions.m. 18 | 19 | if nargin<2, error('specification of w and priors required'), end 20 | n = numel(w); w = w(:); 21 | if abs(sum(w)-1)>1e-9, error('w needs to sum up to 1'), end 22 | if any(w<0), error('w needs to be nonnegative'), end 23 | for i=1:n, if ~iscell(priors{i}), priors{i} = {priors{i}}; end, end % sample 24 | if nargin<3, i = 1+nnz(rand>cumsum(w)); lp = feval(priors{i}{:}); return, end 25 | 26 | lpi = zeros(numel(x),n); dlpi = zeros(numel(x),n); 27 | for i=1:n, [lpi(:,i),dlpi(:,i)] = feval(priors{i}{:},x(:)); end 28 | 29 | mx = max(lpi,[],2); lp = log(exp(lpi-mx*ones(1,n))*w) + mx; 30 | pi = exp(lpi); dlp = ((pi.*dlpi)*w)./(pi*w); -------------------------------------------------------------------------------- /GP/prior/priorSameMulti.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorSameMulti(x) 2 | 3 | % Dummy hyperparameter prior distribution to have a group of hyperparameter 4 | % share the same value. 5 | % The function is not intended to be evaluated but exists merely to make 6 | % the user aware of the possibility to use it. The function is equivalent 7 | % to priorEqualMulti. 8 | % 9 | % For more help on design of priors, try "help priorDistributions". 10 | % 11 | % Copyright (c) by Hannes Nickisch, 2016-10-26. 12 | % 13 | % See also priorDistributions.m. 14 | 15 | error('The function is not intended to be called directly.') -------------------------------------------------------------------------------- /GP/prior/priorSmoothBox1.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorSmoothBox1(a,b,eta,x) 2 | 3 | % Univariate smoothed box prior distribution with linear decay in the log domain 4 | % and infinite support over the whole real axis. 5 | % Compute log-likelihood and its derivative or draw a random sample. 6 | % The prior distribution is parameterized as: 7 | % 8 | % p(x) = 1/w*sigmoid(eta*(x-a))*(1-sigmoid(eta*(x-b))), 9 | % where w = abs(b-a) and sigmoid(z) = 1/(1+exp(-z)) 10 | % 11 | % a(1x1) is the lower bound parameter, b(1x1) is the upper bound parameter, 12 | % eta(1x1)>0 is the slope parameter and x(1xN) contains query hyperparameters 13 | % for prior evaluation. Larger values of eta make the distribution more 14 | % box-like. 15 | % 16 | % The distribution p(x) has mean and variance given by: 17 | % mu = (a+b)/2 and s2 = w^2/(1-exp(-2*g))*(1+(pi/g)^2)/12, g = eta*w/2. 18 | % 19 | % /------------\ 20 | % / \ 21 | % -------- | | --------> x 22 | % a b 23 | % 24 | % For more help on design of priors, try "help priorDistributions". 25 | % 26 | % Copyright (c) by Jose Vallet and Hannes Nickisch, 2015-03-27. 27 | % 28 | % See also priorDistributions.m. 29 | 30 | if nargin<3, error('a, b and eta parameters need to be provided'), end 31 | if b<=a, error('b must be greater than a.'), end 32 | if ~(isscalar(a)&&isscalar(b)&&isscalar(eta)) 33 | error('a, b and eta parameters need to be scalar values') 34 | end 35 | 36 | if nargin<4 % inverse sampling 37 | u = exp((b-a)*eta*rand()); 38 | lp = log((u-1)/(exp(-eta*a)-u*exp(-eta*b)))/eta; 39 | return 40 | end 41 | 42 | [lpa,dlpa] = logr(eta*(x-a)); [lpb,dlpb] = logr(-eta*(x-b)); 43 | lp = lpa + lpb - log(b-a) + log(1-exp((a-b)*eta)); 44 | dlp = eta*(dlpa - dlpb); 45 | 46 | % r(z) = 1/(1+exp(-z)), log(r(z)) = -log(1+exp(-z)) 47 | function [lr,dlr] = logr(z) 48 | lr = z; ok = -350 is the slope parameter and x(1xN) contains query hyperparameters 17 | % for prior evaluation. Larger values of eta make the distribution more 18 | % box-like. 19 | % 20 | % The distribution p(x) has mean and variance given by: 21 | % mu = (a+b)/2 and s2 = (w/2)^2*(eta^3/3+eta^2+4*eta/pi+2/pi)/(eta^3+eta^2). 22 | % 23 | % /------------\ 24 | % / \ 25 | % -------- | | --------> x 26 | % a b 27 | % 28 | % For more help on design of priors, try "help priorDistributions". 29 | % 30 | % Copyright (c) by Jose Vallet and Hannes Nickisch, 2015-03-27. 31 | % 32 | % See also priorDistributions.m. 33 | 34 | if nargin<3, error('a, b and eta parameters need to be provided'), end 35 | if b<=a, error('b must be greater than a.'), end 36 | if ~(isscalar(a)&&isscalar(b)&&isscalar(eta)) 37 | error('a, b and eta parameters need to be scalar values') 38 | end 39 | 40 | w = abs(b-a); sab = w/(eta*sqrt(2*pi)); % box width and boundary slope 41 | 42 | if nargin<4 % return a sample 43 | if randb; 57 | lp(i) = lp(i) - (x(i)-b).^2/(2*sab^2); 58 | dlp(i) = (b-x(i))/sab^2; -------------------------------------------------------------------------------- /GP/prior/priorT.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorT(mu,s2,nu,x) 2 | 3 | % Univariate Student's t hyperparameter prior distribution. 4 | % Compute log-likelihood and its derivative or draw a random sample. 5 | % The prior distribution is parameterized as: 6 | % 7 | % p(x) = ( 1 + (x-mu)^2/(s2*(nu-2)) )^(-(nu+1)/2) / Z, where 8 | % Z = gamma(nu/2) * sqrt(pi*(nu-2)*s2) / gamma((nu+1)/2), 9 | % 10 | % mu(1x1) is the mean parameter, s2(1x1) is the variance parameter, 11 | % nu(1x1) > 2 is the degrees of freedom parameter and x(1xN) contains query 12 | % hyperparameters for prior evaluation. 13 | % 14 | % For more help on design of priors, try "help priorDistributions". 15 | % 16 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-10-16. 17 | % 18 | % See also priorDistributions.m. 19 | 20 | if nargin<3, error('mu, s2 and nu parameters need to be provided'), end 21 | if ~(isscalar(mu)&&isscalar(s2)&&isscalar(nu)) 22 | error('mu, s2 and nu parameters need to be scalars') 23 | end 24 | if nargin<4, lp = mu + sqrt(s2*(nu-2)/priorGamma(nu/2,2))*randn; return, end 25 | 26 | lZ = gammaln((nu+1)/2)-gammaln(nu/2)-log(pi*(nu-2)*s2)/2; 27 | lp = -(nu+1)/2*log(1+(x-mu).^2/(s2*(nu-2))) + lZ; 28 | dlp = (nu+1)*(mu-x)./(mu^2-2*mu*x+s2*(nu-2)+x.^2); -------------------------------------------------------------------------------- /GP/prior/priorTMulti.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorTMulti(mu,s2,nu,x) 2 | 3 | % Multivariate Laplace hyperparameter prior distribution. 4 | % Compute log-likelihood and its derivative or draw a random sample. 5 | % The prior distribution is parameterized as: 6 | % 7 | % p(x) = ( 1 + r2/(nu-2) )^(-(nu+D)/2) / Z, where 8 | % Z = gamma(nu/2) * sqrt(det(pi*(nu-2)*s2)) / gamma((nu+D)/2), and 9 | % r2(x) = (x-mu)'*inv(s2)*(x-mu), 10 | % 11 | % further mu(Dx1) is the mean parameter, s2(Dx1) or s2(DxD) is the variance 12 | % parameter, nu(1x1) > 2 is the degrees of freedom parameter and x(DxN) contains 13 | % query hyperparameters for prior evaluation. 14 | % 15 | % For more help on design of priors, try "help priorDistributions". 16 | % 17 | % Copyright (c) by Hannes Nickisch, 2014-10-16. 18 | % 19 | % See also priorDistributions.m, prior/priorLaplace.m. 20 | 21 | if nargin<3, error('mu, s2 and nu parameters need to be provided'), end 22 | if ndims(mu)~=2 || size(mu,2)~=1, error('mu needs to be (Dx1)'), end 23 | D = size(mu,1); 24 | s2_ok = ndims(s2)==2 && all(size(s2)==[D,1] | size(s2)==[D,D]); 25 | if ~s2_ok, error('s2 needs to be (DxD) or (Dx1)'), end 26 | if size(s2,2)==D % full multivariate case 27 | s = chol(s2)'; lds = sum(log(diag(s))); % lds = log(det(s2))/2 28 | else % diagonal covariance 29 | s = sqrt(s2); lds = sum(log(s)); 30 | end 31 | if ~isscalar(nu), error('nu parameter needs to be a scalar'), end 32 | 33 | if nargin<4 % return a random sample 34 | lp = randn(D,1); 35 | for d=1:D, lp(d) = sqrt((nu-2)/priorGamma(nu/2,2)) * lp(d); end % unit sample 36 | if size(s,2)==D, lp = s*lp+mu; else lp = s.*lp+mu; end % affine transformation 37 | return 38 | end 39 | if D==1 && size(x,1)>1 % both mu/s2 scalar => inflate 40 | D = size(x,1); mu = mu*ones(D,1); s = s*ones(D,1); lds = D*lds; 41 | end 42 | if ~(ndims(x)==2 && size(x,1)==D), error('x needs to be (Dxn)'), end 43 | 44 | oN = ones(1,size(x,2)); oD = ones(D,1); 45 | if size(s,2)==D 46 | xm = x-mu*oN; xs = s\xm; 47 | else 48 | xm = x-mu*oN; xs = xm./(s*oN); 49 | end 50 | 51 | lZ = gammaln((nu+D)/2) - gammaln(nu/2) - D*log(pi*(nu-2))/2 - lds; 52 | r2 = sum(xs.^2,1); 53 | lp = -(nu+D)/2*log(1+r2/(nu-2)) + lZ; 54 | dlp = -(nu+D)/(nu-2)*xs./(oD*(1+r2/(nu-2))); 55 | 56 | if size(s,2)==D, dlp = s'\dlp; else dlp = dlp./(s*oN); end -------------------------------------------------------------------------------- /GP/prior/priorTransform.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorTransform(g,dg,ig,prior,x) 2 | 3 | % Transformed univariate hyperparameter prior distribution. 4 | % Compute log-likelihood and its derivative or draw a random sample. 5 | % The prior distribution is parameterized as: 6 | % 7 | % prior(y), where y = g(x), 8 | % 9 | % g is the transformation function (monotonically increasing and invertable), 10 | % dg is its derivative, ig is its inverse, prior is the distribution to be 11 | % transformed and x(1xN) contains query hyperparameters for prior evaluation. 12 | % Note that p(x) = prior(g(x)) is not necessarily normalised w.r.t. x. 13 | % 14 | % For example, to put a nonnegative gamma prior on the lenthscale 15 | % parameter ell of covSEiso, one can use 16 | % {@priorTransform,@exp,@exp,@log,{@priorGamma,k,t}} 17 | % since ell is represented in the log domain. 18 | % 19 | % For more help on design of priors, try "help priorDistributions". 20 | % 21 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-09-08. 22 | % 23 | % See also priorDistributions.m. 24 | 25 | if nargin<4, error('g, dg, ig and prior parameters need to be provided'), end 26 | if nargin<5, lp = ig(feval(prior{:})); return, end % apply inverse sampling 27 | 28 | if ~iscell(prior), prior = {prior}; end 29 | [lp,dlp] = feval(prior{:},g(x)); dlp = dlp.*dg(x); -------------------------------------------------------------------------------- /GP/prior/priorWeibull.m: -------------------------------------------------------------------------------- 1 | function [lp,dlp] = priorWeibull(lam,k,x) 2 | 3 | % Univariate Weibull hyperparameter prior distribution. 4 | % Compute log-likelihood and its derivative or draw a random sample. 5 | % The prior distribution is parameterized as: 6 | % 7 | % p(x) = (k/lam) * (x/lam)^(k-1) * exp(-(x/lam)^k) 8 | % 9 | % where lam(1x1) is the scale parameter, k(1x1) is the scale parameter 10 | % and x(1xN) contains query hyperparameters for prior evaluation. 11 | % 12 | % For more help on design of priors, try "help priorDistributions". 13 | % 14 | % Copyright (c) by Roman Garnett and Hannes Nickisch, 2014-09-08. 15 | % 16 | % See also priorDistributions.m. 17 | 18 | if nargin<2, error('lam and k parameters need to be provided'), end 19 | if ~(isscalar(lam)&&isscalar(k)) 20 | error('lam and k parameters need to be scalars'), end 21 | if nargin<3,lp = lam*(-log(rand))^(1/k); return, end % return a sample 22 | 23 | lp = log(k/lam) + (k-1)*log(x/lam) - (x/lam).^k; 24 | dlp = (k-1)./x - (k/lam)*(x/lam).^(k-1); 25 | lp(x<0) = -inf; dlp(x<0) = 0; -------------------------------------------------------------------------------- /GP/priorDistributions.m: -------------------------------------------------------------------------------- 1 | function priorDistributions () 2 | % prior distributions to be used for hyperparameters of Gaussian processes 3 | % using infPrior. 4 | % There are two different kinds of prior distributions: simple and composite: 5 | % 6 | % simple prior distributions: 7 | % 8 | % priorGauss - univariate Gaussian 9 | % priorLaplace - univariate Laplace 10 | % priorT - univariate Student's t 11 | % 12 | % priorSmoothBox1 - univariate interval (linear decay in log domain) 13 | % priorSmoothBox2 - univariate interval (quadr. decay in log domain) 14 | % 15 | % priorGamma - univariate Gamma, IR+ 16 | % priorWeibull - univariate Weibull, IR+ 17 | % priorInvGauss - univariate Inverse Gaussian, IR+ 18 | % priorLogNormal - univariate Log-normal, IR+ 19 | % 20 | % priorClamped or - fix hyperparameter to its current value by setting 21 | % priorDelta derivatives to zero, no effect on marginal likelihood 22 | % 23 | % priorGaussMulti - multivariate Gauss 24 | % priorLaplaceMulti - multivariate Laplace 25 | % priorTMulti - multivariate Student's t 26 | % 27 | % priorClampedMulti or - fix hyperparameter to its current value by setting 28 | % priorDeltaMulti derivatives to zero, no effect on marginal likelihood 29 | % 30 | % priorEqualMulti or - make several hyperparameters have the same value by 31 | % priorSameMulti same derivative, no effect on marginal likelihood 32 | % 33 | % composite prior distributions (see explanation at the bottom): 34 | % 35 | % priorMix - nonnegative mixture of priors 36 | % priorTransform - prior on g(t) rather than t 37 | % 38 | % Naming convention: all prior distributions are named "prior/prior*.m". 39 | % 40 | % 41 | % 1) With only a fixed input arguments: 42 | % 43 | % r = priorNAME(par1,par2,parN) 44 | % 45 | % The function returns a random sample from the distribution for e.g. 46 | % random restarts, simulations or optimisation initialisation. 47 | % 48 | % 2) With one additional input arguments: 49 | % 50 | % [lp,dlp] = priorNAME(par1,par2,parN, t) 51 | % 52 | % The function returns the log density at location t along with its first 53 | % derivative. 54 | % 55 | % See also USAGEPRIOR, INFPRIOR 56 | 57 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2019-03-24. 58 | 59 | help priorDistributions 60 | -------------------------------------------------------------------------------- /GP/startup.m: -------------------------------------------------------------------------------- 1 | % startup script to make Octave/Matlab aware of the GPML package 2 | % 3 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch 2018-08-01. 4 | 5 | disp ('executing gpml startup script...') 6 | mydir = fileparts (mfilename ('fullpath')); % where am I located 7 | addpath (mydir); 8 | 9 | % core folders 10 | dirs = {'cov','doc','inf','lik','mean','prior','util'}; 11 | for d = dirs 12 | addpath (fullfile (mydir, d{1})) 13 | end 14 | 15 | % minfunc folders 16 | dirs = {{'util','minfunc'},{'util','minfunc','compiled'}}; 17 | for d = dirs 18 | addpath (fullfile (mydir, d{1}{:})) 19 | end 20 | 21 | addpath([mydir,'/util/sparseinv']) 22 | -------------------------------------------------------------------------------- /GP/util/any2vec.m: -------------------------------------------------------------------------------- 1 | % Extract the numerical values from "s" into the column vector "v". The 2 | % variable "s" can be of any type, including struct and cell array. 3 | % Non-numerical elements are ignored. See also the reverse vec2any.m. 4 | 5 | function v = any2vec(s) 6 | v = []; 7 | if isnumeric(s) 8 | v = s(:); % numeric values are recast to column vector 9 | elseif isstruct(s) 10 | v = any2vec(struct2cell(orderfields(s)));% alphabetize, conv to cell, recurse 11 | elseif iscell(s) 12 | for i = 1:numel(s) % cell array elements are handled sequentially 13 | v = [v; any2vec(s{i})]; 14 | end 15 | end % other types are ignored 16 | -------------------------------------------------------------------------------- /GP/util/covFITC.m: -------------------------------------------------------------------------------- 1 | % Wrapper to apxSparse to remain backwards compatible. 2 | % 3 | % Note that covFITC is not a valid covariance function on its own right. 4 | % 5 | % Copyright (c) by Ed Snelson, Carl Edward Rasmussen 6 | % and Hannes Nickisch, 2016-08-25. 7 | 8 | function varargout = covFITC(varargin) 9 | varargout = cell(nargout, 1); [varargout{:}] = apxSparse(varargin{:}); -------------------------------------------------------------------------------- /GP/util/covGrid.m: -------------------------------------------------------------------------------- 1 | % Wrapper to apxGrid to remain backwards compatible. 2 | % 3 | % Note that covGrid is not a valid covariance function on its own right. 4 | % 5 | % Copyright (c) by Hannes Nickisch and Andrew Wilson 2016-08-25. 6 | 7 | function varargout = covGrid(varargin) 8 | varargout = cell(nargout, 1); [varargout{:}] = apxGrid(varargin{:}); 9 | -------------------------------------------------------------------------------- /GP/util/digamma.m: -------------------------------------------------------------------------------- 1 | function y = digamma(x) 2 | %DIGAMMA Digamma function. 3 | % DIGAMMA(X) returns digamma(x) = d log(gamma(x)) / dx 4 | % If X is a matrix, returns the digamma function evaluated at each element. 5 | 6 | % Reference: 7 | % 8 | % J Bernardo, 9 | % Psi ( Digamma ) Function, 10 | % Algorithm AS 103, 11 | % Applied Statistics, 12 | % Volume 25, Number 3, pages 315-317, 1976. 13 | % 14 | % From http://www.psc.edu/~burkardt/src/dirichlet/dirichlet.f 15 | % Got from https://gist.github.com/renaud/98ee62bb9748904a3432. 16 | 17 | large = 9.5; 18 | d1 = -0.5772156649015328606065121; % digamma(1) 19 | d2 = pi^2/6; 20 | small = 1e-6; 21 | s3 = 1/12; 22 | s4 = 1/120; 23 | s5 = 1/252; 24 | s6 = 1/240; 25 | s7 = 1/132; 26 | s8 = 691/32760; 27 | s9 = 1/12; 28 | s10 = 3617/8160; 29 | 30 | % Initialize 31 | y = zeros(size(x)); 32 | 33 | % illegal arguments 34 | i = find(x == -Inf | isnan(x)); 35 | if ~isempty(i) 36 | x(i) = NaN; 37 | y(i) = NaN; 38 | end 39 | 40 | % Negative values 41 | i = find(x < 0); 42 | if ~isempty(i) 43 | % Use the reflection formula (Jeffrey 11.1.6): 44 | % digamma(-x) = digamma(x+1) + pi*cot(pi*x) 45 | y(i) = digamma(-x(i)+1) + pi*cot(-pi*x(i)); 46 | % This is related to the identity 47 | % digamma(-x) = digamma(x+1) - digamma(z) + digamma(1-z) 48 | % where z is the fractional part of x 49 | % For example: 50 | % digamma(-3.1) = 1/3.1 + 1/2.1 + 1/1.1 + 1/0.1 + digamma(1-0.1) 51 | % = digamma(4.1) - digamma(0.1) + digamma(1-0.1) 52 | % Then we use 53 | % digamma(1-z) - digamma(z) = pi*cot(pi*z) 54 | end 55 | 56 | i = find(x == 0); 57 | if ~isempty(i) 58 | y(i) = -Inf; 59 | end 60 | 61 | % Use approximation if argument <= small. 62 | i = find(x > 0 & x <= small); 63 | if ~isempty(i) 64 | y(i) = y(i) + d1 - 1 ./ x(i) + d2*x(i); 65 | end 66 | 67 | % Reduce to digamma(X + N) where (X + N) >= large. 68 | while(1) 69 | i = find(x > small & x < large); 70 | if isempty(i) 71 | break 72 | end 73 | y(i) = y(i) - 1 ./ x(i); 74 | x(i) = x(i) + 1; 75 | end 76 | 77 | % Use de Moivre's expansion if argument >= large. 78 | % In maple: asympt(Psi(x), x); 79 | i = find(x >= large); 80 | if ~isempty(i) 81 | r = 1 ./ x(i); 82 | y(i) = y(i) + log(x(i)) - 0.5 * r; 83 | r = r .* r; 84 | y(i) = y(i) - r .* ( s3 - r .* ( s4 - r .* (s5 - r .* (s6 - r .* s7)))); 85 | end 86 | -------------------------------------------------------------------------------- /GP/util/elsympol.m: -------------------------------------------------------------------------------- 1 | % Evaluate the order R elementary symmetric polynomials using Newton's identity, 2 | % the Newton-Girard formulae: http://en.wikipedia.org/wiki/Newton's_identities 3 | % 4 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2010-01-10. 5 | % speedup contributed by Truong X. Nghiem, 2016-01-20. 6 | 7 | function E = elsympol(Z,R) 8 | sz = size(Z); % evaluate 'power sums' of the individual terms in Z 9 | E = zeros([sz(1:2),R+1]); % E(:,:,r+1) yields polynomial r 10 | % fast and efficient version of: for r=1:R, P(:,:,r) = sum(Z.^r,3); end 11 | Zr = Z; P(:,:,1) = sum(Zr,3); for r=2:R, Zr = Zr.*Z; P(:,:,r) = sum(Zr,3); end 12 | E(:,:,1) = ones(sz(1:2)); if R==0, return, end % init recursion 13 | E(:,:,2) = P(:,:,1); if R==1, return, end % init recursion 14 | for r=2:R 15 | E(:,:,r+1) = P(:,:,1).*E(:,:,r)/r; % i=1 is simpler than the rest 16 | for i=2:r 17 | E(:,:,r+1) = E(:,:,r+1) + P(:,:,i).*E(:,:,r+1-i)*(-1)^(i-1)/r; 18 | end 19 | end -------------------------------------------------------------------------------- /GP/util/gauher.m: -------------------------------------------------------------------------------- 1 | % compute abscissas and weight factors for Gaussian-Hermite quadrature 2 | % 3 | % CALL: [x,w] = gauher(N) 4 | % 5 | % x = base points (abscissas) 6 | % w = weight factors 7 | % N = number of base points (abscissas) (integrates an up to (2N-1)th order 8 | % polynomial exactly) 9 | % 10 | % p(x)=exp(-x^2/2)/sqrt(2*pi), a =-Inf, b = Inf 11 | % 12 | % The Gaussian Quadrature integrates a (2n-1)th order 13 | % polynomial exactly and the integral is of the form 14 | % b N 15 | % Int ( p(x)* F(x) ) dx = Sum ( w_j* F( x_j ) ) 16 | % a j=1 17 | % 18 | % this procedure uses the coefficients a(j), b(j) of the 19 | % recurrence relation 20 | % 21 | % b p (x) = (x - a ) p (x) - b p (x) 22 | % j j j j-1 j-1 j-2 23 | % 24 | % for the various classical (normalized) orthogonal polynomials, 25 | % and the zero-th moment 26 | % 27 | % 1 = integral w(x) dx 28 | % 29 | % of the given polynomial's weight function w(x). Since the 30 | % polynomials are orthonormalized, the tridiagonal matrix is 31 | % guaranteed to be symmetric. 32 | 33 | function [x,w]=gauher(N) 34 | if N==20 % return precalculated values 35 | x=[ -7.619048541679757;-6.510590157013656;-5.578738805893203; 36 | -4.734581334046057;-3.943967350657318;-3.18901481655339 ; 37 | -2.458663611172367;-1.745247320814127;-1.042945348802751; 38 | -0.346964157081356; 0.346964157081356; 1.042945348802751; 39 | 1.745247320814127; 2.458663611172367; 3.18901481655339 ; 40 | 3.943967350657316; 4.734581334046057; 5.578738805893202; 41 | 6.510590157013653; 7.619048541679757]; 42 | w=[ 0.000000000000126; 0.000000000248206; 0.000000061274903; 43 | 0.00000440212109 ; 0.000128826279962; 0.00183010313108 ; 44 | 0.013997837447101; 0.061506372063977; 0.161739333984 ; 45 | 0.260793063449555; 0.260793063449555; 0.161739333984 ; 46 | 0.061506372063977; 0.013997837447101; 0.00183010313108 ; 47 | 0.000128826279962; 0.00000440212109 ; 0.000000061274903; 48 | 0.000000000248206; 0.000000000000126 ]; 49 | else 50 | b = sqrt( (1:N-1)/2 )'; 51 | [V,D] = eig( diag(b,1) + diag(b,-1) ); 52 | w = V(1,:)'.^2; 53 | x = sqrt(2)*diag(D); 54 | end 55 | -------------------------------------------------------------------------------- /GP/util/glm_invlink_exp.m: -------------------------------------------------------------------------------- 1 | % Compute the log intensity for the inverse link function g(f) = exp(f). 2 | % Output range: 0 <= g(f). 3 | % 4 | % The function can be used in GLM likelihoods such as likPoisson, likGamma, and 5 | % likInvGauss. 6 | % 7 | % Copyright (c) by Hannes Nickisch, 2016-10-04. 8 | 9 | function [lg,dlg,d2lg,d3lg] = glm_invlink_exp(f) 10 | lg = f; 11 | if nargout>1 12 | dlg = ones(size(f)); 13 | if nargout>2 14 | d2lg = zeros(size(f)); 15 | if nargout>2 16 | d3lg = zeros(size(f)); 17 | end 18 | end 19 | end -------------------------------------------------------------------------------- /GP/util/glm_invlink_expexp.m: -------------------------------------------------------------------------------- 1 | % Compute the log intensity for the inverse link function g(f) = exp(-exp(-f)). 2 | % Output range: 0 <= g(f) <= 1. 3 | % 4 | % The function can be used in GLM likelihoods such as likBeta. 5 | % 6 | % Copyright (c) by Hannes Nickisch, 2016-10-04. 7 | 8 | function [lg,dlg,d2lg,d3lg] = glm_invlink_expexp(f) 9 | lg = -exp(-f); 10 | if nargout>1 11 | dlg = -lg; 12 | if nargout>2 13 | d2lg = lg; 14 | if nargout>2 15 | d3lg = -lg; 16 | end 17 | end 18 | end -------------------------------------------------------------------------------- /GP/util/glm_invlink_logistic.m: -------------------------------------------------------------------------------- 1 | % Compute the log intensity for the inverse link function g(f) = log(1+exp(f))). 2 | % Output range: 0 <= g(f). 3 | % 4 | % The function can be used in GLM likelihoods such as likPoisson, likGamma, and 5 | % likInvGauss. 6 | % 7 | % Copyright (c) by Hannes Nickisch, 2016-10-04. 8 | 9 | function [lg,dlg,d2lg,d3lg] = glm_invlink_logistic(f) 10 | l1pef = max(0,f) + log(1+exp(-abs(f))); % safely compute log(1+exp(f)) 11 | lg = log(l1pef); id = f<-15; lg(id) = f(id); % fix log(log(1+exp(f))) limits 12 | if nargout>1 13 | sm = 1./(1+exp(-f)); 14 | dlg = sm./l1pef; dlg(f<-15) = 1; 15 | if nargout>2 16 | sp = 1./(1+exp(f)); 17 | d2lg = dlg.*(sp-dlg); 18 | if nargout>2 19 | d3lg = d2lg.*(sp-2*dlg) - dlg.*sp.*sm; 20 | end 21 | end 22 | end -------------------------------------------------------------------------------- /GP/util/glm_invlink_logistic2.m: -------------------------------------------------------------------------------- 1 | % Compute the log intensity for the inverse link function (twice logistic) 2 | % g(f) = h(f*(1+a*h(f))), where is the logistic h(f) = log(1+exp(f))). 3 | % Output range: 0 <= g(f). 4 | % 5 | % The function can be used in GLM likelihoods such as likPoisson, likGamma, and 6 | % likInvGauss. 7 | % 8 | % See Seeger et al., Bayesian Intermittent Demand Forecasting for Large 9 | % Inventories, NIPS, 2016. 10 | % 11 | % Copyright (c) by Hannes Nickisch, 2016-10-04. 12 | 13 | function [lg,dlg,d2lg,d3lg] = glm_invlink_logistic2(a,f) 14 | [lh,dlh,d2lh,d3lh] = glm_invlink_logistic(f); h = exp(lh); 15 | ft = f + a*f.*h; 16 | dft = 1 + a*h.*(1 + f.*dlh); w = a*h.*(dlh.^2 + d2lh); 17 | d2ft = 2*a*h.*dlh + f.*w; 18 | d3ft = 3*w + a*f.*h.*(dlh.^3+3*d2lh.*dlh+d3lh); 19 | [lgt,dlgt,d2lgt,d3lgt] = glm_invlink_logistic(ft); 20 | lg = lgt; dlg = dlgt.*dft; 21 | d2lg = d2lgt.*dft.^2 + dlgt.*d2ft; 22 | d3lg = d3lgt.*dft.^3 + 3*d2lgt.*dft.*d2ft + dlgt.*d3ft; -------------------------------------------------------------------------------- /GP/util/glm_invlink_logit.m: -------------------------------------------------------------------------------- 1 | % Compute the log intensity for the inverse link function g(f) = 1/(1+exp(-f)). 2 | % Output range: 0 <= g(f) <= 1. 3 | % 4 | % The function can be used in GLM likelihoods such as likBeta. 5 | % 6 | % Copyright (c) by Hannes Nickisch, 2016-10-04. 7 | 8 | function varargout = glm_invlink_logit(f) 9 | varargout = cell(nargout, 1); % allocate the right number of output arguments 10 | [varargout{:}] = glm_invlink_logistic(f); 11 | if nargout>0 12 | elg = exp(varargout{1}); 13 | varargout{1} = min(f-elg,0); % upper bound g by 1 14 | if nargout>1 15 | dlg = varargout{2}; 16 | varargout{2} = 1 - elg.*dlg; 17 | if nargout>2 18 | d2lg = varargout{3}; 19 | varargout{3} = -elg.*(dlg.^2+d2lg); 20 | if nargout>3 21 | d3lg = varargout{4}; 22 | varargout{4} = -elg.*(dlg.^3+3*d2lg.*dlg+d3lg); 23 | end 24 | end 25 | end 26 | end -------------------------------------------------------------------------------- /GP/util/glm_invlink_square.m: -------------------------------------------------------------------------------- 1 | % Compute the log intensity for the inverse link function g(f) = f^2+a. 2 | % Output range: 0 <= g(f). 3 | % 4 | % The function can be used in GLM likelihoods such as likPoisson, likGamma, and 5 | % likInvGauss. 6 | % 7 | % Copyright (c) by Hannes Nickisch, 2021-02-16. 8 | 9 | function [lg,dlg,d2lg,d3lg] = glm_invlink_square(a,f) 10 | f2a = f.*f+a; 11 | lg = log(f2a); 12 | if nargout>1 13 | dlg = 2*f./f2a; 14 | if nargout>2 15 | d2lg = 2*(a-f.*f) ./ f2a.^2; 16 | if nargout>2 17 | d3lg = 4*f.*(f.*f-3*a) ./ f2a.^3; 18 | end 19 | end 20 | end -------------------------------------------------------------------------------- /GP/util/infExact.m: -------------------------------------------------------------------------------- 1 | % Wrapper to infGaussLik to remain backwards compatible. 2 | % 3 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch 2016-08-25. 4 | 5 | function varargout = infExact(varargin) 6 | varargout = cell(nargout, 1); [varargout{:}] = infGaussLik(varargin{:}); -------------------------------------------------------------------------------- /GP/util/infFITC.m: -------------------------------------------------------------------------------- 1 | % Wrapper to infGaussLik to remain backwards compatible. 2 | % 3 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch 2016-08-25. 4 | 5 | function varargout = infFITC(varargin) 6 | varargout = cell(nargout, 1); [varargout{:}] = infGaussLik(varargin{:}); -------------------------------------------------------------------------------- /GP/util/infFITC_EP.m: -------------------------------------------------------------------------------- 1 | % Wrapper to infEP to remain backwards compatible. 2 | % 3 | % FITC-EP approximation to the posterior Gaussian process. The function is 4 | % equivalent to infEP with the covariance function: 5 | % Kt = Q + G; G = diag(g); g = diag(K-Q); Q = Ku'*inv(Kuu + snu2*eye(nu))*Ku; 6 | % where Ku and Kuu are covariances w.r.t. to inducing inputs xu and 7 | % snu2 = mean(diag(Kuu))/1e6 is the noise of the inducing inputs which we 8 | % fixe to 0.1% of signal standard deviation. 9 | % For details, see The Generalized FITC Approximation, Andrew Naish-Guzman and 10 | % Sean Holden, NIPS, 2007. 11 | % 12 | % See also infMethods.m, APXSPARSE.M, APX.M. 13 | % 14 | % Copyright (c) by Hannes Nickisch, 2016-10-13. 15 | 16 | function varargout = infFITC_EP(varargin) 17 | varargout = cell(nargout, 1); [varargout{:}] = infEP(varargin{:}); 18 | -------------------------------------------------------------------------------- /GP/util/infFITC_Laplace.m: -------------------------------------------------------------------------------- 1 | % Wrapper to infLaplace to remain backwards compatible. 2 | % 3 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch 2016-10-13. 4 | 5 | function varargout = infFITC_Laplace(varargin) 6 | varargout = cell(nargout, 1); [varargout{:}] = infLaplace(varargin{:}); -------------------------------------------------------------------------------- /GP/util/lbfgsb/arrayofmatrices.h: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_ARRAYOFMATRICES 2 | #define INCLUDE_ARRAYOFMATRICES 3 | 4 | #include "array.h" 5 | #include "matlabmatrix.h" 6 | #include "mex.h" 7 | 8 | // Class ArrayOfMatrices. 9 | // ----------------------------------------------------------------- 10 | class ArrayOfMatrices : public Array { 11 | public: 12 | 13 | // This version of the constructor behaves just like its parent. 14 | explicit ArrayOfMatrices (int length) 15 | : Array(length) { }; 16 | 17 | // This constructor creates an array of matrices from a Matlab 18 | // array. It accepts either a matrix in double precision, or a cell 19 | // array with entries that are matrices. 20 | explicit ArrayOfMatrices (const mxArray* ptr); 21 | 22 | // This constructor creates an array of matrices from a collection 23 | // of Matlab arrays. The Matlab arrays must be matrices. 24 | ArrayOfMatrices (const mxArray* ptrs[], int numptrs); 25 | 26 | // This constructor creates an array of matrices and the 27 | // associated Matlab structures. The Matlab structures are 28 | // matrices. The second input argument acts as a template for the 29 | // creation of the matrices, but the data from "model" is not 30 | // actually copied into the new ArrayOfMatrices object. It is up 31 | // to the user to make sure that the array of mxArray pointers has 32 | // enough room for the pointers to the Matlab arrays. 33 | ArrayOfMatrices (mxArray* ptrs[], const ArrayOfMatrices& model); 34 | 35 | // This constructor creates an array of matrices using the second 36 | // input argument as a model. The input argument "data" contains 37 | // the element data. Note that the information is NOT copied from 38 | // the model! 39 | ArrayOfMatrices (double* data, const ArrayOfMatrices& model); 40 | 41 | // The copy constructor makes a shallow copy of the source object. 42 | ArrayOfMatrices (const ArrayOfMatrices& source); 43 | 44 | // The destructor. 45 | ~ArrayOfMatrices(); 46 | 47 | // Copy assignment operator that observes the same behaviour as 48 | // the Array copy assignment operator. 49 | ArrayOfMatrices& operator= (const ArrayOfMatrices& source); 50 | 51 | // Return the total number of elements in all the matrices. 52 | int numelems() const; 53 | 54 | protected: 55 | static int getnummatlabmatrices (const mxArray* ptr); 56 | }; 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /GP/util/lbfgsb/matlabexception.cpp: -------------------------------------------------------------------------------- 1 | #include "matlabexception.h" 2 | 3 | // Function definitions for class MatlabException. 4 | // ----------------------------------------------------------------- 5 | MatlabException::MatlabException (const char* message) throw() 6 | : exception() { 7 | this->message = message; 8 | } 9 | 10 | MatlabException::MatlabException (const MatlabException& source) throw() 11 | : exception() { 12 | message = source.message; 13 | } 14 | 15 | MatlabException& MatlabException::operator= (const MatlabException& source) 16 | { 17 | message = source.message; 18 | return *this; 19 | } 20 | -------------------------------------------------------------------------------- /GP/util/lbfgsb/matlabexception.h: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_MATLABEXCEPTION 2 | #define INCLUDE_MATLABEXCEPTION 3 | 4 | #include 5 | 6 | // Class MatlabException 7 | // ----------------------------------------------------------------- 8 | // This class just makes it easier for me to throw exceptions. Its 9 | // functionality really has nothing to do with MATLAB. 10 | class MatlabException : public std::exception { 11 | public: 12 | MatlabException (const char* message) throw(); 13 | ~MatlabException() throw() { }; 14 | 15 | // The copy constructor makes a shallow copy. 16 | MatlabException (const MatlabException& source) throw(); 17 | 18 | // The copy assignment operator makes a shallow copy as well. 19 | MatlabException& operator= (const MatlabException& source); 20 | 21 | // Return the message string. 22 | virtual const char* what () const throw() { return message; }; 23 | 24 | private: 25 | const char* message; // The error message. 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /GP/util/lbfgsb/matlabmatrix.cpp: -------------------------------------------------------------------------------- 1 | #include "matlabmatrix.h" 2 | #include "matlabexception.h" 3 | 4 | // Function definitions. 5 | // ----------------------------------------------------------------- 6 | double* getMatlabMatrixDouble (const mxArray* ptr) { 7 | if (mxGetNumberOfDimensions(ptr) != 2) 8 | throw MatlabException("Matlab array must be a matrix"); 9 | if (!mxIsDouble(ptr)) 10 | throw MatlabException("Matlab array must be of type double"); 11 | return mxGetPr(ptr); 12 | } 13 | 14 | double* createMatlabMatrixDouble (mxArray*& ptr, int height, int width) { 15 | ptr = mxCreateDoubleMatrix(height,width,mxREAL); 16 | return mxGetPr(ptr); 17 | } 18 | 19 | // Function definitions for class Matrix. 20 | // ----------------------------------------------------------------- 21 | Matrix::Matrix (int height, int width) 22 | : Array(height*width) { 23 | h = height; 24 | w = width; 25 | } 26 | 27 | Matrix::Matrix (double* data, int height, int width) 28 | : Array(data,height*width) { 29 | h = height; 30 | w = width; 31 | } 32 | 33 | Matrix::Matrix (const mxArray* ptr) 34 | : Array(getMatlabMatrixDouble(ptr),mxGetNumberOfElements(ptr)) { 35 | h = mxGetM(ptr); 36 | w = mxGetN(ptr); 37 | } 38 | 39 | Matrix::Matrix (mxArray*& ptr, int height, int width) 40 | : Array(createMatlabMatrixDouble(ptr,height,width), 41 | height*width) { 42 | h = height; 43 | w = width; 44 | } 45 | 46 | Matrix::Matrix (const Matrix& source) 47 | : Array(source) { 48 | h = source.h; 49 | w = source.w; 50 | } 51 | 52 | Matrix& Matrix::operator= (const Matrix& source) { 53 | inject(source); 54 | return *this; 55 | } 56 | 57 | bool Matrix::operator== (const Matrix& X) const { 58 | return (h == X.h) && (w == X.w); 59 | } 60 | 61 | double& Matrix::entry (int r, int c) { 62 | return elems[h*c + r]; 63 | } 64 | 65 | double Matrix::entry (int r, int c) const { 66 | return elems[h*c + r]; 67 | } 68 | -------------------------------------------------------------------------------- /GP/util/lbfgsb/matlabscalar.cpp: -------------------------------------------------------------------------------- 1 | #include "matlabscalar.h" 2 | #include "matlabexception.h" 3 | 4 | // Function definitions. 5 | // ----------------------------------------------------------------- 6 | double& getMatlabScalar (const mxArray* ptr) { 7 | if (!mxIsDouble(ptr)) 8 | throw MatlabException("Matlab array must be of type double"); 9 | if (mxGetNumberOfElements(ptr) != 1) 10 | throw MatlabException("The Matlab array must be a scalar"); 11 | return *mxGetPr(ptr); 12 | } 13 | 14 | double& createMatlabScalar (mxArray*& ptr) { 15 | ptr = mxCreateDoubleScalar(0); 16 | return *mxGetPr(ptr); 17 | } 18 | 19 | // Function definitions for class MatlabScalar. 20 | // --------------------------------------------------------------- 21 | MatlabScalar::MatlabScalar (const mxArray* ptr) 22 | : x(getMatlabScalar(ptr)) { } 23 | 24 | MatlabScalar::MatlabScalar (mxArray*& ptr, double value) 25 | : x(createMatlabScalar(ptr)) { 26 | x = value; 27 | } 28 | 29 | MatlabScalar::MatlabScalar (MatlabScalar& source) 30 | : x(source.x) { } 31 | 32 | MatlabScalar& MatlabScalar::operator= (double value) { 33 | x = value; 34 | return *this; 35 | } 36 | -------------------------------------------------------------------------------- /GP/util/lbfgsb/matlabscalar.h: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_MATLABSCALAR 2 | #define INCLUDE_MATLABSCALAR 3 | 4 | #include "mex.h" 5 | 6 | // Class MatlabScalar 7 | // ----------------------------------------------------------------- 8 | // The main appeal of this class is that one can create a scalar 9 | // object that accesses a MATLAB array. 10 | // 11 | // Note that the copy assignment operator is not valid for this class 12 | // because we cannot reassign a reference. 13 | class MatlabScalar { 14 | public: 15 | 16 | // This constructor accepts as input a pointer to a Matlab array 17 | // which must be a scalar in double precision. 18 | explicit MatlabScalar (const mxArray* ptr); 19 | 20 | // This constructor creates a new Matlab array which is a scalar 21 | // in double precision. 22 | MatlabScalar (mxArray*& ptr, double value); 23 | 24 | // The copy constructor. 25 | MatlabScalar (MatlabScalar& source); 26 | 27 | // The destructor. 28 | ~MatlabScalar() { }; 29 | 30 | // Access the value of the scalar. 31 | operator const double () const { return x; }; 32 | 33 | // Assign the value of the scalar. 34 | MatlabScalar& operator= (double value); 35 | 36 | protected: 37 | double& x; 38 | 39 | // The copy assignment operator is kept protected because it is 40 | // invalid. 41 | MatlabScalar& operator= (const MatlabScalar& source) { return *this; }; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /GP/util/lbfgsb/matlabstring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "matlabstring.h" 3 | #include "matlabexception.h" 4 | 5 | // Function definitions. 6 | // ----------------------------------------------------------------- 7 | char* copystring (const char* source) { 8 | int n = strlen(source); // The length of the string. 9 | char* dest = new char[n+1]; // The return value. 10 | strcpy(dest,source); 11 | return dest; 12 | } 13 | 14 | // Function definitions for class MatlabString. 15 | // ----------------------------------------------------------------- 16 | MatlabString::MatlabString (const mxArray* ptr) { 17 | s = 0; 18 | 19 | // Check to make sure the Matlab array is a string. 20 | if (!mxIsChar(ptr)) 21 | throw MatlabException("Matlab array must be a string (of type CHAR)"); 22 | 23 | // Get the string passed as a Matlab array. 24 | s = mxArrayToString(ptr); 25 | if (s == 0) 26 | throw MatlabException("Unable to obtain string from Matlab array"); 27 | } 28 | 29 | MatlabString::MatlabString (const MatlabString& source) { 30 | s = 0; 31 | 32 | // Copy the source string. 33 | s = copystring(source.s); 34 | } 35 | 36 | MatlabString::~MatlabString() { 37 | if (s) 38 | mxFree(s); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /GP/util/lbfgsb/matlabstring.h: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_MATLABSTRING 2 | #define INCLUDE_MATLABSTRING 3 | 4 | #include "mex.h" 5 | #include 6 | 7 | // Function declarations. 8 | // ----------------------------------------------------------------- 9 | // Copy a C-style string (i.e. a null-terminated character array). 10 | char* copystring (const char* source); 11 | 12 | // Class MatlabString. 13 | // ----------------------------------------------------------------- 14 | // This class encapsulates read-only access to a MATLAB character 15 | // array. 16 | class MatlabString { 17 | public: 18 | 19 | // The constructor accepts as input a pointer to a Matlab array, 20 | // which must be a valid string (array of type CHAR). 21 | explicit MatlabString (const mxArray* ptr); 22 | 23 | // The copy constructor makes a full copy of the source string. 24 | MatlabString (const MatlabString& source); 25 | 26 | // The destructor. 27 | ~MatlabString(); 28 | 29 | // Conversion operator for null-terminated string. 30 | operator const char* () const { return s; }; 31 | 32 | protected: 33 | char* s; // The null-terminated string. 34 | 35 | // The copy assignment operator is not proper, thus remains 36 | // protected. 37 | MatlabString& operator= (const MatlabString& source) 38 | { return *this; }; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /GP/util/lbfgsb/timer.f: -------------------------------------------------------------------------------- 1 | c 2 | c L-BFGS-B is released under the “New BSD License” (aka “Modified BSD 3 | c License” or “3-clause license”) 4 | c Please read attached file License.txt 5 | c 6 | subroutine timer(ttime) 7 | double precision ttime 8 | c 9 | real temp 10 | c 11 | c This routine computes cpu time in double precision; it makes use of 12 | c the intrinsic f90 cpu_time therefore a conversion type is 13 | c needed. 14 | c 15 | c J.L Morales Departamento de Matematicas, 16 | c Instituto Tecnologico Autonomo de Mexico 17 | c Mexico D.F. 18 | c 19 | c J.L Nocedal Department of Electrical Engineering and 20 | c Computer Science. 21 | c Northwestern University. Evanston, IL. USA 22 | c 23 | c January 21, 2011 24 | c 25 | temp = sngl(ttime) 26 | call cpu_time(temp) 27 | ttime = dble(temp) 28 | 29 | return 30 | 31 | end 32 | 33 | -------------------------------------------------------------------------------- /GP/util/lik_epquad.m: -------------------------------------------------------------------------------- 1 | % Compute infEP part of a likelihood function based on the infLaplace part using 2 | % Gaussian-Hermite quadrature. 3 | % 4 | % The function is used in GLM likelihoods such as likPoisson, likGamma, likBeta 5 | % and likInvGauss. 6 | % 7 | % Copyright (c) by Hannes Nickisch, 2013-10-16. 8 | 9 | function varargout = lik_epquad(lik,hyp,y,mu,s2) 10 | n = max([length(y),length(mu),length(s2)]); on = ones(n,1); 11 | N = 20; [t,w] = gauher(N); oN = ones(1,N); lw = ones(n,1)*log(w'); 12 | y = y(:).*on; mu = mu(:).*on; sig = sqrt(s2(:)).*on; % vectors only 13 | [lpi,dlpi,d2lpi] = feval(lik{:},hyp,y*oN,sig*t'+mu*oN,[],'infLaplace'); 14 | lZ = s(lpi+lw); 15 | dlZ = {}; d2lZ = {}; 16 | if nargout>1 % 1st derivative wrt mean 17 | % Using p*dlp=dp, p=exp(lp), Z=sum_i wi*pi, dZ = sum_i wi*dpi we obtain 18 | % dlZ = sum_i exp(lpi-lZ+lwi)*dlpi = sum_i ai*dlpi. 19 | a = exp(lpi - lZ*oN + lw); 20 | dlZ = sum(a.*dlpi,2); 21 | if nargout>2 % 2nd derivative wrt mean 22 | % Using d2lZ=(d2Z*Z-dZ^2)/Z^2 <=> d2Z=Z*(d2lZ+dlZ^2) and 23 | % d2Z = sum_i wi*d2Zi, we get d2lZ = sum_i ai*(d2lpi+dlpi^2)-dlZ^2. 24 | d2lZ = sum(a.*(d2lpi+dlpi.*dlpi),2) - dlZ.*dlZ; 25 | end 26 | end 27 | varargout = {lZ,dlZ,d2lZ}; 28 | 29 | % computes y = log( sum(exp(x),2) ), the softmax in a numerically safe way by 30 | % subtracting the row maximum to avoid cancelation after taking the exp 31 | % the sum is done along the rows 32 | function [y,x] = s(logx) 33 | N = size(logx,2); max_logx = max(logx,[],2); 34 | % we have all values in the log domain, and want to calculate a sum 35 | x = exp(logx-max_logx*ones(1,N)); 36 | y = log(sum(x,2)) + max_logx; -------------------------------------------------------------------------------- /GP/util/logphi.m: -------------------------------------------------------------------------------- 1 | % Safe computation of logphi(z) = log(normcdf(z)) and its derivatives 2 | % dlogphi(z) = normpdf(x)/normcdf(x). 3 | % The function is based on index 5725 in Hart et al. and gsl_sf_log_erfc_e. 4 | % 5 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2013-11-13. 6 | function [lp,dlp,d2lp,d3lp] = logphi(z) 7 | z = real(z); % support for real arguments only 8 | lp = zeros(size(z)); % allocate memory 9 | id1 = z.*z<0.0492; % first case: close to zero 10 | lp0 = -z(id1)/sqrt(2*pi); 11 | c = [ 0.00048204; -0.00142906; 0.0013200243174; 0.0009461589032; 12 | -0.0045563339802; 0.00556964649138; 0.00125993961762116; 13 | -0.01621575378835404; 0.02629651521057465; -0.001829764677455021; 14 | 2*(1-pi/3); (4-pi)/3; 1; 1]; 15 | f = 0; for i=1:14, f = lp0.*(c(i)+f); end, lp(id1) = -2*f-log(2); 16 | id2 = z<-11.3137; % second case: very small 17 | r = [ 1.2753666447299659525; 5.019049726784267463450; 18 | 6.1602098531096305441; 7.409740605964741794425; 19 | 2.9788656263939928886 ]; 20 | q = [ 2.260528520767326969592; 9.3960340162350541504; 21 | 12.048951927855129036034; 17.081440747466004316; 22 | 9.608965327192787870698; 3.3690752069827527677 ]; 23 | num = 0.5641895835477550741; for i=1:5, num = -z(id2).*num/sqrt(2) + r(i); end 24 | den = 1.0; for i=1:6, den = -z(id2).*den/sqrt(2) + q(i); end 25 | e = num./den; lp(id2) = log(e/2) - z(id2).^2/2; 26 | id3 = ~id2 & ~id1; lp(id3) = log(erfc(-z(id3)/sqrt(2))/2); % third case: rest 27 | if nargout>1 % compute first derivative 28 | dlp = zeros(size(z)); % allocate memory 29 | dlp( id2) = abs(den./num) * sqrt(2/pi); % strictly positive first derivative 30 | dlp(~id2) = exp(-z(~id2).*z(~id2)/2-lp(~id2))/sqrt(2*pi); % safe computation 31 | if nargout>2 % compute second derivative 32 | d2lp = -dlp.*abs(z+dlp); % strictly negative second derivative 33 | if nargout>3 % compute third derivative 34 | d3lp = -d2lp.*abs(z+2*dlp)-dlp; % strictly positive third derivative 35 | end 36 | end 37 | end -------------------------------------------------------------------------------- /GP/util/logsumexp2.m: -------------------------------------------------------------------------------- 1 | % Compute y = log( sum(exp(x),2) ), the softmax in a numerically safe way by 2 | % subtracting the row maximum to avoid cancelation after taking the exp 3 | % the sum is done along the rows. 4 | % 5 | % Copyright (c) by Hannes Nickisch, 2013-10-16. 6 | 7 | function [y,x] = logsumexp2(logx) 8 | N = size(logx,2); max_logx = max(logx,[],2); 9 | % we have all values in the log domain, and want to calculate a sum 10 | x = exp(logx-max_logx*ones(1,N)); 11 | y = log(sum(x,2)) + max_logx; -------------------------------------------------------------------------------- /GP/util/minfunc/License: -------------------------------------------------------------------------------- 1 | The software on this webpage is distributed under the FreeBSD-style license below. 2 | 3 | Although it is not required, I would also appreciate that any re-distribution of the 4 | software contains a link to the original webpage. For example, the webpage for the 5 | 'minFunc' software is: 6 | http://www.cs.ubc.ca/~schmidtm/Software/minFunc.html 7 | 8 | ======================================================================================= 9 | 10 | Copyright 2005-2015 Mark Schmidt. All rights reserved. 11 | 12 | Redistribution and use in source and binary forms, with or without modification, are 13 | permitted provided that the following conditions are met: 14 | 15 | 1. Redistributions of source code must retain the above copyright notice, this list of 16 | conditions and the following disclaimer. 17 | 18 | 2. Redistributions in binary form must reproduce the above copyright notice, this list 19 | of conditions and the following disclaimer in the documentation and/or other materials 20 | provided with the distribution. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED 23 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 24 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 30 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsAddC.mex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsAddC.mex -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsAddC.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsAddC.mexa64 -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsAddC.mexglx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsAddC.mexglx -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsAddC.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsAddC.mexmaci64 -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsAddC.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsAddC.mexw64 -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsC.mex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsC.mex -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsC.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsC.mexa64 -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsC.mexglx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsC.mexglx -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsC.mexmac: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsC.mexmac -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsC.mexmaci: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsC.mexmaci -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsC.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsC.mexmaci64 -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsC.mexw32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsC.mexw32 -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsC.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsC.mexw64 -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsProdC.mex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsProdC.mex -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsProdC.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsProdC.mexa64 -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsProdC.mexglx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsProdC.mexglx -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsProdC.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsProdC.mexmaci64 -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/lbfgsProdC.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/lbfgsProdC.mexw64 -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/mcholC.mex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/mcholC.mex -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/mcholC.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/mcholC.mexa64 -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/mcholC.mexglx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/mcholC.mexglx -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/mcholC.mexmac: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/mcholC.mexmac -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/mcholC.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/mcholC.mexmaci64 -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/mcholC.mexw32: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/mcholC.mexw32 -------------------------------------------------------------------------------- /GP/util/minfunc/compiled/mcholC.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/GP/util/minfunc/compiled/mcholC.mexw64 -------------------------------------------------------------------------------- /GP/util/minfunc/conjGrad.m: -------------------------------------------------------------------------------- 1 | function [x,k,res,negCurv] = conjGrad(A,b,optTol,maxIter,verbose,precFunc,precArgs,matrixVectFunc,matrixVectArgs) 2 | % [x,k,res,negCurv] = 3 | % conjGrad(A,b,optTol,maxIter,verbose,precFunc,precArgs,matrixVectFunc,matrixVect 4 | % Args) 5 | % Linear Conjugate Gradient, where optionally we use 6 | % - preconditioner on vector v with precFunc(v,precArgs{:}) 7 | % - matrix multipled by vector with matrixVectFunc(v,matrixVectArgs{:}) 8 | 9 | if nargin <= 4 10 | verbose = 0; 11 | end 12 | 13 | x = zeros(size(b)); 14 | r = -b; 15 | 16 | % Apply preconditioner (if supplied) 17 | if nargin >= 7 && ~isempty(precFunc) 18 | y = precFunc(r,precArgs{:}); 19 | else 20 | y = r; 21 | end 22 | 23 | ry = r'*y; 24 | p = -y; 25 | k = 0; 26 | 27 | res = norm(r); 28 | done = 0; 29 | negCurv = []; 30 | while res > optTol & k < maxIter & ~done 31 | % Compute Matrix-vector product 32 | if nargin >= 9 33 | Ap = matrixVectFunc(p,matrixVectArgs{:}); 34 | else 35 | Ap = A*p; 36 | end 37 | pAp = p'*Ap; 38 | 39 | % Check for negative Curvature 40 | if pAp <= 1e-16 41 | if verbose 42 | fprintf('Negative Curvature Detected!\n'); 43 | end 44 | 45 | if nargout == 4 46 | if pAp < 0 47 | negCurv = p; 48 | return 49 | end 50 | end 51 | 52 | if k == 0 53 | if verbose 54 | fprintf('First-Iter, Proceeding...\n'); 55 | end 56 | done = 1; 57 | else 58 | if verbose 59 | fprintf('Stopping\n'); 60 | end 61 | break; 62 | end 63 | end 64 | 65 | % Conjugate Gradient 66 | alpha = ry/(pAp); 67 | x = x + alpha*p; 68 | r = r + alpha*Ap; 69 | 70 | % If supplied, apply preconditioner 71 | if nargin >= 7 && ~isempty(precFunc) 72 | y = precFunc(r,precArgs{:}); 73 | else 74 | y = r; 75 | end 76 | 77 | ry_new = r'*y; 78 | beta = ry_new/ry; 79 | p = -y + beta*p; 80 | k = k + 1; 81 | 82 | % Update variables 83 | ry = ry_new; 84 | res = norm(r); 85 | end 86 | end 87 | -------------------------------------------------------------------------------- /GP/util/minfunc/dampedUpdate.m: -------------------------------------------------------------------------------- 1 | function [old_dirs,old_stps,Hdiag,Bcompact] = dampedUpdate(y,s,corrections,debug,old_dirs,old_stps,Hdiag) 2 | 3 | %B0 = eye(length(y))/Hdiag; 4 | S = old_dirs(:,2:end); 5 | Y = old_stps(:,2:end); 6 | k = size(Y,2); 7 | L = zeros(k); 8 | for j = 1:k 9 | for i = j+1:k 10 | L(i,j) = S(:,i)'*Y(:,j); 11 | end 12 | end 13 | D = diag(diag(S'*Y)); 14 | N = [S/Hdiag Y]; 15 | M = [S'*S/Hdiag L;L' -D]; 16 | 17 | ys = y'*s; 18 | Bs = s/Hdiag - N*(M\(N'*s)); % Product B*s 19 | sBs = s'*Bs; 20 | 21 | eta = .02; 22 | if ys < eta*sBs 23 | if debug 24 | fprintf('Damped Update\n'); 25 | end 26 | theta = min(max(0,((1-eta)*sBs)/(sBs - ys)),1); 27 | y = theta*y + (1-theta)*Bs; 28 | end 29 | 30 | 31 | numCorrections = size(old_dirs,2); 32 | if numCorrections < corrections 33 | % Full Update 34 | old_dirs(:,numCorrections+1) = s; 35 | old_stps(:,numCorrections+1) = y; 36 | else 37 | % Limited-Memory Update 38 | old_dirs = [old_dirs(:,2:corrections) s]; 39 | old_stps = [old_stps(:,2:corrections) y]; 40 | end 41 | 42 | % Update scale of initial Hessian approximation 43 | Hdiag = (y'*s)/(y'*y); -------------------------------------------------------------------------------- /GP/util/minfunc/isLegal.m: -------------------------------------------------------------------------------- 1 | function [legal] = isLegal(v) 2 | legal = sum(any(imag(v(:))))==0 & sum(isnan(v(:)))==0 & sum(isinf(v(:)))==0; -------------------------------------------------------------------------------- /GP/util/minfunc/lbfgs.m: -------------------------------------------------------------------------------- 1 | function [d] = lbfgs(g,s,y,Hdiag) 2 | % BFGS Search Direction 3 | % 4 | % This function returns the (L-BFGS) approximate inverse Hessian, 5 | % multiplied by the gradient 6 | % 7 | % If you pass in all previous directions/sizes, it will be the same as full BFGS 8 | % If you truncate to the k most recent directions/sizes, it will be L-BFGS 9 | % 10 | % s - previous search directions (p by k) 11 | % y - previous step sizes (p by k) 12 | % g - gradient (p by 1) 13 | % Hdiag - value of initial Hessian diagonal elements (scalar) 14 | 15 | [p,k] = size(s); 16 | 17 | for i = 1:k 18 | ro(i,1) = 1/(y(:,i)'*s(:,i)); 19 | end 20 | 21 | q = zeros(p,k+1); 22 | r = zeros(p,k+1); 23 | al =zeros(k,1); 24 | be =zeros(k,1); 25 | 26 | q(:,k+1) = g; 27 | 28 | for i = k:-1:1 29 | al(i) = ro(i)*s(:,i)'*q(:,i+1); 30 | q(:,i) = q(:,i+1)-al(i)*y(:,i); 31 | end 32 | 33 | % Multiply by Initial Hessian 34 | r(:,1) = Hdiag*q(:,1); 35 | 36 | for i = 1:k 37 | be(i) = ro(i)*y(:,i)'*r(:,i); 38 | r(:,i+1) = r(:,i) + s(:,i)*(al(i)-be(i)); 39 | end 40 | d=r(:,k+1); -------------------------------------------------------------------------------- /GP/util/minfunc/lbfgsAdd.m: -------------------------------------------------------------------------------- 1 | function [S,Y,YS,lbfgs_start,lbfgs_end,Hdiag,skipped] = lbfgsAdd(y,s,S,Y,YS,lbfgs_start,lbfgs_end,Hdiag,useMex) 2 | ys = y'*s; 3 | skipped = 0; 4 | corrections = size(S,2); 5 | if ys > 1e-10 6 | if lbfgs_end < corrections 7 | lbfgs_end = lbfgs_end+1; 8 | if lbfgs_start ~= 1 9 | if lbfgs_start == corrections 10 | lbfgs_start = 1; 11 | else 12 | lbfgs_start = lbfgs_start+1; 13 | end 14 | end 15 | else 16 | lbfgs_start = min(2,corrections); 17 | lbfgs_end = 1; 18 | end 19 | 20 | if useMex 21 | lbfgsAddC(y,s,Y,S,ys,int32(lbfgs_end)); 22 | else 23 | S(:,lbfgs_end) = s; 24 | Y(:,lbfgs_end) = y; 25 | end 26 | YS(lbfgs_end) = ys; 27 | 28 | % Update scale of initial Hessian approximation 29 | Hdiag = ys/(y'*y); 30 | else 31 | skipped = 1; 32 | end -------------------------------------------------------------------------------- /GP/util/minfunc/lbfgsProd.m: -------------------------------------------------------------------------------- 1 | function [d] = lbfgsProd(g,S,Y,YS,lbfgs_start,lbfgs_end,Hdiag) 2 | % BFGS Search Direction 3 | % 4 | % This function returns the (L-BFGS) approximate inverse Hessian, 5 | % multiplied by the negative gradient 6 | 7 | % Set up indexing 8 | [nVars,maxCorrections] = size(S); 9 | if lbfgs_start == 1 10 | ind = 1:lbfgs_end; 11 | nCor = lbfgs_end-lbfgs_start+1; 12 | else 13 | ind = [lbfgs_start:maxCorrections 1:lbfgs_end]; 14 | nCor = maxCorrections; 15 | end 16 | al = zeros(nCor,1); 17 | be = zeros(nCor,1); 18 | 19 | d = -g; 20 | for j = 1:length(ind) 21 | i = ind(end-j+1); 22 | al(i) = (S(:,i)'*d)/YS(i); 23 | d = d-al(i)*Y(:,i); 24 | end 25 | 26 | % Multiply by Initial Hessian 27 | d = Hdiag*d; 28 | 29 | for i = ind 30 | be(i) = (Y(:,i)'*d)/YS(i); 31 | d = d + S(:,i)*(al(i)-be(i)); 32 | end 33 | -------------------------------------------------------------------------------- /GP/util/minfunc/lbfgsUpdate.m: -------------------------------------------------------------------------------- 1 | function [old_dirs,old_stps,Hdiag] = lbfgsUpdate(y,s,corrections,debug,old_dirs,old_stps,Hdiag) 2 | ys = y'*s; 3 | if ys > 1e-10 4 | numCorrections = size(old_dirs,2); 5 | if numCorrections < corrections 6 | % Full Update 7 | old_dirs(:,numCorrections+1) = s; 8 | old_stps(:,numCorrections+1) = y; 9 | else 10 | % Limited-Memory Update 11 | old_dirs = [old_dirs(:,2:corrections) s]; 12 | old_stps = [old_stps(:,2:corrections) y]; 13 | end 14 | 15 | % Update scale of initial Hessian approximation 16 | Hdiag = ys/(y'*y); 17 | else 18 | if debug 19 | fprintf('Skipping Update\n'); 20 | end 21 | end -------------------------------------------------------------------------------- /GP/util/minfunc/mchol.m: -------------------------------------------------------------------------------- 1 | function [l,d,perm] = mchol(A,mu) 2 | % [l,d,perm] = mchol(A,mu) 3 | % Compute the Gill-Murray modified LDL factorization of A, 4 | 5 | if nargin < 2 6 | mu = 1e-12; 7 | end 8 | 9 | n = size(A,1); 10 | l = eye(n); 11 | d = zeros(n,1); 12 | perm = 1:n; 13 | 14 | for i = 1:n 15 | c(i,i) = A(i,i); 16 | end 17 | 18 | % Compute modification parameters 19 | gamma = max(abs(diag(A))); 20 | xi = max(max(abs(setdiag(A,0)))); 21 | delta = mu*max(gamma+xi,1); 22 | if n > 1 23 | beta = sqrt(max([gamma xi/sqrt(n^2-1) mu])); 24 | else 25 | beta = sqrt(max([gamma mu])); 26 | end 27 | 28 | for j = 1:n 29 | 30 | % Find q that results in Best Permutation with j 31 | [maxVal maxPos] = max(abs(diag(c(j:end,j:end)))); 32 | q = maxPos+j-1; 33 | 34 | % Permute d,c,l,a 35 | d([j q]) = d([q j]); 36 | perm([j q]) = perm([q j]); 37 | c([j q],:) = c([q j],:); 38 | c(:,[j q]) = c(:,[q j]); 39 | l([j q],:) = l([q j],:); 40 | l(:,[j q]) = l(:,[q j]); 41 | A([j q],:) = A([q j],:); 42 | A(:,[j q]) = A(:,[q j]); 43 | 44 | for s = 1:j-1 45 | l(j,s) = c(j,s)/d(s); 46 | end 47 | for i = j+1:n 48 | c(i,j) = A(i,j) - sum(l(j,1:j-1).*c(i,1:j-1)); 49 | end 50 | theta = 0; 51 | if j < n 52 | theta = max(abs(c(j+1:n,j))); 53 | end 54 | d(j) = max([abs(c(j,j)) (theta/beta)^2 delta]); 55 | if j < n 56 | for i = j+1:n 57 | c(i,i) = c(i,i) - (c(i,j)^2)/d(j); 58 | end 59 | end 60 | end -------------------------------------------------------------------------------- /GP/util/minfunc/mcholinc.m: -------------------------------------------------------------------------------- 1 | function [R,tau] = mcholinc(H,verbose) 2 | % Computes Cholesky of H+tau*I, for suitably large tau that matrix is pd 3 | 4 | p = size(H,1); 5 | 6 | beta = norm(H,'fro'); 7 | if min(diag(H)) > 1e-12 8 | tau = 0; 9 | else 10 | if verbose 11 | fprintf('Small Value on Diagonal, Adjusting Hessian\n'); 12 | end 13 | tau = max(beta/2,1e-12); 14 | end 15 | while 1 16 | [R,posDef] = chol(H+tau*eye(p)); 17 | if posDef == 0 18 | break; 19 | else 20 | if verbose 21 | fprintf('Cholesky Failed, Adjusting Hessian\n'); 22 | end 23 | tau = max(2*tau,beta/2); 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /GP/util/minfunc/mex/lbfgsAddC.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mex.h" 3 | 4 | /* See lbfgsAdd.m for details */ 5 | /* This function will not exit gracefully on bad input! */ 6 | 7 | void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 8 | { 9 | /* Variable Declarations */ 10 | 11 | double *s,*y,*S, *Y, ys; 12 | int i,j,nVars,lbfgs_end; 13 | 14 | /* Get Input Pointers */ 15 | 16 | y = mxGetPr(prhs[0]); 17 | s = mxGetPr(prhs[1]); 18 | Y = mxGetPr(prhs[2]); 19 | S = mxGetPr(prhs[3]); 20 | ys= mxGetScalar(prhs[4]); 21 | lbfgs_end = (int)mxGetScalar(prhs[5]); 22 | 23 | if (!mxIsClass(prhs[5],"int32")) 24 | mexErrMsgTxt("lbfgs_end must be int32"); 25 | 26 | /* Compute number of variables, maximum number of corrections */ 27 | 28 | nVars = mxGetDimensions(prhs[2])[0]; 29 | 30 | for(j=0;j 1 14 | gd2(t3) = gd2(t3) + T(t1,t2,t3)*d(t1)*d(t2); 15 | end 16 | 17 | if nargout > 2 18 | Hd(t2,t3) = Hd(t2,t3) + T(t1,t2,t3)*d(t1); 19 | end 20 | end 21 | 22 | end 23 | end 24 | 25 | f = f + g'*d + (1/2)*d'*H*d + (1/6)*fd3; 26 | 27 | if nargout > 1 28 | g = g + H*d + (1/2)*gd2; 29 | end 30 | 31 | if nargout > 2 32 | H = H + Hd; 33 | end 34 | 35 | if any(abs(d) > 1e5) 36 | % We want the optimizer to stop if the solution is unbounded 37 | g = zeros(p,1); 38 | end -------------------------------------------------------------------------------- /GP/util/solve_chol.c: -------------------------------------------------------------------------------- 1 | /* solve_chol - solve a linear system A*X = B using the Cholesky factorization 2 | * of A (square, symmetric and positive definite) using LAPACK/DPOTRS. 3 | * 4 | * Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch 2014-02-13. 5 | * 6 | * Modifications from 2014-02-09 as suggested by Todd Small: 7 | * - types of q,m,n changed from long to integer, q error check had wrong sign 8 | */ 9 | 10 | #include "mex.h" 11 | #include 12 | 13 | #ifdef MEX_INFORMATION_VERSION /* now we are compiling for Matlab */ 14 | #if defined(_WIN64) 15 | #define integer long long 16 | #else 17 | #define integer long 18 | #endif 19 | #define doublereal double 20 | #else /* now we are compiling for Octave */ 21 | #ifdef __APPLE__ 22 | #include 23 | typedef __CLPK_integer integer; 24 | typedef __CLPK_doublereal doublereal; 25 | #else 26 | typedef int integer; 27 | typedef double doublereal; 28 | #endif 29 | #endif 30 | 31 | #if !defined(_WIN32) || !defined(MEX_INFORMATION_VERSION) /* not Win32/Matlab */ 32 | #define dpotrs dpotrs_ 33 | #endif 34 | 35 | extern integer dpotrs_(char *, integer *, integer *, doublereal *, integer *, 36 | doublereal *, integer *, integer *); 37 | 38 | void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 39 | { /* Matlab call: X = solve_chol(A, B) */ 40 | doublereal *C; 41 | integer n, m, q; 42 | 43 | if (nrhs != 2 || nlhs > 1) /* check input */ 44 | mexErrMsgTxt("Usage: X = solve_chol(A, B)"); 45 | n = mxGetM(prhs[0]); /* number of rows in inputs A and B */ 46 | if (n != mxGetN(prhs[0])) 47 | mexErrMsgTxt("First argument matrix must be square."); 48 | if (n != mxGetM(prhs[1])) 49 | mexErrMsgTxt("Both argument matrices must have the same number of rows."); 50 | m = mxGetN(prhs[1]); /* number of colums in second input B */ 51 | 52 | plhs[0] = mxCreateDoubleMatrix(n, m, mxREAL); /* space for output X */ 53 | C = mxGetPr(plhs[0]); 54 | 55 | if (n == 0) return; /* if argument was empty matrix, do no more */ 56 | memcpy( C, mxGetPr(prhs[1]), m*n*mxGetElementSize(plhs[0]) ); /* copy data */ 57 | dpotrs("U", &n, &m, mxGetPr(prhs[0]), &n, C, &n, &q); /* solve system */ 58 | if (q < 0) mexErrMsgTxt("Error: illegal input to solve_chol"); 59 | } 60 | -------------------------------------------------------------------------------- /GP/util/solve_chol.m: -------------------------------------------------------------------------------- 1 | % solve_chol - solve linear equations from the Cholesky factorization. 2 | % Solve A*X = B for X, where A is square, symmetric, positive definite. The 3 | % input to the function is R the Cholesky decomposition of A and the matrix B. 4 | % Example: X = solve_chol(chol(A),B); 5 | % 6 | % NOTE: The program code is written in the C language for efficiency and is 7 | % contained in the file solve_chol.c, and should be compiled using matlabs mex 8 | % facility. However, this file also contains a (less efficient) matlab 9 | % implementation, supplied only as a help to people unfamiliar with mex. If 10 | % the C code has been properly compiled and is available, it automatically 11 | % takes precendence over the matlab code in this file. 12 | % 13 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch 2010-09-18. 14 | 15 | function X = solve_chol(L, B) 16 | 17 | if nargin ~= 2 || nargout > 1 18 | error('Wrong number of arguments.'); 19 | end 20 | 21 | if size(L,1) ~= size(L,2) || size(L,1) ~= size(B,1) 22 | error('Wrong sizes of matrix arguments.'); 23 | end 24 | 25 | X = L\(L'\B); -------------------------------------------------------------------------------- /GP/util/sparseinv/license.txt: -------------------------------------------------------------------------------- 1 | Taken from https://de.mathworks.com/matlabcentral/fileexchange/33966-sparseinv--sparse-inverse-subset 2 | 3 | License 4 | 5 | Copyright (c) 2014, Tim Davis 6 | All rights reserved. 7 | 8 | Redistribution and use in source and binary forms, with or without 9 | modification, are permitted provided that the following conditions are 10 | met: 11 | 12 | * Redistributions of source code must retain the above copyright 13 | notice, this list of conditions and the following disclaimer. 14 | * Redistributions in binary form must reproduce the above copyright 15 | notice, this list of conditions and the following disclaimer in 16 | the documentation and/or other materials provided with the distribution 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 | POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /GP/util/sparseinv/sparseinv.h: -------------------------------------------------------------------------------- 1 | #ifndef _SPARSEINV_H_ 2 | #define _SPARSEINV_H_ 3 | #include 4 | #ifdef MATLAB_MEX_FILE 5 | #include "mex.h" 6 | #define Int mwSignedIndex 7 | #else 8 | #define Int ptrdiff_t 9 | #endif 10 | 11 | Int sparseinv /* returns 1 if OK, 0 if failure */ 12 | ( 13 | /* inputs, not modified on output: */ 14 | Int n, /* L, U, D, and Z are n-by-n */ 15 | 16 | Int *Lp, /* L is sparse, lower triangular, stored by column */ 17 | Int *Li, /* the row indices of L must be sorted */ 18 | double *Lx, /* diagonal of L, if present, is ignored */ 19 | 20 | double *d, /* diagonal of D, of size n */ 21 | 22 | Int *Up, /* U is sparse, upper triangular, stored by row */ 23 | Int *Uj, /* the column indices of U need not be sorted */ 24 | double *Ux, /* diagonal of U, if present, is ignored */ 25 | 26 | Int *Zp, /* Z is sparse, stored by column */ 27 | Int *Zi, /* the row indices of Z must be sorted */ 28 | 29 | /* output, not defined on input: */ 30 | double *Zx, 31 | 32 | /* workspace: */ 33 | double *z, /* size n, zero on input, restored as such on output */ 34 | Int *Zdiagp, /* size n */ 35 | Int *Lmunch /* size n */ 36 | ) ; 37 | #endif 38 | -------------------------------------------------------------------------------- /GP/util/sparseinv/sparseinv_mex.m: -------------------------------------------------------------------------------- 1 | function varargout = sparseinv_mex(varargin) % compile and run function 2 | 3 | loc = fileparts(mfilename('fullpath')); 4 | src = ['-outdir ',loc,' ',loc,'/sparseinv_mex.c ',loc,'/sparseinv.c']; 5 | 6 | if ~isempty(strfind(computer,'64')) 7 | fprintf ('Compiling sparseinv (64-bit)\n') ; 8 | eval(['mex -largeArrayDims ',src]) 9 | else 10 | fprintf ('Compiling sparseinv (32-bit)\n') ; 11 | eval(['mex ',src]) 12 | end 13 | varargout = cell(nargout,1); [varargout{:}] = sparseinv_mex (varargin{:}); -------------------------------------------------------------------------------- /GP/util/sq_dist.m: -------------------------------------------------------------------------------- 1 | % sq_dist - a function to compute a matrix of all pairwise squared distances 2 | % between two sets of vectors, stored in the columns of the two matrices, a 3 | % (of size D by n) and b (of size D by m). If only a single argument is given 4 | % or the second matrix is empty, the missing matrix is taken to be identical 5 | % to the first. 6 | % 7 | % Usage: C = sq_dist(a, b) 8 | % or: C = sq_dist(a) or equiv.: C = sq_dist(a, []) 9 | % 10 | % Where a is of size Dxn, b is of size Dxm (or empty), C is of size nxm. 11 | % 12 | % Copyright (c) by Carl Edward Rasmussen and Hannes Nickisch, 2010-12-13. 13 | 14 | function C = sq_dist(a, b) 15 | 16 | if nargin<1 || nargin>3 || nargout>1, error('Wrong number of arguments.'); end 17 | bsx = exist('bsxfun','builtin'); % since Matlab R2007a 7.4.0 and Octave 3.0 18 | if ~bsx, bsx = exist('bsxfun'); end % bsxfun is not yet "builtin" in Octave 19 | [D, n] = size(a); 20 | 21 | % Computation of a^2 - 2*a*b + b^2 is less stable than (a-b)^2 because numerical 22 | % precision can be lost when both a and b have very large absolute value and the 23 | % same sign. For that reason, we subtract the mean from the data beforehand to 24 | % stabilise the computations. This is OK because the squared error is 25 | % independent of the mean. 26 | if nargin==1 % subtract mean 27 | mu = mean(a,2); 28 | if bsx 29 | a = bsxfun(@minus,a,mu); 30 | else 31 | a = a - repmat(mu,1,size(a,2)); 32 | end 33 | b = a; m = n; 34 | else 35 | [d, m] = size(b); 36 | if d ~= D, error('Error: column lengths must agree.'); end 37 | mu = (m/(n+m))*mean(b,2) + (n/(n+m))*mean(a,2); 38 | if bsx 39 | a = bsxfun(@minus,a,mu); b = bsxfun(@minus,b,mu); 40 | else 41 | a = a - repmat(mu,1,n); b = b - repmat(mu,1,m); 42 | end 43 | end 44 | 45 | if bsx % compute squared distances 46 | C = bsxfun(@plus,sum(a.*a,1)',bsxfun(@minus,sum(b.*b,1),2*a'*b)); 47 | else 48 | C = repmat(sum(a.*a,1)',1,m) + repmat(sum(b.*b,1),n,1) - 2*a'*b; 49 | end 50 | C = max(C,0); % numerical noise can cause C to negative i.e. C > -1e-14 51 | -------------------------------------------------------------------------------- /GP/vec2any.m: -------------------------------------------------------------------------------- 1 | % Map the numerical elements in the vector "v" onto the variables "s" which can 2 | % be of any type. The number of numerical elements must match; on exit "v" 3 | % should be empty. Non-numerical entries are just copied. See also any2vec.m. 4 | 5 | function [s v] = vec2any(s, v) 6 | if isnumeric(s) 7 | if numel(v) < numel(s) 8 | error('The vector for conversion contains too few elements') 9 | end 10 | s = reshape(v(1:numel(s)), size(s)); % numeric values are reshaped 11 | v = v(numel(s)+1:end); % remaining arguments passed on 12 | elseif isstruct(s) 13 | [s p] = orderfields(s); p(p) = 1:numel(p); % alphabetize, store ordering 14 | [t v] = vec2any(struct2cell(s), v); % convert to cell, recurse 15 | s = orderfields(cell2struct(t,fieldnames(s),1),p); % conv to struct, reorder 16 | elseif iscell(s) 17 | for i = 1:numel(s) % cell array elements are handled sequentially 18 | [s{i} v] = vec2any(s{i}, v); 19 | end 20 | end % other types are not processed 21 | -------------------------------------------------------------------------------- /Moment_Matching_Approximation/KernalCov.m: -------------------------------------------------------------------------------- 1 | function [K] = KernalCov(x,z,hyp) 2 | 3 | % n = 20; 4 | % disp('x = gpml_randn(0.3, n, 1);') 5 | % x =gpml_randn(0.3, n, 1); 6 | % ell = 0.4; 7 | % sf = 1; 8 | % z = 2; 9 | 10 | % hyp= log([ell; sf]); 11 | covfunc = {@covSEard}; 12 | 13 | % disp('K = feval(covfunc{:}, hyp.cov, x);') 14 | K = feval(covfunc{:},hyp,x,z); 15 | 16 | end 17 | 18 | -------------------------------------------------------------------------------- /Presentation_final_harshith.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/Presentation_final_harshith.pdf -------------------------------------------------------------------------------- /Thesis_harshith final.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/Thesis_harshith final.pdf -------------------------------------------------------------------------------- /examples/Dc motor plant/Dc_motor_final_GP_mpc.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/Dc_motor_final_GP_mpc.fig -------------------------------------------------------------------------------- /examples/Dc motor plant/Dc_motor_final_f_mpc.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/Dc_motor_final_f_mpc.fig -------------------------------------------------------------------------------- /examples/Dc motor plant/Gp_mpc.m: -------------------------------------------------------------------------------- 1 | function [U] = Gp_mpc(xtrc,ytr,u0,N,lb,ub,Q,R) 2 | % %% Intializtaion of time step & Prediction horizon 3 | 4 | xk = xtrc(:,1:end-1); 5 | uk = xtrc(:,end); 6 | yk = ytr; 7 | nx =size(xtrc,2)-1; 8 | 9 | %% define the GP hyperparametrs & mean, cov, lik functions 10 | cov = {@covSEard}; 11 | sf = 0.1; 12 | for i =1:size(xtrc,2) 13 | ell(i,:) = 3; % setup the GP 14 | end 15 | hyp0.cov = log([ell;sf]); 16 | mean = {@meanLinear}; a = 0.1; % m(x) = a*x+b 17 | hyp0.mean = [a;0.1;a]; % @meanLinear 18 | % hyp0.mean = [a]; % @meanConst 19 | 20 | lik = {'likGauss'}; % Gauss likelihoods 21 | inf = {'infGaussLik'}; % inference algs 22 | 23 | Ncg = 100; % number of conjugate gradient steps 24 | 25 | sn = 0.2; 26 | hyp0.lik = [log(sn)]; 27 | fprintf('OPT: %s/%s\n',lik{1},inf{1}); 28 | hyp_1 = hyp0; 29 | 30 | %% optimization of hyperparametrs 31 | for i =1:nx 32 | if Ncg==0 33 | hyp = hyp0; 34 | else 35 | hyp_1 = fitGP(hyp_1, inf, mean, cov, lik,xtrc, ytr(:,i),[]); 36 | end 37 | hyp.state(i) = hyp_1; 38 | [ymu(i), ys(i)] = gp(hyp.state(i), inf, mean, cov, lik, xtrc, ytr(:,i),xtrc(end,:)); % prediction xk+1 for ending data point 39 | end 40 | ys(i+1) = 0; % set variance of input u as zero 41 | 42 | %% cost function 43 | u0= u0*ones(1,N); %intial input u 44 | fun = @(u0) cost(u0,xk,uk,ymu,ys,hyp,yk,Q,R); % define cost function 45 | %% minimization of cost function 46 | options = optimoptions('fmincon','Display','iter','Algorithm','sqp'); 47 | A = []; 48 | b = []; 49 | Aeq = []; 50 | beq = []; 51 | [U,fval] = fmincon(fun,u0,A,b,Aeq,beq,lb,ub,[],options); 52 | end -------------------------------------------------------------------------------- /examples/Dc motor plant/KernalCov.m: -------------------------------------------------------------------------------- 1 | function [K] = KernalCov(x,z,hyp) 2 | 3 | 4 | covfunc = {@covSEard}; 5 | K = feval(covfunc{:},hyp,x,z); 6 | 7 | end 8 | 9 | -------------------------------------------------------------------------------- /examples/Dc motor plant/cost.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | function [J] = cost(ute,xtr,utr,ymu,ys,hyp,ytr,Q,R) 4 | N = size (ute,2); 5 | xtrc = [xtr,utr]; 6 | J =0; 7 | for i=1:N 8 | if i==1 9 | mu_p = [ymu,ute(i)]; % µ_tilmda = [µt,ut] 10 | var_p = diag(ys); % Σ_tilda = blkdiag[Σt, 0] 11 | else 12 | mu_p = [mu_a(i-1,:),ute(i)]; 13 | var_p = blkdiag(var_a(:,:,i-1),0); 14 | end 15 | [mu_a(i,:),var_a(:,:,i)] = Gp_transition_change(mu_p, var_p,hyp,xtrc, ytr); 16 | 17 | J = J + mu_a(i,:)*Q*mu_a(i,:)'+ ute(i)*R*ute(i); 18 | end 19 | end 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /examples/Dc motor plant/costf.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | function [J] = costf(ute,yk,Q,R,f,f1,rand) 4 | N = size (ute,2); 5 | x1(1) = yk(end,1); 6 | x2(1) = yk(end,2); 7 | J =0; 8 | for i=1:N 9 | mu_a(i,1) = f(x1(i),x2(i),ute(i)); %,rand(i,1)); 10 | mu_a(i,2) = f1(x1(i),x2(i),ute(i)); %,rand(i,1)); 11 | 12 | J = J + mu_a(i,:)*Q*mu_a(i,:)'+ ute(i)*R*ute(i); 13 | 14 | x1(i+1)=mu_a(i,1); 15 | x2(i+1)=mu_a(i,2); 16 | 17 | end 18 | 19 | end 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data_dc_Gp_test_1.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data_dc_Gp_test_1.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data_dc_Gp_test_2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data_dc_Gp_test_2.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data_dc_Gp_test_final.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data_dc_Gp_test_final.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data_dc_motor.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data_dc_motor.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data_dc_motor_2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data_dc_motor_2.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data_dc_motor_3.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data_dc_motor_3.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data_dc_motor_5.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data_dc_motor_5.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data_dc_motor_6.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data_dc_motor_6.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data_dc_motor_7.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data_dc_motor_7.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data_dc_motor_8.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data_dc_motor_8.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data_dc_motor_9.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data_dc_motor_9.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data_dc_motor_noise.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data_dc_motor_noise.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/data/data_dc_motor_wo_noise.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Dc motor plant/data/data_dc_motor_wo_noise.mat -------------------------------------------------------------------------------- /examples/Dc motor plant/getopts.m: -------------------------------------------------------------------------------- 1 | function opts=getopts(opts,newOpts) 2 | if isempty(newOpts); return; end 3 | ch1=fieldnames(newOpts); 4 | ch2=fieldnames(opts); 5 | for i=1:length(ch1) 6 | for j=1:length(ch2) 7 | if strcmp(ch1{i},ch2{j}) 8 | if isstruct(opts.(ch2{j})) && isstruct(newOpts.(ch1{i})) 9 | opts.(ch2{j})=getopts(opts.(ch2{j}),newOpts.(ch1{i})); 10 | else 11 | opts.(ch2{j})=newOpts.(ch1{i}); 12 | end 13 | break; 14 | end 15 | if j == length(ch2) 16 | opts.(ch1{i})=newOpts.(ch1{i}); 17 | end 18 | end 19 | end 20 | end 21 | 22 | -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/Gp_mpc.m: -------------------------------------------------------------------------------- 1 | function [U] = Gp_mpc(xtrc,ytr,u0,N,lb,ub,Q,R) 2 | % %% Intializtaion of time step & Prediction horizon 3 | 4 | xk = xtrc(:,1:end-1); 5 | uk = xtrc(:,end); 6 | yk = ytr; 7 | nx =size(xtrc,2)-1; 8 | 9 | %% define the GP hyperparametrs & mean, cov, lik functions 10 | cov = {@covSEard}; 11 | sf = 0.1; 12 | for i =1:size(xtrc,2) 13 | ell(i,:) = 3; % setup the GP 14 | end 15 | hyp0.cov = log([ell;sf]); 16 | mean = {@meanLinear}; a = 0.1; 17 | hyp0.mean = [a;a;a]; % @meanLinear 18 | % hyp0.mean = [a]; % @meanConst 19 | 20 | lik = {'likGauss'}; % Gauss likelihoods 21 | inf = {'infGaussLik'}; % inference algs 22 | 23 | Ncg = 100; % number of conjugate gradient steps 24 | 25 | sn = 0.2; 26 | hyp0.lik = [log(sn)]; 27 | fprintf('OPT: %s/%s\n',lik{1},inf{1}); 28 | hyp_1 = hyp0; 29 | 30 | %% optimization of hyperparametrs 31 | for i =1:nx 32 | if Ncg==0 33 | hyp = hyp0; 34 | else 35 | hyp_1 = fitGP(hyp_1, inf, mean, cov, lik,xtrc, ytr(:,i),[]); 36 | end 37 | hyp.state(i) = hyp_1; 38 | [state(i).m, state(i).s2] = gp(hyp.state(i), inf, mean, cov, lik, xtrc, ytr(:,i),xtrc); % prediction xk+1 for ending data point 39 | [ymu(i), ys(i)] = gp(hyp.state(i), inf, mean, cov, lik, xtrc, ytr(:,i),xtrc(end,:)); 40 | end 41 | ys(i+1) = 0; % set variance of input u as zero 42 | 43 | %% cost function 44 | u0= u0*ones(1,N); %%intial input u 45 | fun = @(u0) cost(u0,xk,uk,ymu,ys,hyp,yk,Q,R); % define cost function 46 | %% minimization of cost function using fmincon 47 | options = optimoptions('fmincon','Display','iter','Algorithm','sqp'); 48 | A = []; 49 | b = []; 50 | Aeq = []; 51 | beq = []; 52 | [U,fval] = fmincon(fun,u0,A,b,Aeq,beq,lb,ub,[],options) 53 | 54 | end -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/KernalCov.m: -------------------------------------------------------------------------------- 1 | function [K] = KernalCov(x,z,hyp) 2 | 3 | % hyp= log([ell; sf]); 4 | covfunc = {@covSEard}; 5 | 6 | % disp('K = feval(covfunc{:}, hyp.cov, x);') 7 | K = feval(covfunc{:},hyp,x,z); 8 | 9 | end 10 | 11 | -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/VanDerPol_dynamics.m: -------------------------------------------------------------------------------- 1 | classdef VanDerPol_dynamics 2 | properties 3 | delta_t 4 | mu 5 | alpha 6 | beta 7 | end 8 | 9 | methods 10 | function obj = VanDerPol_dynamics(delta_t, mu, alpha, beta) 11 | beta = 0.8; 12 | alpha = 5; 13 | mu = 2; 14 | delta_t = 0.2; 15 | obj.delta_t = delta_t; 16 | obj.mu = mu; 17 | obj.alpha = alpha; 18 | obj.beta = beta; 19 | end 20 | 21 | function dx = f(obj, x) 22 | dx = [2*x(2); obj.mu*(1-obj.alpha*x(1)*x(1))*x(2)-obj.beta*x(1)]; 23 | end 24 | 25 | function dx = f_u(obj, x, u) 26 | dx = obj.f(x) + [0; u]; 27 | end 28 | 29 | function k1 = k1(obj, x, u) 30 | k1 = obj.f_u(x, u); 31 | end 32 | 33 | function k2 = k2(obj, x, u) 34 | k2 = obj.f_u(x + obj.k1(x, u) * obj.delta_t / 2, u); 35 | end 36 | 37 | function k3 = k3(obj, x, u) 38 | k3 = obj.f_u(x + obj.k2(x, u) * obj.delta_t / 2, u); 39 | end 40 | 41 | function k4 = k4(obj, x, u) 42 | k4 = obj.f_u(x + obj.k1(x, u) * obj.delta_t, u); 43 | end 44 | 45 | function x_next = f_ud(obj, x, u) 46 | x_next = x + (obj.delta_t / 6) * (obj.k1(x, u) + 2 * obj.k2(x, u) + 2 * obj.k3(x, u) + obj.k4(x, u)); 47 | end 48 | end 49 | end 50 | 51 | -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/VanDerPol_dynamics_test.m: -------------------------------------------------------------------------------- 1 | % Create an instance of the VanDerPol class 2 | clear all; 3 | % close all; 4 | vdp = VanDerPol_uncertanity_dynamics(); 5 | 6 | N = 30; 7 | 8 | % Define initial state and control input 9 | x0 = [1; 1]; 10 | u = randn(N+1,1); 11 | noise = randn(N+1,1); 12 | % Define time span 13 | t_span = 0:vdp.delta_t:N*vdp.delta_t; 14 | 15 | % Initialize array to store state trajectory 16 | x_traj = zeros(2, length(N+1)); 17 | y_traj = zeros(2, length(N)); 18 | x_traj(:, 1) = x0; 19 | 20 | % Simulate the system using the Runge-Kutta method 21 | for i = 2:length(t_span) 22 | x_traj(:, i) = vdp.f_ud(x_traj(:, i-1), u(i),0); 23 | dx(:,i-1)= x_traj(:, i)-x_traj(:, i-1); 24 | y_traj(:, i-1) = x_traj(:, i); 25 | end 26 | figure(6), hold on 27 | plot(x_traj(1,:),x_traj(2,:)) 28 | scatter(x_traj(1,1),x_traj(2,1), 'k', 'o', 'SizeData', 10); 29 | % quiver(x_traj(1,1:end-1),x_traj(2,1:end-1),dx,dx,0) 30 | grid on 31 | xlabel('x_1') 32 | ylabel('x_2') 33 | title('Phase portrait Van der Pol oscillator') 34 | hold off 35 | plot3(x_traj(1,:),x_traj(2,:),u) 36 | % xk = x_traj(:,1:end-1)'; 37 | % yk= y_traj'; 38 | % uk= u(1:end-1,:); 39 | % save('Van_pol_uncertainty6',"xk","yk","uk"); 40 | -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/VanDerPol_uncertanity_dynamics.m: -------------------------------------------------------------------------------- 1 | classdef VanDerPol_uncertanity_dynamics 2 | properties 3 | delta_t 4 | mu 5 | alpha 6 | beta 7 | gamma 8 | end 9 | % +0.001*randn(1) 10 | methods 11 | function obj = VanDerPol_uncertanity_dynamics(delta_t, mu, alpha, beta,gamma) 12 | beta = 0.8;%+0.8*0.05*randn(1); 13 | alpha = 5; %+5*0.05*randn(1); 14 | mu = 2; %+2*0.05*randn(1); 15 | gamma = 2; %+2*0.01*randn(1); 16 | delta_t = 0.2; 17 | obj.delta_t = delta_t; 18 | obj.mu = mu; 19 | obj.alpha = alpha; 20 | obj.beta = beta; 21 | obj.gamma = gamma; 22 | end 23 | 24 | function dx = f(obj, x,noise) 25 | dx = [(obj.gamma+2*0.01*noise)*x(2); (obj.mu+2*0.01*noise)*(1-(obj.alpha+5*0.01*noise)*x(1)*x(1))*x(2)-(obj.beta+0.8*0.05*noise)*x(1)]; 26 | end 27 | 28 | function dx = f_u(obj, x, u,noise) 29 | dx = obj.f(x,noise) + [0; u]; 30 | end 31 | 32 | function k1 = k1(obj, x, u,noise) 33 | k1 = obj.f_u(x, u,noise); 34 | end 35 | 36 | function k2 = k2(obj, x, u,noise) 37 | k2 = obj.f_u(x + obj.k1(x, u,noise) * obj.delta_t / 2, u,noise); 38 | end 39 | 40 | function k3 = k3(obj, x, u,noise) 41 | k3 = obj.f_u(x + obj.k2(x, u,noise) * obj.delta_t / 2, u,noise); 42 | end 43 | 44 | function k4 = k4(obj, x, u,noise) 45 | k4 = obj.f_u(x + obj.k1(x, u,noise) * obj.delta_t, u,noise); 46 | end 47 | 48 | function x_next = f_ud(obj, x, u,noise) 49 | x_next = x + (obj.delta_t / 6) * (obj.k1(x, u,noise) + 2 * obj.k2(x, u,noise) + 2 * obj.k3(x, u,noise) + obj.k4(x, u,noise)); 50 | end 51 | end 52 | end 53 | 54 | -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/cost.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | function [J] = cost(ute,xtr,utr,ymu,ys,hyp,ytr,Q,R) 4 | N = size (ute,2); 5 | n =size(xtr,2); 6 | xtrc = [xtr,utr]; 7 | J =0; 8 | 9 | final= [0,0]; % Final set point / target point 10 | 11 | 12 | 13 | for i=1:N 14 | if i==1 15 | mu_p = [ymu,ute(i)]; % µ_tilmda = [µt,ut] 16 | var_p = diag(ys); % Σ_tilda = blkdiag[Σt, 0] 17 | 18 | else 19 | mu_p = [mu_a(i-1,:),ute(i)]; 20 | var_p = blkdiag(var_a(:,:,i-1),0); 21 | 22 | end 23 | [mu_a(i,:),var_a(:,:,i)] = Gp_transition_change(mu_p, var_p,hyp,xtrc, ytr); 24 | J = J + (mu_a(i,:)-final)*Q*(mu_a(i,:)-final)'+ ute(i)*R*ute(i); 25 | 26 | end 27 | 28 | end 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/costf.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | function [J] = costf(ute,yk,Q,R,ra) 4 | vdp = VanDerPol_uncertanity(); 5 | N = size (ute,2); 6 | x = yk(end,:); 7 | final= [1,0]; 8 | J =0; 9 | for i=1:N 10 | mu_a(i,:) = vdp.f_ud(x(i,:)',ute(i),ra(i)'); %,rand(i,1)); 11 | % mu_a(i,2) = f1(x1(i),x2(i),ute(i)); %,rand(i,1)); 12 | 13 | J = J + (mu_a(i,:)-final)*Q*(mu_a(i,:)-final)'+ ute(i)*R*ute(i); 14 | 15 | x(i+1,:)=mu_a(i,:); 16 | % x2(i+1)=mu_a(i,2); 17 | 18 | end 19 | % J = sum(diag(mu_a'*Q*mu_a)) + ute*R*ute'; 20 | end 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/data/Van_pol_1.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/data/Van_pol_1.mat -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/data/Van_pol_2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/data/Van_pol_2.mat -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/data/Van_pol_3.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/data/Van_pol_3.mat -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/data/Van_pol_4.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/data/Van_pol_4.mat -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/data/Van_pol_5.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/data/Van_pol_5.mat -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/data/Van_pol_6.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/data/Van_pol_6.mat -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/data/Van_pol_uncertainty.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/data/Van_pol_uncertainty.mat -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/data/Van_pol_uncertainty1.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/data/Van_pol_uncertainty1.mat -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/data/Van_pol_uncertainty3.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/data/Van_pol_uncertainty3.mat -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/data/Van_pol_uncertainty4.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/data/Van_pol_uncertainty4.mat -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/data/Van_pol_uncertainty5.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/data/Van_pol_uncertainty5.mat -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/data/Van_pol_uncertainty6.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/data/Van_pol_uncertainty6.mat -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/results/Phase_portrait/phase_portrait_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/results/Phase_portrait/phase_portrait_1.pdf -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/results/Phase_portrait/phase_portrait_12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/results/Phase_portrait/phase_portrait_12.jpg -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/results/Phase_portrait/phase_portrait_13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/results/Phase_portrait/phase_portrait_13.jpg -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/results/Phase_portrait/phase_portrait_vector.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/results/Phase_portrait/phase_portrait_vector.pdf -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/results/Phase_portrait/pp.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/results/Phase_portrait/pp.fig -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/results/Phase_portrait/pp_1.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/results/Phase_portrait/pp_1.fig -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/results/validation/gp_valdation_w_uncert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/results/validation/gp_valdation_w_uncert.png -------------------------------------------------------------------------------- /examples/Van_der_pol_oscillator/results/validation/long_w_uncert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/examples/Van_der_pol_oscillator/results/validation/long_w_uncert.png -------------------------------------------------------------------------------- /multi_states_GP_MPC/Gp_mpc.m: -------------------------------------------------------------------------------- 1 | function [U] = Gp_mpc(xtrc,ytr,u0,N,lb,ub) 2 | % %% Intializtaion of time step & Prediction horizon 3 | 4 | xk = xtrc(:,1:end-1); 5 | uk = xtrc(:,end); 6 | yk = ytr; 7 | n = size(xtrc,1); 8 | k = 1:1:n; 9 | %% define the GP hyperparametrs & mean, cov, lik functions 10 | cov = {@covSEard}; 11 | sf = 0.1; 12 | for i =1:size(xtrc,2) 13 | ell(i,:) = 3; % setup the GP 14 | end 15 | % ell = [ell_1;ell_2;ell_3]; 16 | hyp0.cov = log([ell;sf]); 17 | mean = {@meanConst}; a = 0.1; % m(x) = a*x+b 18 | hyp0.mean = [a]; 19 | 20 | lik = {'likGauss'}; % Gauss likelihoods 21 | inf = {'infGaussLik'}; % inference algs 22 | 23 | Ncg = 100; % number of conjugate gradient steps 24 | 25 | sn = 0.2; 26 | hyp0.lik = [log(sn)]; 27 | fprintf('OPT: %s/%s\n',lik{1},inf{1}); 28 | hyp_1 = hyp0; n =size(xtrc,2)-1; 29 | %% minimization of hyperparametrs 30 | for i =1:n 31 | for j=1:5 32 | fprintf('Hyperparameters of %d state minimization iteration: %d\n',i,j); 33 | if Ncg==0 34 | hyp = hyp0; 35 | else 36 | hyp_1 = minimize(hyp_1,'gp', -Ncg, inf, mean, cov, lik, xtrc, ytr(:,i)); % opt hypers 37 | end 38 | end 39 | hyp.state(i) = hyp_1; 40 | [ymu(i), ys(i)] = gp(hyp.state(i), inf, mean, cov, lik, xtrc, ytr(:,i),xtrc(end,:)); 41 | end 42 | ys(i+1) = 0; 43 | 44 | 45 | %% cost function 46 | u0= u0*ones(1,N); %%intial input u 47 | 48 | fun = @(u0) cost(u0,xk,uk,ymu,ys,hyp,yk); 49 | 50 | %% minimization of cost function 51 | options = optimoptions('fmincon','Display','iter','Algorithm','sqp'); 52 | % options.StepTolerance=1.000000e-12; 53 | 54 | A = []; 55 | b = []; 56 | Aeq = []; 57 | beq = []; 58 | % A = 1*eye(5); 59 | % b = [3,3,3,4,5]; 60 | % fn= @(u) fq(yk(end),u,f); 61 | [U,fval] = fmincon(fun,u0,A,b,Aeq,beq,lb,ub,[],options) 62 | 63 | display('-----------------------------------------') 64 | end -------------------------------------------------------------------------------- /multi_states_GP_MPC/Gp_mpc_MS_test.m: -------------------------------------------------------------------------------- 1 | % Copyright (c) Institute of Control Systems, Hamburg University of Technology. All rights reserved. 2 | % Licensed under the GPLv3. See LICENSE in the project root for license information. 3 | % Author(s): Harshith Gowda 4 | 5 | clear all; 6 | close all; 7 | 8 | %% Intialization of States and U input as functions for data 9 | % f = @(t) sin(1*t)+0.00002*randn(size(t)); 10 | % f = @(t) exp(0.08*t)+0.00002*randn(size(t)); 11 | % g= @(t) exp(0.01*t)+0.00002*randn(size(t)); 12 | % f1 = @(t) exp(0.1*t)+0.00002*randn(size(t)); 13 | f = @(x,uk) 0.2*x+0.7*uk; 14 | f1 = @(x,uk) 0.7*x+0.2*uk; 15 | % g = @(t) 1* t+0.00002*randn(size(t)); 16 | N = 10; %prediction horizon 17 | n = 40; % no of data points 18 | k = 1:1:n; 19 | uk = randn(n,1); 20 | % uk = 1*ones(1,n); 21 | % uk = 0.05:0.05:n*0.05; 22 | x1 = 1; x2 = 1; 23 | for i=0:n-1 24 | yk(i+1,1) = f(x1(i+1),uk(i+1)); 25 | % yk(i+1,2) = f1(x2(i+1),uk(i+1)); 26 | x1(i+2) = yk(i+1,1); 27 | % x2(i+2) = yk(i+1,2); 28 | 29 | end 30 | xk = [x1(1:n)']; 31 | % xk = [x1(1:n)',x2(1:n)']; 32 | % uk = uk'; 33 | xtrc=[xk,uk]; % aug. xk,uk 34 | 35 | %% Plotting data points 36 | 37 | figure(1), hold on 38 | plot(k,yk,'Color','r','LineWidth',2) 39 | plot(k,uk,'Color','k','LineWidth',2) 40 | hold off 41 | %% input constraints 42 | u0=1*uk(end); 43 | lb = -10*ones(1,N); 44 | ub = 10*ones(1,N); 45 | 46 | %% GP-MPC 47 | for i=1:10 48 | display(i,'iteration'); 49 | [U] = Gp_mpc(xtrc,yk,u0,N,lb,ub); 50 | u0=U(1); 51 | xtrc(size(xtrc,1)+1,:) = [yk(end,:),U(1)]; 52 | % yk(size(yk,1)+1,:) = [f(yk(end,1),U(1)),f1(yk(end,2),U(1))]; 53 | yk(size(yk,1)+1,:) = [f(yk(end,1),U(1))]; 54 | display(u0,'Optimal U'); 55 | display(yk(end,:),'yk'); 56 | display(xk(end,:),'xk') 57 | display('---------------------------'); 58 | 59 | 60 | k = n:1:size(xtrc,1); 61 | figure(1), hold on 62 | plot(k,yk(n:end,:),'Color','g','LineWidth',1) 63 | plot(k,xtrc(n:end,end),'Color','b','LineWidth',1) 64 | % legend('yk1','yk2','uk','opt. Yk1','opt. Yk2','opt. Uk') 65 | hold off 66 | 67 | end 68 | projected_yk = yk(n:end,:) 69 | optimal_uk = xtrc(n:end,end) 70 | 71 | 72 | 73 | % xu=yk(end); 74 | % 75 | % for j=1:N 76 | % yuk(j,1) = f(xu(j),U(j)); 77 | % xu(j+1) = yuk(j,1); 78 | % end 79 | % figure(2), hold on 80 | % fprintf('%g ',yuk) 81 | % plot(n+1:1:n+1*N,yuk,'Color','y') 82 | % plot(n+1:1:n+1*N,U,'Color','k') 83 | % 84 | % % legend('yk','uk','opt. Yk','opt. Uk') 85 | % hold off 86 | 87 | -------------------------------------------------------------------------------- /multi_states_GP_MPC/KernalCov.m: -------------------------------------------------------------------------------- 1 | function [K] = KernalCov(x,z,hyp) 2 | 3 | 4 | % hyp= log([ell; sf]); 5 | covfunc = {@covSEard}; 6 | 7 | % disp('K = feval(covfunc{:}, hyp.cov, x);') 8 | K = feval(covfunc{:},hyp,x,z); 9 | 10 | end 11 | 12 | -------------------------------------------------------------------------------- /multi_states_GP_MPC/cost.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | function [J] = cost(ute,xtr,utr,ymu,ys,hyp,ytr) 4 | N = size (ute,2); 5 | xtrc = [xtr,utr]; 6 | for i=1:N 7 | 8 | if i==1 9 | mu_p = [ymu,ute(i)]; % µ_tilmda = [µt,ut] 10 | var_p = diag(ys); % Σ_tilda = blkdiag[Σt, 0] 11 | else 12 | mu_p = [mu_a(i-1,:),ute(i)]; 13 | var_p = blkdiag(var_a(:,:,i-1),0); 14 | end 15 | 16 | [mu_a(i,:),var_a(:,:,i)] = Gp_transition_change(mu_p, var_p,hyp,xtrc, ytr); 17 | 18 | end 19 | % disp("--------") 20 | Q=1*eye(N); 21 | R=0.1*eye(N); 22 | J = sum(diag(mu_a'*Q*mu_a)) + ute*R*ute'; 23 | 24 | end 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /multi_states_GP_MPC/untitled.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/multi_states_GP_MPC/untitled.fig -------------------------------------------------------------------------------- /multi_states_GP_MPC/untitled2.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/multi_states_GP_MPC/untitled2.fig -------------------------------------------------------------------------------- /papers/Data-Efficient Reinforcement Learning with Probabilistic MPC.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/papers/Data-Efficient Reinforcement Learning with Probabilistic MPC.pdf -------------------------------------------------------------------------------- /papers/DeiHubHan09.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/papers/DeiHubHan09.pdf -------------------------------------------------------------------------------- /papers/Gp_for data-effecient_leaning_in_robotics and control.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/papers/Gp_for data-effecient_leaning_in_robotics and control.pdf -------------------------------------------------------------------------------- /papers/NIPS-2002-gaussian-process-priors-with-uncertain-inputs-application-to-multiple-step-ahead-time-series-forecasting-Paper.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/papers/NIPS-2002-gaussian-process-priors-with-uncertain-inputs-application-to-multiple-step-ahead-time-series-forecasting-Paper.pdf -------------------------------------------------------------------------------- /papers/PILCO.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/papers/PILCO.pdf -------------------------------------------------------------------------------- /papers/blockdia.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Harshithgowdasm/GP_MPC/e63779069e98131525c42534765a74872ac819a4/papers/blockdia.png -------------------------------------------------------------------------------- /single_state_GP_MPC/Gp_mpc.m: -------------------------------------------------------------------------------- 1 | % Copyright (c) Institute of Control Systems, Hamburg University of Technology. All rights reserved. 2 | % Licensed under the GPLv3. See LICENSE in the project root for license information. 3 | % Author(s): Harshith Gowda 4 | 5 | 6 | 7 | function [U] = Gp_mpc(xtrc,yk,u0,N,lb,ub) 8 | %% states and input 9 | xk = xtrc(:,1); 10 | uk = xtrc(:,2); 11 | n = size(xtrc,1); 12 | k = 1:1:n; 13 | %% define the GP hyperparametrs & mean, cov, lik functions 14 | cov = {@covSEard}; sf = 0.9; ell_1 = 1; 15 | ell_2 = 1; % setup the GP 16 | ell = [ell_1;ell_2]; 17 | hyp0.cov = log([ell;sf]); 18 | mean = {@meanConst}; a = 0.3; % m(x) = a*x+b 19 | hyp0.mean = [a]; 20 | 21 | lik = {'likGauss'}; % Gauss likelihoods 22 | inf = {'infGaussLik'}; % inference algs 23 | 24 | Ncg = 50; % number of conjugate gradient steps 25 | 26 | sn = 0.2; 27 | hyp0.lik = [log(sn)]; 28 | fprintf('OPT: %s/%s\n',lik{1},inf{1}); 29 | 30 | %% minimization of hyperparametrs 31 | if Ncg==0 32 | hyp = hyp0; 33 | else 34 | hyp = minimize(hyp0,'gp', -Ncg, inf, mean, cov, lik, xtrc, yk); % opt hypers 35 | end 36 | 37 | %% prediction of xk+1 using GP 38 | [ymu2, ys2] = gp(hyp, inf, mean, cov, lik, xtrc, yk,xtrc(end,1:2) ); % predict % Xtr = [X,U) 39 | 40 | %% cost function 41 | u0= u0*ones(1,N); %%intial input u 42 | 43 | fun = @(u0) cost(u0,xk,uk,ymu2,ys2,hyp,yk); 44 | 45 | %% minimization of cost function 46 | options = optimoptions('fmincon','Display','iter','Algorithm','sqp'); 47 | % options.StepTolerance=1.000000e-12; 48 | 49 | A = []; 50 | b = []; 51 | Aeq = []; 52 | beq = []; 53 | % A = 1*eye(5); 54 | % b = [3,3,3,4,5]; 55 | [U,fval] = fmincon(fun,u0,A,b,Aeq,beq,lb,ub,[],options) 56 | 57 | end -------------------------------------------------------------------------------- /single_state_GP_MPC/Gp_mpc_test.m: -------------------------------------------------------------------------------- 1 | % Copyright (c) Institute of Control Systems, Hamburg University of Technology. All rights reserved. 2 | % Licensed under the GPLv3. See LICENSE in the project root for license information. 3 | % Author(s): Harshith Gowda 4 | 5 | 6 | clear all; 7 | close all; 8 | 9 | %% Intialization of States and U input as functions for data 10 | N = 10; %prediction horizon 11 | n = 40; % no of data points 12 | k = 1:1:n; 13 | uk = randn(n,1); 14 | % uk = 1*ones(1,n); 15 | % uk = 0.05:0.05:n*0.05; 16 | x = 1; 17 | % f=@(x,u) -0.5*(1 + x)*cos(x)+1.8*cos(x) + 1*u; 18 | f = @(x,u) 0.99*x + 0.1*u; 19 | % f=@(x,u) 1*(-0.5*(1 + x)*x+0.8*x + 2*u); 20 | % f = @(x,u) 1*(x-u)^2+0.1*(x)^2;%+0.0002*randn(1); 21 | for i=0:n-1 22 | yk(i+1,1) = f(x(i+1),uk(i+1)); 23 | x(i+2) = yk(i+1,1); 24 | end 25 | xk = x(1:n)'; 26 | % uk = uk'; 27 | xtrc=[xk,uk]; % aug. xk,uk 28 | 29 | %% ploting data points 30 | 31 | figure(1), hold on 32 | plot(k,yk,'Color','r','LineWidth',2); 33 | plot(k,uk,'Color','k','LineWidth',2); 34 | hold off 35 | 36 | %% Model Learning & Generating Controller(MPC) 37 | u0=1*uk(end); 38 | lb = -3*ones(1,N); 39 | ub = 3*ones(1,N); 40 | 41 | for i=1:5 42 | display(i,'iteration'); 43 | [U] = Gp_mpc(xtrc,yk,u0,N,lb,ub); 44 | u0=U(1); 45 | xtrc(size(xtrc,1)+1,1:2) = [yk(end),U(1)]; 46 | yk(size(yk,1)+1) = f(yk(end),U(1)); 47 | 48 | display(u0,'Optimal U'); 49 | display(yk(end),'yk'); 50 | display(xk(end),'xk'); 51 | display('---------------------------'); 52 | 53 | k = n:1:size(xtrc,1); 54 | figure(1), hold on 55 | plot(k,yk(n:end),'Color','g','LineWidth',1); 56 | plot(k,xtrc(n:end,2),'Color','b','LineWidth',1); 57 | legend('yk','uk','opt. Yk','opt. Uk'); 58 | hold off 59 | 60 | end 61 | projected_yk = yk(n:end) 62 | optimal_uk = xtrc(n:end,2) -------------------------------------------------------------------------------- /single_state_GP_MPC/KernalCov.m: -------------------------------------------------------------------------------- 1 | function [K] = KernalCov(x,z,hyp) 2 | 3 | % n = 20; 4 | % disp('x = gpml_randn(0.3, n, 1);') 5 | % x =gpml_randn(0.3, n, 1); 6 | % ell = 0.4; 7 | % sf = 1; 8 | % z = 2; 9 | 10 | % hyp= log([ell; sf]); 11 | covfunc = {@covSEard}; 12 | 13 | % disp('K = feval(covfunc{:}, hyp.cov, x);') 14 | K = feval(covfunc{:},hyp,x,z); 15 | 16 | end 17 | 18 | -------------------------------------------------------------------------------- /single_state_GP_MPC/cost.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | function [J] = cost(ute,xtr,utr,ymu2,ys2,hyp,ytr) 4 | N = size (ute,2); 5 | X_tilda = [xtr,utr]; 6 | 7 | %% long term prediction by Moment Matching 8 | for i=1:N 9 | if i==1 10 | mu_p = [ymu2,ute(i)]; % µ_tilmda = [µt,ut] 11 | var_p = blkdiag(ys2,0); % Σ_tilda = blkdiag[Σt, 0] 12 | else 13 | mu_p = [mu_up(i-1),ute(i)]; 14 | var_p = blkdiag(var_up(i-1),0); 15 | end 16 | Kaa = KernalCov(X_tilda,X_tilda,hyp.cov); 17 | [mu_up(i),var_up(i)] = Gp_transition_change(mu_p, var_p,Kaa,exp(hyp.lik), X_tilda, ytr(1:end),hyp.cov(1:end-1),hyp.cov(end),utr); 18 | 19 | end 20 | 21 | %% define cost 22 | 23 | Q=10*eye(N); 24 | R=0.01*eye(N); 25 | J = mu_up*Q*mu_up'+ute*R*ute'; 26 | end 27 | 28 | 29 | 30 | --------------------------------------------------------------------------------