Thursday, April 15, 2021

another dice game (expected value)

This is a question I often ask(ed) in interviews: 

Alice charges you $1 to play a game. She rolls a (fair) die 3 times. You win if the rolls are in strictly increasing order e.g. 1, 5, 6. You lose otherwise e.g. 1, 3, 3 or 2, 6, 1. Question is, how much should she pay you when you win, for this to be a fair bet? 

To answer this question correctly, you need to consider all cases, count them properly, and then use the formula for expected value. 

There are 216 different combinations of 3 dice - starting from (1, 1, 1) going all the way to (6, 6, 6). of these, we want to only include the ones that are strictly increasing. If we count them carefully, we find 20: (1, 2, 3); (1, 2, 4); (1, 2, 5); (1, 2, 6); (1, 3, 4); (1, 3, 5); (1, 3, 6); (1, 4, 5); (1, 4, 6); (1, 5, 6); (2, 3, 4); (2, 3, 5); (2, 3, 6); (2, 4, 5); (2, 4, 6); (2, 5, 6); (3, 4, 5); (3, 4, 6); (3, 5, 6); (4, 5, 6)

That is 20/216. So you win in 20 cases, and lose your 1$ in 216-20= 196 cases. So, to make up, you need $x such that: x*20=196*1 or x = 9.8$

The difficulty in solving this problem is not so much in the math, but in the mechanics. It takes work, most candidates are not willing to put in the effort during the interview. I'm more than happy to overlook counting errors (after all, an interview is a somewhat high pressure setting) but do expect to see how they think through the problem. 

Solving this problem in python is quite easy. Code is below to enumerate all cases and pick out the right ones. The expected value calculation is trivial once this is available.

import itertools;

x=[i for i in itertools.product(range(1,7),repeat=3)]; # all 3-dice score combinations possible

print(len(x)) # computer prints 216

y=[i for i in x if i[0]<i[1] and i[1]<i[2]]; # all 3-dice score combinations where you win

print (len(y)) # computer prints 20


A (cursory) look at ESG investing

Lately, a lot is being said about ESG (Environmental, Social and Governance) sensitive investing. Many claims are made about how people these days are more aware of environmental issues with one eye on global warming, etc. However, change begins at home with each and every one of us, we shouldn't just wait for others to change to save the planet. Regardless, this post is about ESG investing, so let's get to it. 

The UN has published a list of 17 SDGs or Sustainable Development Goals. These can be classified into 5 categories called the 5 Ps - the focus is on People, Planet, Prosperity, Peace, and Partnerships. The goals are: No Poverty; Zero Hunger; Good Health & Well-being; Quality Education; Gender Equality; Affordable and Clean Energy; Decent Work and Economic Growth; Industry, Innovation and Infrastructure; Reducing Inequalities; Sustainable Cities and Communities; Climate Action; Life Below Water; Life on Land; Peace, Justice and Strong Institutions; and Partnerships for the Goals. 

read more about UN SDGs here 

Socially Responsible Investing (SRI) or Impact Investing is effectively applying similar (but not necessarily the same) principles for value-driven investing. The goal is to do social good as you invest - integrating ethical and social causes into your investment framework. 

ESG is a more broad-based structure within whose context people try to incorporate various aspects of the SDGs and the SRI frameworks to invest money in ways that benefit society and the planet as a whole. 

Ben Felix explains some of the considerations in this interesting video: 

A useful description of an ESG framework in practice comes from Stashaway, a robo-advisor in Singapore that went from starting up to over 1B$ in AUM in less than 5 years! (I am not affiliated with them). 

read more from Stashaway.sg on this topic here 

Cliff Asness (AQR Capital) says in a recent interview, if you cut out a part of the investment universe that introduces additional constraints into the process and cannot by itself be value accretive. However, in recent years ESG is having an impact in the way corporates are able to raise funds. 

watch from youtube: AQR's Asness explains the quant view on ESG investing

