2022/07/27

Nile basin mechanism design

Part 1 made the case for GERD in the short and medium term. Now for the really big picture

The human population of the Nile basin will probably double in the next century. Even if the Nile's water flow increases (some climate change scenario models indicate that rainfall could actually increase in the Nile basin over the next 50-100 years), it seems inevitable that demand will grow faster. And as mentioned in part 1, 100% of the flow is already being consumed. But this doesn't have to cause conflict. Globally, 70% of water use is for agriculture. So that's where the adjustments would have to be. From a natural resource optimization point of view, just like it doesn't make sense to grow almonds in California, or cotton in Kazakhstan, growing cotton and wheat in Egypt is probably not the most efficient use of water. 

What do we mean by efficient? Imagine for a second the whole region was one country; if an allocation of water to different uses maximizes total benefit, i.e. there is no other allocation that has a larger total benefit, then that's an efficient outcome. To achieve this efficiency, obviously some water intensive agriculture should migrate to other regions. But of course, the Nile doesn't have one owner and we don't have perfect cooperation, so we can't expect individual players (a country or a farmer or a business) to sacrifice their immediate interest and give up some water use for the greater good.  Game theory teaches us that an efficient resource allocation is useless if it is not feasible. And feasible means it's an equilibrium where each party benefits more from sticking to it than from deviating unilaterally. 

What would such an equilibrium look like? It's not as simple as dividing it equally.  For example, one issue is that if two people get the same amount of water, but one of them doesn't actually need it, that's a waste, i.e. inefficient.  Even the notion of need, beyond bare survival, is subjective: you can argue about the relative merit of washing clothes, how often people should take a shower or bath etc. 

Fortunately, there is a way to turn subjective values into an objective agreement: a price. What pricing mechanism might work in this scenario? For example, in a hypothetical v2.0 of the CFA all the countries in the basin could agree on a uniform Nile water tax. Each country would be liable to pay the tax for its total usage yearly. Of course, it would be up to each government to determine how the cost is distributed in its society: as a tax explicitly passed on to water consumers, or paid by general government revenue, or something in between. Passing the cost on is not as hard as it sounds since in most places that matter (homes and factories with running water, and farms with irrigation) water usage can easily be metered or is already. And non-consumptive uses like electricity generation would naturally be neutral. 

To keep each other honest, the countries could easily agree on verifiable data sources. Egypt doesn't have to trust the metering in Ethiopia and vice versa, they could rely on aggregate measurements of the water balance, a lot of which can be done using currently existing satellite data that is freely available from neutral sources.

The revenue from this would be collected in a common fund and automatically redistributed to member countries in pre-set proportions. The proportions are negotiated in advanced and fixed, and of course that would be the hardest part of the whole deal. One basis for this negotiation could be a share proportional to the present fraction of the total Nile basin population in that country (not the total population, obviously as countries have different fractions of territory and population falling within the basin).  

Naturally the price would have to be adjustable, say yearly, with a protocol agreed to in advance, so it regulates annual usage at sustainable levels i.e. below 100% of flow volume with a safety margin. If total usage is too high, the price goes up. If a lot of water goes unused, the price goes down. And if the total usage stays well below the sustainability level for a long time, the price would keep going down all the way to zero. This too is not as difficult as it may seem, it's basically the same idea as a carbon tax to fight climate change but much easier: the set of players that need to agree is much smaller (it's "only" 10 countries not 200), the consequences of water are immediately felt by all participants every year (unlike climate change which plays out over longer periods), and the target quantity is much easier to compute (total flow is well known, unlike the effect of different levels of greenhouse gases in the atmosphere which requires complex models with lots of uncertainty). (As an aside, the carbon tax itself is much better than cap and trade or carbon offsets, as I wrote on this blog a long time ago). With a pricing mechanism like that, no need for arguments about cotton in Egypt or irrigation in Ethiopia. Instead we would see a graceful phasing out of sub-optimal uses of water, and maximize the benefit of this shared resource. 

