GLM.jl

GLM.jl is a library for fitting generalized linear models in Julia.

MathOptAI.jl supports embedding two types of regression models from GLM:

  • GLM.lm(X, Y)
  • GLM.glm(X, Y, GLM.Bernoulli())

Linear regression

The input x to add_predictor must be a vector with the same number of elements as columns in the training matrix. The return is a vector of JuMP variables with a single element.

julia> using GLM, JuMP, MathOptAI
julia> X, Y = rand(10, 2), rand(10);
julia> predictor = GLM.lm(X, Y);
julia> model = Model();
julia> @variable(model, x[1:2]);
julia> y, formulation = MathOptAI.add_predictor(model, predictor, x);
julia> y1-element Vector{JuMP.VariableRef}: moai_Affine[1]
julia> formulationAffine(A, b) [input: 2, output: 1] ├ variables [1] │ └ moai_Affine[1] └ constraints [1] └ 0.5437974486821808 x[1] + 0.18582208565812255 x[2] - moai_Affine[1] = 0

Logistic regression

The input x to add_predictor must be a vector with the same number of elements as columns in the training matrix. The return is a vector of JuMP variables with a single element.

julia> using GLM, JuMP, MathOptAI
julia> X, Y = rand(10, 2), rand(Bool, 10);
julia> predictor = GLM.glm(X, Y, GLM.Bernoulli());
julia> model = Model();
julia> @variable(model, x[1:2]);
julia> y, formulation = MathOptAI.add_predictor(model, predictor, x);
julia> y1-element Vector{JuMP.VariableRef}: moai_Sigmoid[1]
julia> formulationAffine(A, b) [input: 2, output: 1] ├ variables [1] │ └ moai_Affine[1] └ constraints [1] └ -0.6072438616223336 x[1] + 0.5148084413475199 x[2] - moai_Affine[1] = 0 MathOptAI.Sigmoid() ├ variables [1] │ └ moai_Sigmoid[1] └ constraints [3] ├ moai_Sigmoid[1] ≥ 0 ├ moai_Sigmoid[1] ≤ 1 └ moai_Sigmoid[1] - (1.0 / (1.0 + exp(-moai_Affine[1]))) = 0

DataFrames

DataFrames.jl can be used with GLM.jl.

The input x to add_predictor must be a DataFrame with the same feature columns as the training DataFrame. The return is a vector of JuMP variables, with one element for each row in the DataFrame.

julia> using DataFrames, GLM, JuMP, MathOptAI
julia> train_df = DataFrames.DataFrame(x1 = rand(10), x2 = rand(10));
julia> train_df.y = 1.0 .* train_df.x1 + 2.0 .* train_df.x2 .+ rand(10);
julia> predictor = GLM.lm(GLM.@formula(y ~ x1 + x2), train_df);
julia> model = Model();
julia> test_df = DataFrames.DataFrame( x1 = rand(6), x2 = @variable(model, [1:6]), );
julia> test_df.y, _ = MathOptAI.add_predictor(model, predictor, test_df);
julia> test_df.y6-element Vector{JuMP.VariableRef}: moai_Affine[1] moai_Affine[1] moai_Affine[1] moai_Affine[1] moai_Affine[1] moai_Affine[1]