Stochastic Algorithms: Monte Carlo and Las Vegas Variations

Stochastic Algorithms
Some of the fastest known algorithms for certain tasks rely on
chance
Stochastic/Randomized Algorithms
Two common variations
Monte Carlo
Las Vegas
We have already encountered some of both in this class
Monte Carlo algorithms may return an incorrect/poor answer,
but we can increase run time to improve accuracy of answer
But we 
can
 have a predictable run time
Las Vegas algorithms return a correct answer, but there is a
probability that it could take a relatively long time or not return
an answer at all -  runtime not exactly predictable
Why "Las Vegas"? - Because the house always wins (will get a correct
answer for the house) even though it might take a while
CS 312 – Stochastic Algorithms
1
CS 312 – Stochastic Algorithms
2
Monte Carlo Algorithm – Fermat Test
If a number is prime then it will pass the Fermat primality test
a
c
-1 
 1 mod 
c 
for a random 
a
 such that 1 
 
a
 < 
c
There is less than a 50% probability that a composite number 
c
 passes
the Fermat primality test
So try the test 
k
 times
This is called 
Amplification of Stochastic Advantage
function primality2
(
N
)
Input: Positive integer 
N
Output: yes/no
Choose 
a
1
a
k
 (
k 
< 
N
) random integers between 1 and 
N
-1
if 
 
a
i
 
N
-1 
 1 mod 
N
 for all 
a
i
 
then
  
return yes with probability 1 - 1/(2
k
)
else:
 
return no
Dealing with Local Optima
Assume an algorithm has a probability 
p
 of finding the
optimal answer for a certain problem
Just run it multiple times with different random start states
If chance of hitting a true optima is 
p
, then running the problem 
k
times gives probability 1-(1-
p
)
k
 of finding an optimal solution
This is a Monte Carlo approach
Another approach is to add some randomness to the
algorithm and occasionally allow it to move to a neighbor
which 
increases 
the objective, allowing it to potentially
escape local optima (e.g. simulated annealing)
This (like GA) mixes both Monte Carlo and Las Vegas approaches
CS 312 – Stochastic Algorithms
3
Monte Carlo Algorithms – General Approach
Powerful approach for many tasks with no efficient
deterministic algorithm
Especially useful in complicated tasks with a large number of
coupled variables
Also useful when there is uncertainty in the inputs
1.
Define a domain of possible inputs
2.
Generate inputs randomly from the domain using an
appropriate specified probability distribution (sampling)
3.
Perform a deterministic computation using the sampled
inputs
4.
Aggregate the results of the individual computations into
the final result
CS 312 – Stochastic Algorithms
4
Monte Carlo Algorithm to Calculate π
 
CS 312 – Stochastic Algorithms
5
r
Want to calculate the value of π using darts and a blindfold
Monte Carlo Algorithm to Calculate π
for every 4 darts thrown about π of them will land in the circle
CS 312 – Stochastic Algorithms
6
r
Want to calculate the value of π using darts and a blindfold
Ratio of the area of the inscribed circle to the area of the square is
 
  
         π
r
2
/(2
r
)
2
 = π/4
Monte Carlo Algorithm to Calculate π
for every 4 darts thrown about π of them will land in the circle
Choose 
n
 points in the square from a uniform random
distribution (e.g. throw 
n
 darts with a blindfold, etc.)
n
π/4 darts should land in the circle
π = 4*(number of darts in circle)/
n
Does this follow the general form?
How does the accuracy of the approximation change with 
n
?
Make sure you sample from the representative distribution
CS 312 – Stochastic Algorithms
7
r
Want to calculate the value of π using darts and a blindfold
Ratio of the area of the inscribed circle to the area of the square is
 
  
         π
r
2
/(2
r
)
2
 = π/4
Monte Carlo Integration
Monte Carlo Integration
Solving complex definite integrals
Symbolic Integration software
How long would that take?
Solve all integrals?
What would be a Monte Carlo solution?
How hard to code?
How general and how well would it do?
Multi-variate, high dimensional, non-uniform probability integrals,
etc.
CS 312 – Stochastic Algorithms
8
Markov Chain Monte Carlo Techniques
Sampling complex probability distributions?
MCMC  - Markov Chain Monte Carlo algorithms
Start from an initial arbitrary state
Travel a Markov chain representing the distribution
Probability of next state is just a function of the current state
Follow this chain of states, sampling as you go, until space is
"sufficiently" sampled according to the probability distribution
Need a short "burn-in" phase where we discard early samples to avoid
dependency on the arbitrary initial state
Good approach for solving Bayesian Networks, etc.
Monte Carlo methods commonly used in computational
physics, molecular biology, applied statistics, finance,
optimization, machine learning, etc.
CS 312 – Stochastic Algorithms
9
CS 312 – Stochastic Algorithms
10
Las Vegas Algorithms
 
Quicksort sorts in place, using 
partitioning
Example: Pivot about a random element (e.g. first element (3))
 
3
  1  4  1  5  9  2  6  5  3  5  8  9 --- before
   2  1  3  1  
