I am a math nerd. “A maths nerd,” my partner corrects me, because we live in London now. Fine. I love puzzles and formulae and bullet-pointed plans. I’ve spent many a winter morning with a steaming cup of tea and an Excel file. I don’t often make major forecasting errors, but I’m in the middle of my life’s biggest miscalculation.

Until I was eight, in 1987, I lived in Isfahan, Iran, in a big, warm family of science and maths types. I had a bike and a best friend and my own calculator. I loved a boy named Ali Mansouri. But then my mother was jailed for converting to Christianity and, when she was temporarily released, we had to escape Iran. Before you could calculate the probability of losing every toy and friend and photo, it was gone, favourite calculator and all. We were in a refugee hostel in Dubai and then in Rome. And then two years had passed and I was the foreign kid in early 1990s Oklahoma. Every subject was foreign to me: English, Oklahoma history, the topography of who knows what. But one subject hadn’t changed; in fact, in this one area, I was ahead of everyone else. In maths, I shone. I could do a sheet of 100 multiplications in less than a minute.

At 12, when I started to feel our poverty, I asked my mother how much money an average person needed not to stay awake all night, punching my calculator. She said, flatly, $5,000 a month. Sixty thousand a year, I thought. I went to a library and looked at average income levels. I learned that to make that much right out of college, I had to get into an east coast university (I had yet to learn about the regional cost of living). Screw this life, I thought. I’m going to live comfortably. I had the grades, but back then it wasn’t so marketable to be an Iranian refugee: even trying felt like a risk.

The university guides said I needed sports. I needed a national championship. I calculated the probability of winning trophies in the sports I loved: tennis and swimming. Other girls loved those, too. Wealthy Oklahoma suburbs were teeming with country-club girls who had way more practice and nicer rackets than me. I needed a sport that bent to my juvenile analytics: a sport with trophies handed out by weight levels, age levels, belt levels. A sport that didn’t attract rich girls with trainers. So I signed up for taekwondo.

I dropped 20lb, put in five hours of practice a day alongside the boys. I counted calories, fat grams, the hours on the Stairmaster. At 13% body fat, I stopped menstruating and won a national championship.

At Princeton, I decided to find a boyfriend. I had never had one, never been kissed, never had sex. I made a secret chart of the boys I knew. I quickly threw it away, ashamed of myself. I hated the entitled rich boys. I didn’t want another financial aid kid – the probability of poverty was too high. I was planning to go into finance or consulting, so I joined a business organisation and met an awkward boy with a kind heart who loved my OCD and the way I counted on my fingers. He wasn’t hungry like me; he was enjoying his life. So I gave him some of my hunger, that missing ingredient, and he thrived. We married and bought a canal house in Amsterdam. He grew handsome and ambitious. He had rows of wooden shoe racks and the most beautiful suits.

I followed the numbers to New York, to McKinsey & Co, and he came, too. My life was perfect on paper, an immigrant girl’s fantasy: the midtown consulting job, the apartment, the husband. We made way more than $5,000 a month. In one of our earliest photos, we’re both in Brooks Brothers trench coats, leaning on a Princeton umbrella and sporting his-and-hers corporate haircuts. A friend said, “That’s the yuppiest thing I’ve ever seen.” Then I went to Harvard Business School; we made a plan for our lives. He would have the low-beta career and I the high-beta (beta being the finance term for risk and potential reward). We actually did the maths for this.

I remember thinking, 'If I date him, there’s an 80% chance I’ll get a weird infection'

Through the years, I’ve had periods when something snaps. When I turn deaf to the data and do something crazy, because I crave joy, creativity, a jolt. It happens every decade or so. In 2011, it happened. I became a writer. We divorced.

I moved back to New York and made a statistical game out of dating. I downloaded a few dating apps and quickly figured out which had the best men: the best apps centred on photos. After all, I had undergone enough institutional brainwashing to be able to weed out, from a few snaps, the cultured, educated ones from the ones who were faking. I learned that a hat means he’s bald, no smile means bad teeth, grainy pictures means lying about age. From photos, I could figure out their travel smarts, their creativity, insularity, intelligence level, and even education and political bent. Believe it or not, something as simple as a baseball cap, choice of sunglasses or favourite sport is enough accurately to differentiate (on an aggregate level, at least) a midwestern Republican bible-thumper on a two-year work stint in New York from a pro-choice, dual citizen who makes his own bechamel sauce and reads Sebald.



