PSY8219 : Week 12
Homework 10 Due Wednesday at 1pm
Homework 12 Due Wednesday November 28 at 1pm
Readings for Today
Attaway Chapter 13 (section on GUIs)
Readings for Next Week
None
Graphical User Interfaces (GUIs)
http://www.mathworks.com/help/pdf_doc/matlab/buildgui.pdf
GUIs in Matlab
let you get inputs from users or interact with graphical objects
Two ways to create GUIs: 1) GUIDE, the MATLAB Graphical User Interface Development Environment, provides interactive tools for creating GUIs 2) programming directly using GUI functions
GUIs in Matlab
GUIs are built within Matlab figures
fh=figure('Visible','off','Position',[11sxsy],...'Color','white','Name','AnexampleGUI');
size of figureinvisible while we create
figure handle
Formatting
You need to realize the internally Matlab has a hierarchical representation of figures.
root
figure
GUIobjects
axes
plotobjects
otherobjects
GUIs in Matlab
GUIs are built within Matlab figures
fh=figure('Visible','off','Position',[11sxsy],...'Color','white','Name','AnexampleGUI');
.
.
.
movegui(fh,'center');set(fh,'Visible','on');
move to the center of the screen
make the figure visible
GUIs in Matlab
uicontrol is one key routine for building GUIs
uicontrol(fh,'Style','text','Units','Normalized',... 'Position',[.05.80.2.1],'FontSize',18,...'String','SubjectName','BackgroundColor','white');
GUIs in Matlab
uicontrol is one key routine for building GUIs
uicontrol(fh,'Style','text','Units','Normalized',... 'Position',[.05.80.2.1],'FontSize',18,...'String','SubjectName','BackgroundColor','white');
figure handle
GUIs in Matlab
uicontrol is one key routine for building GUIs
uicontrol(fh,'Style','text','Units','Normalized',... 'Position',[.05.80.2.1],'FontSize',18,...'String','SubjectName','BackgroundColor','white');
what kind of GUI control
'text','sliders','radiobutton','popupmenu',etc.
GUIs in Matlab
uicontrol is one key routine for building GUIs
uicontrol(fh,'Style','text','Units','Normalized',... 'Position',[.05.80.2.1],'FontSize',18,...'String','SubjectName','BackgroundColor','white');
use normalized (percent) units
GUIs in Matlab
uicontrol is one key routine for building GUIs
uicontrol(fh,'Style','text','Units','Normalized',... 'Position',[.05.80.2.1],'FontSize',18,...'String','SubjectName','BackgroundColor','white');
where this text will go and how big it is 5% on x, 80% on y, 20% high, 10% wide
GUIs in Matlab
uicontrol is one key routine for building GUIs
uicontrol(fh,'Style','text','Units','Normalized',... 'Position',[.05.80.2.1],'FontSize',18,...'String','SubjectName','BackgroundColor','white');
this is the value of the text to display
GUIs in Matlab
uicontrol is one key routine for building GUIs
snameh=uicontrol(fh,'Style','edit',... 'Units','Normalized',... 'Position',[.30.83.4.1],'FontSize',18);
editable text box
GUIs in Matlab
uicontrol is one key routine for building GUIs
snameh=uicontrol(fh,'Style','edit',... 'Units','Normalized',... 'Position',[.30.83.4.1],'FontSize',18);
control handle, important to read out value
GUIs in Matlab
uicontrol is one key routine for building GUIs
snameh=uicontrol(fh,'Style','edit',... 'Units','Normalized',... 'Position',[.30.83.4.1],'FontSize',18);...sname=get(snameh,'String');
read out value typed in
GUIs in Matlab
uicontrol is one key routine for building GUIs
llh=uicontrol(fh,'Style','slider',...'Units','Normalized','Position',[.42.24.50.2],... 'Min',minl,'Max',maxl,'Value',minl,...'SliderStep',[.1.2],'CallBack',@llcallback);
control handle, important to read out value
GUIs in Matlab
uicontrol is one key routine for building GUIs
llh=uicontrol(fh,'Style','slider',...'Units','Normalized','Position',[.42.24.50.2],... 'Min',minl,'Max',maxl,'Value',minl,...'SliderStep',[.1.2],'CallBack',@llcallback);
slider
GUIs in Matlab
uicontrol is one key routine for building GUIs
llh=uicontrol(fh,'Style','slider',...'Units','Normalized','Position',[.42.24.50.2],... 'Min',minl,'Max',maxl,'Value',minl,...'SliderStep',[.1.2],'CallBack',@llcallback);
min, max, and starting value
GUIs in Matlab
uicontrol is one key routine for building GUIs
llh=uicontrol(fh,'Style','slider',...'Units','Normalized','Position',[.42.24.50.2],... 'Min',minl,'Max',maxl,'Value',minl,...'SliderStep',[.1.2],'CallBack',@llcallback);
function called any time control element used
GUIs in Matlab
uicontrol is one key routine for building GUIs
llh=uicontrol(fh,'Style','slider',...'Units','Normalized','Position',[.42.24.50.2],... 'Min',minl,'Max',maxl,'Value',minl,...'SliderStep',[.1.2],'CallBack',@llcallback);
ll=round(get(llh,'Value'));
read out the number based on slider position
GUIs in Matlab
>>get(llh) BackgroundColor=[0.9294120.9294120.929412] Callback=[(1by1)function_handlearray] CData=[] Enable=on Extent=[000.006666670.008] FontAngle=normal FontName=Helvetica FontSize=[10] FontUnits=points FontWeight=normal ForegroundColor=[000]… Parent=[1] Selected=off SelectionHighlight=on Tag= Type=uicontrol UIContextMenu=[] UserData=[] Visible=on
justlikefigurehandlesyoucangetandsetparticularvalues
GUIs in Matlab
llh=uicontrol(fh,'Style','slider',...'Units','Normalized','Position',[.42.24.50.2],... 'Min',minl,'Max',maxl,'Value',minl,...'SliderStep',[.1.2],'CallBack',@llcallback);llth=uicontrol(fh,'Style','text',...'Units','Normalized','Position',[.30.35.08.1],... 'FontSize',18,'String',num2str(minl),...'BackgroundColor','white');
functionllcallback(source,eventdata) ll=get(llh,'Value'); set(llth,'String',num2str(round(ll)));end
slider
showvalueofslider
GUIs in Matlab
llh=uicontrol(fh,'Style','slider',...'Units','Normalized','Position',[.42.24.50.2],... 'Min',minl,'Max',maxl,'Value',minl,...'SliderStep',[.1.2],'CallBack',@llcallback);llth=uicontrol(fh,'Style','text',...'Units','Normalized','Position',[.30.35.08.1],... 'FontSize',18,'String',num2str(minl),...'BackgroundColor','white');
functionllcallback(source,eventdata) ll=get(llh,'Value'); set(llth,'String',num2str(round(ll)));end
getvalueofslider
GUIs in Matlab
llh=uicontrol(fh,'Style','slider',...'Units','Normalized','Position',[.42.24.50.2],... 'Min',minl,'Max',maxl,'Value',minl,...'SliderStep',[.1.2],'CallBack',@llcallback);llth=uicontrol(fh,'Style','text',...'Units','Normalized','Position',[.30.35.08.1],... 'FontSize',18,'String',num2str(minl),...'BackgroundColor','white');
functionllcallback(source,eventdata) ll=get(llh,'Value'); set(llth,'String',num2str(round(ll)));end updatetextdisplayofvalue
GUIs in Matlab
once created a GUI is a standalone object – you need to interrogate it explicitly via its handles, or let it impact your program via its callback functions
MainProgram GUI
usehandlestoaccessGUIdata
GUIcanchangeglobalvariables
GUIs in Matlab
Week12_GUI.m
keeps GUI open until all data entered and user presses OK
Week12_Figure.m
creates a GUI that is simply closed by clicking it closed in principle, you could essentially create something that acted like a full-blown program with menus and multiple windows all spawned by callback functions attached to GUI elements
Optimization and Curve Fitting
LinearRegressionModel
vs
NonlinearStatistical,Cognitive,NeuralModel
slopeandinterceptparametersarecalculatedusingclosed-formmathematicalexpressions
modelparameterscanbeestimatedusingavarietyofsearch/optimizationapproaches
Week12_Figure.m
imagine trying to fit data by adjusting all the parameters by hand
motion coherence
accu
racy
fittingapsychometricfunctiontoobserveddata
whatvaluesofα, β, γ, λ providethebestaccountoftheobserveddata?
P(correct | x) = γ + (1− λ − γ )F(x;α ,β )F(x;α ,β ) = 1− exp(−(x / β )α )
motion coherence
accu
racy
e.g.,whatvalueofcoherencegiveschanceperformance
fittingapsychometricfunctiontoobserveddata
motion coherence
accu
racy
luminance #1
luminance #2
motion coherence
luminance #1
luminance #2
isthesamepsychometricfunctionobservedwhenanothervariableismanipulated(ornot)?
isthethresholdsignificantlyhigherinoneconditionthantheother?
amount of learning (n)
resp
onse
tim
e
fitapowerlawfunctiontoalearningcurve
RT (n) = A + Bn−C
amount of learning (n)
resp
onse
tim
e
isapowerlaworanexponentiallawabetterfunction?
vs.RT (n) = A + Bn−C RT (n) = A + Be−C (n−1)
Zhang,W.,&Luck,S.J.(2008).Discretefixed-resolutionrepresentationsinvisualworkingmemory.Nature,453,233-235.
usedtocharacterizeVWMinagivencondition
probabilityanitemisstoredinVWM
fidelityofthememory
meancontentofthememory
Prob(x | µ,σ ,Pm ) = Pmecos(x−µ )/σ
2π I0 (1 /σ )+ (1− Pm )
12π
µ,σ ,Pm
StochasticAccumulationofEvidenceModels
perceptual processing
time
motorresponse
time
z
adrift
TR TM
Different kinds of models
• purely mathematical / statistical models – nonlinear regression
– power law of learning
– psychometric function
• hybrid mathematical / process model – mixture models of power law functions
– mixture model of VWM
• process / mechanistic model (cognitive or neural) – diffusion model of perceptual decision making
– temporal context model of memory
optimizationandparameterestimation
model
optimizationandparameterestimation
model
parameters
optimizationandparameterestimation
model
parameters
prediction
optimizationandparameterestimation
model
parameters
prediction data≡
optimizationandparameterestimation
model
parameters
prediction data≡
weneedtofindparametersthatcausethemodeltomakepredictionsthatmatchthedata
optimizationandparameterestimation
social,behavioral,orneuralprocess
parameters
unknownandunobservable observable
data
optimizationandparameterestimation
social,behavioral,orneuralprocess
parameters data
modelofthatprocess
observableunknownandunobservable
hypothesized
perhapsoneofseveralcompetingmodelsbeingconsidered
optimizationandparameterestimation
social,behavioral,orneuralprocess
parameters data
modelofthatprocess predictionparameters
observableunknownandunobservable
hypothesized
generated
optimizationandparameterestimation
social,behavioral,orneuralprocess
parameters data
modelofthatprocess predictionparameters
observableunknownandunobservable
hypothesized
generated
maximizethecorrespondence
minimizethediscrepancy
optimizationandparameterestimation
social,behavioral,orneuralprocess
parameters data
modelofthatprocess predictionparameters
observableunknownandunobservable
hypothesized
byadjustingtheseparameters
maximizethecorrespondence
minimizethediscrepancy
generated
optimizationandparameterestimation
social,behavioral,orneuralprocess
parameters data
modelofthatprocess predictionparameters
observableunknownandunobservable
hypothesized
byadjustingtheseparameters
maximizethecorrespondence
minimizethediscrepancy
generated
ifwe’veoptimizedcompetingmodels,wecanstatisticallydeterminewhethersomemodelspredictbetterthanothers
optimizationandparameterestimation
social,behavioral,orneuralprocess
parameters data
modelofthatprocess predictionparameters
observableunknownandunobservable
hypothesized
byadjustingtheseparameters
maximizethecorrespondence
minimizethediscrepancy
generated
ifwe’veoptimizedcompetingmodels,wecanstatisticallydeterminewhethersomemodelspredictbetterthanothers
optimizationandparameterestimation
social,behavioral,orneuralprocess
parameters data
modelofthatprocess predictionparameters
observableunknownandunobservable
hypothesized
byadjustingtheseparameters
maximizethecorrespondence
minimizethediscrepancy
generated
sometimes,parameterscanbesetbasedononesetofdataandused(withoutfitting)foranothersituation…
measuresofcorrespondenceordiscrepancy
Someexamples(oftencalled“objectivefunctions”)
Sum-of-SquaredError(SSE)
Correlation
χ2
Likelihood various
SSE = (obsi − prdi )2
i∑
r =(obsi − obs)(prdi − prd)
i∑(obsi − obs)
2
i∑ (prdi − prd)
2
i∑
χ 2 = (obsi − prdi )2
prdii∑
minimized
maximized
minimized
maximized
someobjectivefunctionsaremoreconducivetostatisticaltests
p
SSEwithonlyoneparameter,wewanttofindthevalueofparameterpthatmakestheSSEassmallaspossible–bestfittingvalueofp
optimal valueofp
smallestSSE
One-parameter model
p1
p2
SSE
valuesofp1andp2thatminimizeSSE
Two-parameter model
Three-parameter (or more) model
cannot visualize graphically, but the concept is the same …
what values of parameters minimize SSE (or maximize correlation, or maximize likelihood)
abriefsurveyofoptimizationmethods
abriefsurveyofoptimizationmethods
BruteForce:GridSearch
tryeverycombinationofparametersandkeeptheonethatminimizes/maximizesyourobjectivefunction(e.g.,SSE)
abriefsurveyofoptimizationmethods
BruteForce:GridSearch
tryeverycombinationofparametersandkeeptheonethatminimizes/maximizesyourobjectivefunction(e.g.,SSE)
basically,it'sthesameprogrammingyouwouldusetographthefunction,onlythatyou'realsotryingtofindthemin
abriefsurveyofoptimizationmethods
BruteForce:GridSearch
tryeverycombinationofparametersandkeeptheonethatminimizes/maximizesyourobjectivefunction(e.g.,SSE)
parameter 1
para
met
er 2
likeanearlierhomeworkproblem
abriefsurveyofoptimizationmethods
BruteForce:GridSearch
tryeverycombinationofparametersandkeeptheonethatminimizes/maximizesyourobjectivefunction(e.g.,SSE)
okayformodelswithdiscrete-valuedparameters…whentheyhavecontinuousvaluesitisimpossibletotryeverycombination
okayformodelswithacoupleofparameters…impossibleformodelswithdozensorhundredsofparameters
abriefsurveyofoptimizationmethods
BruteForce:GridSearch
imaginewehaveamodelwith5parameters…
andforeachparameterwearegoingtoevaluate100valuesofit…
Howmanytotalevaluationsisthat?
abriefsurveyofoptimizationmethods
BruteForce:GridSearch
imaginewehaveamodelwith5parameters…
andforeachparameterwearegoingtoevaluate100valuesofit…
Howmanytotalevaluationsisthat?
1005or1010=10,000,000,000
Isthatalot?Wellitdepends.
abriefsurveyofoptimizationmethods
BruteForce:GridSearch
imaginewehaveamodelwith5parameters…
andforeachparameterwearegoingtoevaluate100valuesofit…
Howmanytotalevaluationsisthat?
1005or1010=10,000,000,000
Isthatalot?Wellitdepends.
Ifittakes1nanosecond(10-9seconds)perevaluation,thenitwouldtakeonly10secondstodoall1010evaluations…butthatwouldhavetobeaverysimplemodel
abriefsurveyofoptimizationmethods
BruteForce:GridSearch
imaginewehaveamodelwith5parameters…
andforeachparameterwearegoingtoevaluate100valuesofit…
Howmanytotalevaluationsisthat?
1005or1010=10,000,000,000
Isthatalot?Wellitdepends.
Ifittakes100seconds(102seconds)perevaluation,thenitwouldtakenearly32,000yearstodoalltheevaluations…
wearetestingsomeneuralmodelsthattakefarlongerthanthattorun
abriefsurveyofoptimizationmethods
BruteForce:GridSearch
imaginewehaveamodelwith5parameters…
andforeachparameterwearegoingtoevaluate100valuesofit…
Howmanytotalevaluationsisthat?
1005or1010=10,000,000,000
Isthatalot?Wellitdepends.
Ifittakes100seconds(102seconds)perevaluation,thenitwouldtakenearly32,000yearstodoalltheevaluations…evenifweranthison1000processors,itwouldtake32years
abriefsurveyofoptimizationmethods
Calculus
formathematicallysimplemodelsandobjectivefunctions,youcansometimesoptimizeusingcalculus…
abriefsurveyofoptimizationmethods
Calculus
SSE(p)
dSSE(p)dp
derivativeofafunctionisanotherfunctionthatspecifiestheslopeofthatfunction
abriefsurveyofoptimizationmethods
Calculus
SSE(p)
dSSE(p)dp
derivativeofafunctionisanotherfunctionthatspecifiestheslopeofthatfunction
abriefsurveyofoptimizationmethods
Calculus
SSE(p)
dSSE(p)dp
derivativeofafunctionisanotherfunctionthatspecifiestheslopeofthatfunction
abriefsurveyofoptimizationmethods
Calculus
SSE(p)
dSSE(p)dp
minimizingormaximizingstartswithfindinganyplacesonthefunctionwherethederivativeiszero
abriefsurveyofoptimizationmethods
Calculus
SSE(p)dSSE(p)dp
= 0 canoftenbedoneanalytically,solvingforvaluesofp
abriefsurveyofoptimizationmethods
Calculus
p1
p2
SSE
∂SSE(p1)∂p1
= 0 ∂SSE(p2)∂p2
= 0and
abriefsurveyofoptimizationmethods
Hill-climbingAlgorithms
abriefsurveyofoptimizationmethods
SSE(p)
Hill-climbingAlgorithms
abriefsurveyofoptimizationmethods
SSE(p)
Hill-climbingAlgorithms
abriefsurveyofoptimizationmethods
SSE(p)
Hill-climbingAlgorithms
abriefsurveyofoptimizationmethods
SSE(p)
Hill-climbingAlgorithms
abriefsurveyofoptimizationmethods
SSE(p)
Hill-climbingAlgorithms
abriefsurveyofoptimizationmethods
SSE(p)
Hill-climbingAlgorithms
abriefsurveyofoptimizationmethods
SSE(p)
Hill-climbingAlgorithms
abriefsurveyofoptimizationmethods
SSE(p)
Hill-climbingAlgorithms
abriefsurveyofoptimizationmethods
SSE(p)
Hill-climbingAlgorithms
abriefsurveyofoptimizationmethods
SSE(p)
Hill-climbingAlgorithms
abriefsurveyofoptimizationmethods
Hill-climbingAlgorithms
Enrico Fermi and Nicholas Metropolis used one of the first digital computers, the Los Alamos Maniac, to determine which values of certain theoretical parameters (phase shifts) best fit experimental data (scattering cross sections). They varied one theoretical parameter at a time by steps of the same magnitude, and when no such increase or decrease in any one parameter further improved the fit to the experimental data, they halved the step size and repeated the process until the steps were deemed sufficiently small. Their simple procedure was slow but sure, and several of us used it on the Avidac computer at the Argonne National Laboratory for adjusting six theoretical parameters to fit the pion-proton scattering data we had gathered using the University of Chicago synchrocyclotron [7].
W. C. Davidon, Variable Metric Method for Minimization, Tech. Rep. 5990, Argonne National Laboratory, Argonne, IL, 1959.
these techniques only emerged 60 years ago (Calculus was invented 400 years ago)
abriefsurveyofoptimizationmethods
Hill-climbingAlgorithms
Enrico Fermi and Nicholas Metropolis used one of the first digital computers, the Los Alamos Maniac, to determine which values of certain theoretical parameters (phase shifts) best fit experimental data (scattering cross sections). They varied one theoretical parameter at a time by steps of the same magnitude, and when no such increase or decrease in any one parameter further improved the fit to the experimental data, they halved the step size and repeated the process until the steps were deemed sufficiently small. Their simple procedure was slow but sure, and several of us used it on the Avidac computer at the Argonne National Laboratory for adjusting six theoretical parameters to fit the pion-proton scattering data we had gathered using the University of Chicago synchrocyclotron [7].
W. C. Davidon, Variable Metric Method for Minimization, Tech. Rep. 5990, Argonne National Laboratory, Argonne, IL, 1959.
these techniques only emerged 60 years ago (Calculus was invented 400 years ago)
p1
p2
SSE
howmanypointsdoyouneedtoevaluatewitheachstep?
2 parameters
21 3
4
567
8
abriefsurveyofoptimizationmethods
SimpleHillClimbing
N parameters
5 parameters
10 parameters
3N-1 evaluations per step
242 evaluations per step
59049 evaluations per step
this ends up being inefficient because you can need to take 1000’s of steps
howmanypointsdoyouneedtoevaluatewitheachstep?
abriefsurveyofoptimizationmethods
SimpleHillClimbing
Kolda, T.G., Lewis, R.M., & Torczon, V. (2003). Optimization by direct search: New perspectives on some classical and modern methods. SIAM Review, 45, 385-482.
Kelley, C.T. (1999). Iterative Methods for Optimization. SIAM. Chapters 6-8.
abriefsurveyofoptimizationmethods
Moresophisticatedalgorithms
OptimizationToolboxhttp://www.mathworks.com/help/pdf_doc/optim/optim_tb.pdf
GlobalOptimizationToolboxhttp://www.mathworks.com/help/pdf_doc/gads/gads_tb.pdf
CurveFittingToolboxhttp://www.mathworks.com/help/pdf_doc/curvefit/curvefit.pdf
MATLAB:fminsearch(…)
http://en.wikipedia.org/wiki/Nelder-Mead_method
http://www.scholarpedia.org/article/Nelder-Mead_algorithm
abriefsurveyofoptimizationmethods
Nelder-MeadeSimplex
[opt fit] = fminsearch(@myfun,p0)
params fit
change params to try to decrease fit
myfun()
p0=-2;[optfit]=fminsearch(@myfun1,p0);fprintf('p0=%5.2f\toptimalp=%5.2f\tf(p)=%6.3f\n',… p0,opt,fit);
functiony=myfun1(x) y=-humps(x); end
Week12_fmin.m
p0=[-20];[optfit]=fminsearch(@myfun2,p0);
functionz=myfun2(x,y) z=3*(1-x(:,1)).^2.*exp(-(x(:,1).^2)-(x(:,2)+1).^2)...-10*(x(:,1)/5-x(:,1).^3-x(:,2).^5).*... exp(-x(:,1).^2-x(:,2).^2)... -1/3*exp(-(x(:,1)+1).^2-x(:,2).^2);end
Week12_fmin2.m
p0=[1023];[optfit]=fminsearch(@myfun3,p0);
functionSSE=myfun3(p)%calculuatemodelpredictionsgivenparametervectorp
%thencalculateSSEcomparingpredictionstodata
%returnSSEasthevaluetominimizeend
thiscouldbeanonlinearfunctionifyouarecurvefittingorthiscouldbeaneuralorcognitivemodelifyouaretheorizing
p0=[351431];[optfit]=fminsearch(@myfun4,p0);
functionlnL=myfun4(p)%calculuatemodelpredictionsgivenparametervectorp
%thencalculatelnLcomparingpredictionstodata
%return-lnLasthevaluetominimizeend
maximizingissimply-minimizing