When Sophia Westwood decided to take computer science as a freshman at Stanford, she knew she was running with a high-octane crowd: elite program, startup factory, genius classmates who had been coding since birth.

Then again, why not? Westwood had been dabbling with programming since she was a kid, and she rocked the Advanced Placement test in high school. So she went for it -- and almost immediately confronted the reality that every woman in the field faces.

"Oh, you must be Sophia," her instructor for an introductory computer science class said.

It wasn't Westwood's reputation that preceded her. It was her gender. Westwood was the only woman among the eight or so students in the discussion section, and it wouldn't be the last time she would be severely outnumbered.

Even as women have made big strides in once-male-dominated professions such as law and medicine, they've been left far behind when it comes to computer science, a lucrative discipline that is the primary driver of the 21st century economy.

At Stanford, only 23.5 percent of computer science degrees last year were awarded to women. At UC Berkeley, the number was 16.2 percent. The schools broadly mirror the national picture. But that's only part of the story.

The fact is, the numbers have been getting worse. In 1984, more than 37 percent of computer science bachelor's degrees in the United States were awarded to women. By 1995, the figure had dropped to about 28.5 percent. The latest U.S. Department of Education figures from 2011 put the number at 17.6 percent.

All of which helps explain why women hold less than one quarter of computing jobs in the country, according to an analysis by the Anita Borg Institute, a Palo Alto nonprofit working to boost women's participation in the tech economy.

Both Stanford and Cal, whose computer science graduates are the human seeds of Silicon Valley's startups, are working on the problem. Stanford has raised its percentage of women receiving CS degrees from 16 percent in 2005. Cal, which recently launched a number of initiatives, has yet to see results when it comes to graduation statistics.

Nowhere is the lack of women in computer science a bigger issue than in Silicon Valley, where programming skills and computing know-how fuel the world-changing startups and global behemoths that shape our everyday lives. It's what builds companies like Google, Facebook, Twitter and Apple. It's the discipline that launches IPOs. It is, in essence, the life blood of the innovation engine.

In this series, "Women and Computing: The Promise Denied," I'll look at the reasons so few women study computer science and talk about how that harms the economy and, of course, the women themselves. Finally, I'll talk to some of those who are working to turn around the numbers about the ways they think they can change the world.

It's true that other demographic groups, blacks and Latinos for example, are also woefully underrepresented in computer science. But I chose to look at women because women, representing more than half the population, make up the richest vein of potential talent to solve one of the U.S. economy's most pressing problems: the growing shortage of skilled computer programmers.

"If we want to hire the best software engineers in the world," says Jocelyn Goldfein, a Facebook engineering director, "how can we be confident that we're doing that when we're drawing practitioners from only about half the available talent pool?"

The truth is that in a time when women are being told to lean in, they have been left out of some of the hottest jobs in the country -- jobs such as software engineers, system analysts, computer research scientists, database administrators. Jobs that come with median salaries that start for women at $60,000 nationally, according to the Borg Institute, and top out at twice that in Silicon Valley, according to career site Glassdoor.

The shrinking proportion of women in computer science is at first glance bewildering. Women have been gaining for years in many math and science fields, and they are receiving more than half the degrees in some of those areas. All of which makes their absence in computer science more frustrating, infuriating even, because it doesn't have to be so.

Stanford students Sophia Westwood, center, a computer science major graduate student, and Wendy Shi, right, a junior computer science major, chat with
Stanford students Sophia Westwood, center, a computer science major graduate student, and Wendy Shi, right, a junior computer science major, chat with Stanford computer networking professor Phil Levis, far left, at the Gates Computer Science Building at Stanford University in Stanford, Calif., on Tuesday, Oct. 8, 2013. Westwood started a series of dinners called "Casual Dinner for Women Studying Computer Science" that brings female computer science students together with role models to help build community and to network. (Nhat V. Meyer/Bay Area News Group) (Nhat V. Meyer)

Those who have studied the issue are reluctant to identify one culprit in what is a tangle of political, social, educational and personal considerations.

"Who is to blame for all of this?" asks UCLA senior researcher Jane Margolis, the co-author of a well-regarded study, "Unlocking the Clubhouse: Women in Computing," that dives into the obstacles women confront in the field. "I don't know if there is a who. There is a whole set, a convergence of different factors, but I do think there is a question of political will. I think that if people did want to do something about it they could."

Yes, it's complicated. But after talking to dozens of researchers, academics, technologists, educators and students, it is evident that the nation's education system, from kindergarten through college, simply has not lived up to the task of sufficiently encouraging women to pursue courses and careers in computer science.

"Absolutely," says Margolis. "The education system -- and it's not just women and girls; it's also students of color and students from low-resourced communities -- they have let them all down."