3
  9  5  6  5  4  5  8  9  --- after
At most 
n
 swaps
Pivot element ends up in its final position
No element left or right of pivot will flip sides again
Sort each side independently
Recursive Divide and Conquer approach
Average case complexity is O(
n
log
n
)
Speed depends on how well the random pivot splits the data
Guaranteed to do a correct sort, but time complexity not predictable -
Worst case is O(
n
2
) – Thus a Las Vegas algorithm
Selection algorithm for median finding is also a Las Vegas algorithm
 
 
When to use what Algorithm?
Not always just one best approach for a task
Can depend on what style you like
However, some algorithms will usually fit a task more efficiently
Have given you an initial toolkit:
Divide and Conquer
Graph Algorithms
Greedy Algorithms
Dynamic Programming
Linear Programming
Intelligent Search
Local Search
Stochastic Algorithms
TSP, Sort, Shortest Path, Knapsack, Multiplication, …
New Problem? can find a similar common problem and its solutions
CS 312 – Stochastic Algorithms
11
When to use what Algorithm?
Divide and Conquer
Problem has natural hierarchy with independent branches. 
Speed-up happens when we can
find short-cuts during partition/merge that can be taken because of the divide and conquer
paradigm
Graph Algorithms
When finding reachability, paths, and properties of tasks represented as graphs, specific
algorithms often fall under other approaches
Greedy
When making a greedy choice leads to reasonable solutions, common simple and fast
approximation approach, occasionally optimal
Dynamic Programming
Overlapping subproblems (given by a recursive definition) that are only slightly (constant
factor) smaller than the original problem, solved with the proper ordering of saved results
Linear Programming
 
Any optimization with linear objective and constraints
Intelligent Search
Effective when we have some heuristic knowledge of the search space to allow pruning
Local Search
Simple optimization technique for very complex search spaces – potential local optima
Stochastic Algorithms
Sampling based problems, amplification of stochastic advantage, takes advantage of fast
computers
CS 312 – Stochastic Algorithms
12
Algorithms
Can often use a combination of different algorithms
Divide and Conquer followed by different algorithm on
subproblems
Stochastic algorithms can often improve many of the base
algorithms for certain tasks
Greedy components within algorithms
etc.
Be Creative!
Don't get stuck on just one "Hammer"
The ways in which you apply these algorithmic techniques will not
always be initially obvious
You now have a powerful toolbox of algorithmic
techniques and philosophies which will prove beneficial as
you solve complex tasks in the future
CS 312 – Stochastic Algorithms
13
Slide Note

Quit a little early so they have time to discuss final group project issues

What MC and LV algs have we done already in class?

MC: Fermat, Local Minima

LV: Selection, Quicksort

