You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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!
The text was updated successfully, but these errors were encountered: