Another possible confounding factor in why SOME fields in STEM seem to lose adolescent girls is that biology drives a more social learning style for girls.
So team activities, and those with "partners" or mentors tend to be more successful at retention. There are those in Computer Science who are exploring these gender based differences with an eye toward making coding an activity that girls and young women find fulfilling enough to want to pursue.
Apparently, it doesn't take a lot of tweaking to get them there-- much of the problem is in the solitary nature of traditional programming instruction.
How much of it is also the perception many girls tend to have that computers/math are for boys or if they like those subjects, they are dorks/geeks? The social aspect is not just learning style but the perceptions among the girls, which drives their motivation to be in one of those fields or avoid those fields. I went to an all women's college, which meant all our CS and science students were women, and all our computer clusters run and staffed by the students - again, all women. Half of my sports team in college were science majors. It was odd for me to go from college where our science center was full of women to working in a corporate environment where there were only a handful of women in the technical groups.
This is why even though DD is not even preschool, I have decided to take a chance on the Augie and the Green Knight book (on kickstart) - because that is about a mathy/science girl on a cool adventure. Those are the type of books I am going to be hoping to find for DD among the usual classics as she grows up - where the main female girl characters are in science/math and cool.