
I thought I had this figured out back in 2023. I built a Node.js script that pulled data from the Google Fitness API to calculate my TDEE, got the calorie costs for building muscle and fat from an actual nutrition textbook, put the constants in the code, and considered the problem solved. Past me closed the laptop very satisfied with himself.
Then Google Fit started getting deprecated, I lost the thing that script was built on, and I ended up building Musclog, my own open-source fitness tracker. One of the features I wanted was weight projections: you tell the app your goal and how many calories you’re eating, and it tells you how long it’ll take to get there.
So I plugged in the same logic from 2023, and the projections were wrong. Consistently, confidently wrong.
If you ever looked up how to lose or gain weight, you probably heard the golden rule: 7700 calories equals one kilogram of fat (or roughly 3500 kcal per pound if you’re used to the American version). This comes from a 1958 paper by Max Wishnofsky in the American Journal of Clinical Nutrition, “Caloric equivalents of gained or lost weight”, and it’s been copy-pasted into every diet app and fitness calculator since then. I was using it too, and that was exactly the problem.
So I went down a massive rabbit hole of human physiology and thermodynamics to figure out how to model weight projections properly in Musclog, and this is what I found, plus what I built the second time around that actually worked.
When math isn’t mathing#
Quick disclaimer about what’s new here vs. what I already covered in 2023: the base constants, the calorie costs for building muscle and fat, are the same. What’s new is the P-ratio model that takes training experience into account, the Hall-Forbes weight loss equations, and the Lambert W function to calculate lean-to-fat loss ratios during a cut. Also, this time I traced the numbers back to the original papers instead of just asking ChatGPT. Well, the Deep Research mode from Gemini is what got me to these papers, but I still had to go to the original sources myself.
I’ve been obsessively tracking my food and body composition for a few years now. I weigh my food, I track my macros, I log every workout. I am that guy, and at this point I’ve fully accepted it.
So when Musclog started generating weight projections I trusted the data, and the data told me that after two months of a modest caloric surplus and consistent training, I should have gained about 2.5 kg.
I had gained 1.1 kg (and yes, mostly fat, it is what it is).
At first I thought I was miscounting calories, then I thought maybe I calculated my TDEE wrong (which is probably also true), but the numbers kept being off in the same direction, month after month. Something was wrong with the model itself.
So I started digging, and it turns out the 7700 kcal/kg rule, the one basically every calorie calculator on the internet uses, makes one massive physiological mistake that I had never even thought to question.
The difference between storing and building#
The classic 7700 kcal/kg rule mixes up the energy contained in a tissue with the energy needed to build that tissue, and those are not the same thing.
When you look at the thermodynamic models for human tissue synthesis, things get interesting:
Adipose Tissue (Fat)
- Fat is about 86% lipid, 5% water, and a bit of protein.
- Energy stored inside 1 kg of fat: ~7730 kcal
- Energy cost to build 1 kg of fat: ~8840 kcal
- Efficiency: ~87%. The human body is depressingly efficient at storing excess energy as fat.
Skeletal Muscle
- Muscle is mostly water (~70%), about 24% protein, and some trace lipids.
- Energy stored inside 1 kg of muscle: ~1250 kcal (water has zero calories).
- Energy cost to build 1 kg of muscle: ~3900 kcal.
- Efficiency: ~32%. Muscle hypertrophy is an expensive, inefficient process, your body is constantly breaking down and rebuilding peptide bonds just to make you slightly less weak.
If you use the standard 7700 kcal/kg rule for a lean bulk, your app will massively overestimate how much weight the user gains, because building muscle burns a huge chunk of energy in the construction process itself, energy that never ends up stored in the tissue.
That’s why my projections were so far off. Musclog was assuming all weight gain was basically fat storage, but in reality a good chunk of my surplus was being burned as the cost of building muscle. The calories didn’t disappear, they were spent constructing new tissue, not filling it.
Down the rabbit hole#
Once I understood the difference between stored energy and construction cost, I had a new problem: where do I get the real numbers?
I started with ChatGPT (obviously), which, in my defense, is exactly how I got these numbers in 2023, except that time I pasted the ChatGPT conversation directly into the blog post and called it a citation. This time I wanted something I could trace back to a real source, so I ended up on Google Books (thanks Gemini) reading parts of The Nutritionist: Food, Nutrition, and Optimal Health by Robert Wildman at 11pm on a Tuesday, which was not in my plans for that week. That’s where I found the exact tissue composition breakdowns I needed.
For the synthesis efficiency numbers, 23% for protein and ~93% for lipids (and this is where the keto gang starts getting angry), I traced them back to the thermodynamic analyses by J.P. Flatt, specifically his 1978 chapter “The biochemistry of energy expenditure”, which a bunch of the more serious calorie modeling papers cite as a source. And with all of that, I could finally do the math myself.
This is how the calculations work:
For muscle, 1 kg contains 200g of protein and 50g of lipids. Protein synthesis runs at about 23% efficiency, meaning it takes ~17 kcal to put 1g of protein into tissue (4 kcal stored, 13 kcal burned in the process). Lipid synthesis runs at ~93% efficiency, so about 10 kcal per gram.
- Protein: 200 x 17 = 3400 kcal
- Fat: 50 x 10 = 500 kcal
- Total to build 1 kg of muscle: 3900 kcal
For body fat, 1 kg contains 850g of lipids and 20g of protein:
- Lipids: 850 x 10 = 8500 kcal
- Protein: 20 x 17 = 340 kcal
- Total to build 1 kg of fat: 8840 kcal
And those are the constants that ended up in my code. I also cross-checked them against the data from my earlier TDEE tracking work to sanity-check the model against real numbers from my own body, which helped a lot.
Translating thermodynamics into code#
To make Musclog accurate I had to ditch the static math and build a dynamic model. First, the energy constants:
// Established thermodynamic model for human adipose tissue & skeletal muscle
const CALORIES_STORED_KG_FAT = 7730;
const CALORIES_BUILD_KG_FAT = 8840;
const CALORIES_STORED_KG_MUSCLE = 1250;
const CALORIES_BUILD_KG_MUSCLE = 3900;But the constants are not enough, we also need to know if the user is mostly gaining fat or muscle. That’s where the P-ratio comes in, and the P-ratio was the thing that surprised me the most in this whole research. The concept was formalized by Gilbert Forbes in his 1987 book Human Body Composition and has been studied a lot since then in the context of caloric surplus and deficit. I was assuming my bulk was roughly 50/50 fat-to-muscle (the generic intermediate estimate), but the actual split depends a lot on training experience, in ways I hadn’t accounted for.
A beginner, someone in their first year of lifting, can gain muscle way faster relative to fat. Newbie gains are real, and they’re physiologically different from what happens to an intermediate or advanced lifter in a surplus. An advanced lifter on a bulk ends up storing proportionally more fat, because their muscle synthesis is already close to their genetic ceiling. Fun times.
So I wrote a helper to figure out this split based on the experience level the user picks:
export function getGainFatFraction(liftingExperience?: LiftingExperience): number {
switch (liftingExperience) {
case 'beginner':
return 0.4; // ~60% lean gain
case 'advanced':
return 0.6; // ~40% lean gain
case 'intermediate':
default:
return 0.5; // 50/50
}
}Now, instead of blindly dividing a caloric surplus by 7700, Musclog calculates the effective caloric cost of gaining a kilogram of mixed body weight based on the user’s experience level:
export function getEffectiveKcalPerKgGain(liftingExperience?: LiftingExperience): number {
const fatFraction = getGainFatFraction(liftingExperience);
return fatFraction * CALORIES_BUILD_KG_FAT + (1 - fatFraction) * CALORIES_BUILD_KG_MUSCLE;
}For a beginner: (0.4 * 8840) + (0.6 * 3900) = 5,876 kcal per kg. That’s a massive difference from the generic 7700 kcal rule, and exactly the kind of gap that explains why my projections were always wrong in the same direction.

