0

I need to find the alpha value in [0,1] of a linear combination alpha*Id+(1-alpha)*M, where Id is the identity matrix, M is a given matrix, such that this linear combination has given mean.

At the moment I am using scipyt.optimize.fsolve but it does not admit the range [0,1] as an input. Any suggestion ?

userLx
  • 81
  • 8

1 Answers1

0

You can define alpha using a sigmoid function:

alpha = 1/(1+exp(-x))

https://en.wikipedia.org/wiki/Sigmoid_function

Based on this definition, alpha will always be in the range [0, 1]. Then, you can change the target of the optimization in scipy.optimize.fsolve to calibrate the value of x instead of alpha directly.

The variable x is free of constraints, so any optimization method works.


PS. This technique is very common in machine learning.


PS2. Adding constraints to an optimizer is only important when they are not being fulfilled. So for example, if your alpha solution is already in the range [0,1], then you can keep the optimizer as it is.

C-3PO
  • 1,181
  • 9
  • 17
  • Thanks for the suggestion. But I have to change also the definition of the function if I change the target ? For example, for a certain matrix, `fsolve` gives 0.28 as root. This is a good value for `alpha` because is in [0,1]. If this value was `x`, then `alpha=1/(1+exp(-0.28)) = 0.57` and the result would be wrong – userLx Nov 19 '22 at 11:19
  • Yes, the function has to be changed, such that `x` is the target. But if your system is already working without any constraints (since you get alpha = 0.28), then you can keep everything as it is. Adding constraints is only important, when they are not being respected. – C-3PO Nov 19 '22 at 11:28
  • It is working now with a specific given matrix. In future the input Matrix M can change and the constraints might not be respected. – userLx Nov 19 '22 at 11:29
  • Then, try optimizing for `x` :) – C-3PO Nov 19 '22 at 11:30
  • yes, thanks. I guess I also have to change the starting point x0 which is one of the input of fsolve ? When the target is alpha, I set it to 0, if the target is x, what is x0? – userLx Nov 19 '22 at 11:32
  • You can give a negative value, like `x0=-4`. This yields `alpha0=0.017986` – C-3PO Nov 19 '22 at 11:32