In two years, I had many high-quality boyfriends, ones who scored well by every known metric. And, as predicted by my personal algorithm, I went on roughly 12 dates per eventual boyfriend. Once, I segmented the population of Iranian-American men into four categories and devised a plan to date one from each kind. The experiment effortlessly settled the question: “Should you be with an Iranian?” The answer was no.

As a rule, I wasted no time. I had a tight schedule. I had many pretty dresses. I kept my body fat next to nothing. Sometimes, I accompanied friends to freeze their eggs. I considered it, but in the end I believed in my eggs. Throughout all this, I found my way into a decent writing career. At 35, I had it together again.

Then, out of nowhere, chaos.

***

I met Sam, not on a Tinder date, but at a writer’s colony. He was English, divorced, 39, jobless. His shirts were full of holes. He hadn’t cut his hair in six months and washed it maybe every two weeks; it was a crazy curly mess that reminded me of Sideshow Bob from the Simpsons. Back in New York, I was dating a handsome Mexican businessman who fit all the criteria. A low-beta career, love of travel, a sense of humour. But, suddenly, I found myself falling for the unwashed writer, and I was confused. I actually remember thinking, “If I date him, there’s an 80% chance I’ll get a weird infection.”

Over many meals, I learned that Sam had spent the last year wandering from residency to residency, writing a novel about a Vichy demographer so devoted to his work that he didn’t stop to think maybe he shouldn’t be calculating census numbers for the Nazis. Now, he was on trial for crimes against humanity.

“So you’re claiming that he did it mostly for the love of the census?” I said.

“Right,” Sam said. “Culpability is a complex thing.”

“I think he’s guilty,” I said, surprising myself. “Maths is just a tool. You have to care about the thing you’re calculating.”

Silently, I did the forecasts on Sam: he wouldn’t make a dime for years. But I loved his novel. He lent me The Reader. He lent me Stoner. We walked in the woods. I stopped crunching the numbers. Slowly, I fell for his distractedness, his wandering, the life he had scattered in three storage spaces. He loved my OCD and the way I counted on my fingers. He called me Rain Man.

“How much do you love me?” I asked.

“There’s beggary in the love that can be reckoned,” he said.

Screw that, I thought. Everything can be measured, even love. If it was a job, I’d be amazing at it. I should be a yenta.

Sometimes, Sam watched me do puzzles. He read me short stories as I slept.

Then, at some point between a history in French demography and a night with the works of Annie Dillard, my once-a-decade insanity came early and I got pregnant.

Soon, every sphere of my life, once neatly rolled skeins arranged in a basket, unravelled into one big tangled mess. Early in the summer, early in our relationship and also in our pregnancy, Sam and I decided to leave New York and start over. “Let’s wander!” we said, delirious with our news, with our brand new romance, each of us seeing in the other a strange twist on the qualities we had always chased in others.

Nayeri in Provence with Sam and their daughter, Elena. Photograph: courtesy of Dina Nayeri

We drifted to a tiny French village with a population of 3,000 geriatrics who dipped their toes in the fountain in front of the local restaurant, spoke only fast, accented French, and tested positive for toxoplasmosis at alarmingly high rates. Within three months, I’d dismantled my life and landed inside my own worst nightmare.

But we also relished our aimlessness. We plunged into the pregnancy, thinking that we could build a world around our unborn baby, caring for her, feeding her, monitoring her heartbeat. We looked at her sonogram: her big ears and the way she touched the wall of my uterus. Sam cooked me exceptional French dishes every night. Friends delivered American vitamins to France. Sam read my writing. I read his.

Unable to make sense of my new life, I slowly went feral. When I got too big to shave my legs, Sam offered to do it, but I decided just to stop. When I got a yeast infection and didn’t want to drive to the doctor, he stepped up with a bowl of yoghurt and a turkey baster. He cooked gingery salmon. He cooked a daube full of lamb. He cooked sour sauces to assuage my cravings. I devoured them all with my fingers.

“This isn’t the life I imagined,” I said.

“I know, love,” he said.

Fingers shaking, I sank my teeth into chicken thighs, gnawing to the bone.

***

To preserve a sense of my own space, I turned to geometry. I created a perimeter: my suitcase, my backpack with my laptop and work stuff, a big leather purse of important documents. I put them in a corner of the room and closed off that corner. I said, “Sam, this corner is mine. Don’t move it or touch it.” I figured, everything that surrounds me can change – we can move down the road or to another country – but in every place I’ll have this square metre that’s mine.

