SlideShare a Scribd company logo
A giant python swallows an alligator in Everglades National Park, Fla..
      Paul Zenk/PBS Nature “Invasion of the Giant Pythons” (yesterday’s New York Times)




cs1120 Fall 2011
David Evans
                                               Class 28:
28 October 2011                                Entropy
Plan
Story so Far: Liberal Arts Recap
Reversing in Python
Shannon’s Information Theory: Entropy
Returning Quiz 3, PS5, etc.




                                        2
Ch 1: Computing
                               Ch 2: Language




Analysis
                                                    Synthesis


                               Ch 3: Programming

                               Ch 4: Procedures

                               Ch 5: Data
            Ch 6: Machines

                 Ch 7: Cost

             Ch 8: Sorting and Searching
                        PS5, Ch 9: State
                 You are      PS6, Ch 10: Objects
                                                      Course Roadmap




                  here
                PS7, Ch 11: Interpreters

      Ch 12: Computability
                           PS8, 9
              Intractability
cs1120 Main Topics
Language: How to describe information processes by
  defining procedures (Chapters 3, 4, 5, 9, 10)
   Programming with procedures, lists, recursion (PS1-4)
   Programming with state (PS5), objects (PS6), languages (PS7)
Logic: How to predict properties about information
  processes (Chapter 6, 7, 8)
   Predicting how running time grows with input size
   Are there problems which can’t be solved by algorithms? (Ch 12)
Machines: How to efficiently implement information
 processes (Chapter 6) (not much on this)
   Machines, Digital Logic
   How to implement a Scheme interpreter (Ch 11, PS7)
From Lecture 1:
Grammar: study of meaning in                  BNF, RTN, rules of
               written expression                        evaluation for meaning
Trivium

             Rhetoric: comprehension of verbal             Interfaces between
                                                               components
               and written discourse                     (PS6), evaluation rules

             Logic: argumentative discourse for          Rules of evaluation, if,
                                                          recursive definitions
               discovering truth
             Arithmetic: understanding                     adding with logic,
                                                         representing numbers
               numbers
Quadrivium




                                                         Curves as procedures,
             Geometry: quantification of space               fractals (PS3)

             Music: number in time                         Ada, GEB Chapter

             Astronomy                                          Nothing!
                                         But…read the Neil DeGrasse Tyson essay!
Lists (Tuples) in Python
     Scheme (list)             Python (tuple)
> (define p (list 1 2 3))   >>> p = (1, 2, 3)
> (car p)                   >>> p[0]
1                           1
> (cdr p)                   >>> p[1:]
(2 3)                       (2, 3)
> (cdr (cdr (cdr p)))       >>> p[1:][1:][1:]
null                        ()

                                                7
Python Shortcuts
>>> p = (1, 2, 3)
>>> p[2]
3          list[i]
>>> p[3:]  Evaluates to ith (counting from
             0) element of tuple/list
()
           Runs in (approximately)
>>> p[:2] constant time!
(1, 2)
Mutable Lists (Lists) in Python
Scheme (mutable list)             Python [List]
 > (define p (mlist 1 2 3))   >>> p = [1, 2, 3]
 > (mcar p)                   >>> p[0]
 1                            1
 > (mcdr p)                   >>> p[1:]
 (2 3)                        (2, 3)
 > (mcdr (mcdr (mcdr p)))     >>> p[1:][1:][1:]
 null                         ()

                                                  9
Using Mutable Lists
>>> p = [1, 2, 3]   Scheme Application:
>>> p[0]            (<verb> <operands>)
1                   Python procedure call:
>>> p[0] = 2        <verb>(<operand>)
>>> p               Method call:
[2, 2, 3]           <object>.<verb>(<parameters>)
>>> p.append(4) Note: this is different from mappend!
>>> p             It take a single element, not a list.
[2, 2, 3, 4]
Trick Question

>>> p = (1, 2, 3)
>>> p[0] = 2
Traceback (most recent call last):
 File "<pyshell#23>", line 1, in <module>
  p[0] = 2
TypeError: 'tuple' object does not
support item assignment
Reversing a Mutable List

Scheme:      p:

                   1           2   3




