Machine Learning in Clojure with libpython‑clj: Unlocking Causal Insights Using Microsoft’s EconML [Series 3]

“Beyond A/B testing: causal inference meets functional programming.”

In the first two parts of this series, we kept our core in Clojure while still using the best of Python.

  • Series 1: In the first part, we explore how libpython-clj lets you use Python’s machine learning libraries right from the JVM, without needing to jump between languages.
  • Series 2: In the second part, we explored Bayesian Networks. They are great for building models that actually make sense to humans, not just machines. That is a big deal in fields like healthcare or finance, where you can not mess around with black-box answers. Clarity is important. 

Now, in Series 3, we focus on causal inference with EconML from Microsoft Research. 

  • Predictive models answer “what might happen?”
  • Causal inference asks “why did it happen?” and “what if we change the action?” 

That difference is important when you need decisions you can trust, not just guesswork.

A simple A/B test might say a campaign increased conversions by 5%. EconML goes a layer deeper. It can show that students saw a 20% lift, while retirees saw no change. So you don ot get an average. You get heterogeneous treatment effects across segments. That is what you need to act with confidence.

If you work in Clojure, the process does not really change. You write the same clean and functional code. When you need Python’s causal tools, just use libpython-clj. You run your models, whether they use observational or experimental data. Then send the results right back into your JVM apps, without leaving Clojure.

Where this approach really shines:

  • Dynamic pricing: Set prices by segment, not a bland one-size-fits-all.
  • Marketing: Focus on people who actually benefit, skip the rest.
  • Healthcare: See how treatments work for different patient groups.
  • Policy: Compare what works and break it down by demographic.

No need to make big promises. You get smarter decisions, with models that separate signals through the noise and actually show what’s making a difference for different people.

Visual: Infographic that shows “Prediction: Discount → +5% sales” vs “Causality: Students +20%, retirees no effect.”

🔗 Internal links:

  • [Series 1: ML in Clojure with libpython‑clj]
  • [Series 2: Bayesian Networks with libpython‑clj]

🌐 External link: Microsoft Research EconML – This page provides the official library overview with docs, papers, and examples.

The Problem with Traditional ML

Limits of A/B Testing

Most traditional ML models are built on the idea that:

  • One pattern fits the whole population
  • One prediction applies to everyone
  • One model captures the “average” relationship

This works only when people behave similarly, which they don’t. This is the homogeneity assumption.

A/B testing compares two groups and reports one average result.

But that average hides:

  • Who loved it
  • Who didn’t care
  • Who reacted negatively

This variation is called heterogeneous treatment effects, the same issue ML struggles with.

“Traditional ML and A/B testing both look at averages. But averages hide differences. People don’t respond the same way, so relying on one number leads to decisions that don’t match what your audience actually needs.”

So, what should we keep an eye on?

  • Segment differences: Don’t stop at the average. Break results down by audience groups to see who benefits and who doesn’t.
  • Negative effects: A “winning” variant can still hurt certain groups. Look for where performance drops. 
  • Context matters: Timing, demographics, past behavior, and geography all shape how people respond.

The Contrast Explained:

Metric Overall Impact Key Takeaway
Aggregate Lift ($+5\%$ Overall) Shows mild success, but hides differences between groups. Average view is misleading- do not assume one strategy fits all.
Segmented LiftStudents ($+20\%$) Very strong response. Action: Invest more in promotions for this group.
Bargain Hunters ($+12\%$) Strong positive effect. Action: Keep a moderate investment and try new offers.
Retirees ($0\%$) No impact. Action: Stop spending here and move the budget elsewhere.

Why Causal Inference is Needed

Traditional ML predicts outcomes from inputs. Useful, but it does not tell you why things happen. And it will not tell you what changes if you take a different action. That is the gap causal inference fills.

Causal methods aim to separate correlation from cause. They work with real‑world logs and customer behavior (observational data). They also work with controlled trials like A/B tests (experimental data). So you can use them even when a clean experiment is not possible.

Here is where it helps:

  • Dynamic pricing: Adjust prices based on how different segments actually respond, not just who looks similar.
  • Churn reduction: Treatment effect estimates show which actions reduce cancellations, and for whom.
  • Policy evaluation: Compare new vs old programs and see effects across demographics (heterogeneous effects).