Finally to further solidify the positive economics and minimize the negative politics of the system, the countries should facilitate investments and trade across the region. If for example investors from each basin country were free to invest in other basin countries in farming and industry while still supplying the outputs to their domestic market, there would be less political friction around the natural geographic distribution of agriculture and industrial production. 

There are many examples of more complex cooperative agreements between countries around the world today, so it doesn't seem infeasible for the Nile basin countries to reach this kind of equilibrium. And recall we have plenty of time to achieve this long term goal, as the short term issue of GERD itself is win-win as discussed in part 1. But the chances of achieving this outcome will be greatly enhanced if in the meantime, the region's economies grow and become better diversified  across farming, industry and services.  Which brings us back to the present. Electrification is the sine qua non of developing a diversified economy. And GERD is a big step in the right direction, one which is immediately beneficial to not just Ethiopia but also Sudan and Egypt. 

2022/07/25

The case for GERD

As the third filling of the Grand Ethiopian Renaissance Dam (GERD) goes ahead, we should expect what is now becoming an annual uptick in media coverage and geopolitical controversy.  I've been thinking of writing a version of this blog post ever since the project started more than 10 years ago, but always ended up assuming this is adequately covered elsewhere. Years later, I'm still surprised by the frequency of incorrect assumptions dominating the discussion.  Not just in the media, but also in countless conversations. So it sounds like there might be some value in exposing the basic facts.

Power

GERD will have the capacity to generate 6GW of power at peak. However, due to seasonal variations, the average is expected to be about 40% of the peak. So on average, it should generate about 80 million GJ or 20 billion kWh of energy per year. Electricity production in 2019 was about 15 billion kWh, so GERD will more than double the  country's capacity. 
Electricity generation by source, Ethiopia 1990-2019


Economic impact

What is the economic value of this additional energy? Note that we are not asking what is the cost to produce it, nor the price at which it is sold. We are asking what is the economic value of consumer and industrial uses that it enables.  One way to estimate that is to look at the relationship between energy and GDP.  From a widely cited paper, "Energy and Economic Growth: The Stylized Facts",  we can deduce that each Gigajoule of energy corresponds to about $100 of GDP:  
Double checking with another source, "Our World in Data", gives us about $0.40 of GDP for every kWH.  This data has the added benefit that it shows a similar relationship, not just across countries but also on the same country over time: 


The two datasets are in almost perfect agreement. And they imply GERD's impact will be about $8B/year, or an increase of about 7% of GDP.[1] 

Considering the cost of the dam is about $5B, a return of $8B per year is great. Of course it will take a couple of more years for it to reach it's maximum generation capacity,  many years to develop the transmission and distribution of all this additional power to 100M consumers, and even more years for industries to grow that will take advantage of it. So the full impact is still far down the road, and depends on quite a few things happening correctly (not the least of which is finding ways to sell the "stranded" generated energy to finance the development of the distribution infrastructure, a topic which I will expand upon in the future). Still, the long term benefit is so large that there is no question the dam is a phenomenally good investment by Ethiopia.

You can also view it with a "social impact" lens if you are so inclined. Can you think of many projects where a one-time investment generates 160% return per year for many many years, increasing income by 7% for more than 100M people, most of whom are among the poorest in the world? Indeed GERD is possibly the biggest and perhaps most effective poverty reduction effort in the entire world today.


Climate impact