Python:
             p:

                   1   2   3
mlist-reverse!
(define (mlist-reverse! p)
 (if (null? (mcdr p))
    (void)
    (let ((rest (mcdr p))
          (carp (mcar p)))
      (mlist-reverse! rest)
      (set-mcar! p (mcar rest))
      (set-mcdr! p (mcdr rest))
      (mlist-append! p (mcons carp null)))))
Literal Translation
(define (mlist-reverse! p)
 (if (null? (mcdr p))
    (void)
    (let ((rest (mcdr p))
          (carp (mcar p)))
      (mlist-reverse! rest)
      (set-mcar! p (mcar rest))
      (set-mcdr! p (mcdr rest))
      (mlist-append!
         p
         (mcons carp null)))))
Literal Translation
(define (mlist-reverse! p)                 def sreverse(p):
 (if (null? (mcdr p))                        if len(p) == 1:
    (void)
                                                return
    (let ((rest (mcdr p))
          (carp (mcar p)))                   else:
      (mlist-reverse! rest)                     rest = p[1:]
      (set-mcar! p (mcar rest))                 first = p[0]
      (set-mcdr! p (mcdr rest))                 sreverse(rest)
      (mlist-append!
                                                p[0] = rest[0]
         p
         (mcons carp null)))))                  p[1:] = rest[1:]
                                                p.append(first)
                                                          >>> p=[1,2,3]
          This is correct, but no sane Python             >>> sreverse(p)
          programmer would do it this way!                >>> p
                                                          [3, 2, 1]
Python for
Statement ::=                  (define (loop index result test update proc)
  for Varible in Expression:    (if (test index)
                                    (loop (update index)
    Block                                  (proc index result)
                                           test update proc)
                                    result))
def gaussSum (n):
  sum = 0                      (define (gauss-sum n)
                                 (loop 1 0 (lambda (i) (<= i n))
  for i in range(1, n+1):                  (lambda (i) (+ i 1))
                                           (lambda (i sum) (+ i sum))))
       sum = sum + i
  return sum
Reverse in Python
                   p:

                                    1     2     3




      Note: there is a built-in list method, p.reverse().
int(len(p) / 2)    len(p) >> 1
def reverse(p):
  for i in range(0, len(p) >> 1):
    p[i], p[len(p) – 1 - i] = p[len(p) – 1 - i], p[i]
Information Theory




                     19
Entropy




H = - pi log2 pi
                   20
flickr cc:Daniel Dale


 (Ideal) Coin Toss

H = - pi log2 pi




                                       21
Nucleotide
               N      {A, C, G, T}
Intuition: need two bits to distinguish four possible values
               00 = A, 01 = C, 10 = G, 11 = T


          H = - pi log2 pi


                                                               22
23
Real
   Coin
   Toss?

Claude Shannon’s
Juggling W. C. Fields
                        24
Real Coin Toss
According to Susan Holmes, 51% of the time, a
coin will land same way it started. [link]

           H = - pi log2 pi



                                                25
26
Are Randall Munroe’s entropy estimates correct?
Left for course blog discussion.
                                                  27
0.9880135796356116381290864023063602427958811193759308857790...




                            Class Sorting using “Odette”
           Assuming alphabet
             randomly
             distributed: O is
             15th out of 26
             letters in alphabet



                                              = 0.988…
                                                                  28
0.9880135796356116381290864023063602427958811193759308857790...




                             Class Sorting using “Odette”
           The real class: 41 out
             of 49 students have
             names before
             “Odette”!


         -((41/49) * log_2 (41/49) + (7/49) * log_2 (7/49))
         = 0.6162235892609252


                                                                  29
Better Class Sorting
         Front of Class

crm Caroline    Hannah – Li-
  - Gregory       Chang
 Anthony -      mdc Michael
  Casey           - Yuan
                               30
Charge
Office hours:
  Peter – right now
  Sunday, 1-6pm
  Everyone should be making progress on PS6
Monday:
  Trick-or-Treat Protocols!
  Inheritance

More Related Content

