function [mean_generalisability, std_generalisability] = find_generalisability(model, n_iterations, verbose)
% FIND_GENERALISABILITY: Find the generalisability of a model.
%
% Code written by Katherine Smith, 2003
%
%    GENERAL
%
%      [mean_generalisability, std_generalisability] = 
%       find_generalisability(model, n_iterations, verbose)
%
%    INPUT/S
%
%
%      -model:
%           The model for which generalisability is calculated.
%
%      -n_iteration:
%           Number of iterations to control the complexity of
%           this function?
%
%      -verbose:
%           Verbose flag.
%           
%    OUTPUT/S
%
%      -mean_generalisability:
%           The mean generalisability.
%
%      -mean_generalisability:
%           The standard deviation (?) of the generalisability values?
%
%    PENDING WORK
%
%      -Resolve meaning of inputs and outputs.
%      -Add more comments to code.
%
%    KNOWN BUG/S
%
%      -None.
%
%    COMMENT/S
%
%      -Smith: Attempt to reconstruct an unseen example 
%              and measure distance from the example
%              (mean squared difference).
%
%    RELATED FUNCTION/S
%
%      FIND_SPECIFICITY
%
%    ABOUT
%
%      -Created:     November 23rd, 2003
%      -Last update: Novermber 25th, 2003
%      -Revision:    0.0.2
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================

% search parameters with simplex
% hack in white width for now

verbose='off';
    % RSS, added 8th December 2003 to get eval_1d_app_model_obj_fn to run

white_width = 0.2;
%subs = figure;
for i=1:n_iterations
  [imagelist,example,points, his, los] = make_1d_images(1, size(model.intensity_model.pcs(:,1),1), white_width);
  [params, scores(i)] = fminsearch(@eval_msd,zeros(1,size(model.params,2)),optimset('Display',verbose,'TolX',0.001,'TolFun',0.001),model,example);
%  scores(i)
%  difference_image = abs(example - construct_model_example(params, model, 1:50));
%  figure(subs),hold on,subplot(5,1,i),plot(difference_image),gca,title(['score: ' num2str(scores(i))]);
end
mean_generalisability = mean(scores);
std_generalisability = std(scores);