Let's say you have 1M$ to invest. If cigarette making companies give you higher returns, you would invest there. However, if more and more people shun these investments on an ethical basis and money flows into say, Solar or Wind power companies, then the so called "sin industries" - cigarettes, gambling, etc. will have some trouble raising funds leading to an increase in the interest they have to pay, thereby raising their internal project hurdle rate, so less projects get funded and growth slows. Or so the theory goes. But if investors are all focused on the so called sustainability-friendly firms, this trade gets crowded, the stocks get bid up, and these pricier assets generate lower returns overall as a result.

Of course, just because a board is more diverse ("diversity takes time, but inclusion can be immediate" as some people like to say regarding the DNI or Diversity aNd Inclusion movement), or a company is more socially responsible, there are no guarantees that the company generates better revenues. However, it does stand to reason that a company that takes good care of its stakeholders - suppliers, consumers, and the entire supply chain - as opposed to just the shareholders in the company - would better weather the inevitable storms that every business has to face. So then, is ESG investing more an attempt to protect the downside vs. capture the upside of various investments? 

Litigation around environmental, social and governance (ESG) issues is on the rise, with both civil and criminal cases being heard by courts around the world. Such litigation poses not only financial risk but also reputational risk to the corporations involved. This trend therefore adds an additional layer of investment risk to companies with weak ESG metrics.                       -- Gregory Kunz, Head of Research, Pictet Asset Management (article)

When the last major financial crisis happened, there wasn't this much attention paid to the ESG movement, so we do not know how various firms weathered the volatility that resulted (rather, we can figure how they reacted to volatility during that period, but due to lack of ESG being mainstream, we cannot claim that the reaction factored in ESG aspects). However, people like to claim that in 2020 with Covid, corporates with higher (better) ESG scores outperformed the competition. There are research papers that indicate that it wasn't that environmentally aware companies did better but that the less environmentally friendly sectors were badly impacted due to stay-at-home restrictions and business closures. Lower demand for oil due to slowing growth is not necessarily an ESG driven theme, though perhaps the gradual shift towards electric vehicles is. It helps if savvy investors look deeper to understand the various drivers that move stocks in various sectors, and use that as a basis for drawing conclusions as opposed to just picking a favorite cause and running with it.

A fascinating course on Coursera offered by Erasmus University called "Principles of Sustainable Finance" explores these and other ideas from both sides and presents what I think is a fair and balanced view of the topic, all while educating us on what Sustainable Investing really means. Worth the time.

We can try to save the planet (we live here after all), invest wisely, and make money while doing good. Everyone wins. 

Using Simulation to Solve Probability Questions

Professor P runs two experiments: in A, he rolls a die till he gets two consecutive 6s. In B, he rolls a die till he gets a six followed by a 5. After running each experiment 100,000 times, he takes the average number of rolls needed - for A this number is x, for experiment B, this is y. The question is, is x=y? True or False?

The naive or lazy reader usually concludes that the answer is True. But this is not so. Let's think through this. 

In A, if he rolls a 6 and then rolls a different number, he has to start all over. In B, if he rolls a 6 and then rolls another number that is not a 6, he has to start over. But in B, if he rolls two sixes, he doesn't start over, he has another chance to try to roll a 5. This means that y < x in average. But how do we prove this? Mathematically, this can be done using the principle of iterated expectation. A well-written solution is available here. This tells us that the average number of rolls for A is 42, and that for B it is 36. 

In this post though, let's look at how to solve this problem if you are not able to do the slightly involved math. Simulations are a great way to do this. And python makes it simple beyond describing. Let's look at code: 

import random as r; 

def die_roll(): return r.randint(1,6); 

# experiment A
x=[]; # store the results of the simulation
for i in range(100000): 
 t=[die_roll(), die_roll()]; 
 while t[-1]!=t[-2] or t[-1]!=6: t+=[die_roll()];
 x+=[len(t)];

