1 year ago

#348546

test-img

JED HK

Modelling with scipy with a binary (dummy) variable

I am trying to optimize an equation in Python. The equation has to account for sex as a binary variable.

Until now, I modelled the equations separately for sex (i.e., I optimized for male, then optimized for female), and this was fine.

Now, I need to model them together in one equation.

Since the reference data differs between the males and females, I am not sure how I can do this because optimizing for one sex then worsens the results for the other (as you'd expect, since the references are different).

So, I need a unified model that knows sex can be 1 or 0, and the reference values will be different whether it is 1 or 0, and will optimize the rest of the values in the equation to account for this (Note: My original function is longer and more complicated. This is worth mentioning since obviously in this simplified scenario it's a lot easier to deal with).

The equation would look like this:

W(time|SEX) = G*(time) + Bi *(SEX)

I use this function:

def BinaryVar(parameters, time):
    if sex == 'Male':
        sex_dummy = 0
    else:
        sex_dummy = 1
    return parameters[0]*time + parameters[1]*sex_dummy

to then optimize using scipy's least squares further down. Parameters is a list or parameters I want to optimize and time is a list of time points.

scipy

binary

modeling

scipy-optimize

0 Answers

Your Answer

Accepted video resources