PPT
Profiling and optimization
PDF
Fuzzy Bitopological Ideals Spaces
PPT
Euro python2011 High Performance Python
PDF
Python tour
PDF
Hathor@FGV: Introductory notes by Dr. A. Linhares
PPTX
Algorithm Homework Help
PDF
Lezione03
Profiling and optimization
Fuzzy Bitopological Ideals Spaces
Euro python2011 High Performance Python
Python tour
Hathor@FGV: Introductory notes by Dr. A. Linhares
Algorithm Homework Help
Lezione03

What's hot (20)

PDF
Beyond tf idf why, what & how
PDF
CPSC 125 Ch 1 sec 4
PDF
«Отладка в Python 3.6: Быстрее, Выше, Сильнее» Елизавета Шашкова, JetBrains
PPT
Hash presentation
PDF
Python Performance 101
PDF
20130928 automated theorem_proving_harrison
PDF
SPU Optimizations-part 1
PPTX
Programming Homework Help
PPTX
The theory of concurrent programming for a seasoned programmer
KEY
A Proposition for Business Process Modeling
PPTX
Introduction to TensorFlow 2 and Keras
PPTX
C Programming Homework Help
PDF
A born-again programmer's odyssey
ODP
Clojure made simple - Lightning talk
PPTX
Signal Processing Assignment Help
Beyond tf idf why, what & how
CPSC 125 Ch 1 sec 4
«Отладка в Python 3.6: Быстрее, Выше, Сильнее» Елизавета Шашкова, JetBrains
Hash presentation
Python Performance 101
20130928 automated theorem_proving_harrison
SPU Optimizations-part 1
Programming Homework Help
The theory of concurrent programming for a seasoned programmer
A Proposition for Business Process Modeling
Introduction to TensorFlow 2 and Keras
C Programming Homework Help
A born-again programmer's odyssey
Clojure made simple - Lightning talk
Signal Processing Assignment Help
Ad

Similar to Class 28: Entropy (20)

PPTX
Class 25: Reversing Reverse
PDF
01. haskell introduction
PDF
GE3151_PSPP_UNIT_4_Notes
PDF
Introduction to Python
ODP
Python quickstart for programmers: Python Kung Fu
PDF
Python for text processing
PPT
Python 101 language features and functional programming
PDF
Python Usage (5-minute-summary)
PDF
PDF
Mementopython3 english
PDF
Python Cheat Sheet
PDF
python_lab_manual_final (1).pdf
PDF
Mementopython3 english
PPTX
Class 16: Making Loops
PDF
Pydiomatic
PDF
Python idiomatico
PDF
Iteration
PDF
Python3 cheatsheet
PPT
python language programming presentation
PDF
Introduction to python cheat sheet for all
Class 25: Reversing Reverse
01. haskell introduction
GE3151_PSPP_UNIT_4_Notes
Introduction to Python
Python quickstart for programmers: Python Kung Fu
Python for text processing
Python 101 language features and functional programming
Python Usage (5-minute-summary)
Mementopython3 english
Python Cheat Sheet
python_lab_manual_final (1).pdf
Mementopython3 english
Class 16: Making Loops
Pydiomatic
Python idiomatico
Iteration
Python3 cheatsheet
python language programming presentation
Introduction to python cheat sheet for all
Ad

More from David Evans (20)

