Optimization on MSE Not Working Correctly

Test your code in smaller snippets instead of attempting to test it in larger chunks. Add print test statements to verify inputs and outputs. If the outputs are not providing the expected output, then you have a smaller code to focus on, making it easier to debug. For example, for demo purposes, assume that the left sided image represents the function minimize_MSE. Instead of attempting to test it as a whole, test individual parts of it in isolation. This way you verify that the smaller parts are working as expected. This will greatly simplify your code verification methodology.

For example, if you have a function that accepts two parameters, and provides the multiplication of the two parameters, test it for various input pairs. Say you enter 5 and 6, the output should be 30. If you enter 10 and 20, and the output is 345, debug the function. The same goes for iterable loops. Test them in isolation with known inputs and expected outputs.

Here is another example. In the following lines of code, have you verified that you are obtaining the expected values for these three variables?

k_off_sampled = np.random.uniform(*param_bounds[0])
P_in_sampled = np.random.uniform(*param_bounds[1])
P_out_sampled = np.random.uniform(*param_bounds[2])

Test them by printing their values:

print(k_off_sampled)
print( P_in_sampled)
print(P_out_sampled)

Are the values within reason?

How about the input arguments to the function:

k_off, P_in, P_out = parameters
print(k_off)
print(P_in)
print(P_out)

Have you verified and tested the function with known values?
Test your code incrementally verifying at each step along the way that the code is providing the expected outputs as you’re testing each line of code, one at a time.

Just performed a quick check of the variables currently being used in the function. I found the following variables which have yet to be assigned yet were being used in mathematical operations or in for loop iterables.

N_values
delta_r
kappa_t
lambda_max

They also were not being passed in as arguments. Are these global variables? If not, you have to assign them (define them).

@onePythonUser I am sorry to say this, but please don’t try to give advice to others when your understanding of python is also still a bit shaky.

objective_function is a perfectly acceptable name within the realm of optimization problems. As long as there is only one such function per file, this is fine.

Because this is a pretty standard way to get this float value, and there isn’t really a strictly better one.

This is a standard pattern to signal that you don’t care about the variable. This is fine.

Yes, this is basic python syntax to unpack arguments.

Your limited understanding of python is not helping OP solve their problem.

1 Like

Yes, I stand corrected on the *param_bounds[n] reference.

One potential solution is to add margins above and below the desired test value. However, the margins will be dependent on how tightly you want to test the measured value. In the following code snippet, I have chosen arbitrary values. You will have to decide the acceptable values for the MARGINS based on your application.

measured_value = 25.45 # Assume this is a value that you measured or calculation result, etc.
DESIRED_VALUE = 25.0 # Ideal
MARGIN = 0.75        # Margins that are acceptable away from the ideal

if measured_value >= DESIRED_VALUE - MARGIN and measured_value <= DESIRED_VALUE + MARGIN:
    # Execute code if condition is satisfied

Refer to the sketch. The blue line is the desired test value (DESIRED_VALUE). If your value falls within the red square, then it will satisfy the test condition.

potential_test_method

So, for your particular case:

# Set the calculation result to your desired test value for the conditional statement.
DESIRED_VALUE = (IC_50*lambda_0*k_on*current_z[1])/(lambda_max * current_z[0])
MARGIN = 0.5 # Arbitrary value for test purposes - you decide what value is acceptable for your applicaiton

if current_z[2] >= DESIRED_VALUE - MARGIN and current_z[2] <= DESIRED_VALUE + MARGIN:
    # Execute code if condition is satisfied

Does this make sense to you?

Hello all. Thank you for your valuable input. I rectified my code by using a different optimization technique altogether with perfectly logical syntax to what my problem was, i.e. what I did or did not care about.

Thank you for your efforts and advice all around.