And that is why it matters: with causal inference, you move from “what’s likely” to “what works,” using observational data or experiments when you have them. 

Introducing EconML

What is EconML?

EconML is a library from Microsoft Research. It estimates causal effects using machine learning. Most ML predicts outcomes. EconML asks why an outcome happened, and what would change if you took a different action.

The core method is called Double Machine Learning (DML). It trains two models, not one:

  • Propensity score model: Estimates the chance of receiving a treatment (for example, whether a customer is likely to see a discount).
  • Outcome model: Predicts the result of that treatment (like whether the discount leads to a purchase).

By combining these, EconML works to separate correlation from cause. That makes the insights more dependable than simple averages.

Key Use Cases

  • Marketing campaign effectiveness: Find out which groups actually benefit from a promotion. Maybe students respond really well, but retirees show nothing. Spend your money where it works, skip the rest.
  • Dynamic pricing:  Set prices based on how different people react. Younger customers usually hunt for deals, while loyal ones are not so price-sensitive. Do not just price for the average- match prices to the people. 
  • Medical treatment outcomes: Figure out how the impact of treatment changes with age, gender, or medical background. These details help doctors shape care that fits each patient better.
  • SaaS churn reduction: Identify which actions actually keep people from canceling- and who benefits from them. Focus on what makes a difference, and drop the stuff that does not.
  • Policy impact in economics: Compare new and old programs for different groups. Focus on policies that really make a positive impact. Microsoft Research shares real examples and case studies using the EconML toolkit.

Visual placeholder: Diagram of segment‑level effects for each use case (bar or tree charts).

  • Marketing campaign effectiveness.
  • Dynamic pricing.
  • Medical treatment outcomes.
  • SaaS churn reduction.
  • Policy impact in economics.

🌐 External link: Microsoft Research EconML – Case Studies – This page shows how EconML is used in real cases. It’s the official source from Microsoft Research.

Why EconML + Clojure via libpython‑clj

Seamless Python Interop

You do not have to leave the JVM just to use Python’s machine learning tools. With libpython-clj, you can use Python libraries like EconML right from your Clojure code. Reuse your old machine learning scripts, call familiar Python functions, and stay in your Clojure environment. It is all connected. There is no need to jump between languages or platforms.

🔗 Internal link: [Flexiana’s Clojure ML projects]

Advantages for Teams

  • Faster experimentation: Test new ideas quickly without jumping between different tech stacks.
  • Expressive functional code: you get the simplicity of Clojure and the power of Python’s machine learning tools.
  • JVM ecosystem integration: your results move straight into enterprise systems without any awkward workaround code.
  • Lower barriers: You can just build on what you have- no need to start over from scratch.

Example: Newsletter Campaign Case Study

Let’s say you want to figure out what happens when you send out newsletters.

  • Dataset structure:
    • XXX = It stands for things you know about your customers
    • TTT = It tells you if they got a newsletter
    • YYY = It shows whether they bought something or how much revenue you made.

EconML calculates τ(X), which basically tells you how much extra revenue you get from sending a newsletter, broken down by customer type. Instead of just giving you one big average, you actually see how different groups react. For example,

  • Dormant customers suddenly spend 30% more.
  • VIP buyers spend 2% less when you send them a newsletter. (Negative effect)
  • Bargain hunters go up by 12%, but only in certain situations. (Conditional effect)

So, now you know exactly who likes your emails- and who does not.

Table Comparing A/B Test vs EconML Uplift

Segment A/B Test Result (Average) EconML Uplift (Segment‑level) Key Takeaway
Aggregate +5% overall lift — Average hides subgroup variation
Students Not visible on average +20% Strong positive effect → invest more
Bargain Hunters Not visible on average +12% Moderate effect → keep testing offers
Retirees Not visible on average 0% No effect → stop spending here

Applying EconML in Practice

Decision Layer

After EconML gives you the uplift scores for each customer, you have got what you need to make smart choices. Here is how it usually goes:

  1. Rank everyone on your mailing list by their predicted lift.
  2. Choose the top half of those with an uplift above zero- and send your newsletters to them. 
  3. Skip the bottom half to save yourself time and avoid any negative impact.
  4. Customize the newsletter for each group. Give every segment the version that fits them best, so you are not just blasting the same thing to everyone.