print(sum(x)/float(len(x))); # computer prints 42.06674

#experiment B
x=[];
for i in range(100000):
 t=[die_roll(), die_roll()]; 
 while t[-1]!=5 or t[-2]!=6: t+=[die_roll()];
 x+=[len(t)];

print(sum(x)/float(len(x))); # computer prints 36.08444

As you can see, a relatively lazy 5 minute piece of code can generate the same answers quite comfortably and makes it possible for us to confidently answer questions like this one. 

Why do interviewers ask questions like this in financial services interviews? 
This kind of evolution of a number series (through die rolls here) with the end of the series being defined by the first time a particular score appears similar in many ways, to the evolution of price movements and the pricing of knock-out options. True, the mechanics of option pricing is quite well defined in literature and there are formulas for achieving that, but in an interview setting the interviewer is trying to assess whether the candidate is able to think through issues like this and propose a method to solve this kind of problem. When I conduct interviews (and I have conducted several hundred over the years), I tell the candidate up front I don't care what numeric answer they come up with, I just want  to understand their approach to solving these problems, and what they are thinking.



The real cost of things

If there was something that was fairly cheap to buy and use, provided convenience or pleasure, but meant either that a. you would have longer term ill-effects from the use of the product, or b. that the product became progressively more expensive to use, you would find some ways of avoiding the product in question. 

Several examples of this kind of situation abound: 
  1. Cigarettes provide short-term pleasure and are known to cause cancer longer term.
  2. Recreational drugs of various kinds fall into a similar category - near term high, longer term brain damage
  3. Adrenaline junkies take larger and larger risks to get the rush, sometimes sacrificing safety risking severe injury or loss of life and limb.
In all these cases, taking a longer term view would help people make wiser choices. But what about situations where the quality of your choice changes with time? Let's look at a couple of examples of these:
  1. You buy shampoos, perfumes, and mouthwash in small containers - you know you'll use up larger quantities, but buy smaller containers for convenience, but the plastic is not biodegradable, and the ecosystem chokes up
  2. Garments you purchase are synthetic - similar issue - and with rapidly changing fashion, they get outmoded and are discarded quickly but do not disintegrate.
Even 15 years ago, these might not have been considered particularly bad ideas, but they would be frowned upon now. Single use plastics are another such item - utility is for limited time, but the damage caused is long lasting. This balance is to be set right if we believe we do not inherit the planet from our parents but borrow it from our children. There was even a recent article in the news that adults on average ingest a credit-card's amount of plastic each week through the food they consume. Microplastics are now invading water supplies, aquatic life, and other areas of the ecosystem that impact humans.

The true cost of a good thus, is not just the cost of its production, distribution and sale. The true cost should include the cost of harvesting the waste generated and recycling it away from the environment. As regulation ramps up (as it must), prices will reflect environmental realities. True, for a while corporations and people can get by through regulatory arbitrage, but this will likely be a limited period before laws are stringently enforced globally. In a related post in this blog, I explore some interesting ideas companies are pursuing in terms of becoming more planet-friendly. 

As individuals we cannot by ourselves solve global issues such as plastic pollution, but if we move together, convince our friends, etc. The movement can change the planet for the better. There are some outstanding efforts like the man who started a movement to clean up Versova Beach in Bombay, the couple that worked to re-forest areas of Latin America, and the movement to plant new trees in Singapore.

Saving our planet is not Greta Thunberg (or other young people)'s responsibility alone. We all live here. We all need to take an interest and do what little we can do to protect and conserve natural resources. As they say, "reduce, reuse, recycle". It appears that a greater awareness and adoption of ESG principles in investing will force a gradual movement in that direction.

Tuesday, August 7, 2018

recent recommended reads

this book was beyond excellent. very simple ideas beautifully explained. the four agreements is easy to read, much harder to practice, but will help you become a better human being in the true sense of the word. the agreements are: a. be impeccable with your word (integrity), b. do not take anything personally, c. never make any assumptions, and d. always do your best. 

