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

Resources for PS 2 and 3 #7

Open
keertanavc opened this issue Jan 13, 2020 · 0 comments
Open

Resources for PS 2 and 3 #7

keertanavc opened this issue Jan 13, 2020 · 0 comments

Comments

@keertanavc
Copy link

I received a few questions for the coding part of the assignment and I felt it would be useful to share a few functions that might help you with PS 2 and 3. Good luck!

# Converting a symbolic function to a regular python function that takes arguments
import sympy as sy

x = sy.symbols('x')
f = (2 * sy.pi * sy.cos(x)) 
# Note: when you use functions like cosine/sine or constants like pi, not using the
# sympy version of the function / constant can lead to errors
print('function:', f)

# Method 1: lamdifying the function
g = sy.lambdify(x, f)
# g is a regular python function that takes x and correspondingly
# evaluates f(x)
print('lamdified evaluation:', g(10)) # = 2 * pi * cos(10)

# Method 2: substitution
print('substitution evaluation:', f.subs({x:10}).evalf())

# Keeping track of time to run a code
import time

# Record start time
start_time = time.clock()
count = 0
for i in range(100000):
    count += i
end_time = time.clock()
print('Running time for code = ', end_time-start_time, ' seconds')

# Drawing values from distribution
import scipy.stats as ss
import numpy as np
x, mu, sigma = (0, 0, 1)
# Normal distribution
# Cummulative probability
print(ss.norm.cdf(x, mu, sigma))
# Probability density function
print(ss.norm.pdf(x, mu, sigma))

# Lognormal distribution
# Cummulative probability
x = 1
print(ss.lognorm.cdf(x, sigma, mu, np.exp(mu)))
# Probability density function
print(ss.lognorm.pdf(x, sigma, mu, np.exp(mu)))

# Random draws from a uniform distribution
np.random.seed = 25
x_lb, x_ub, N = 0, 1, 10
# Setting the seed allows you to get the same random draws everytime
print(np.random.uniform(x_lb, x_ub, N))
# This code draws 10 random samples uniformly 
# distributed between 0 and 1

# Some standard numpy arrays
print(np.zeros((3, 5))) # Creates a matrix of size 3x5 filled with zeros
print(np.eye(4)) # Creates an identity matrix of size 4x4
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

1 participant