简体   繁体   中英

I want to calculate slope and intercept of a linear fit using pykalman module

Consider the linear regression of Y on X , where (xi, yi) = (2, 7), (0, 2), (5, 14) for i = 1, 2, 3 . The solution is (a, b) = (2.395, 2.079) , obtained using the regression function on a hand-held calculator.

I want to calculate the slope and the intercept of a linear fit using the pykalman module. I'm getting

ValueError: The shape of all parameters is not consistent.  Please re-check their values. 

I'd really appreciate if someone would help me.

Here is my code :

from pykalman import KalmanFilter
import numpy as np

measurements = np.asarray([[7], [2], [14]])

initial_state_matrix = [[1], [1]]

transition_matrix = [[1, 0], [0, 1]]

observation_covariance_matrix = [[1, 0],[0, 1]]

observation_matrix = [[2, 1], [0, 1], [5, 1]]

kf1 = KalmanFilter(n_dim_state=2, n_dim_obs=6, 
                   transition_matrices=transition_matrix, 
                   observation_matrices=observation_matrix, 
                   initial_state_mean=initial_state_matrix, 
                   observation_covariance=observation_covariance_matrix)

kf1 = kf1.em(measurements, n_iter=0)

(smoothed_state_means, smoothed_state_covariances) = kf1.smooth(measurements)

print smoothed_state_means

Here's the code snippet:

from pykalman import KalmanFilter

import numpy as np

kf = KalmanFilter()

(filtered_state_means, filtered_state_covariances) = kf.filter_update(filtered_state_mean = [[0],[0]], filtered_state_covariance = [[90000,0],[0,90000]], observation=np.asarray([[7],[2],[14]]),transition_matrix = np.asarray([[1,0],[0,1]]), observation_matrix = np.asarray([[2,1],[0,1],[5,1]]), observation_covariance = np.asarray([[.1622,0,0],[0,.1622,0],[0,0,.1622]]))

print filtered_state_means

print filtered_state_covariances

for x in range(0, 1000):

    (filtered_state_means, filtered_state_covariances) = kf.filter_update(filtered_state_mean = filtered_state_means, filtered_state_covariance = filtered_state_covariances, observation=np.asarray([[7],[2],[14]]),transition_matrix = np.asarray([[1,0],[0,1]]), observation_matrix = np.asarray([[2,1],[0,1],[5,1]]), observation_covariance = np.asarray([[.1622,0,0],[0,.1622,0],[0,0,.1622]]))

print filtered_state_means

print filtered_state_covariances

filtered_state_covariance was chosen large because we have no idea where our filter_state_mean is initially and the observations are just [[y1],[y2],[y3]]. Observation_matrix is [[x1,1],[x2,1],[x3,1]] thus giving second element as our intercept. Imagine it like this y1 = m*x1+c where m and c are slope and intercept respectively. In our case filtered_state_mean = [[m],[c]]. Notice that the new filtered_state_means is used as filtered_state_mean for new kf.filter_update() (in iterating loop) because we now know where mean lies with filtered_state_covariance = filtered_state_covariances. Iterating it 1000 times converges the mean to real value. If you want to know about the function/method used the link is: https://pykalman.github.io/

If the system state does not change between measurements (also called vacuous movement step), then transition_matrix φ = I.

I'm not sure if what I'm going to say now is true or not. So please correct me if I am wrong

observation_covariance matrix must be of size mxm where m is the number of observations (in our case = 3). The diagonal elements are just variances I believe variance_y1, variance_y2 and variance_y3 and off-diagonal elements are covariances. For example element (1,2) in matrix is standard deviation of y1,(COMMA NOT PRODUCT) standard deviation of y2 and is equal to element (2,1). Similarly for other elements. Can someone help me include uncertainty in x1, x2 and x3. I mean how do you implement uncertainties in x in the above code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM