Skip to content

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

Merged
merged 9 commits into from
Oct 17, 2018
Merged

Add Monte Carlo implementation in Factor #434

merged 9 commits into from
Oct 17, 2018

Conversation

nic-hartley
Copy link
Contributor

@nic-hartley nic-hartley commented Oct 3, 2018

@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.

@Gathros Gathros added Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) Hacktoberfest The label for all Hacktoberfest related things! labels Oct 3, 2018
@jiegillet
Copy link
Member

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 jiegillet self-assigned this Oct 6, 2018
@nic-hartley
Copy link
Contributor Author

@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:

  1. You can run this integration equally well with 10 >bignum 50 ^ dup -1 * swap [a,b] dup as your input ranges, which makes this more precise than single-precision floats. In fact, you can make it as precise as you want.
  2. There's no built-in way to generate floats from a range in Factor. No, not even 1.0 -1.0 [a,b] random; that'll only ever return 1.0 or -1.0. Not even 0.

@jiegillet
Copy link
Member

jiegillet commented Oct 7, 2018

I stand corrected, you code doesn't do what I thought it did.
But it's still quite complicated. Looking at the documentation I found a function that generate a float from a range: 0 1 uniform-random-float. I think you can simplify the code a lot using that.

Also, we are assuming that we know the size of the square already, you don't need to estimate that.

Also, your monte-carlo doesn't do what the other implementations do, so that's confusing. I suggest you rename it monte-carlo-pi and make it return an estimation for pi.

@nic-hartley
Copy link
Contributor Author

@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:

: monte-carlo-pi ( -- pi )
  -1 1 [a,b] dup 1000 [ [ 2 ^ ] bi@ 1 < ] monte-carlo ;

(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 :)

Copy link
Member

@jiegillet jiegillet left a 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.

@nic-hartley
Copy link
Contributor Author

@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 jiegillet merged commit 2eb30cb into algorithm-archivists:master Oct 17, 2018
@nic-hartley nic-hartley deleted the factor-monte-carlo branch October 17, 2018 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants