-
-
Notifications
You must be signed in to change notification settings - Fork 360
Add Monte Carlo implementation in Factor #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Monte Carlo implementation in Factor #434
Conversation
This isn't a Monte Carlo method, you're just integrating the square and the circle with a grid. For this simple case you will get the same result, but the point of Monte Carlo is to use randomness when grids would be too big. This one won't fly as is. |
@jiegillet I think you need to read the code more carefully, if that's really what you think. Let me draw your attention to one part specifically: xr random yr random That randomly samples a value from the X range and a value from the Y range. It's not just iterating over the entire grid; to prove it, try running this on your machine: -1000000000 1000000000 [ >bignum ] bi@ [a,b] dup
100
[ ! in-circle check
2 ^ swap 2 ^ + ! get the distance from the center
1000000000 2 ^ < ! see if it's less than the radius
]
monte-carlo You'll notice it runs nearly instantly, despite supposedly needing to traverse the entire quintillion points. It would take a billion gigahertz CPUs to do that in a full second if there was a one-operation way to do a Monte Carlo sampling, so that's a really impressive machine you have there! Well, that, or it's randomly sampling 100 points from the field. But I dunno, either seems likely. I'll close by saying two things:
|
I stand corrected, you code doesn't do what I thought it did. Also, we are assuming that we know the size of the square already, you don't need to estimate that. Also, your |
@jiegillet We're supposed to write idiomatic code, right? The single biggest reason to use a concatenative language, and one of the main tenets of Factor's philosophy, is that you can and should refactor code into chunks as small as you can. So, for example, a function to find pi with the Monte Carlo approximation should call a generic Monte Carlo function. I'm happy to do that; I'm not happy to mix in pi calculations with a generic area ratio approximation algorithm. For example:
(give or take some syntax/vocab errors) And, honestly, that's what I think the rest should look like, too -- the page is still just about Monte Carlo, with the pi as an instructive example. The code should be like that, too. But that's a discussion for another time :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not against idiomatic code, quite the contrary. In fact the implementations of Verlet integration currently in the AAA make my skin crawl.
However in this case, Monte Carlo methods really mean any method using approximations with random numbers. It's not like there is a generic Monte Carlo function. For example your monte-carlo
only integrates 2D shapes in the [0, 1] range. Well written and easily extendable, but hardly generic.
Anyway I have no problem with your current implementation if you want to keep it.
contents/monte_carlo_integration/code/factor/monte_carlo.factor
Outdated
Show resolved
Hide resolved
contents/monte_carlo_integration/code/factor/monte_carlo.factor
Outdated
Show resolved
Hide resolved
contents/monte_carlo_integration/code/factor/monte_carlo.factor
Outdated
Show resolved
Hide resolved
@jiegillet "A generic Monte Carlo method" doesn't mean "A method which can perfectly handle all possible Monte Carlo approximations", it means "a method not specifically tied to a single solution when the prose explicitly states that you can substitute out one function for something else and do something different". |
@jiegillet You might be interested in this, too ;)
By the way -- I got a little annoyed of writing the same Factor config code into book.json. If you merge the other Factor issues first it should be fine.