PPTX
Cryptocurrency Jeopardy!
PPTX
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
PPTX
Hidden Services, Zero Knowledge
PPTX
Anonymity in Bitcoin
PPTX
Midterm Confirmations
PPTX
Scripting Transactions
PPTX
How to Live in Paradise
PPTX
Bitcoin Script
PPTX
Mining Economics
PPTX
Mining
PPTX
The Blockchain
PPTX
Becoming More Paranoid
PPTX
Asymmetric Key Signatures
PPTX
Introduction to Cryptography
PPTX
Class 1: What is Money?
PPTX
Multi-Party Computation for the Masses
PPTX
Proof of Reserve
PPTX
Silk Road
PPTX
Blooming Sidechains!
PPTX
Useful Proofs of Work, Permacoin
Cryptocurrency Jeopardy!
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Hidden Services, Zero Knowledge
Anonymity in Bitcoin
Midterm Confirmations
Scripting Transactions
How to Live in Paradise
Bitcoin Script
Mining Economics
Mining
The Blockchain
Becoming More Paranoid
Asymmetric Key Signatures
Introduction to Cryptography
Class 1: What is Money?
Multi-Party Computation for the Masses
Proof of Reserve
Silk Road
Blooming Sidechains!
Useful Proofs of Work, Permacoin

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Tartificialntelligence_presentation.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Machine learning based COVID-19 study performance prediction
PDF
Electronic commerce courselecture one. Pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Getting Started with Data Integration: FME Form 101
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
Building Integrated photovoltaic BIPV_UPV.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
MYSQL Presentation for SQL database connectivity
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Tartificialntelligence_presentation.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Big Data Technologies - Introduction.pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Encapsulation_ Review paper, used for researhc scholars
Machine learning based COVID-19 study performance prediction
Electronic commerce courselecture one. Pdf
Unlocking AI with Model Context Protocol (MCP)
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Getting Started with Data Integration: FME Form 101
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Digital-Transformation-Roadmap-for-Companies.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation

