# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # `numpy`
#
# These problems go along with the material presented in the `Basic Numpy` notebook in the python prep folder.
#
# ## Basic Problems
#
# 1. Import `numpy` and check your version.
#
# 2. Create an array from $-10$ to $10$ with steps of $1$ using `np.arange`. Hint: If you don't know how `np.arange` works search for it on the web.
#
# 3. Create an array from $-10$ to $10$ with $1000$ evenly spaced steps using `np.linspace`.
#
# 4. Use `numpy`'s `random` subpackage (accessed using `np.random`) to generate a random draw from $[0,1]$.
#
# 5. Use `numpy`'s `random` to make a random array of length 20 and store it in a variable called `x`.  Run `x > .2`, what comes out? Then try `x[x >.2]`, what happens?

# +
## 1. ANSWER
import numpy as np

print(np.__version__)


# +
## 2. ANSWER

## arange docs, https://numpy.org/doc/stable/reference/generated/numpy.arange.html
np.arange(-10,11,1)




# +
## 3. ANSWER

## linspace docs, https://numpy.org/doc/stable/reference/generated/numpy.linspace.html

np.linspace(-10,10,100)


# +
## 4. ANSWER

## docs, https://numpy.org/doc/stable/reference/random/generated/numpy.random.random.html
np.random.random()



# +
## 5. ANSWER
## Note you don't need to print out the results, I 
## just did that so I'd only need one code chunk

## make the array
x = np.random.random(20)
print(x)
print()

## check where x > .2
print(x > .2)
print()

## get the values of x where x > .2
print(x[x > .2])


# -

# ## More Advanced Problems
#
# 1. Rewrite your answer to the <i>Random Walk Problem</i> in Practice Problems Notebook 4, no need to write the result to file this time.
#
# 2. Write a function that produces a random walk of length $n$ using `numpy`.

# +
## 1. ANSWER

### MAKING THE FLIPS ###
# This is the same as a coin flip
# Tails would be a 0 and 1 would be a Heads
print(np.random.binomial(1,p=.5))
print()

# We can get 100 flips like so
print(np.random.binomial(1,p=.5,size=100))
print()


# flip the coin 100 times
flips = np.random.binomial(1,p=.5,size=100)

# We can map 0,1 to -1,1 using y=2x-1
flips = 2*flips - 1
print(flips)
print()

### GETTING THE POSITONS ###

# initialize the positions array
pos = np.zeros(101)

# use cumsum to get the walk
pos[1:] = np.cumsum(flips)

print(pos)


# +
## 2. ANSWER

# Now we just need to package the above code into a function :)
def random_walk(N):
    # get the flips
    flips = np.random.binomial(1,p=.5,size=N)
    flips = 2*flips - 1

    # Get the positions
    pos = np.zeros(N+1)
    pos[1:] = np.cumsum(flips)
    
    # return the positions
    return pos


# -

# let's print some short walks
for i in range(10):
    print(random_walk(10))
    print()

# This notebook was written for the Erd&#337;s Institute C&#337;de Data Science Boot Camp by <NAME>, Ph. D., 2021.
#
# Redistribution of the material contained in this repository is conditional on acknowledgement of <NAME>, Ph.D.'s original authorship and sponsorship of the Erdős Institute as subject to the license (see License.md)



StarcoderdataJupyterScriptsDedupFiltered