functions.py 9.39 KB
Newer Older
Emmanuel PIGNAT's avatar
Emmanuel PIGNAT committed
1
2
3
4
5
6
import numpy as np
from scipy.interpolate import interp1d

colvec = lambda x: np.array(x).reshape(-1, 1)
rowvec = lambda x: np.array(x).reshape(1, -1)

Emmanuel PIGNAT's avatar
Emmanuel PIGNAT committed
7
8
realmin = np.finfo(np.float32).tiny
realmax = np.finfo(np.float32).max
Emmanuel PIGNAT's avatar
Emmanuel PIGNAT committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253

def limit_gains(gains, gain_limit):
	"""

	:param gains:			[np.array]
	:param gain_limit 	[float]

	:return:
	"""
	u, v = np.linalg.eig(gains)
	u[u > gain_limit] = gain_limit

	return v.dot(np.diag(u)).dot(np.linalg.inv(v))


def eigs(X):
	''' Sorted eigenvalues and eigenvectors'''
	D, V = np.linalg.eig(X)
	sort_perm = D.argsort()[::-1]
	return D[sort_perm], V[:, sort_perm]


def mul(X):
	''' Multiply an array of matrices'''
	x = X[0];
	for y in X[1:]:
		x = np.dot(x, y)
	return x


def spline(x, Y, xx, kind='cubic'):
	''' Attempts to imitate the matlab version of spline'''
	from scipy.interpolate import interp1d
	if Y.ndim == 1:
		return interp1d(x, Y, kind=kind)[xx]
	F = [interp1d(x, Y[i, :]) for i in range(Y.shape[0])]
	return np.vstack([f(xx) for f in F])


def get_canonical_system(n_vars, dt):
	''' Create a n_vars discrete canonical system with time step dt. '''
	# Continouse dynamical system:
	A = np.kron([[0, 1], [0, 0]], np.eye(n_vars))
	B = np.kron([[0], [1]], np.eye(n_vars))
	C = np.kron(np.eye(2), np.eye(n_vars))

	# Discretize:
	Ad = A * dt + np.eye(A.shape[0])
	Bd = B * dt

	return (Ad, Bd, C)


def get_dynamical_feature_matrix(n_varspos, n_derivs, n_data, n_samples, dt):
	'''Get the dynamical feature matrix that extracts n_derivs dynamical features from
	a n_varspos*n_data*n_samples vector of data points using dt as time discritization.

	Output: (PHI1,PHI,T1,T)
	o PHI1: Dynamical feature matrix for one sample
	o PHI : Dynamical feature matrix for n_samples
	'''

	T1 = n_data
	T = n_data * n_samples

	# Create op Matrix for one dimension:
	op1D = np.zeros((n_derivs, n_derivs))
	op1D[0, n_derivs - 1] = 1

	for i in range(1, n_derivs):
		op1D[i, :] = (op1D[i - 1,] - np.roll(op1D[i - 1,], -1)) / dt

	# Extend to other dimensions
	# put the operator in a big matrix that we
	# use for the roll operation
	op = np.zeros((T1 * n_derivs, T1))
	i1 = (n_derivs - 1) * n_derivs
	i2 = n_derivs * n_derivs
	op[i1:i2, 0:n_derivs] = op1D;

	PHI0 = np.zeros((T1 * n_derivs, T1))

	# Create Phi
	for t in range(0, T1 - n_derivs + 1):
		tmp = np.roll(op, t * n_derivs, axis=0)  # Shift in the first dimension
		tmp = np.roll(tmp, t, axis=1)  # Shift in the second dimension
		PHI0 = PHI0 + tmp  # Add to PHI

	# Handle borders:
	for i in range(1, n_derivs):
		op[n_derivs * n_derivs - i,] = 0
		op[:, i - 1] = 0
		tmp = np.roll(op, -i * n_derivs, axis=0)
		tmp = np.roll(tmp, -i, axis=1)
		PHI0 = PHI0 + tmp
	# Construct Phi matrices:
	PHI1 = np.kron(PHI0, np.eye(n_varspos))
	PHI = np.kron(np.eye(n_samples), PHI1)

	return PHI1, PHI


def condition_gaussian(Mu, Sigma, sample, input, output):
	slii = np.ix_(input, input)
	sloi = np.ix_(output, input)
	sloo = np.ix_(output, output)
	slio = np.ix_(input, output)

	InvSigmaInIn = np.linalg.inv(Sigma[slii])
	InvSigmaOutIn = np.dot(Sigma[sloi], InvSigmaInIn)

	MuOut = Mu[output] + np.dot(InvSigmaOutIn,
								(sample - Mu[input]).T)

	SigmaOut = Sigma[sloo] - \
						  np.dot(InvSigmaOutIn, \
								 Sigma[slio])
	return MuOut, SigmaOut