The role schools could play in closing the gender gap becomes even clearer when you consider the reasons social scientists say boys gravitate to computers at an early age and girls don't.

There's simple socialization: the same sort of spoken and unspoken cues from parents, teachers, media and peers that result in boys playing with trucks and Legos and girls playing with dolls and My Little Pony.

There's the image problem: Girls see computer scientists as socially awkward male nerds who spend their days alone tapping out code -- people, in other words, who are not them.

There's the lack of female role models to shatter the image: Media reports and pop culture are filled with boy wonders like Mark Zuckerberg, the Google guys and the game-making "brogrammers," who start companies and change the world.

"Men can look at Mark Zuckerberg or basically every founder in Silicon Valley and say, 'He's cool. He did something with computer science,'" says Ellora Israni, 21, a Stanford computer science major who cofounded she++, a group working to encourage girls and young women to pursue computing. "And you ask women, 'Who are your role models?' And their role models are never programmers."

And no wonder. Consider the San Francisco startup incubator that advertised a "Hackers and Hookers" Halloween party, or the mainstream tech conference that featured two guys describing a fantasy app that creates selfies of men staring at women's breasts.

"There is still a lot of hostility toward women in some parts of the industry," says Ellen Spertus, a computer science professor at Mills College in Oakland and a senior research scientist at Google. "There is subtle bias and there also is some quite open bias."

Add to all that a self-fulfilling prophecy: The small number of women studying computer science in college can make those women feel alone, vulnerable and unwelcome. For decades, computer science in grade school and high school, when there has been any computer science instruction at all, has amounted to an exercise in self-selection. Boys join extracurricular computing clubs and sign up for CS classes with the encouragement of parents and teachers, while girls are left to shy away from a field they don't fully understand. In the end, without the encouragement and exposure to computing that boys often get, girls start behind when it comes to computing and often don't get what they need to catch up late in the game.

What would encouragement and exposure look like? Harvey Mudd College, a small liberal arts school in Claremont, provides a powerful Petri dish. By 2006, administrators were determined to close the gender gap, which had been frustrating them for years. But to do so, they had to change computer science's appeal.

"The difference is, females in general are much more interested in what you can do with the technology, than with just the technology itself," says Harvey Mudd President Maria Klawe, a computer scientist herself.

So administrators created an introductory course specifically for students without programming experience. They emphasized coding's connection to other disciplines. They paid for freshman women to attend the annual Grace Hopper Celebration of Women in Computing, a chance to meet programming role models in diverse fields. And they provided early research opportunities for women students to inspire them to stick with the field.

The result? The percentage of female computer science majors at Harvey Mudd increased from about 10 percent before the initiatives to 43 percent today.

Stanford started adopting some strategies similar to Harvey Mudd's in 2008. Berkeley, however, is just now beginning to make some changes, which might explain the differences in the percentages of female computer scientists they are graduating. Until about three years ago, Berkeley did what most colleges did to encourage women in computer science: almost nothing.

"The concern is real," says David Culler, chair of UC Berkeley's Electrical Engineering and Computer Sciences department. "The numbers historically have been low, and for quite some time. At the same time, I think we may also be finally moving the needle. I could imagine that we actually do see a sea change in the next few years."

Imagine the possibilities if girls encountered a Harvey Mudd-like effort well before college. It's the early years, after all, that hold the most promise -- the years before girls' career aspirations and expectations are fully formed and before the experience gap between boys and girls grows to the point that girls feel they don't belong.

Stanford student Westwood, 22, knows the importance of encouragement -- the earlier the better -- as well as anyone. Along with the obstacles, she heard encouraging words, from her parents -- an engineer and a doctor -- and teachers and classmates at Stanford. She found her own support system and is working to build one for other women. And, after pursuing it in college, she fell in love with computer science.

"This is such an exciting field and you want people to be able to make the decision on whether to pursue it or not based on affinity, not based on gender," says Westwood, now a graduate student, who has launched a series of campus dinners for women in computer science. "It's really not fair and not right that there are so many people feeling not welcome in a field that they would do well in."

A master's degree in the subject is her focus now. And next summer she will take her programming skills to Quip, a startup formed by Google and Facebook alums who are working on document collaboration products for the mobile computing era. She was thrilled to sign up as employee No. 14, the company's first recruit right out of school. Oh, and she represented another milestone: The company's first female engineering hire.

"I'm really excited," Westwood says. "The caliber of the people there is off the charts."

Listening to her talk is not only a reminder of the opportunity that has been lost by leaving so many women behind, but also a measure of the potential that is still out there to be tapped.

Contact Mike Cassidy at womenincs@mercurynews.com or 408-859-5325. Follow him at Twitter.com/mikecassidy.