Reputation: 608
I have about 100 7x7 matrices of dependent variables (so 49 dependent variables). My independent variable is time. I am doing a physics project in which I am supposed to get a matrix function (every element of the matrix is a function of time) by solving an ODE. I used numpy's ODE solver which goves me numerical answers of my matrix function evaluated at different times. Now with these matrices and times I want to find a time-dependent expression for each element matrix to get the time-dependent matrix. I have heard that what I should do is find a hat matrix and I guess that the predicted or fitted values would be my 7x7 matrices and the response values would be the array of times. So how can I find this hat matrix in Python?
I was originally thinking of doing polynomial regression in scikit-learn using their LinearRegression model. Will that work? Is there a possible way in StatsModel, or better, in scipy or numpy?
Basically I want to go from:
to:
Clearly, I will use more test cases, but this is the overall idea. So I will have univariate X (X will be an array of the different times) and multivariate Y (Y will be the matrices evaluated at different times)
In the above example, t=1 would be included in the X array and the Y array would have the first matrix
Upvotes: 5
Views: 7553
Reputation: 5408
Given that the task you would like to do is the classical linear regression:
Using the matrix notation in numpy
(you would have to manually account for an intercept by adding a row of ones to X) :
import numpy as np
a = np.linalg.inv(np.dot(X.T,X))
c = np.dot(X.T,Y)
b = np.dot(a,c)
Using numpy
np.polyfit(X,Y,1)
Using scipy:
scipy.linalg.solve(X,Y)
scipy.stats.linregr(X,Y)
and many more
Upvotes: 6