I am currently working on setting up a constrained regression in Python using
import statsmodels.api as sm
model = sm.GLM(Y,X)
model.fit_constrained
'''Setting the restrictions on parameters in the form of (R, q), where R
and q are constraints' matrix and constraints' values, respectively. As
for the restriction in the aforementioned regression model, i.e.,
c = b - 1 or b - c = 1, R = [0, 1, -1] and q = 1.'''
function from StatsModel but running into some issues when I try to set it up with multiple constraints. I have seven coefficients, including a constant. I want to set it up so that a weighted sum of dummy 1 and dummy 2 equals zero and a weighted sum of dummy 3 and dummy 4 equals zero. To use a single constraint example,
results = model.fit_constrained(([0, 0, 0, a, b, 0, 0], 0))
where a and b are the weights on dummy 3 and dummy 4 and are variables I've predefined.
If I didn't have the a and b variables, and the dummies were equally weighted, I could just use the syntax
fit_constrained('Dummy1 + Dummy2, Dummy3 + Dummy4')
but when I try to use a similar syntax using
results = model.fit_constrained(([0, 0, 0, a, b, 0, 0], 0),([0, c, d, 0, 0, 0, 0], 0))
I get the error
ValueError: shapes (2,) and (7,6) not aligned: 2 (dim 0) != 7 (dim 0)
Does anyone have any ideas? Thanks so much!
I am still not sure which model you are running (posting a Minimal, Complete, and Verifiable example would certainly help), but the following should work for GLMs. From the docs, we have,
constraints (formula expression or tuple) – If it is a tuple, then the constraint needs to be given by two arrays (constraint_matrix, constraint_value), i.e. (R, q). Otherwise, the constraints can be given as strings or list of strings. see t_test for details.
This implies the function call should be along the following lines,
R = [[0, 0, 0, a, b, 0, 0],
[0, c, d, 0, 0, 0, 0]]
q = [0, 0]
results = model.fit_constrained((R, q))
This should work, but since we do not have your model I do not know for sure if R * params = q, which must hold according to the documentation.
Related
(My previous posting has just been closed. However, I cannot see what's inappropriate with the question.)
I'm dealing with a linear equations-solving problem, in which the value for each variable is either 0 or 1.
Hopefully, I would like to develop a solver that can tell whether the value for each variable is definitely 0 or 1. For the final output, the value would be assigned to the variable if it is solved; otherwise it would be assigned None.
For example, the inputs of
a + b + c = 1
b + c = 1
should generate the outputs of
{a=0, b=None, c=None}
And the inputs of
a + b + 2c + d = 2
a + d = 1
should give
{a=None, b=1, c=0, d=None}
As far as I know, there already exist some general linear solvers in python (e.g. numpy.linalg.solve). Is it possible to utilize them and with modifications? If no, what is the recommended approach instead?
Thank you~
Your idea is very close. np.linalg.solve(a,b) can only be used, if a is square and of full-rank, i.e., all rows (or, equivalently, columns) must be linearly independent. Otherwise use for instance lstsq for the least-squares best "solution" of the system/equation.
import numpy as np
A = np.array([[1, 1, 1], [0, 1, 1]])
B = np.array([1, 1])
X = np.linalg.lstsq(A, B)[0] #only interested of the best solution
###solution for [a, b, c]:
###[-1.11022302e-16 5.00000000e-01 5.00000000e-01]
A = np.array([[1, 1, 2, 1], [1, 0, 0, 1]])
B = np.array([2, 1])
X = np.linalg.lstsq(A, B)[0]
###solution for [a, b, c, d]:
###[0.5 0.2 0.4 0.5]
Question
I want to show the steps of calculation (for example, in text book) in markdown file which is created by a python code. here is what I need in original python code
from sympy import *
angle = 60 # this will be changed to created different markdown files
theta = symbols('ss')
x = symbols('xx')
a = Matrix([[cos(theta), -sin(theta), 0], [sin(theta), cos(theta), 0], [0, 0, 1]])
b = Matrix([[x, 0, 0], [0, x, 1], [0, 0, 0]])
print(
'$$',
latex(a), latex(b), '=', # step 1
latex(a).replace('ss', latex(rad(angle))), latex(b).replace('xx', '2'), '=', # step 2
latex(a.subs('ss', rad(60))), latex(b.subs('xx', '2')), '=', # step 3
latex((a*b).subs({'ss': rad(60), 'xx':2}).evalf(2)), # step 4
'$$'
)
you may find that step 1 lists the common matrix, step 2 substitutes the element of matrix by given value, step 3 calculates/simplifies the matrix and step 4 evaluates the matrix elements to float form.
There are too many calls of latex which make the code too long and hard to read.
First try
I write
from sympy import *
class T_Rotate(object):
def __init__(self, theta):
self.eq = Matrix([[cos(theta), -sin(theta), 0], [sin(theta), cos(theta), 0], [0, 0, 1]])
self.str = latex(self.eq)
def __mul__(self, mat):
return self.eq * mat
def __rmul__(self, mat):
return mat * self.eq
a = T_Rotate(60)
b = Matrix([[1, 0, 0], [0, 1, 1], [0, 0, 0]])
print('$$a*b = %s * %s = %s$$' % (a.str, latex(b), latex(a*b)))
print('$$b * a = %s * %s = %s$$' % (latex(b), a.str, latex(b*a)))
but above a * b is a wrong answer which is a 3*3 matrix but whose elements are all 3*3 matrix!
so, what is the problem?
Further thought
In case the above be fixed, there are still call of latex function. Any hints to wrap sympy expression so that the python code can be more terse?
thanks
Now I wrote https://github.com/retsyo/expand_expression to answer the post partly. But I am also still seeking for a more common method without supplying every functions by the user
I released it for it can help others. Here is a example:
If you have defined a function, then you can use it like
T1 = T_Rotate('pi/6 + pi/2', useRad=True)
fOut.write(latexExpression('T1'))
Is it easy? I think so.
Given the product of a matrix and a vector
A.v
with A of shape (m,n) and v of dim n, where m and n are symbols, I need to calculate the Derivative with respect to the matrix elements.
I haven't found the way to use a proper vector, so I started with 2 MatrixSymbol:
n, m = symbols('n m')
j = tensor.Idx('j')
i = tensor.Idx('i')
l = tensor.Idx('l')
h = tensor.Idx('h')
A = MatrixSymbol('A', n,m)
B = MatrixSymbol('B', m,1)
C=A*B
Now, if I try to derive with respect to one of A's elements with the indices I get back the unevaluated expression:
diff(C, A[i,j])
>>>> Derivative(A*B, A[i, j])
If I introduce the indices in C also (it won't let me use only one index in the resulting vector) I get back the product expressed as a Sum:
C[l,h]
>>>> Sum(A[l, _k]*B[_k, h], (_k, 0, m - 1))
If I derive this with respect to the matrix element I end up getting 0 instead of an expression with the KroneckerDelta, which is the result that I would like to get:
diff(C[l,h], A[i,j])
>>>> 0
I wonder if maybe I shouldn't be using MatrixSymbols to start with. How should I go about implementing the behaviour that I want to get?
SymPy does not yet know matrix calculus; in particular, one cannot differentiate MatrixSymbol objects. You can do this sort of computation with Matrix objects filled with arrays of symbols; the drawback is that the matrix sizes must be explicit for this to work.
Example:
from sympy import *
A = Matrix(symarray('A', (4, 5)))
B = Matrix(symarray('B', (5, 3)))
C = A*B
print(C.diff(A[1, 2]))
outputs:
Matrix([[0, 0, 0], [B_2_0, B_2_1, B_2_2], [0, 0, 0], [0, 0, 0]])
The git version of SymPy (and the next version) handles this better:
In [55]: print(diff(C[l,h], A[i,j]))
Sum(KroneckerDelta(_k, j)*KroneckerDelta(i, l)*B[_k, h], (_k, 0, m - 1))
I'm working on a project involving binary patterns (here np.arrays of 0 and 1).
I'd like to modify a random subset of these and return several altered versions of the pattern where a given fraction of the values have been changed (like map a function to a random subset of an array of fixed size)
ex : take the pattern [0 0 1 0 1] and rate 0.2, return [[0 1 1 0 1] [1 0 1 0 1]]
It seems possible by using auxiliary arrays and iterating with a condition, but is there a "clean" way to do that ?
Thanks in advance !
The map function works on boolean arrays too. You could add the subsample logic to your function, like so:
import numpy as np
rate = 0.2
f = lambda x: np.random.choice((True, x),1,p=[rate,1-rate])[0]
a = np.array([0,0,1,0,1], dtype='bool')
map(f, a)
# This will output array a with on average 20% of the elements changed to "1"
# it can be slightly more or less than 20%, by chance.
Or you could rewrite a map function, like so:
import numpy as np
def map_bitarray(f, b, rate):
'''
maps function f on a random subset of b
:param f: the function, should take a binary array of size <= len(b)
:param b: the binary array
:param rate: the fraction of elements that will be replaced
:return: the modified binary array
'''
c = np.copy(b)
num_elem = len(c)
idx = np.random.choice(range(num_elem), num_elem*rate, replace=False)
c[idx] = f(c[idx])
return c
f = lambda x: True
b = np.array([0,0,1,0,1], dtype='bool')
map_bitarray(f, b, 0.2)
# This will output array b with exactly 20% of the elements changed to "1"
rate=0.2
repeats=5
seed=[0,0,1,0,1]
realizations=np.tile(seed,[repeats,1]) ^ np.random.binomial(1,rate,[repeats,len(seed)])
Use np.tile() to generate a matrix from the seed row.
np.random.binomial() to generate a binomial mask matrix with your requested rate.
Apply the mask with the xor binary operator ^
EDIT:
Based on #Jared Goguen comments, if you want to change 20% of the bits, you can elaborate a mask by choosing elements to change randomly:
seed=[1,0,1,0,1]
rate=0.2
repeats=10
mask_list=[]
for _ in xrange(repeats):
y=np.zeros(len(seed),np.int32)
y[np.random.choice(len(seed),0.2*len(seed))]=1
mask_list.append(y)
mask = np.vstack(mask_list)
realizations=np.tile(seed,[repeats,1]) ^ mask
So, there's already an answer that provides sequences where each element has a random transition probability. However, it seems like you might want an exact fraction of the elements to change instead. For example, [1, 0, 0, 1, 0] can change to [1, 1, 0, 1, 0] or [0, 0, 0, 1, 0], but not [1, 1, 1, 1, 0].
The premise, based off of xvan's answer, uses the bit-wise xor operator ^. When a bit is xor'd with 0, it's value will not change. When a bit is xor'd with 1, it will flip. From your question, it seems like you want to change len(seq)*rate number of bits in the sequence. First create mask which contains len(seq)*rate number of 1's. To get an altered sequence, xor the original sequence with a shuffled version of mask.
Here's a simple, inefficient implementation:
import numpy as np
def edit_sequence(seq, rate, count):
length = len(seq)
change = int(length * rate)
mask = [0]*(length - change) + [1]*change
return [seq ^ np.random.permutation(mask) for _ in range(count)]
rate = 0.2
seq = np.array([0, 0, 1, 0, 1])
print edit_sequence(seq, rate, 5)
# [0, 0, 1, 0, 0]
# [0, 1, 1, 0, 1]
# [1, 0, 1, 0, 1]
# [0, 1, 1, 0, 1]
# [0, 0, 0, 0, 1]
I don't really know much about NumPy, so maybe someone with more experience can make this efficient, but the approach seems solid.
Edit: Here's a version that times about 30% faster:
def edit_sequence(seq, rate, count):
mask = np.zeros(len(seq), dtype=int)
mask[:len(seq)*rate] = 1
output = []
for _ in range(count):
np.random.shuffle(mask)
output.append(seq ^ mask)
return output
It appears that this updated version scales very well with the size of seq and the value of count. Using dtype=bool in seq and mask yields another 50% improvement in the timing.
I have an object which is described by two quantities, A and B (in real case they can be more than two). Objects are correlated depending on the value of A and B. In particular I know the correlation matrix for A and for B. Just as example:
a = np.array([[1, 1, 0, 0],
[1, 1, 0, 0],
[0, 0, 1, 1],
[0, 0, 1, 1]])
b = np.array([[1, 1, 0],
[1, 1, 1],
[0, 1, 1]])
na = a.shape[0]
nb = b.shape[0]
correlation for A:
so if an element has A == 0.5 and the other equal to A == 1.5 they are fully correlated (red). Otherwise if an element has A == 0.5 and the second item has A == 3.5 they are uncorrelated (blue).
Similarly for B:
Now I want multiply the two correlation matrixes, but I want to obtain as final matrix a matrix with two axis, where the new axes are a folded version of the original axes:
def get_folded_bin(ia, ib):
return ia * nb + ib
here what I am doing:
result = np.swapaxes(np.tensordot(a, b, axes=0), 1, 2).reshape(na* nb, na * nb)
visually:
and in particular this must hold:
for ia1 in xrange(na):
for ia2 in xrange(na):
for ib1 in xrange(nb):
for ib2 in xrange(nb):
assert(a[ia1, ia2] * b[ib1, ib2] == result[get_folded_bin(ia1, ib1), get_folded_bin(ia2, ib2)])
actually my problem is to do it with more quantities (A, B, C, ...) in a general way. Maybe there is also a simpler function within numpy to do that.
np.einsum lets you simplify the tensordot expression a bit:
result = np.einsum('ij,kl->ikjl',a,b).reshape(-1, na * nb)
I don't think there's a way of eliminating the reshape.
It may also be easier to generalize to more arrays, though I wouldn't get carried away with too many iteration variables in one einsum expression.
I think finally I have found a solution:
np.kron(a,b)
and then I can compose with
np.kron(np.kron(a,b), c)