The Goals Management screen in Musclog, which is where the projection engine actually runs. The calorie targets and weight goals you set here go straight into the model from this post.
The math of losing weight (the Hall-Forbes model)#
Losing weight needs a completely different approach, and this is where things got complicated.
When you’re in a caloric deficit you’re releasing stored energy instead of building new tissue, so now what matters is the stored energy density, not the construction cost. The problem is that you’re not losing pure fat. You lose a mix of fat and lean mass, and that mix changes depending on how lean you already are: the leaner you are, the more lean mass you lose relative to fat in a deficit. This is well documented, and it changes how you should calculate weight loss projections.
To model this I implemented the energy density equations from Kevin Hall’s 2008 paper “What is the required energy deficit per unit weight loss?” (published in the International Journal of Obesity) and the Forbes curve from the book I mentioned above. And to calculate the lean-to-fat loss ratio dynamically based on the current body fat percentage, I had to implement the principal branch of the Lambert W function in TypeScript to solve the underlying differential equations, which is a technique described in Hall’s follow-up 2010 paper “Predicting metabolic adaptation, body weight, and body fat changes in humans” in the American Journal of Physiology:
function lambertW(z: number): number {
if (z < -1 / Math.E) return NaN;
if (z === 0) return 0;
let w = z < 1 ? z : Math.log(z);
for (let i = 0; i < 30; i++) {
const ew = Math.exp(w);
const f = w * ew - z;
if (Math.abs(f) < 1e-10) return w;
const fp = ew * (w + 1);
w = w - f / fp;
}
return w;
}To be clear, I did not sit down and derive this from scratch, I worked from the Hall (2008) paper and adapted the implementation to TypeScript. But I did validate it against real data from my own cutting phases (thanks to the tracking setup I built back in 2023), and the model holds up well in practice.
With this, Musclog calculates how many calories are in a kilogram of weight lost for each specific user, based on their current body fat percentage. If the user doesn’t know their body fat, the app falls back to the classic ~7700 kcal/kg rule, because some approximation beats no approximation.
This still isn’t perfect#
These are population-level estimates. Individual differences in muscle protein synthesis, hormones, training, sleep, and genetics mean no static model will perfectly predict what happens to any specific person’s body.
The P-ratio values I’m using (0.4 / 0.5 / 0.6) are reasonable averages from the literature, not numbers calibrated for you. In a future version I’d love to calculate each user’s personal P-ratio from their historical data, Musclog already has everything it needs for that, I just didn’t implement it yet.
Still, it’s way more accurate than “7700 kcal/kg for everything”, and the projections have been noticeably better since I shipped this.
Building side projects is fun because you start thinking “I’ll just make a simple calculator”, and a week later you’re reading 1970s physiology papers and implementing Lambert W functions in TypeScript at midnight. Classic.
This whole thing started with the Google Fitness API TDEE script I wrote in 2023, so if you haven’t read that one and you’re into this kind of obsessive fitness tracking, go check it out.
If you want to poke around the code, Musclog is fully open-source on GitHub, and if you actually want to use it, you can download it on Google Play. Drop a comment if you want to nerd out over the math.
See you in the next one!