function [model] = regress_ridge_fw_tr(X,Y,param)
% feature-weighted ridge regression. Can set separate regularization for
% each ft according to prior knowledge. Can also set weight for each sample.
% Please see classf_lr_fw_tr for meanings of the parameters

% Please refer to Ke Yan et al., Improving the transfer ability of 
% prediction models for electronic noses, Sensors and Actuators	B: Chemical, 2015
%	Copyright 2015 YAN Ke, Tsinghua Univ. http://yanke23.tk, xjed09@gmail.com


[nSmp,nFt] = size(X);

% default parameters
ftPenal = ones(1,nFt); % penalization weight of each feature. The larger,
% the feature will be less relied in the model
smpWt = ones(nSmp,1); % importance of each sample. The larger the more important
lambda = 1; % regularization parameter

defParam

W = diag(smpWt);

b0 = mean(Y);
Yz = Y-b0; % shift Y
[Xz, muZ, sigmaZ] = zscore(X); % scale X
R = diag(ftPenal)*lambda; % Tikhonov Regularization Matrix
b = (Xz'*W*Xz + R) \ (Xz' *W* Yz);

% scale and shift back
bs = b./sigmaZ';
model.b = [b0-muZ*bs; bs];

end