the author uses his experiences from SEAL training and subsequent missions to deliver hard-hitting life lessons on leveraging one's skill set with a never say die attitude to achieve success. inspiring. but I still liked "unleash the warrior within" and "the mission, the men, and me" better. good read regardless.

what can I say? this book blew me away. the stories were interesting to read, but also require deep reflection on the reader's part. beautiful, even soulful, book.

these authors do a good job explaining deep learning from the fundamentals. it is nice(r) if you have some understanding of tensor calculus and tensorflow going in. I'd recommend siraj raval's excellent youtube video on the differences between facebook's pytorch and google's tensorflow platforms to at least get to a nodding level of familiarity to supplement this reading. 

michael lewis tells the story about kanheman and tversky and behavioral economics, and how the field blossomed. excellent read. would also recommend "thinking fast and slow" and "models. behaving. badly"

well, if you need to find a job, you need to brush up your interview skills. having recently interviewed several people for roles in my team, this was a book I read to conduct interviews competently - interviewing is an art form at both ends of the interview table - don't let anyone tell you otherwise.

GRRM writes a very nice tale set in a futuristic universe. tugs at your heartstrings. 

NDGT does not disappoint. very engrossing book, and very readable too - unlike hawking's "a brief history of time" which I found very difficult to read. really understanding the latter took me several weeks. this one is a much more friendly read.
 how to say yes and be open to exploring possibilities vs. closing your mind to what might happen while staying comfortably ensconced well within your comfort zone. must give these ideas a try.

interesting book that covers not just the basics of finance in valuing potential investments, but also how to present these to a portfolio manager. having worked in both equity research and as a PM, I think this is a good book for people to refine their skill-sets before they set foot into a wall street job. it's based off a course the authors teach at columbia university business school. highly recommended reading.

What makes a table, a table?

I was recently speaking with a friend who happens to be a philosophy major. Most of my other friends graduated with degrees in hard sciences, so my conversations with this person in particular tend to sometimes follow interesting tangents. When engineers get together those conversations tend to be, well, different... even if interesting in their own way.

We got to talking about a number of different topics, and ended up discussing the question of this post "What makes a table a table?". At first I thought this was a joke, but was assured it wasn't. Do you consider something to be a table because it has a flat top? because it has 4 legs? the ability to support or hold other objects in a relatively stable way? some other feature or characteristic?

