25
W1005 Intro to CS and Programming in MATLAB Project Lecture Fall 2014 hAp://www.cs.columbia.edu/~vovsha/w1005

W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

W1005  Intro  to  CS  and  Programming  in  MATLAB  

 Project  Lecture  

Fall  2014  hAp://www.cs.columbia.edu/~vovsha/w1005  

 

Page 2: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

Fraud  Analysis  of  2000  PresidenMal  ElecMon  Results  in  Florida  using  

Matlab    

Page 3: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

Background  

•  ElecMon  forensics:  field  of  poliMcal  science  that  uses  staMsMcal  analysis  to  examine  elecMon  data  and  detect  irregular  paAerns  that  may  be  indicaMve  of  fraud  

•  Benford’s  Law:  mathemaMcal  law  that  states  that  the  frequency  distribuMon  digits  in  real-­‐life  sources  of  data  is  not  uniformly  distributed.  Instead,  smaller  digits  (1,  2,  3)  appear  more  frequently  than  larger  digits  (7,  8,  9)  

Page 4: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

ApplicaMon  of  Benford’s  Law  Application  of  Benford’s  Law

• Applied to leading digits

– Distribution follows form P(d) = log10( )

• Applied to second digits – rj = ∑ log (1 + (10 ∗ 𝑘 + 𝑗)-1.

Page 5: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

Example  of  Benford’s  Law  applied  to  world’s  countries  populaMon    

Freq

uency  of  digit  

Digit  

Page 6: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

Project  Set  Up  

1.  Read  data  from  file  (found  online)  to  Matlab  2.  Isolate  only  data  that  I  want  to  analyze  –  First  isolate  vote  counts,  then  1st  and  2nd  digits  

3.  Perform  staMsMcal  tests  4.  Plot  results    

Page 7: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

ImplementaMon  in  Matlab  

Page 8: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

A  look  at  the  data  file  

Page 9: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

Reading  Input  File/storing  data  

•  Found  elecMon  data  online  as  a  tab-­‐delimited  file  

•  Used  textscan  because  of  the  file  type  that  I  used  (.txt)  

•  Isolated  the  data  that  I  wanted  to  analyze  – Removed  all  0s  (ie  if  a  candidate  didn’t  receive  any  votes)  

– Removed  all  #s  <10  for  2nd  digit  analysis  •  This  was  the  most  difficult  task  for  me  

Page 10: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

StaMsMcal  Analysis  •  Used  staMsMcal  toolbox,  which  can  be  downloaded  from  the  Mathworks  website  for  free  as  part  of  your  student  license  

•  Difficulty  here  is  in  choosing  which  staMsMcal  tests  to  use  –  Chi  square  test:  compares  observed  values  to  expected  values.  Expected  values  generated  from  formulas  found  in  literature  

–  Kolmogorov-­‐Smirnov  Test:  compares  the  theoreMcal  cumulaMve  distribuMon  and  expected  cumulaMve  distribuMon  

Page 11: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

Results  from  1st  digit  analysis  

Page 12: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

Findings  

•  In  analysis  of  both  leading  and  second  digits,  there  was  insufficient  evidence  to  show  that  the  data  did  not  come  from  a  Benford  distribuMon  

Page 13: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

How  to  Make  a  GUI  

Page 14: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

What  is  a  GUI?  •  Graphical  user  interface  •  Point-­‐and-­‐click  interface  that  allows  users  to  run  a  program  

•  Allows  others  to  use  your  code  without  wriMng  code  

Page 15: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

Making  the  GUI  

•  Write  .m  file  directly      •  Through  GUIDE  

Page 16: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

GUIDE  –  Gehng  Started  

Use  Open  ExisMng  GUI  to  change  your  .fig  file    

Page 17: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

GUIDE  –  Making  Objects  

Click  and  drag  Adding  lines  of  code  

Page 18: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

GUIDE  –  Changing  Object  ProperMes  

Double  click  on  object,  and  type  in  values  in  the  Inspector  

Page 19: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

GUIDE  -­‐  Results  

.fig  file  is  what  users  see  

.m  file  contains  instrucMons  for  GUI  

Page 20: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

“Handling”  the  .m  file  

Depending  on  object,  your  objects  will  create  Callback  and  CreateFcn  funcMons  -­‐Callback  funcMon  is  what  reacts  to  someone  pushing  a  buAon,  dragging  slider,  etc.  -­‐Mainly  implemenMng  your  code  in  Callback  funcMon  

Page 21: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

“Handling”  the  .m  file  

guidata()  stores  all  of  the  GUI’s  data  in  one  place    This  allows  you  to  use  hObject  to  represent  each  object,  and  the  handles  struct  to  store  all  of  your  variables  

Page 22: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

Puhng  it  All  Together  

For  instance,  my  textbox  (tag  =  edit1)  will  take  in  some  sort  of  number  input  -­‐I  retrieve  the  number  by  using  get()  on  hObject,  which  represents  the  textbox  -­‐Then  I  store  it  in  the  GUI  by  using  a  field  in  the  handles  struct  -­‐Note  that  I  re-­‐update  guidata()  

Page 23: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

Puhng  it  All  Together  

Note  that:  -­‐Objects  from  .fig  file  are  already  stored  in  handles  (e.g.  handles.axes1)  and  can  be  used  -­‐Fields  I  created  are  accessible  in  other  funcMons  as  long  as  part  of  same  GUI    (e.g.  handles.number)  -­‐set()  is  useful  for  objects  with  no  input  (e.g.  set(handles.text1,’String’,’Hello’))  

Page 24: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

Done!  

•  Now  let’s  see  what  it  can  do…  

Page 25: W1005 Intro%to%CS%and%Programming%in%MATLAB% …vovsha/w1005/notes/w1005_ta.pdf · W1005 Intro%to%CS%and%Programming%in%MATLAB% % ProjectLecture% Fall%2014% hAp://vovsha/w1005% %

Main  Takeaways  •  GUIs  are  really  easy  •  GUIDE  is  useful  if  you  don’t  want  to  write  from  scratch  

•  Clean  way  to  ask  for  input  and  display  data  (if  done  well)  – Example:  mobile  devices  

•  Do  not  make  them  the  meat  of  your  project  – Built-­‐in  technical  work,  but  not  very  conceptual