Each morning, I woke in a stifling 500-year-old room, a constellation of mosquito bites covering my feet and calves, every tooth threatening to fall out as I struggled against a half-conscious panic attack. It always took me a few seconds to recognise the shock of black curls on the pillow beside me, the gentle English accent: “What’s wrong, love?”

My first thought: “I don’t have an address any more, or a phone. We’re in a town with no maternity store and I’m wearing your boxers and I’ve known you for 10 months.”

I counted on my fingers:

Probability of relationship failing and becoming a single mother: 60%

Probability that current lonely feeling is caused by hormones: 90%

Probability that that cheese I ate earlier in the market was unpasteurised and carrying The Tox: 99% (I returned again and again to have my blood tested).

How trifling and small they seemed now, my formulae and aspirations and plans

I wanted my charts and my formulae back. I wanted something to strive for. Twelve dates equals new boyfriend. Two months in a colony equals a book draft. It wasn’t just the indignities of impending motherhood or having to forfeit all the carefree bliss of early romance: Sam was a stranger to me. Would my child be a stranger, too? And what did this turn of events say about the way I had lived my life so far? Was it all for nothing, all that calculating, all that striving?

Worst yet, had I chosen wrong? I spent a month trying to find a formula for Sam. His unwashed hair (that smelled so nice), the scattershot holes around the collar of his T-shirt (the softest shirt I’ve worn), the way he took five minutes to get out a sentence (oh, but what sentences!). I remembered the day his parents had come to Heathrow to pick me up three months earlier. They didn’t know my face – I knew theirs because they were each identical to Sam in unrelated ways: his mother had his curls and his long, angular face; his father had his expressive eyes and aquiline nose. I watched them scanning the crowd, confusion blanching their cheeks, for several long beats before I approached them. What were they thinking? Who was this woman their son had brought home? In what configurations would our genes meld together to create a new person?

One morning, I woke up soaking wet. I had sweated through the sheets and the stress was giving me acid reflux. “I have no plan,” I murmured into the pillow.

“We can make a plan,” Sam said.

“You look terrible on paper,” I said.

“But we don’t live on paper,” he said.

“We’re living out of a suitcase,” I said. “Do you know how much stuff we need? We need a stroller and car seat and diapers and burp cloths and a changing station and 10,000 other things. There are a dozen vaccines and two dozen signs of meningitis to memorise, and allergens and baby cribs. Do we put her on her back or on her front? Should we have life insurance and godparents? How will we build a whole life?”

“It’ll build itself,” he said. “Have a little trust.”

But the universe gave me data so I don’t have to trust. Often, I wonder about the ratio of chaos and order that would equal a happy life. Clearly, I wasn’t satisfied in a life of diversified betas and shoe racks all aligned. But going feral almost broke me. Is life only chaos or an ordered game of averages? Should I welcome it and its beautiful wildness, or fight to rein it in? So far, all I know is that my life has been a constant act of letting go, of changing what I need to survive, recalibrating the perimeter.

After France, we built a life slowly. We moved again, and once more after that. We bought a changing station. We chose godparents. We borrowed a car seat. Suddenly, we were surrounded by aunts, uncles, cousins. We inherited bundles of baby clothes. We had the funniest, cleverest girl (there is no comparing). It came together by itself. No, Elena brought it together. How did she do it, with her little hands?

A few months ago, I stumbled on a piece of paper from 2014. It was labelled, “My five-year plan”. It was full of silly goals: publish second novel, finish third novel, decorate apartment, find community, read 50 books a year. Reading it, I missed my old self. I wanted to edit it, to make a new list, to build a predictive model of my future, and Sam’s, and Elena’s. Where had this vital part of my identity gone? Without it, I was no longer entirely me.

And yet it was frightening what I had left off. Where was my Elena? How trifling and small they seemed now, my formulae and aspirations and plans.

I looked again at the page, the unfamiliar language of it. I kept thinking, without my deviations and the screw-ups, the probability of Elena would be zero.

• Dina Nayeri’s new novel, Refuge, is published in July by Riverhead Books.

• This article was amended on 13 May 2016. An earlier version mistakenly said Nayeria went down to 3% body fat. This has been corrected to say 13%.