Example Outcome

  • Control A/B test: +$50k revenue
  • EconML targeting: +$65k revenue

The uplift comes from sending fewer emails that do not matter. Less spam, better delivery, more revenue.

Technical Deep Dive: EconML Under the Hood

Residualization

Residualization is EconML’s way of making causal estimates more reliable.

  • It works by predicting what would’ve happened if the treatment never happened. Basically, the counterfactual outcome. 
  • To avoid overfitting, EconML splits the data into training and validation sets. 
  • There is also cross-fitting: models train on one chunk of the data and get tested on a different chunk. 

All of this helps cut through the noise and get to the real causal signals.  

Causal Forests

EconML also supports causal forests, which are decision trees designed to capture heterogeneous treatment effects.

  • They split the data into subgroups and estimate effects for each branch.
  • This helps discover new customer segments that respond differently to interventions.

Example: “Customers under 35 who browse frequently and have not bought in 60 days increase spend by 22% when shown Instagram ads.”

Flexiana’s Role

Flexiana has been building Clojure solutions for 9 years. Our team brings deep expertise in functional programming, machine learning integration, and global software delivery. Our projects span healthcare, fintech, SaaS, and enterprise systems.

Why Flexiana invests in bridging Clojure + Python ML

  • To empower teams with causal inference tools.
  • To make advanced ML accessible to Clojure developers.

Flexiana’s focus is clear: Help organizations use causal ML without leaving their Clojure stack.

🔗 Internal link: Flexiana’s About page 

🌐 External link: Flexiana GitHub

FAQs (People Also Ask)

What is EconML used for?

EconML is a Python library that helps you figure out the cause-and-effect of your actions. It takes observational data or experimental data and uses machine learning to apply econometric models. The goal is to determine why an intervention (or “treatment”) led to a specific outcome. It is about moving past simple prediction to understand individualized treatment effects (ITE).

How does EconML differ from A/B testing?

They have different goals. A/B testing tells you the average effect- it answers whether a change works for everyone overall. EconML focuses on the heterogeneous effects- it tells you who is most (or least) impacted by that change.

Plus, EconML can use data you already have (observational data) to target people better, saving you the time and cost of running a separate experiment for every targeting idea.

Can EconML handle observational data?

Yes, that’s what it is built for. Observational data is messy. EconML uses smart techniques, like Double Machine Learning (DML), to manage all the confusing variables that might skew your results. This helps it sort out common issues like selection bias, giving you honest, reliable causal estimates from non-experimental data.

Why use Clojure with Python ML libraries?

It is about using the best tool for each job. Python has the best ML libraries (scikit-learn, EconML, TensorFlow) for building the models. Clojure, running on the Java Virtual Machine (JVM), gives you a robust, concurrent, and highly stable production environment for running those models at scale. You get Python’s excellent science ecosystem with the JVM’s rock-solid backend.

What are causal forests in EconML?

Think of a causal forest as a special kind of random forest. In regular random forests, the tree splits are based on predicting an outcome. In a causal forest (like CausalForestDML), the tree splits are based on maximizing the difference in the treatment effect between the groups. This lets the algorithm quickly find and highlight the specific customer traits (features) that cause the uplift to vary.

Conclusion: The Future of ML in Clojure

EconML changes how we use machine learning. Predictive models tell us what might happen. EconML helps explain why it happens and what changes if we act differently. That is useful when you need decisions based on cause and effect, not just averages.

With Clojure and libpython‑clj, you get a clean, functional way to build models while reusing Python’s ML libraries. It is simple to keep your JVM stack and still tap into proven tools.

  • Expressive code: Your code stays clear and easy to follow.
  • Python interop: You can use existing ML libraries without leaving the JVM.
  • Enterprise fit: You can send those causal insights straight into production systems- no extra steps.

Together, Clojure and EconML make machine learning more than just predictions. You can test faster, ship better, and actually trust what your models tell you.

 

Explore EconML with Flexiana. Let’s build causal ML solutions together.

 

🔗 Internal link: Contact Flexiana page 

🌐 External link: libpython‑clj GitHub repo

Source: https://flexiana.com/news/2025/12/machine-learning-in-clojure-with-libpython-clj-unlocking-causal-insights-using-microsofts-econml-series-3