GA: a bit of both since you set how long it runs like Monte Carlo, but if you just let it run long enough? it should eventually find optimal (though you wouldn't know when)

Embed
Share

Stochastic algorithms, including Monte Carlo and Las Vegas variations, leverage randomness to tackle complex tasks efficiently. While Monte Carlo algorithms prioritize speed with some margin of error, Las Vegas algorithms guarantee accuracy but with variable runtime. They play a vital role in primality testing, dealing with local optima in optimization problems, and handling uncertainties in inputs. These algorithms offer a powerful and flexible approach for diverse computational challenges.

  • Stochastic Algorithms
  • Monte Carlo
  • Las Vegas
  • Optimization
  • Randomized

Uploaded on Oct 07, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Stochastic Algorithms Some of the fastest known algorithms for certain tasks rely on chance Stochastic/Randomized Algorithms Two common variations Monte Carlo Las Vegas We have already encountered some of both in this class Monte Carlo algorithms may return an incorrect/poor answer, but we can increase run time to improve accuracy of answer But we can have a predictable run time Las Vegas algorithms return a correct answer, but there is a probability that it could take a relatively long time or not return an answer at all - runtime not exactly predictable Why "Las Vegas"? - Because the house always wins (will get a correct answer for the house) even though it might take a while CS 312 Stochastic Algorithms 1

  2. Monte Carlo Algorithm Fermat Test If a number is prime then it will pass the Fermat primality test ac-1 1 mod c for a random a such that 1 a < c There is less than a 50% probability that a composite number c passes the Fermat primality test So try the test k times This is called Amplification of Stochastic Advantage function primality2(N) Input: Positive integer N Output: yes/no Choose a1 ak (k < N) random integers between 1 and N-1 if aiN-1 1 mod N for all aithen return yes with probability 1 - 1/(2k) else: return no CS 312 Stochastic Algorithms 2

  3. Dealing with Local Optima Assume an algorithm has a probability p of finding the optimal answer for a certain problem Just run it multiple times with different random start states If chance of hitting a true optima is p, then running the problem k times gives probability 1-(1-p)k of finding an optimal solution This is a Monte Carlo approach Another approach is to add some randomness to the algorithm and occasionally allow it to move to a neighbor which increases the objective, allowing it to potentially escape local optima (e.g. simulated annealing) This (like GA) mixes both Monte Carlo and Las Vegas approaches CS 312 Stochastic Algorithms 3

  4. Monte Carlo Algorithms General Approach Powerful approach for many tasks with no efficient deterministic algorithm Especially useful in complicated tasks with a large number of coupled variables Also useful when there is uncertainty in the inputs Define a domain of possible inputs Generate inputs randomly from the domain using an appropriate specified probability distribution (sampling) Perform a deterministic computation using the sampled inputs Aggregate the results of the individual computations into the final result 1. 2. 3. 4. CS 312 Stochastic Algorithms 4

  5. Monte Carlo Algorithm to Calculate Want to calculate the value of using darts and a blindfold r CS 312 Stochastic Algorithms 5

  6. Monte Carlo Algorithm to Calculate Want to calculate the value of using darts and a blindfold Ratio of the area of the inscribed circle to the area of the square is r2/(2r)2= /4 r for every 4 darts thrown about of them will land in the circle CS 312 Stochastic Algorithms 6

  7. Monte Carlo Algorithm to Calculate Want to calculate the value of using darts and a blindfold Ratio of the area of the inscribed circle to the area of the square is r2/(2r)2= /4 r for every 4 darts thrown about of them will land in the circle Choose n points in the square from a uniform random distribution (e.g. throw n darts with a blindfold, etc.) n /4 darts should land in the circle = 4*(number of darts in circle)/n Does this follow the general form? How does the accuracy of the approximation change with n? Make sure you sample from the representative distribution CS 312 Stochastic Algorithms 7

  8. Monte Carlo Integration Monte Carlo Integration Solving complex definite integrals Symbolic Integration software How long would that take? Solve all integrals? What would be a Monte Carlo solution? How hard to code? How general and how well would it do? Multi-variate, high dimensional, non-uniform probability integrals, etc. CS 312 Stochastic Algorithms 8

  9. Markov Chain Monte Carlo Techniques Sampling complex probability distributions? MCMC - Markov Chain Monte Carlo algorithms Start from an initial arbitrary state Travel a Markov chain representing the distribution Probability of next state is just a function of the current state Follow this chain of states, sampling as you go, until space is "sufficiently" sampled according to the probability distribution Need a short "burn-in" phase where we discard early samples to avoid dependency on the arbitrary initial state Good approach for solving Bayesian Networks, etc. Monte Carlo methods commonly used in computational physics, molecular biology, applied statistics, finance, optimization, machine learning, etc. CS 312 Stochastic Algorithms 9

  10. Las Vegas Algorithms Quicksort sorts in place, using partitioning Example: Pivot about a random element (e.g. first element (3)) 3 1 4 1 5 9 2 6 5 3 5 8 9 --- before 2 1 3 1 3 9 5 6 5 4 5 8 9 --- after At most n swaps Pivot element ends up in its final position No element left or right of pivot will flip sides again Sort each side independently Recursive Divide and Conquer approach Average case complexity is O(nlogn) Speed depends on how well the random pivot splits the data Guaranteed to do a correct sort, but time complexity not predictable - Worst case is O(n2) Thus a Las Vegas algorithm Selection algorithm for median finding is also a Las Vegas algorithm CS 312 Stochastic Algorithms 10

  11. When to use what Algorithm? Not always just one best approach for a task Can depend on what style you like However, some algorithms will usually fit a task more efficiently Have given you an initial toolkit: Divide and Conquer Graph Algorithms Greedy Algorithms Dynamic Programming Linear Programming Intelligent Search Local Search Stochastic Algorithms TSP, Sort, Shortest Path, Knapsack, Multiplication, New Problem? can find a similar common problem and its solutions CS 312 Stochastic Algorithms 11

  12. When to use what Algorithm? Divide and Conquer Problem has natural hierarchy with independent branches. Speed-up happens when we can find short-cuts during partition/merge that can be taken because of the divide and conquer paradigm Graph Algorithms When finding reachability, paths, and properties of tasks represented as graphs, specific algorithms often fall under other approaches Greedy When making a greedy choice leads to reasonable solutions, common simple and fast approximation approach, occasionally optimal Dynamic Programming Overlapping subproblems (given by a recursive definition) that are only slightly (constant factor) smaller than the original problem, solved with the proper ordering of saved results Linear Programming Any optimization with linear objective and constraints Intelligent Search Effective when we have some heuristic knowledge of the search space to allow pruning Local Search Simple optimization technique for very complex search spaces potential local optima Stochastic Algorithms Sampling based problems, amplification of stochastic advantage, takes advantage of fast computers CS 312 Stochastic Algorithms 12

  13. Algorithms Can often use a combination of different algorithms Divide and Conquer followed by different algorithm on subproblems Stochastic algorithms can often improve many of the base algorithms for certain tasks Greedy components within algorithms etc. Be Creative! Don't get stuck on just one "Hammer" The ways in which you apply these algorithmic techniques will not always be initially obvious You now have a powerful toolbox of algorithmic techniques and philosophies which will prove beneficial as you solve complex tasks in the future CS 312 Stochastic Algorithms 13

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#