function gpdisc(call)

% This is the machine-generated representation of a MATLAB object

% and its children.  Note that handle values may change when these

% objects are re-created. This may cause problems with some callbacks.

% The command syntax may be supported in the future, but is currently 

% incomplete and subject to change.

%

% To re-open this system, just type the name of the m-file at the MATLAB

% prompt. The M-file and its associtated MAT-file must be on your path.



if nargin==0, call='start'; end



switch call



case 'start'



load gpdisc                            



a = figure('Color',[0.8 0.8 0.8], ...

	'Colormap',mat0, ...

	'Name','Learning about p using discrete models', ...

	'PointerShapeCData',mat1, ...

	'Position',[-2 30 1028 702], ...

	'Tag','Fig1');

b = axes('Parent',a, ...

	'Box','on', ...

	'ButtonDownFcn','gpdisc(''adjust_bars'')', ...

	'CameraUpVector',[0 1 0], ...

	'CameraUpVectorMode','manual', ...

	'Color',[1 1 1], ...

	'ColorOrder',mat2, ...

	'NextPlot','add', ...

	'Position',[0.116803 0.697039 0.727459 0.15262], ...

	'Tag','prior plot', ...

	'UserData',mat3, ...

	'XColor',[0 0 0], ...

	'XLim',[-0.1 1.1], ...

	'XLimMode','manual', ...

	'YColor',[0 0 0], ...

	'YLim',[0 0.181818], ...

	'YLimMode','manual', ...

	'ZColor',[0 0 0]);

c = text('Parent',b, ...

	'Color',[0 0 0], ...

	'HandleVisibility','callback', ...

	'HorizontalAlignment','center', ...

	'Position',[0.499196 -0.0454545 0], ...

	'String','p', ...

	'Tag','Text1', ...

	'VerticalAlignment','cap');

set(get(c,'Parent'),'XLabel',c);

c = text('Parent',b, ...

	'Color',[0 0 0], ...

	'HandleVisibility','callback', ...

	'HorizontalAlignment','center', ...

	'Position',[-0.169973 0.0917666 0], ...

	'Rotation',90, ...

	'String','prob', ...

	'Tag','Text2', ...

	'VerticalAlignment','baseline');

set(get(c,'Parent'),'YLabel',c);

c = text('Parent',b, ...

	'Color',[0 0 0], ...

	'HandleVisibility','callback', ...

	'HorizontalAlignment','right', ...

	'Position',[-0.293834 0.362778 0], ...

	'Tag','Text3', ...

	'Visible','off');

set(get(c,'Parent'),'ZLabel',c);

c = text('Parent',b, ...

	'Color',[0 0 0], ...

	'HandleVisibility','callback', ...

	'HorizontalAlignment','center', ...

	'Position',[0.499196 0.198113 0], ...

	'String','PRIOR DISTRIBUTION', ...

	'Tag','Text4', ...

	'VerticalAlignment','bottom');

set(get(c,'Parent'),'Title',c);

c = line('Parent',b, ...

	'Color',[0 0 1], ...

	'Tag','pbars', ...

	'UserData',mat4, ...

	'XData',mat5, ...

	'YData',mat6);

b = axes('Parent',a, ...

	'Units','points', ...

	'Box','on', ...

	'CameraUpVector',[0 1 0], ...

	'CameraUpVectorMode','manual', ...

	'Color',[1 1 1], ...

	'ColorOrder',mat7, ...

	'Position',[70.7586 129.724 466.759 75.7241], ...

	'Tag','post plot', ...

	'UserData',mat8, ...

	'XColor',[0 0 0], ...

	'XLim',[-0.1 1.1], ...

	'XLimMode','manual', ...

	'YColor',[0 0 0], ...

	'YLim',[0 0.255409], ...

	'YLimMode','manual', ...

	'ZColor',[0 0 0]);

c = line('Parent',b, ...

	'Color',[0 0 1], ...

	'Tag','post bars', ...

	'UserData',0, ...

	'XData',mat9, ...

	'YData',mat10);

c = text('Parent',b, ...

	'Color',[0 0 0], ...

	'HandleVisibility','callback', ...

	'HorizontalAlignment','center', ...

	'Position',[0.5 -0.0559367 0], ...

	'String','p', ...

	'Tag','Text5', ...

	'VerticalAlignment','cap');

set(get(c,'Parent'),'XLabel',c);