Of course, hydroelectric power is 100% renewable, and outside of the materials used in construction, the on-going operations have zero greenhouse gas emissions. Less obvious but also important is the fact that this electricity will displace current sources of energy which are dirtier. For example, millions of people in Ethiopia today often cook with wood charcoal, which from an emissions perspective, is worse than oil, let alone gas, or clean electricity. The amount is tiny on the scale of global emissions and climate change, but still moving from burning wood to electricity is a positive transition from dirty energy to clean energy. Further, the wood comes from cutting trees. Thus, electrification helps combat deforestation, and trees take CO2 out of the atmosphere through photosynthesis. For a good discussion on the relationship between electrification, deforestation and climate, I recommend the book "Apocalypse Never",  which explains this same point in detail using an example from the Democratic Republic of Congo. (As an aside, I also recommend my  review of that book on this blog).  So GERD not only does not emit, it reduces other carbon emissions, and saves trees which take carbon out of the atmosphere, a triple win in terms of reducing anthropogenic climate change


Water balance

Increased rainfall?

An additional argument, articulated by Ugandan president Museveni in this video, is that saving trees helps rainfall, which is a positive for total water balance of the overall Nile basin (water balance is a crucial point of contention as we shall see below).  
   
This particular argument is debatable since forests increase rainfall but trees also consume water. Here's a good paper on the links between forest cover and rainfall.  So it's probably a stretch to argue that water balance will increase. But hey, trees do enough for us even if they are neutral in the water balance equation. The overwhelming consensus is that preserving forests as much as possible is good, and electrification happens to help that.

No reduction in flow

The bigger question regarding water balance is of course whether the dam itself will reduce water availability downstream. This is where there is the biggest misunderstanding. Egyptians are extremely fearful that the dam will reduce the flow of the Nile, and they view it as an existential threat. But the reality is that the GERD will not reduce the amount of water that gets to Sudan and Egypt:
  1. Electricity generation doesn't consume water. As water, pulled by gravity, flows through turbines, the kinetic energy of the water becomes electric energy, and all the water comes out on the other side and flows downhill from there as always. 
  2. When there is loss of water from a dam, it is because it has a reservoir, a lake. The larger the area of the lake, the larger the loss due to evaporation. Indeed at the High Aswan Dam in Egypt, located more than a thousand kilometers downstream from the GERD in a flatter and hotter area, the reservoir (Lake Nasser) is large and shallow, causing a significant loss of water to evaporation. The GERD however is situated in a gorge, so the lake it creates is much narrower and deeper (about 1,900 km2 for GERD vs 5,250 km2 for Lake Nasser). It's also in a cooler area. Thus the evaporation impact of GERD is much less than Aswan's. Further, the purpose of the reservoir is to regulate the flow, like a battery. In theory, if you have a reservoir upstream, you can reduce the size of a reservoir downstream. So if we naively forget political boundaries for a second, and assume Egypt, Sudan and Ethiopia were 100% cooperative, to manage the total flow optimally, they would achieve the same magnitude of regulation by reducing the volume of Lake Nasser by the volume of GERD lake. Since GERD has relatively lower evaporation, this would be a net reduction in evaporation. But to keep things in perspective, evaporation accounts for less than 2 billion out of about 90 billion m3 /year of water flow on the Nile, so it's a minor issue.
  3. A much larger fear for downstream people is that the GERD might enable additional consumptive uses, like irrigation for agriculture. This is a legitimate general concern of course, and fairness and efficiency in consumptive uses is important. However, in the case of the GERD, its location at the most downstream point in Ethiopia, near the point where the river exits to Sudan, means that it would be infeasible to use any of the water from that point for agriculture, as you would have to pump it uphill to reach farms within Ethiopia. This effectively guarantees that GERD cannot physically be used for irrigation or any consumptive activity in Ethiopia.  
For more on this, see the seminar on 'The economic impacts of large dams: a comparative analysis of the Nile and Colorado Rivers' . In particular the evaporation question and non-consumptive nature of GERD are addressed at 1:09:23 in the video

Bottom line: GERD will not decrease the net amount of water that reaches Egypt and Sudan. Regardless of what you think about the historical sharing of water, the fear that it can harm downstream people is just not supported by facts.


Floods and drought mitigation