Class 28: Entropy

  • 1. A giant python swallows an alligator in Everglades National Park, Fla.. Paul Zenk/PBS Nature “Invasion of the Giant Pythons” (yesterday’s New York Times) cs1120 Fall 2011 David Evans Class 28: 28 October 2011 Entropy
  • 2. Plan Story so Far: Liberal Arts Recap Reversing in Python Shannon’s Information Theory: Entropy Returning Quiz 3, PS5, etc. 2
  • 3. Ch 1: Computing Ch 2: Language Analysis Synthesis Ch 3: Programming Ch 4: Procedures Ch 5: Data Ch 6: Machines Ch 7: Cost Ch 8: Sorting and Searching PS5, Ch 9: State You are PS6, Ch 10: Objects Course Roadmap here PS7, Ch 11: Interpreters Ch 12: Computability PS8, 9 Intractability
  • 4. cs1120 Main Topics Language: How to describe information processes by defining procedures (Chapters 3, 4, 5, 9, 10) Programming with procedures, lists, recursion (PS1-4) Programming with state (PS5), objects (PS6), languages (PS7) Logic: How to predict properties about information processes (Chapter 6, 7, 8) Predicting how running time grows with input size Are there problems which can’t be solved by algorithms? (Ch 12) Machines: How to efficiently implement information processes (Chapter 6) (not much on this) Machines, Digital Logic How to implement a Scheme interpreter (Ch 11, PS7)
  • 6. Grammar: study of meaning in BNF, RTN, rules of written expression evaluation for meaning Trivium Rhetoric: comprehension of verbal Interfaces between components and written discourse (PS6), evaluation rules Logic: argumentative discourse for Rules of evaluation, if, recursive definitions discovering truth Arithmetic: understanding adding with logic, representing numbers numbers Quadrivium Curves as procedures, Geometry: quantification of space fractals (PS3) Music: number in time Ada, GEB Chapter Astronomy Nothing! But…read the Neil DeGrasse Tyson essay!
  • 7. Lists (Tuples) in Python Scheme (list) Python (tuple) > (define p (list 1 2 3)) >>> p = (1, 2, 3) > (car p) >>> p[0] 1 1 > (cdr p) >>> p[1:] (2 3) (2, 3) > (cdr (cdr (cdr p))) >>> p[1:][1:][1:] null () 7
  • 8. Python Shortcuts >>> p = (1, 2, 3) >>> p[2] 3 list[i] >>> p[3:] Evaluates to ith (counting from 0) element of tuple/list () Runs in (approximately) >>> p[:2] constant time! (1, 2)
  • 9. Mutable Lists (Lists) in Python Scheme (mutable list) Python [List] > (define p (mlist 1 2 3)) >>> p = [1, 2, 3] > (mcar p) >>> p[0] 1 1 > (mcdr p) >>> p[1:] (2 3) (2, 3) > (mcdr (mcdr (mcdr p))) >>> p[1:][1:][1:] null () 9
  • 10. Using Mutable Lists >>> p = [1, 2, 3] Scheme Application: >>> p[0] (<verb> <operands>) 1 Python procedure call: >>> p[0] = 2 <verb>(<operand>) >>> p Method call: [2, 2, 3] <object>.<verb>(<parameters>) >>> p.append(4) Note: this is different from mappend! >>> p It take a single element, not a list. [2, 2, 3, 4]
  • 11. Trick Question >>> p = (1, 2, 3) >>> p[0] = 2 Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> p[0] = 2 TypeError: 'tuple' object does not support item assignment
  • 12. Reversing a Mutable List Scheme: p: 1 2 3 Python: p: 1 2 3
  • 13. mlist-reverse! (define (mlist-reverse! p) (if (null? (mcdr p)) (void) (let ((rest (mcdr p)) (carp (mcar p))) (mlist-reverse! rest) (set-mcar! p (mcar rest)) (set-mcdr! p (mcdr rest)) (mlist-append! p (mcons carp null)))))
  • 14. Literal Translation (define (mlist-reverse! p) (if (null? (mcdr p)) (void) (let ((rest (mcdr p)) (carp (mcar p))) (mlist-reverse! rest) (set-mcar! p (mcar rest)) (set-mcdr! p (mcdr rest)) (mlist-append! p (mcons carp null)))))
  • 15. Literal Translation (define (mlist-reverse! p) def sreverse(p): (if (null? (mcdr p)) if len(p) == 1: (void) return (let ((rest (mcdr p)) (carp (mcar p))) else: (mlist-reverse! rest) rest = p[1:] (set-mcar! p (mcar rest)) first = p[0] (set-mcdr! p (mcdr rest)) sreverse(rest) (mlist-append! p[0] = rest[0] p (mcons carp null))))) p[1:] = rest[1:] p.append(first) >>> p=[1,2,3] This is correct, but no sane Python >>> sreverse(p) programmer would do it this way! >>> p [3, 2, 1]
  • 16. Python for Statement ::= (define (loop index result test update proc) for Varible in Expression: (if (test index) (loop (update index) Block (proc index result) test update proc) result)) def gaussSum (n): sum = 0 (define (gauss-sum n) (loop 1 0 (lambda (i) (<= i n)) for i in range(1, n+1): (lambda (i) (+ i 1)) (lambda (i sum) (+ i sum)))) sum = sum + i return sum
  • 17. Reverse in Python p: 1 2 3 Note: there is a built-in list method, p.reverse().
  • 18. int(len(p) / 2) len(p) >> 1 def reverse(p): for i in range(0, len(p) >> 1): p[i], p[len(p) – 1 - i] = p[len(p) – 1 - i], p[i]
  • 20. Entropy H = - pi log2 pi 20
  • 21. flickr cc:Daniel Dale (Ideal) Coin Toss H = - pi log2 pi 21
  • 22. Nucleotide N {A, C, G, T} Intuition: need two bits to distinguish four possible values 00 = A, 01 = C, 10 = G, 11 = T H = - pi log2 pi 22
  • 23. 23
  • 24. Real Coin Toss? Claude Shannon’s Juggling W. C. Fields 24
  • 25. Real Coin Toss According to Susan Holmes, 51% of the time, a coin will land same way it started. [link] H = - pi log2 pi 25
  • 26. 26
  • 27. Are Randall Munroe’s entropy estimates correct? Left for course blog discussion. 27
  • 28. 0.9880135796356116381290864023063602427958811193759308857790... Class Sorting using “Odette” Assuming alphabet randomly distributed: O is 15th out of 26 letters in alphabet = 0.988… 28
  • 29. 0.9880135796356116381290864023063602427958811193759308857790... Class Sorting using “Odette” The real class: 41 out of 49 students have names before “Odette”! -((41/49) * log_2 (41/49) + (7/49) * log_2 (7/49)) = 0.6162235892609252 29
  • 30. Better Class Sorting Front of Class crm Caroline Hannah – Li- - Gregory Chang Anthony - mdc Michael Casey - Yuan 30
  • 31. Charge Office hours: Peter – right now Sunday, 1-6pm Everyone should be making progress on PS6 Monday: Trick-or-Treat Protocols! Inheritance