This might seem to be flippant, but even though we can agree that most humans might eventually come to some agreement on what a table is (or isn't), it becomes extremely important and quite an interesting question when you view it from the standpoint of machine learning.

Think of this question from a machine's perspective, especially in a supervised learning setup. Supervised learning, for those unaware of the term, is a training setup for machine learning where the system is provided with several labeled examples of objects - say tables, and not tables, and is then given unlabeled images and is asked to say whether the picture is of a table (or not).

Now the computer has to answer the same question that we as humans can think of philosophically, but it has to answer that question using data. It is building up knowledge of our world from examples we give it. Some things might appear obvious to us, and we can expect we'll come to "reasonable" agreement with people that have similar ideas about things. Computers on the other hand learn from hard data, and while some of their conclusions might initially be questionable, a model that learns from experience might surprise us with its sophistication in its ability to answer difficult questions. Take for example the AI that beat Lee Sedol on Go - widely known as one of the most complex games known to humankind. In a later interview, Lee and other experts were unable to interpret what led the computer to make certain moves - only in the overall context of its wins did the conceptual leaps it made in reasoning become clear.

Purists might say I am mixing examples (if not metaphors) here - AlphaGo (the Go AI) used reinforcement learning, whereas the program from my table identification problem used Supervised Learning. My response to that is that the views expressed here are high-level arguments analyzing the complexity of knowledge we often take for granted, and that this can be learned - not a focus on particular algorithms in each case.

If you've read this far, you need a reward. Check out this link for some rather unique table designs. I particularly like that floating set of cubes - looks like a design win to me, though I wonder if form there defeats function!

Friday, August 3, 2018

The robots are coming

Digital assistants on your mobile phone are useful. But do you wonder how else the innovations that led to these might apply in other places? After all, useful as they are, making your interface with the world at large in general, and with devices in particular, more seamless, digital assistants don't seem to be changing the world, do they? But wait... the technology behind these is being used in other ways for greater impact.

With the advent of computers, two forces arise in the workplace with a tension between them. One is the ability to execute more complicated workflows. The other is the capacity to process workflows more quickly through automation.

As workflows become more complex, there is a greater chance for errors - there may be process flow legs that are not accounted for during the design and deployment phase, and these can throw off the automation.

So, while automation is nice, what is the next "advancement" we can make there? How do we automate the process of automation? Enter RPA or Robotic Process Automation.

How does it work? How can it work? Let's look at some history.

In Excel there is a capability to record a macro - this is not something you need to program - Excel software "records" or remembers the sequence of keys you press, and then replicates that process i.e. the series of key presses, in the same order multiple times flawlessly.

In manufacturing, they do the same thing with CNC machines where a human operator trains a machine that replicates his/her movements - the machine can then do what the operator does, only more quickly, and with greater precision. This is possible now because of advances in artificial intelligence and computer vision. The computer can see and recognize objects. And it can grasp them with mechanical "hands" - things it was not able to do earlier.

Now there is software that generalizes the Excel macro recording capability more broadly, so complex process flows can be automated relatively effortlessly. The computer then executes these flows as discussed previously, replicating the operators actions. But what happens if the computer throws an error or shows different screens for a particular record the computer is processing? The automation software might not be able to adapt, since there is no intelligence built in, and this could cause very rapid, but also completely erroneous processing of records.

So what to do? We need intelligence to automate the automation. The machine has to learn the process steps it needs to execute - the Robotic Process Automation part, then it needs something else to be able to intelligently handle exception cases. That is done through Cognitive Computing.

Cognitive Computing brings 4 new capabilities to things already discussed:

  1. Process is Adaptive - the software can adapt to changes in the workflow by watching an operator process a large number of cases, or through working through cases using a standard template, then asking for operator input if it ever gets stuck (with added intelligence it knows when it is stuck)
  2. It is Interactive - the software learns from watching the operator, can interact with users, etc.
  3. It is Iterative and Stateful - the software is able to learn from cases processed previously, iteratively, and manage its state (internal representation of learned knowledge) accordingly.
  4. It is Contextual - the software is aware of what processing steps it needs to perform in what contexts, and how it can use this knowledge to take on ever more complex operations going forward.
More advanced systems today support natural language interfaces to make user interaction much more seamless. Again, this is a capability that becomes possible through AI techniques - natural language processing, text analytics, and the like. 

So is everything hunky-dory with the advent of RPA and CC? No, there are several new issues that arise. 

First, there are societal problems: we need to determine what we want to do with the human work-force that is displaced. They need to be retrained and move up the value chain. This is along the lines of the Beveridge Curve, but the displacement here is not just due to macro factors but a larger scale move due to technology.

Second, there is a temptation to automate sub-optimal processes - why care if the process is not the simplest it could be, or is more tedious then necessary? Machines will do it after all. And machines do not complain if you make them work longer on mind-numbing tasks.

Third, why improve processes? If it ain't broke, don't fix it. Innovative techniques available to simplify processing? "Ignore them! We've just spend $10M on RPA. Don't tell me I have to spend more to improve processes now!" This could slow down innovation and make legacy systems live way beyond their useful natural life. 

This article covers a lot of these in some detail.  Robots are no longer replacing only blue collar jobs. Lawyers, doctors, traders of financial instruments, etc. watch out - the Robots are coming for you.