c = text('Parent',b, ...

	'Color',[0 0 0], ...

	'HandleVisibility','callback', ...

	'HorizontalAlignment','center', ...

	'Position',[-0.155126 0.127704 0], ...

	'Rotation',90, ...

	'String','prob', ...

	'Tag','Text6', ...

	'VerticalAlignment','baseline');

set(get(c,'Parent'),'YLabel',c);

c = text('Parent',b, ...

	'Color',[0 0 0], ...

	'HandleVisibility','callback', ...

	'HorizontalAlignment','right', ...

	'Position',[-0.281358 1.03958 0], ...

	'Tag','Text7', ...

	'Visible','off');

set(get(c,'Parent'),'ZLabel',c);

c = text('Parent',b, ...

	'Color',[0 0 0], ...

	'HandleVisibility','callback', ...

	'HorizontalAlignment','center', ...

	'Position',[0.5 0.275462 0], ...

	'String','POSTERIOR DISTRIBUTION', ...

	'Tag','Text8', ...

	'VerticalAlignment','bottom');

set(get(c,'Parent'),'Title',c);

b = uicontrol('Parent',a, ...

	'Units','points', ...

	'Callback','gpdisc(''get_data'')', ...

	'Position',[127.241 240.828 49.0345 19.2414], ...

	'String','Get Data', ...

	'Tag','get data', ...

	'UserData',[0 0]);

b = uicontrol('Parent',a, ...

	'Units','points', ...

	'BackgroundColor',[0.752941 0.752941 0.752941], ...

	'Position',[266.276 243.931 54 15.5172], ...

	'String','s=0 f=0', ...

	'Style','text', ...

	'Tag','text_data', ...

	'UserData',[1 1]);

b = uicontrol('Parent',a, ...

	'Units','points', ...

	'Callback','gpdisc(''update'')', ...

	'Position',[385.448 241.448 50.2759 19.8621], ...

	'String','Update', ...

	'Tag','Pushbutton1', ...

	'UserData',[23 100]);

b = uicontrol('Parent',a, ...

	'Units','points', ...

	'Callback','gpdisc(''define_prior'')', ...

	'Position',[248.897 399.724 107.379 16.1379], ...

	'String','Define Prior Grid', ...

	'Tag','define prior', ...

	'UserData',[0 1 11]);

b = uicontrol('Parent',a, ...

	'Units','points', ...

	'BackgroundColor',[0.752941 0.752941 0.752941], ...

	'Position',[369.931 36 89.3793 50.8966], ...

	'Style','text', ...

	'Tag','summaries');

b = uicontrol('Parent',a, ...

	'Units','points', ...

	'BackgroundColor',[1 1 1], ...

	'Position',[102.414 33.5172 137.793 54], ...

	'Style','listbox', ...

	'Tag','list probs', ...

	'Value',1);

b = uicontrol('Parent',a, ...

	'Units','points', ...

	'BackgroundColor',[0.752941 0.752941 0.752941], ...

	'Position',[119.793 92.4828 94.9655 12.4138], ...

	'String','PROBABILITY TABLE', ...

	'Style','text', ...

	'Tag','StaticText1');

b = uicontrol('Parent',a, ...

	'Units','points', ...

	'Position',[372.414 93.7241 81.3103 11.1724], ...

	'String','SUMMARIES', ...

	'Style','text', ...

	'Tag','StaticText1');



case 'define_prior'



prompt={'Enter smallest value for p::','Enter largest value for p:',...

   'Enter number of values:'};

def={'','',''};

title='Define prior grid';

lineNo=1;

answer=inputdlg(prompt,title,lineNo,def);

lo=eval(answer{1}); hi=eval(answer{2}); n=eval(answer{3}); w=hi-lo;

set(gco,'userdata',[lo hi n]);



p=linspace(lo,hi,n);

prior=1/n+0*p;



t=findobj('tag','pbars');

X=[p;p;p]; Y=[0*prior;prior;0*prior+NaN];

set(t,'ydata',Y(:));

set(t,'xdata',X(:));



t=findobj('tag','prior plot');

set(t,'xlim',[lo-w/(n-1) hi+w/(n-1)]);

set(t,'ylim',[0 2*prior(1)]);

set(t,'userdata',[p;prior]);