In fact it's actually beneficial to them. As I tweeted some time ago, this excellent paper entitled 'Understanding and managing new risks on the Nile with the Grand Ethiopian Renaissance Dam' explains it:
  1. "Sudan will clearly be better off ... because GERD operations will smooth Blue Nile flows, eliminating flood losses, increasing hydropower generation, decreasing sediment load to the reservoirs and canals, and, most importantly, increasing water for summer irrigation in the Gezira Scheme and other irrigated areas along the Blue Nile".  To get a sense of the magnitude of this benefit, consider that flooding in 2020 caused over 100,000 homes to collapse and Sudan to declare a 3-month state of emergency.
  2. During droughts, it is expected that the existence of the GERD will cause "decreased water deficits to Egypt and increased water availability". 
It is also extremely important to note that, as the paper explains, these benefits to Egypt and Sudan do not depend on generosity and goodwill from Ethiopia. Keeping the flow steady by boosting it during droughts and throttling it during floods is also necessary from the self-interested electricity generating perspective of GERD, so it's a win-win-win proposition even without explicit cooperation.  In other words, long term incentives are aligned between Ethiopia, Sudan and Egypt, which should offer the strongest reassurance to back whatever political understanding is (hopefully) reached.


Filling

Now besides the long-term incentives, there is a separate question of what happens during the initial filling of the GERD reservoir, which started in 2020 and is expected to last 4 to 7 years. Filling the reservoir obviously must temporarily decrease the downstream flow. But here two facts should be understood. First, filling takes place in the rainy season (July and August) each year, where typically there is "too much" flow, so there should be no detrimental effect downstream.  Second, by chance, the first and second fillings took place during above average rainfall years 2020 and 2021. It's almost as if nature decided to be pro-GERD at this most critical time!
It's possible that the filling has already helped reduce the severity of floods in Sudan, although that effect may be limited by the fact that filling stopped as scheduled halfway through the rainy season (the Sudanese irrigation minister even complained that the filling didn't go fast enough to help).


Geopolitics

That is not to say Egypt and Sudan don't have any legitimate concerns. Future upstream uses of the Nile water could reduce their supply. The total water flow, while abundant, is currently almost 100% consumed: no Nile water actually reaches the Mediterranean Sea, except what's needed to push back salinity. So, even though GERD itself is a win-win-win,  in the bigger picture, the Nile water use is a zero sum game.  Currently, Egypt consumes 79%, Sudan 18%, and the rest of the countries combined less than 3%.

But there is international law and precedent on how to share rivers between multiple countries. The right way to deal with this case is the Nile Basin Initiative's Cooperative Framework Agreement  (CFA) which should be able to handle the issues of the next few decades at least. Uganda, Ethiopia,  Rwanda, Tanzania, Kenya, Burundi and South Sudan are on board. Sudan and Egypt initially joined, then "froze" their participation, but from what I gathered at the aforementioned seminar, Sudan has recently rejoined.  

The main problem is the recalcitrance of the Egyptian government. Given that their country consumes 79% of the Nile's water, perhaps they feel that acceptance of any upstream change jeopardizes this entitlement. The military government of Egypt has taken a hard line and it seems like they fear any compromise abroad might weaken their political power at home. This political trap has far reaching consequences for the region's stability and peace. Very unfortunate. Let's hope reason beats politics for once and things work out rationally, since GERD itself is actually beneficial to Egypt. 

Part 2 of this post explores the longer term sharing of the Nile beyond GERD.

P.S. This post is dedicated to my dear friend Ahmed Amr. A brilliant and hyper-informed Egyptian who during a conversation last year, was surprised by some of these technical facts.  Sadly Ahmed passed away from a long illness a few months ago. Ahmed, wherever you are, I hope you enjoy this post and I look forward to chatting with you again in the afterlife!

