Skip to content

Instantly share code, notes, and snippets.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@aeglon97
aeglon97 / gaussian_probability_density.py
Last active September 1, 2021 05:01
Python function to calculate the probability density function of a Gaussian distribution
# The Gaussian probability density function is equal to:
# (1 / sqrt(2 * pi * sigma^2)) * e^ ((-(x - mu)^2) / 2 * sigma^2)
# - mu = the average
# - sigma = standard deviation
import math
import numpy as np
def gaussian_density(x, mu, sigma):
two_pi_sigma_squared = 2 * np.pi * np.power(sigma, 2)
@aeglon97
aeglon97 / gaussian_area_under_curve--standard_normal_table.py
Last active September 1, 2021 05:15
Calculate Guassian probability density using SciPy
#Calculate the area under a Gaussian curve between 2 points, given their x-values and y-values (probability density functions).
#scipy norm() uses a Z-distribution table approach (standard normal table) to calculate the area under the curve between x1 and x2.
#NOTE: The area under the curve is the probability of the scenario happeniing between x1 and x2.
# example: how likely will San Francisco fluctuate between 20 to 30 degrees celsius on a random day?
from scipy.stats import norm
import numpy as np
import math
@aeglon97
aeglon97 / central_limit_theorem--simulation.py
Created September 1, 2021 05:20
A simulation of the central limit theorem. Returns an array of random sample means of size "iterations"
def central_limit_theorem(population, sample_size, iterations):
sample_means = []
for i in range(iterations):
random_sample = np.random.choice(population, size = sample_size)
sample_mean = np.mean(random_sample)
sample_means.append(sample_mean)
return sample_means
p=[0.2, 0.2, 0.2, 0.2, 0.2]
world=['green', 'red', 'red', 'green', 'green']
Z = 'red'
pHit = 0.6
pMiss = 0.2
def sense(p, Z):
for i in range(len(p)):
at_Z = (Z == world[i])
#######################APPROACH 1#######################
def shift1(lst, shift_by):
shift_by = shift_by % len(lst)
#retrieves last -U number of elements (counting backwards)
last_elements_shifted = lst[-shift_by:]
#removes last -U number of elements (retrieves front of list)
first_elements_shifted = lst[:-shift_by]
p=[0.2, 0.2, 0.2, 0.2, 0.2]
#sense variables
world=['green', 'red', 'red', 'green', 'green']
measurements = ['red', 'red']
pHit = 0.6
pMiss = 0.2
#move variables
motions = [1,1]
#measurement
def update(mean1, var1, mean2, var2):
new_mean = float(var2 * mean1 + var1 * mean2) / (var1 + var2)
new_var = 1./(1./var1 + 1./var2)
return [new_mean, new_var]
#motion
def predict(mean1, var1, mean2, var2):
new_mean = mean1 + mean2
new_var = var1 + var2
#state = initial state, [intial_x_pos, velocity]
#dt = change in time
#predicted_state = change in state.
#constant velocity model, displacement
def predict_state_mtx(state, dt):
const_vel_matrix = matrix.Matrix([ [1, dt],
[0, 1] ])
predicted_state = const_vel_matrix * state
return predicted_state
#Vector Addition
def add(vector_1, vector_2):
if len(vector_1) != len(vector_2):
print("error! vectors must be same size to add")
return
new_vector = []
for i in range(len(vector_1)):
new_vector.append(vector_1[i] + vector_2[i])