def get_state_prediction_matrix(A, B, Np, **kwargs):
	''' Returns matrix to be used for batch prediction of the state of the discrete system
	x_k+1 = A*x_k + B*u_k
	'''
	# Check if number of control predictions was specified,
	# if not take the same as specified Np
	Nc = kwargs.get('Nc', Np)

	# Get dimensions:
	(nA, mA) = A.shape
	(_, mB) = B.shape

	# Construct Sx:
	Sx = np.zeros((nA * Np, mA))
	c1 = np.zeros((nA * Np, mB))
	Sx[0:nA, ] = A
	c1[0:nA, ] = B

	for kk in range(1, Np):
		ind1 = slice((kk - 1) * nA, kk * nA, 1)
		ind2 = slice(kk * nA, (kk + 1) * nA, 1)
		Sx[ind2, :] = Sx[ind1, :].dot(A)
		c1[ind2, :] = Sx[ind1, :].dot(B)

	Su = np.zeros((Np * nA, mB * Nc))
	for kk in range(0, Nc):
		rInd1 = kk * nA
		rInd2 = (Np - kk) * nA
		cInd = slice(kk * mB, (kk + 1) * mB, 1)

		Su[rInd1::, cInd] = c1[0:rInd2, :]

	return (Su, Sx)


def multi_variate_normal_old(x, mean, covar):
	'''Multi-variate normal distribution

	x: [n_data x n_vars] matrix of data_points for which to evaluate
	mean: [n_vars] vector representing the mean of the distribution
	covar: [n_vars x n_vars] matrix representing the covariance of the distribution

	'''

	# Check dimensions of covariance matrix:
	if type(covar) is np.ndarray:
		n_vars = covar.shape[0]
	else:
		n_vars = 1

	# Check dimensions of data:
	if x.ndim == 1 and n_vars == len(x):
		n_data = 1
	else:
		n_data = x.shape[0]

	diff = (x - mean).T

	# Distinguish between multi and single variate distribution:
	if n_vars > 1:
		lambdadiff = np.linalg.inv(covar).dot(diff)
		scale = np.sqrt(
			np.power((2 * np.pi), n_vars) * (abs(np.linalg.det(covar)) + 1e-200))
		p = np.sum(diff * lambdadiff, 0)
	else:
		lambdadiff = diff / covar
		scale = np.sqrt(np.power((2 * np.pi), n_vars) * covar + 1e-200)
		p = diff * lambdadiff

	return np.exp(-0.5 * p) / scale

def prod_gaussian(mu_1, sigma_1, mu_2, sigma_2):
	prec_1 = np.linalg.inv(sigma_1)
	prec_2 = np.linalg.inv(sigma_2)
	# Compute covariance of p	roduct:

	Sigma = np.linalg.inv(prec_1 + prec_2)

	# Compute mean of product:
	Mu = Sigma.dot(prec_1.dot(mu_1) + prec_2.dot(mu_2))

	return Mu, Sigma

def mvn_pdf(x, mu, sigma_chol, lmbda, sigma=None, reg=None):
	"""

	:param x: 			np.array([nb_dim x nb_samples])
		samples
	:param mu: 			np.array([nb_states x nb_dim])
		mean vector
	:param sigma_chol: 	np.array([nb_states x nb_dim x nb_dim])
		cholesky decomposition of covariance matrices
	:param lmbda: 		np.array([nb_states x nb_dim x nb_dim])
		precision matrices
	:return: 			np.array([nb_states x nb_samples])
		log mvn
	"""
	N = mu.shape[0]
	D = mu.shape[1]

	if len(x.shape) > 1:  # TODO implement mvn for multiple xs
		raise NotImplementedError

	dx = mu - x

	if reg is not None:
		if isinstance(reg, list):
			reg = np.power(np.diag(reg), 2)
		else:
			reg = np.power(reg * np.eye(D), 2)

		lmbda_ = np.linalg.inv(np.linalg.inv(lmbda) + reg)
		sigma_chol_ = sigma_chol + reg
	else:
		lmbda_ = lmbda
		sigma_chol_ = sigma_chol

	return -0.5 * np.einsum('...j,...j', dx, np.einsum('...jk,...j->...k', lmbda_, dx)) \
		  - D / 2 * np.log(2*np.pi) - np.sum(np.log(sigma_chol_.diagonal(axis1=1, axis2=2)), axis=1)

	# return np.asarray([-0.5 * dx[i].T.dot(lmbda[i]).dot(dx[i]) \
	# 				   - D / 2 * np.log(2 * np.pi) - np.sum(
	# 	np.log(sigma_chol[i].diagonal(axis1=0, axis2=1)), axis=0)
	# 				   for i in range(N)])

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
def multi_variate_t(x, nu, mu, sigma=None, log=True, gmm=False, lmbda=None):
	"""
	Multivariatve T-distribution PDF
	https://en.wikipedia.org/wiki/Multivariate_t-distribution

	:param x:		np.array([nb_samples, nb_dim])
	:param mu: 		np.array([nb_dim])
	:param sigma: 	np.array([nb_dim, nb_dim])
	:param log: 	bool
	:return:
	"""
	from scipy.special import gamma
	if not gmm:
		if type(sigma) is float:
			sigma = np.array(sigma, ndmin=2)
		if type(mu) is float:
			mu = np.array(mu, ndmin=1)
		if sigma is not None:
			sigma = sigma[None, None] if sigma.shape == () else sigma

		mu = mu[None] if mu.shape == () else mu
		x = x[:, None] if x.ndim == 1 else x

		p = mu.shape[0]

		dx = mu - x
		lmbda_ = np.linalg.inv(sigma) if lmbda is None else lmbda

		dist = np.einsum('...j,...j', dx, np.einsum('...jk,...j->...k', lmbda_, dx))
		# (nb_timestep, )

