Skip to content
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

First vectorized approach in intro is incorrect algorithm. #37

Closed
BoydstonNicholas opened this issue Jan 10, 2017 · 2 comments
Closed

Comments

@BoydstonNicholas
Copy link

So I started reading the book and have gotten to the introduction so far (section 2.1). I noted a couple of issues and thought "I can help!...kinda".

The first vectorized approach to the random walk, which used the code steps = random.sample([1, -1]*n, n) does not in fact produce a random walk.

This is because the random.sample() function performs sampling without replacement. All other random walk definitions I have seen, including the rest of the ones in the introduction, perform their sampling with replacement.

In order to bring this implementation into line with the others in the introduction I might suggest using the random.choices() function.

steps = random.choices([1,-1], k=n)

I also noticed a minor notational issue that could use some clarification. I believe that many people would refer to the "Functional approach" as a "Procedural approach". The word "Functional" is sometimes reserved for the functional programming style which makes heavy use of higher order functions. I cannot say, however, how universal such terminology is.

Looking forward to reading onward.

@rougier
Copy link
Owner

rougier commented Jan 10, 2017

Thanks, I didn't noticed sample was doing sampling without replacement. I'll change in favor of your solution which is also more readable. But in this specific case, taking n samples among 2n samples (having only 2 different values), would that make a statistical difference ?

I'll also correct the Functional approach since the term is ambiguous as you underlined.

Thanks again.

rougier added a commit that referenced this issue Jan 10, 2017
@BoydstonNicholas
Copy link
Author

Cool. Glad to be able to help.
I'm pretty sure it does make a statistical difference, because it would tend to stay more balanced. That is, if a bunch of -1s had already been chosen the next choice would be more likely to be a +1 because there would be more +1s left.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants