'Trouble with using a uniform distribution variable in an equation
I am supposed to plug in the parameters A and B which are uniform distributions, but I do not know the proper way to plug them into the state model. so it gives me the error, " setting an array element with a sequence."
x[k] = [(k+1)A(1-B)+0.35**(1-B)]**(1/(1-B))
'''
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
import scipy.io as sci
N = 500 #recommend number
Q = 0.1
R = 0.001
x = np.zeros(N)
y = np.zeros(N)
def PF(y,N,R):
x_estimate = np.zeros([len(y),N]) #Tool Wear
p_estimate = np.zeros([2,N]) #Parameter A and B in the Tool wear model
x_estimate = x_estimate[0]+ np.random.rand(N)
p_estimate[0,:] = np.random.uniform(0.1, 3, N) #Parameter A initialization
p_estimate[1,:] = np.random.uniform(0.001, 0.1, N) #Parameter B initialization
y_estimate = np.zeros([len(y),N])
w = np.zeros(N)
for i in range(len(y)):
#Prediction (State Distribution)
x_estimate[i] = ((x_estimate[i]*p_estimate[0,:]*(1-p_estimate[1,:]))+(0.35)**(1-p_estimate[1,:]))**(1/(1-p_estimate[1,:])) + 0.01
y_estimate[i] = x_estimate[i] + 0.01
#Calculate Weights
w = 1/(np.sqrt(2*np.pi)*np.sqrt(R)) * np.exp(-1*(y[i+1]-y_estimate[i+1])**2/(2*R))
#normalize weights
w_sum = np.sum(w)
w = w/w_sum
#particle resampling
for j in range(N):
rand = np.random.rand(1)
w_c = 0
for k in range(N):
w_c += w[K]
if w_c >= rand:
x_estimate[i+1,j] = x_estimate[i+1,k]
p_estimate[i+1,j] = p_estimate[i+1,k]
break
return x_estimate
N = 100
x_estimate = PF(y, N , R)
x_estimate_mean = np.mean(x_estimate,1)
plt.plot(np.arange(50), x, marker='*')
plt.plot(np.arange(50), x_estimate_mean, 'r',marker='*')
plt.figure()
plt.hist(x_estimate[10], bins=30)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
