How does HealthKit calculate activeEnergyBurned and other variables?
Apple doesn't disclose the algorithms is uses to calculate these so you're going to need to reverse engineer it or hopefully find someone else who has reverse engineered it.

What is publicly known is that Apple has hired and contracted experts to make an entire team of research physiologists, cardiologists, dietitians, nurses as well as likely hundreds to thousands of people to workout with the Apple Watch and other sensors where they calibrate all sorts of measurements and correlate that data collection with metabolic carts (reportedly 50 in this gym alone). Also, this data collection has been going on since 2015 so I would expect Apple has revised the formula over time to make it better and better as they learn which types of activities and which body types and ages don't fit the last version of the algorithm.


https://www.technologyreview.com/s/608250/how-apples-ai-secretive-gym-is-shaping-its-activity-tracking-algorithms/
https://appleinsider.com/articles/17/09/01/apples-watch-oriented-fitness-lab-tops-66000-hours-of-data-10000-subjects
https://daringfireball.net/linked/2017/09/05/apple-fitness-lab
https://www.menshealth.com/technology-gear/a18923364/inside-apples-secret-performance-lab/


I would expect that Apple has severely restricted the code it uses as trade secrets as that high cost to invest in the team is clearly a competitive advantage for it to market and sell and satisfy people with good estimates of health activity. Perhaps they will disclose what you need, but I would guess you should just use the values Apple provides and build your app around the data stream Apple generates as opposed to compete with them unless your aim is to compete with Apple on generating better health data.

As a developer, you also should look into ResearchKit - https://www.apple.com/researchkit/ to get much more technical detail and API than the mainstream press (or even MIT's technology review) will cover in articles about Apple's efforts to make their health data accurate.