Emmanuel PIGNAT's avatar
Emmanuel PIGNAT committed
285
286
287
288
289
290
291
292
293
		if not log:
			lik = gamma((nu + p)/2) * np.linalg.det(lmbda_) ** 0.5/\
				  (gamma(nu/2) * nu ** (p/2) * np.pi ** (p/2) ) * \
				  (1 + 1/nu * dist) ** (-(nu+p)/2)
			return lik
		else:
			log_lik = np.log(gamma((nu + p)/2)) + 0.5 * np.linalg.slogdet(lmbda_)[1] - \
					  (np.log(gamma(nu / 2)) + (p / 2) * np.log(nu)  + (p / 2) * np.log(np.pi)) + \
					  ((-(nu + p) / 2) * np.log(1 + 1 / nu * dist))
294

Emmanuel PIGNAT's avatar
Emmanuel PIGNAT committed
295
			return log_lik
296
297
298
299
300
	else:
		raise NotImplementedError


def multi_variate_normal(x, mu, sigma=None, log=True, gmm=False, lmbda=None):
Emmanuel PIGNAT's avatar
Emmanuel PIGNAT committed
301
302
303
304
305
306
307
308
309
310
	"""
	Multivariatve normal distribution PDF

	:param x:		np.array([nb_samples, nb_dim])
	:param mu: 		np.array([nb_dim])
	:param sigma: 	np.array([nb_dim, nb_dim])
	:param log: 	bool
	:return:
	"""
	if not gmm:
311
312
313
314
		if type(sigma) is float:
			sigma = np.array(sigma, ndmin=2)
		if type(mu) is float:
			mu = np.array(mu, ndmin=1)
315
316
317
		if sigma is not None:
			sigma = sigma[None, None] if sigma.shape == () else sigma

318
319
		mu = mu[None] if mu.shape == () else mu
		x = x[:, None] if x.ndim == 1 else x
Emmanuel PIGNAT's avatar
Emmanuel PIGNAT committed
320
321

		dx = mu - x
322
		lmbda_ = np.linalg.inv(sigma) if lmbda is None else lmbda
Emmanuel PIGNAT's avatar
Emmanuel PIGNAT committed
323

324
325
326
		log_lik = -0.5 * np.einsum('...j,...j', dx, np.einsum('...jk,...j->...k', lmbda_, dx))

		if sigma is not None:
327
			log_lik -= 0.5 * (x.shape[1] * np.log(2 * np.pi) + np.linalg.slogdet(sigma)[1])
328
		else:
329
			log_lik += 0.5 * np.linalg.slogdet(2 * np.pi * lmbda)[1]
330
331


Emmanuel PIGNAT's avatar
Emmanuel PIGNAT committed
332
333
334
335
336
		return log_lik if log else np.exp(log_lik)
	else:
		raise NotImplementedError


337

Emmanuel PIGNAT's avatar
Emmanuel PIGNAT committed
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
	# # Check dimension of data
	# if x.ndim == 1:
	# 	n_vars = 1
	# 	n_data = len(x)
	# else:
	# 	n_vars, n_data = x.shape
	#
	# if type(covar) == float:
	# 	covar = np.eye(n_vars) * covar
	#
	# diff = x - mean[:, None]
	#
	# # Distinguish between multi and single variate distribution:
	# if n_vars > 1:
	# 	lambdadiff = np.linalg.inv(covar).dot(diff)
	# 	scale = np.sqrt(
	# 		np.power((2 * np.pi), n_vars) * (abs(np.linalg.det(covar)) + 1e-200))
	# 	p = np.sum(diff * lambdadiff, 0)
	# else:
	# 	lambdadiff = diff / covar
	# 	scale = np.sqrt(np.power((2 * np.pi), n_vars) * covar + 1e-200)
	# 	p = diff * lambdadiff
	#
	# if not log:
	# 	return np.exp(-0.5 * p) / scale
	# else:
	# 	return -0.5 * p - np.log(scale)