Naming conventions for mathematical models


#1

I need a different set of naming conventions when using Julia to write mathematical models than what is generally recommended for general purpose code. For example, it is often important to be able to distinguish at a glance between model variables (which change during the course of a single model run) and model parameters (which don’t, although they may vary from one run to another). Both of these would be represented by ordinary variables in Julia.

So far I’ve been using the same conventions for my Julia models that I originally came up with for models written in GAMS or AMPL. I use CamelCase with an initial capital for model variables and lower_case_with_underscore for parameters. This works fine for me, but since I intend to start open-sourcing my models I’m becoming uncomfortable with the fact that my scheme conflicts with standard Julia naming conventions (which reserves CamelCase for modules and types).

The distinction between variables and parameters is important because you often need to be aware of where your nonlinearities are. For example, using my convention I can immediately tell that both these equations are nonlinear, and would require a nonlinear solver:

BioNPP = base_NPP * (1 + fertilization*log(Concentration[:CO2]/preindustrial_CO2))
dt_BioReservoir = BioNPP*biobox_weight - BioReservoir*bio_Q10_factor^(TempLand/10)./biobox_time

… while these are linear and are therefore much easier to solve:

BioNPP = base_NPP * (1 + fertilization*log(concentration[:CO2]/preindustrial_CO2))
dt_BioReservoir = BioNPP*biobox_weight - BioReservoir*bio_Q10_factor^(temp_land/10)./biobox_time

I use another convention here as well: variables prefixed with dt_ indicate a time derivative. Other models may have other requirements. For example, in a mixed-integer programming model written in JuMP you may want to use different naming schemes for integer and continuous variables.

So I’m curious what other modelers have come up with. Do you sacrifice mathematical clarity to follow standard Julian conventions, or do you break conventions when there is a good reason to do so? Is there an alternative convention I can use that works better for mathematical models in Julia? I know someone who prefixes all parameters with p_ and all variables with v_, but that just looks too ugly to me.


#2

Why do you find it so ugly, given that you use dt_ for some variables? A possibility would be to use a Unicode character as as a prefix.


#3

Here’s what those assignments look like with p_ and v_ prefixes:

v_bioNPP = p_base_NPP * (1 + p_fertilization*log(v_concentration[:CO2]/p_preindustrial_CO2))
dt_v_bioreservoir = v_bioNPP*p_biobox_weight - v_bioreservoir*p_bio_Q10_factor^(v_temp_land/10)./p_biobox_time

That is just objectively ugly. :slight_smile:

Seriously though. Variables and parameters aren’t as visually distinguished as in my scheme, so your brain has to spend some processing power parsing the initial letter instead of immediately “seeing” the difference. The prefix version is also more verbose, costing 10-15 extra characters per line (so more expressions require line breaks and get harder to read).

I don’t mind the dt_ prefix because it looks similar to how you would write it on paper, and not many variables are derivatives anyway so the code doesn’t get polluted with underscores.

The Unicode prefix idea is interesting, I’ll see if I can find an unobtrusive but clear character. It might even look nice when combined with some tweaking of the syntax highlighter of my editor.


#4

you’d find this fascinating.