[1]Another way of getting economic impact is to multiply production by average price to get the direct value of the energy, and then apply a GDP "multiplier" which estimates the downstream GDP impact (electricity enables goods and services, which in turn enable other goods and services etc.) The problem as you can imagine is that multipliers are very inexact. In a tweet on this topic a couple of years ago, I used the a multiplier of 1.6 which I now realize is too low. I also incorrectly used peak power instead of average. Coincidentally the two inaccuracies cancelled out and the GDP estimate was about the same.

2022/06/03

The 4th wave of Bitcoin FUD

I just came across Why This Computer Scientist Says All Cryptocurrency Should “Die in a Fire”. I can't find any point in there that hasn't already been refuted many times. But it's relatively rare to find so many of them in one place, and it has been going around, so I thought I should make a little effort to rebut it. 

Security

Though not the most important aspect of the article, the "computer scientist" in the title is a not-too-subtle argument from authority, so it behooves us to take a look. The computer scientist in question is Nicholas Weaver, who I haven't heard of before, though from a brief look at his publications, I recognize some of his co-authors. It seems like his expertise is network security. So his most important contribution as an expert would be if he could find an actual technical security problem in Bitcoin. But of course he hasn't, in fact no one has successfully exploited Bitcoin. This is a rarely appreciated aspect of the network. Even though it's the world's largest honey pot, with literally several hundred billion dollars there for the taking, the entire codebase is open source, and all the data is on the public blockchain, no one has actually technically been able to "crack" Bitcoin. There is plenty of theft of Bitcoin of course, because people make mistakes with their keys etc. A scary bug was luckily fixed in the early days. Still no one has exploited the system itself. For any computer scientist, or anyone who has ever written software, this is very remarkable. As a network security expert,  you'd think Weaver would at least mention it. 

Maybe he has motivation for not saying anything positive? Indeed, apparently he's been declaring the death of Bitcoin so many times since 2013 that Weaver has earned a place in the Bitcoin Skeptic Hall of Fame.  It seems like he has dug himself into an anti-Bitcoin emotional trap which is hard to climb out of.

Bubbles

Credentialism aside, his actual criticism consists of economic arguments. He points to the price of Bitcoin in USD and "bubbles" where it rose from $10 to $100 then "crashed". Then to $1000 and crashed. Then to $20,000 and crashed. Then to $60,000 and crashed. And confidently asserts that there won't be a fifth bubble, that this time it's really dead.  But this only inadvertently points to the fact that he's been wrong so many times. Without any coherent explanation of why his previous predictions have failed, it's hard to believe him this time. A more honest view is to zoom out and look at it on a log scale, and notice that each "crash" bottoms out much higher than the previous one. So if one is going to reason purely from historical prices, then a reasonable observer would not confidently say that the last peak happens to be the final one before it goes to zero forever. That's like looking at a toddler learning how to walk and after the fourth time he falls down saying the kid will never walk. A more reasonable take is that if the Bitcoin price chart tells us anything, it's more likely the story of an emergent store of value.   Of course, chart analysis to predict future prices is generally a fool's errand, and even more so with this unique phenomenon. There are not many analogues in history -- we don't have exchange rates of gold from 2500 years ago. It's better to think about Bitcoin from first principles and think about long term adoption while avoiding short term price predictions.   

Adjacent crypto: altcoins, blockchains etc.

To make matters more confusing, most critics (and Weaver is no exception) put Bitcoin in a bucket with all the other cryptocurrencies, ICOs, NFTs etc. But almost all of the other stuff around "crypto" is junk, much of it unethical or even fraudulent.

Leaving aside the many outright frauds, the whole "altcoin" space reminds me a bit of the history of the Internet.  In the 1980s and 90s, TCP/IP had alternatives like ATM (Asynchronous Transfer Mode). A lot argued that the IP network wouldn't scale, or wouldn't offer good enough QoS, etc. They argued that the net would never be used for serious things like the phone network or television. It's true that there are various trade-offs in the design of TCP and IP, even some arbitrary choices. You can argue for different ones in hindsight. And things do evolve, albeit slowly. Witness IPv6 getting deployed in a backward compatible way over more than 2 decades, while IPv4 continues to chug along. Even ATM was absorbed as a short-lived layer 2 protocol under IP. But there's only one Internet. That's the so-called network effect. If the protocol is good enough, early enough, it becomes the standard.  