t=findobj('tag','post bars');

  t2=findobj('tag','post plot');

  pdist=get(t2,'userdata');

  p=pdist(1,:);

  y=[0*p;0*p;NaN+0*p];

  set(t,'ydata',y(:))

  set(t2,'userdata',[p;0*p]);



 set(findobj('tag','summaries'),'string','')

  set(findobj('tag','list probs'),'string','')



case 'get_data'



prompt={'Enter the number of successes:','Enter the number of failures:'};

def={'',''};

title='Input Binomial Data';

lineNo=1;

answer=inputdlg(prompt,title,lineNo,def);



if prod(size(answer))==2

if (length(answer{1})>0)&(length(answer{2})>0)==1

set(gco,'userdata',[eval(answer{1}),eval(answer{2})]) 

t=findobj('tag','text_data');

set(t,'string',['s=' answer{1} ' f=' answer{2}])

t=findobj('tag','post bars');

flag=get(t,'userdata');

if flag==1

  set(t,'userdata',0)

  t2=findobj('tag','post plot');

  pdist=get(t2,'userdata');

  p=pdist(1,:);

  y=[0*p;0*p;NaN+0*p];

  set(t,'ydata',y(:))

  set(t2,'userdata',[p;0*p]);

  set(findobj('tag','summaries'),'string','')

  set(findobj('tag','list probs'),'string','')

end

end

end



case 'update'



t=findobj('tag','prior plot');

pdist=get(t,'userdata');

t=findobj('tag','get data');

data=get(t,'userdata');

post=p_disc(pdist(1,:),pdist(2,:),data);

x=ones(3,1)*pdist(1,:);

y=[0*post;post;NaN+0*post;];

t=findobj('tag','post bars');

set(t,'xdata',x(:))

set(t,'ydata',y(:))

set(t,'userdata',1)

t=findobj('tag','post plot');

t2=findobj('tag','define prior');

lohi=get(t2,'userdata');

lo=lohi(1); hi=lohi(2); w=hi-lo; n=lohi(3);

set(t,'xlim',[lo-w/(n-1) hi+w/(n-1)])

set(t,'ylim',[0 max(post)*1.1])

postdist=[pdist(1,:);post];

set(t,'userdata',postdist);



clear

t=findobj('tag','post plot');

S=c_summ(get(t,'userdata'));

set(findobj('tag','summaries'),'string',S)



set(findobj('tag','list probs'),'string', ...

   num2str(get(t,'userdata')','%5.3f  %5.3f'));



case 'adjust_bars'



t=findobj('tag','define prior');

lohi=get(t,'userdata');

lo=lohi(1); hi=lohi(2); n=lohi(3);

p=get(gca,'currentpoint');                           % reads point at mouseclick

dx=(hi-lo)/(n-1);

x=round((p(1,1)-lo)/dx+1);                           % finds the closest value of p

y=p(1,2);

t=findobj('tag','prior plot');

pdist=get(t,'userdata');

pr=pdist(2,:);

pr(x)=y;                                             % put new value of p in probability vector

pr=pr/sum(pr);                                       % normalizes

Y=[0*pr;pr;0*pr+NaN];



prior_handle=findobj('tag','pbars');

set(t,'ylim',[0 max(pr)*1.2])

set(prior_handle,'ydata',Y(:));                      % resets current ydata of line

pdist=get(t,'userdata');

p=pdist(1,:); pdist=[p;pr];

set(gca,'userdata',pdist);



t=findobj('tag','post bars');

flag=get(t,'userdata');

if flag==1

   set(t,'userdata',0)

  x=[p;p;NaN+p];

  y=[0*p;0*p;NaN+0*p];

  set(t,'xdata',x(:))

  set(t,'ydata',y(:))

  t=findobj('tag','post plot');

  set(t,'userdata',[pdist(1,:);0*p]);

  set(findobj('tag','summaries'),'string','')

  set(findobj('tag','list probs'),'string','')

end



end



function S=c_summ(postdist)



mn=sum(postdist(1,:).*postdist(2,:));

sd=sqrt(sum(postdist(1,:).^2.*postdist(2,:))-mn^2);

set=disc_int(postdist',.9); lo=min(set); hi=max(set);

t=findobj('tag','summaries');

S=strvcat(['Mean = ' num2str(mn)],...

          ['St dev = ' num2str(sd)], ...

          ['90% interval: (' num2str(lo) ',' num2str(hi) ')']);       