And that is where proponents and critics of "altcoins" are causing confusion and driving unjustified hostility to Bitcoin. Viewing Bitcoin as one of many "cryptocurrencies" masks a basic reality: Bitcoin is like the Internet of money and it is here to stay.

That said, I'm not against all other cryptocurrencies. For example a broader smart contract platform makes sense long term, and Ethereum may be the one for the ages. But there are significant technical hurdles remaining. And it's already so bloated very few people actually run a full Ethereum node. And that's all before the much delayed eth 2.0 migration, which if it succeeds may introduce a potentially fatal governance change called proof-of-stake. Building a "world computer" as it needs to be is much harder than what has been achieved to date. 

"Blockchain not Bitcoin" is another common theme among "crypto" hopefuls. But without a real reason for decentralization, a blockchain is just an expensive and slow database. Most of the envisioned applications for blockchains can be more easily achieved with traditional databases.

Bitcoin's proof-of-work ledger for sound commodity money is to date the only real world blockchain use case.

Energy and Proof-of-Work

Speaking of proof of work, energy use is the most common and dangerous vector of FUD against Bitcoin, and Weaver recycles the usual points. He claims that Bitcoin miners are "wasting tons of electricity". This topic is deep and generally misunderstood. Here's my attempt to distill it in my paper entitled "Dynamics of Bitcoin mining":

Does mining use too much energy?

This question assumes the system requires some amount of computation to be done and that it ”wants” to minimize the energy to achieve it. That is indeed how most systems work. But not Bitcoin. Proof-of-work does the reverse of that. The system ”wants” a certain value to be spent on energy, and the amount of computation adjusts to achieve it. Of course individual miners compete by being as efficient as possible, but the resulting collective behavior is to achieve a certain cost of energy with variable amounts of computation, not to perform a specific amount of computation with variable amounts of energy. 

This unusual combination – individual participants being efficiency-seeking but their collective behavior being efficiency-neutral – is very counter-intuitive and probably the root cause of much misguided hostility. It’s also worth emphasizing that the amount of energy doesn’t matter, only the cost. If the price of electricity relative to everything else in the world doubles, but nothing else changes, then Bitcoin would simply use half the amount of energy to achieve the same relative cost[...] The cost of energy is a feature not a bug, and ”waste” is impossible by design. All of the energy is ”work”. 

And where there’s no ”waste”, the question of energy use boils down to a moral judgement. Can you argue that heating in the winter, even if perfectly efficient, is not justified and people should move to warmer climates? What about air conditioning, or electric clothes dryers, or ice cream? When is any purposeful energy use justified? Morally, as long as access to and the price of energy is fair, what it’s used for should be accepted as a subjective choice. Bitcoin offers the possibility of inflation-resistant savings, low-cost long-distance value transfer, and censorship-resistant money. For its users, these are important benefits which are no less justified than most other uses of energy.

In the same interview, Weaver attacks the notion that Bitcoin "incentivizes green power", and goes on to misrepresent the incentives, and the supply and demand dynamics of electric power. I covered this too in the same paper:

Many sources of renewable energy are highly variable: solar and wind power depend on time of day and weather, hydroelectric power is seasonal, etc. In general, these ups and downs on the supply side do not line up perfectly with the demand for electricity. Further, even with the largest possible batteries, water reservoirs, etc., electric energy remains extremely difficult to store for later use at a large scale. Thus there is often a lot of ”stranded” energy when using renewable sources. Just like off-peak bandwidth in telecommunication networks, or empty seats on scheduled airline flights, the cost of production is already sunk, and so for the supplier, selling stranded power at any price is better than letting it go unused. [...] The competitive dynamics of Bitcoin mining are such that it shifts in time and space to the lowest available cost of electricity. This occurs not just by deploying hardware to various locations, but also by turning miners on or off instantly. This flexible demand-side support makes mining the ideal customer to balance variable supply, and as variability tends to affect renewable much more than fossil fuel sources, in effect, Bitcoin subsidizes the development of ”green” electricity.

Adoption

Finally, Weaver claims that Bitcoin will permanently fall apart Real Soon Now™, when it runs out of suckers. But there's really no basis for his claim. He doesn't give any reason why the number of suckers is a particular fraction of the world's population and why that limit has been reached now. Why didn't it run out after 1M people? Or 100M? Why not 8 billion people?  

Of course, the success of Bitcoin depends on widespread adoption. Why is gold used as money? You can try to explain it based on some key properties: it's impossible to synthesize, the supply is limited, it's fungible and can be shaped easily, it doesn't degrade... Those are useful, but we don't know if they are sufficient.  The emergence of a monetary good is a fascinating topic, one that most people don't understand and don't even realize that they don't know. ("The Origins of Money", an article which predates Bitcoin, is a good read). Ultimately, Bitcoin is just a Schelling point whose emergence is highly path dependent.That's just a fancy way of saying "we'll see", but every day that passes makes the ultimate success more likely, and it's been almost 5000 days already.

2022/01/22

Doomsday argument

And now for something completely different: a fun little probability puzzle. 

What's the probability that the human race will end some time in the next 100 years? Surprisingly this question has a logical answer. And not because we have some magic crystal ball.  In fact, our puzzle  specifically assumes we have no information at all about the future.  

Here's how it goes.  Let's step out of time for a second, and consider the total number of humans who will ever exist. Let's say that number is N.  If you are of the Abrahamic faiths, you can call the first one Adam. But we're just having fun so we'll just number them from first to last: 1,2,3, ..., N.  Now let n be your number. So 1 <  n < N, you are somewhere between the first  and the last person ever.  Since we  have no information about the future, we have no clue if you are near the end or near the beginning or somewhere in the middle.  You just happened to land at some random position in the long line of  humans. So we have to assume that any position is equally likely, or technically that n is uniformly distributed between 1 and N. The chance that you are in a particular interval is equal to how big that interval is relative to the whole sequence. There's a 50% chance that you are in the first half and 50% chance that you are in the second half,  there's a 95% chance that you are in the first 95% and a 5% chance that you are in the last 5% of people, etc.  So P(n < f*N) = f and P(n>f*N)  = 1-f, for any fraction f between 0 and 1.  

We don't know N, but we can estimate n, because we can approximately calculate the cumulative population to date. This is more accurate  than you might think because the parts really long time ago where we have poor estimates are also the times where there were very few people.  The left tail is long but thin. Estimates now are around  n = 117 billion.

From the above, the distribution of N is P(N<n/f) = 1-f. That means there's a 5% chance that N < 123B i.e. that there are only 6 billion babies to go before the last one. If we translate that into time, using the current rate of 140M births per year,  it means there's a 5% chance that we have less than 43 years left! And a 50-50 chance that we'll be around for another 800 years. At the other end, a 5% chance that we have more than 16,000 years left, and so on.

I heard about this puzzle known as the "doomsday argument" about a year ago. Of course you can debate about whether this is a realistic model, but it's a cute way to provoke thought about all the minor risks we collectively worry about and the big ones we don't consider rationally. 

Reminds me of a few scenarios discussed in this blog a long time ago:  ineffective posturing on climate changethe asteroid lottery , political pandering in a pandemic... Ouch ouch ouch! Sadly humanity doesn't seem to have gotten wiser in the decade (!) since those posts... 43 years seems like an awfully short time. At least math is eternal!