Thursday, 19 October 2023

An Introduction To Financial Risk Management

 


Introduction

The term risk refers to the uncertainty surrounding outcomes, here the outcome could be positive/upside side or negative/downside. However, an investor is generally more concerned about the negative impact as it would lead to losses. 
A natural trade-off between risk and return is observed, high risk has the potential for high returns and lower risk also has return potential. On the other hand, risk is not always related to the size of potential loss, for instance, predictable losses can be accounted for using risk management techniques. Risk-taking means to accept known risks to pursue incremental gains.
As risk is about the variability of losses and not about the size of the loss, it can be measured using standard deviation.

Risk Management Process

  • Risk Management includes the set of activities aimed to eliminate or reduce expected or unexpected losses and determine if the perceived reward justifies the expected risk. 
  • In other words risk is unexpected volatility in asset prices or earnings.
  • Natural trade-off between risk and returns is observed, higher risk there leads to the potential of higher returns.
  • Risk is not necessarily related to the size of the potential loss.
  • The risk management process is a formal series of actions designed to determine if the perceived reward justifies the expected risks.

Steps involved in the Risk Management Process:
  • Identify Risk
    • Through brainstorming sessions
      • Key business leaders and relevant teams
      • Industry level experts - eg., regulatory standards, industry surveys, expert opinions, third-party tools, external sources
    • Scenario analysis
      • Used for identifying risks.
    • Quantitative analysis
      • Actual loss data to discern the magnitudes and frequency of various losses. 
  • Measure and Manage Risks
  • Distinguish between Expected or Unexpected Risk
  • Address the Relationships among Risks
  • Develop a risk mitigation strategy
    • Avoid Risk
      • Avoiding risk refers to a strategy in risk management where an organization takes deliberate actions to completely eliminate exposure to certain risks.
      • Avoiding risk entirely is often not feasible or desirable for organizations, as it may prevent them from seizing opportunities.
    • Retain Risk
      • Depending on the expected rewards based on the probability, frequency and impact
    • Mitigate Risk
      • Reduce the magnitude or frequency of exposure to the given risk factor
    • Transfer Risk
      • Using third party derivatives or structured products
      • Purchase of insurance
  • Monitor the risk mitigation strategy and adjust as required
Loss Categories
  • Expected Loss
    • Simply the average loss that we would expect from the given exposure over a period of time.
    • Probability of Default
    • Built Pricing of Product
    • EL = Probability * Exposure * LGD (Loss Given Default)
  • Unexpected Loss
    • Is the amount of loss that actually exceeds the expected amount.
    • Separate capital base to meet this loss.
Classification of Risk on the basis of knowledge of Risk
  • Known
    • Known
      • Easy to identify to manage
      • Probability and Impact are known
      • Eg: Losing customer to the competition
    • Unknown
      • Also called as Knightian uncertainty
      • The firm is aware of this risk
      • Probability and Impact is unknown
      • Eg: Law suite impact
  • Unknown 
    • Known
      • Aware of the risk but negligent in treating risk
    • Unknown
      • Also known as tail risk events
      • No knowledge about the existence of the risk
      • Eg: Covid before 2019
Zero Sum Game
  • Risk management is also known as a zero-sum game, in this some "winning" parties will gain at the expense of some "losing" parties. If enough parties suffer devastating losses due to an excessive assumption of risk, it could lead to widespread economic cries.
Challenges in the Risk Management Process
  • Fail to consistently prevent market disruption or financial fraud
  • Use of derivatives as complex trading strategies of many entities
  • Inaccurate information would not allow the policies to be effective

Measuring Risk

Quantitative Risk Measures

  • Value at Risk(VaR)
    • Calculates an estimated loss amount given a certain probability of occurrence assuming normal market returns. 
    • Under known circumstances the maximum loss. 
    • There are different approaches for calculating VaR.
  • ES (Shortfall)
  • EC (Economic Capital)
    • Is the amount of liquid capital necessary to cover unexpected losses.
    • Expected tail risk event.
  • RAROC (Risk Adjusted Return On Capital)
    • One valuable metric to consider risk exposures across business units, related to economic capital.
    • RAROC = after-tax risk-adjusted expected return / economic capital
    • Application of this formula
      • This formula is essentially a reward per unit of risk.
      • Used in calculating the value of the cost of equity.
      • Business comparison, when different levels of economic capital exist for each segment.
      • Investment analysis, evaluate potential new product offerings.
      • Pricing strategy, the current pricing strategy provides sufficient return relative to the estimated risk taken.
      • Risk management, highlight areas where risk is not being properly covered with expected rewards.

Qualitative Risk Assessment

Using historical data alone calculates the risk, but history may not repeat itself. Such scenarios use hypothetical forecasts based on a risk manager's assumptions, though such a model can be error-prone and involve risk but a useful exercise to fully understand a firm's risk.

Scenario Analysis
  • Compare the best-case scenario to the worst-case scenario, by shocking variables to the extreme known values.
  • Considers potential future risk factors and associated alternative outcomes.
  • To understand the assumed full magnitude of potential losses even if the probability of the loss is very small.
Stress Testing
  • The primary objective of stress testing is to evaluate the resilience of a system or portfolio to extreme, adverse events or scenarios.
  • A crucial evaluation method to ensure a firm's sustainability. 
  • It assesses extreme scenarios that may occur rarely but can have a significant impact on losses. 
  • On the other hand, probabilistic risk metrics, such as VaR, ES, or Standard Deviation, are suitable when the frequency of losses is high, and the severity of losses is low.
Sensitivity Analysis
  • Sensitivity analysis is used to understand how changes in specific input variables or assumptions affect the output or outcome of a model, system, or decision.
  • It focuses on assessing the sensitivity of results to changes in individual factors.


Managing Risk - Enterprise Risk Management

  • ERM is a process of risk management centrally and highly integrative deployed at the enterprise level.
  • One challenge with the ERM approach is a tendency to reduce risk management to a single value (e.g., either VaR or economic capital), but in dynamic risk management this attempt is too simplistic.
  • Risk managers learned from the financial crisis of 2007-2009 that risk is multi-dimensional and requires consideration from different perspectives or angles.
  • ERM framework requires both statistical analysis and informed judgement on the part of risk managers and plays an equally important role in assessing and managing risks.


Expected and Unexpected Loss

  • Expected Loss
    • Measure of the probability is known - Expected Loss
    • Formula:
      • EL = EAD * PD * LGD
      • Here,
        • EL = Expected Loss
        • EAD = Dollar Exposure At Default
        • PD = Probability of Default
        • LGD = Loss Given Default - The expected severity of the loss if the risk event does occur
    • When EL can be modelled with confidence, it can be treated as a predictable expense or a variable cost
  • Unexpected Loss
    • Measure of the probability is unknown - Unexpected Loss
    • Unfavourable events happen together, and the correlation risk drives potential losses to unexpected levels.


Risk and Reward

  • Higher the risk then higher the expected profit.
  • There is a trade-off between risk and reward, as it becomes much more complex to analyze assets that are thinly traded or not publicly traded.
  • Illiquid assets as well is very complex to analyze.
  • In complex systems extreme unexpected losses may occur.
  • Drivers of the potential list of loss drivers could be exhaustive.
  • Potential for conflicts of interest: Those in the position to be most aware of the presence, probability and potential impact of various risk factors are sometimes the ones who try to profit from its presence.
    • Risk recognition by frontline employees and division managers.
    • A robust risk management system with daily oversight.
    • Periodic independent audits to ensure that steps 1 and 2 are functioning properly.
  • Danger arises when the frequency of tail events increases because the pace of structural uncertainty accelerates such as behavioural shifts, industry trends, government interventions and new innovations.


Types of Risk

Market Risk

Market risk refers to the potential for losses in investments due to movements in market factors. The key to mitigating these risks is to understand the relationship between positions. 
  • Equity Price Risk
    • Refers to the volatility of stock prices.
    • Two parts
      • General market risk, which is the sensitivity of the price of a stock to change in broad market indices. Cannot be diversified away.
      • Specific risk, which is the sensitivity of the price of a stock due to company-specific factors eg rising cost of inputs, strategic weakness. Could be mitigated by holding assets with less-than-perfect correlations.
  • Interest Rate Risk
    • Interest rate risk refers to the potential for a financial institution or investor to experience losses due to fluctuations in interest rates.
      • Uncertainty flowing from changes in interest rate levels.
      • Interest Price Goes Down Bond Value Goes Up
    • Change in the shape of the yield curve
      • The yield curve is a graphical representation of the relationship between the yields (interest rates) and the maturity dates of bonds of similar credit quality but different maturity dates. 
      • It shows the yield (return) investors can expect for bonds with different maturities (how long until they mature).
      • Normally, the yield curve slows upwards, indicating that longer-term bonds have higher yields than shorter ones. 
      • This is because investors typically demand higher yields for locking in their money for longer periods due to the increased uncertainty and risk associated with longer-term investments and reflects the concept of the time value of money and the inherent risk of lending for longer periods
      • Changes in the shape of the yield curve can occur for various reasons and can have different implications:
        • Steeper curve: Good for banks, bad for long-term bondholders (potential price decrease). 
          • Steepening Yield Curve: Imagine you're comparing two roads. One road is a gentle slope upwards, and the other is a steeper incline. The steepening yield curve is like the steeper road. If you're willing to take the longer journey (investing for the long term), you'll see a bigger payoff (higher interest rates). It's like choosing the steeper road because you know the view at the top will be worth it. 
        • Flatter curve: Not ideal for banks, but okay for long-term bondholders (stable prices). 
          • Flattening Yield Curve: Picture a hill that's gradually leveling out as you walk along it. At the start, it's steep, but as you keep going, it gets flatter and flatter. That's what happens with a flattening yield curve. It means that whether you're walking a short distance or a long one (investing for the short or long term), the effort and payoff are becoming more similar. It's like walking along that hill and noticing that the difficulty doesn't change much whether you go a short distance or a long one.
        • Inverted curve: Risky for banks, potentially good for long-term bondholders (opportunity to buy high-yielding bonds).
          • Inverted Yield Curve: Think of a situation where you lend something to a friend, but instead of getting back more later (like with interest), you actually get back less. That's what happens with an inverted yield curve. It means that if you lend your money for a short time, you get more back (higher interest rates) than if you lend it for a long time. It's like your friend saying, "I'll give you back less if you lend me your stuff for a longer time." It's unusual and often signals that something unusual or worrying might be happening with the economy.
    • Positions that are partially or completely unhedged
      • **Unhedged Positions**: When an investment or a portfolio is unhedged, it means that there is no protection against adverse movements in interest rates. For example, if you invest in long-term bonds without any measures in place to mitigate the impact of potential interest rate changes, your investment is considered unhedged.
      • **Partially Hedged Positions**: Partial hedging means that some, but not all, of the risks associated with an investment are mitigated. In the context of interest rate risk, this might involve hedging against changes in short-term interest rates but not long-term rates, or vice versa. Alternatively, it could involve hedging only a portion of the portfolio rather than the entire position.
      • Here's how interest rate risk arises from having positions that are either completely or partially unhedged:
        • **Exposure to Interest Rate Movements**: Unhedged or partially hedged positions leave investors vulnerable to adverse movements in interest rates. If interest rates rise, the value of fixed-income investments such as bonds typically decreases, leading to losses for investors holding unhedged positions. Conversely, if interest rates fall, unhedged investors may miss out on potential gains.
        • **Duration Mismatch**: Duration is a measure of the sensitivity of a bond's price to changes in interest rates. Unhedged positions may have a duration mismatch, meaning that the duration of the investment does not align with the investor's time horizon or interest rate outlook. For example, holding long-term bonds in an unhedged position when expecting interest rates to rise can lead to significant losses due to the higher sensitivity of long-term bonds to interest rate changes.
        • **Cash Flow Risks**: Unhedged positions can also expose investors to cash flow risks. For instance, if an investor has liabilities or obligations that are sensitive to changes in interest rates (such as variable-rate debt), holding unhedged positions that are adversely affected by interest rate movements can lead to financial difficulties.
      • To manage interest rate risk effectively, investors often employ hedging strategies such as using interest rate swaps, options, or futures contracts to offset the impact of interest rate changes on their portfolios. By hedging their positions, investors can reduce their exposure to interest rate risk and potentially minimize losses or take advantage of opportunities in changing interest rate environments.
    • Basis risk
      • Basis risk refers to the potential pitfall you encounter when trying to hedge (protect) yourself from a price movement in one asset using another asset. It arises because the two assets, although related, might not move in perfect lockstep. 
      • Imagine you're using an umbrella (hedge) to protect yourself from the rain (price movement of the asset you care about), but there's a chance the umbrella might not fully cover you (basis risk).  
        • Hedging: Imagine you own a house (asset) and you're worried about the price going down. You buy insurance (hedge) to protect yourself in case the value drops. 
        • Basis: The insurance policy (hedge) might not perfectly match the value of your house (asset). There might be a difference in coverage (basis). 
        • Basis Risk: This difference in coverage between your asset and the hedge is basis risk. In a financial context, it's the risk that your hedge won't completely offset the losses in your main investment.
  • Forex Risk
    • Monetary losses that arise from either fully or partially unhedged foreign currency positions.
    • Occurs from imperfect correlation in currency price movement and international interest rates.
  • Commodity Risk
    • Price volatility/fluctuations of commodities e.g., precious metal, base metals, agricultural products, energy.
    • These prices can be influenced by various factors like weather, geopolitics, and supply and demand imbalances.
    • Volatility due to specific commodities are concentrated in the hands of relatively few market participants.
    • Sudden price jumps from one level to another.
    • Lack of Trading Liquidity: This means that it can be difficult to quickly buy or sell a particular commodity at a fair price. Unlike stocks or bonds that trade on exchanges with many buyers and sellers, some commodities might have a limited number of participants or complex purchasing processes.
      • Trading Liquidity: Liquidity refers to the ease with which an asset can be bought or sold in the market without significantly affecting its price. In financial markets, assets such as stocks and bonds often have high liquidity because they are actively traded, meaning many buyers and sellers are willing to transact at any given time. 
      • Commodity Markets: In contrast, commodity markets can sometimes lack trading liquidity, especially for certain types of commodities or in specific geographic regions. This lack of liquidity can be due to factors such as limited trading volumes, fewer market participants, or logistical challenges associated with physical delivery or storage of the commodity. 
      • Impact on Price Volatility: When a market lacks liquidity, even relatively small trades can have a significant impact on prices. In commodity markets with low liquidity, a large buyer or seller entering or exiting the market can cause prices to move sharply in response to the imbalance between supply and demand. This phenomenon is often referred to as "slippage."

Credit Risk

Credit risk refers to a loss suffered by a party where the counterparty fails to meet its contractual obligations. Increasing risk of default by the counterparty throughout the contract.
  • Default Risk
    • Potential non-payment of interest and/or principal on a loan by the borrower.
    • Probability of Default is a fundamental concept in risk management, providing a quantitative measure of credit risk that enables financial institutions to assess, monitor, and mitigate the potential for default within their lending and investment activities.
  • Downgrade Risk
    • Decreased creditworthiness of a counterparty.
    • Creditor would charge higher lending rate to compensate for the increased risk.
    • Downgrade risk could lead to default risk.
  • Settlement Risk
    • Using derivatives transactions between two counterparties.
    • At the settlement rate, one is in net gain (winning) and the other is in net loss (losing) position. The position which is losing my refuse to pay and fulfil obligation. Also known as counterparty risk or herstatt risk.
  • Bankruptcy Risk
    • Stops Operating
    • Real Value < Loan Amount
Example:
  • Net gain of $1000 at settlement date.
  • Counterparty experienced financial difficulty
    • Recovery rate/value, estimated payment while handling above risk - Only able to pay $800 or 80%
    • Loss given default (LGD) - $200 loss or 20%
    • If the recovery value is 0% or LGD is 100%, complete default and the possibility of a bankruptcy scenario.
Risk managers consider the below while credit risk identification process.
  • Instruments diversified both geographically and by industry?
  • Interest charged on the instrument is in proportion to the risk taken?
  • Correlation between instruments and risk factors been properly considered?
  • Firm or industry-specific financial ratio indicating cause for concern?
  • Exposed to a large number of small loans or a small number of large loans leading to concentration risk?
  • PD of various instruments owned?
  • Probabilities of default correlated in any way?

Liquidity Risk

  • Risk of sustaining significant losses due to inability to take or exit position at a price.
  • Trading Liquidity Risk
    • Losses flowing from a temporary inability to find a needed counterparty.
    • Ability to turn assets into cash at any reasonable price.
    • Bid-ask spread, can be thought of as the loss that would be sustained by a trader who sells an asset and then immediately buys it bank. The higher the spread the lower the market liquidity and vice versa.
  • Funding Liquidity Risk
    • Refers to the risk that an institution will not be able to, raise the cash necessary to make debt payments
      • Unable to pay down - cash obligations to counterparties or fund capital withdrawals.
        • Fulfill cash, margin and collateral requirements of counterparties.
        • Meet capital withdrawals resulting in a loss.
    • Asset Liability Mismatch
    • Redemption risk
      • Some investment funds, particularly those that invest in less liquid assets like real estate or private equity, might not hold enough cash readily available to meet all redemption requests immediately. If a large number of investors redeem their shares at once, the fund manager might be forced to:
        • Sell other assets, potentially at a loss if they need to sell quickly.
        • Suspend redemptions temporarily, restricting investors' access to their money.
    • Margin/haircut funding risk
      • Imagine you want to buy a house (investment) but don't have enough cash upfront. The bank (broker) requires a down payment (margin) and might also consider the value of your car (collateral) as additional security. However, they might not value your car at its full market price (haircut). This all affects how much you can borrow (leverage). If you experience a financial setback and need to sell your car quickly to raise cash (funding liquidity), you might face difficulties due to a less liquid market, potentially leading to financial problems.
    • Rollover risk
      • Risk that investors may not be able to roll over short-term debt to finance the purchase of an asset.

Operational Risk

  • Potential losses flowing from inadequate or failed internal processes, human error or external events.
  • Technology risk - inadequate computer system
  • Natural disaster
  • Cyber security risks
  • Fraud
  • Accidental mistakes
  • Very challenging to quantify

Legal and Regulatory Risk

  • Legal Risk
    • Potential litigation to create uncertainty for a firm.
    • One party suing another party..
  • Regulatory Risk
    • Uncertainty surrounding actions by government entities.
  • Legal and regulatory risks are highly integrated with both operational and reputational risks.

Business Risk 

  • Variability in inputs that influence revenues
    • customer demand trends
    • product pricing policies
  • Cost structures
    • cost of production inputs
    • supplier negotiations
  • Diverse business elements
    • new product innovations
    • shipping delays
    • production cost overrun

Strategic Risk

  • Long-term decision-making about fundamental business strategy. Eg: after spending millions of dollars in developing a new product but then that failed in the marketplace.
  • Regulatory landscape could change and materially alter the profitability of a project.

Reputation Risk

  • Reduce brand value, will suffer loss in public perception or consumer acceptance.
  • Loss of confidence in the firm's financial soundness.
  • Perception of a lack of fair dealing with stakeholders.
  • Experiencing a loss in another risk category could lead to reputation risk.
  • Social media amplify reputation risk which may or may not be accurate.
  • Could start with loss of profits but then could lead to bankruptcy.

Risk Factor Interactions

  • Correlated risks
    • Significant danger in risk management occurs when independent risk factors are correlated. 
    • For example default risk leading to credit risk, business risk and reputation risk.
    • Most dangerous with unexpected losses.
  • Risk managers could consider historical correlations between identified risk factors and forecast the nature of these relationships to measure the risk planning process.
  • Challenge of understanding risk aggregation which can be applied to measure all risks at the enterprise level.
    • Using notional could cancel out cancel out each other, although risk is involved.
    • Market participants have resorted to using option Greeks to model uncertainty, but these values cannot be aggregated with other positions at the enterprise level.
  • Drawback of VaR
    • Can alter the computed value by adjusting the number of days or confidence level.
    • It measures the largest loss at a specified cutoff point but not the magnitude of tail risk.
  • Expected shortfall
    • Statistical measure designed to estimate the magnitude of aggregate tail risk losses.

Credits and References

https://www.javatpoint.com/financial-risk-management
https://www.investopedia.com/
SchweserNotes 2023
BionicTurtle Notes 2023
Chapter 1

Thursday, 5 October 2023

Time Value of Money in Financial Management

Introduction

The time value of money (TVM) is a fundamental concept in finance that states that a certain amount of money is worth more today than the same amount in the future. This is because the money has the potential to earn interest or increase in value over time. As a result, having the money now is more advantageous than receiving the same amount at a later date.


Interest Rate

The interest rate is the amount charged on top of the principal by a lender to a borrower for the loaned amount. Interest rate and discount rate are almost used interchangeably.

Interest Rate Interpretation

  • Discount Rate
  • Required Rate of Returns
  • Opportunity Cost


Components of Interest Rate

  • Real Risk-Free Rate
    • Real - When there is no inflation
    • Risk-Free - When there is no risk
      • Risk Types
        • Default Risk - Risk of not recovering of money on time
        • Reinvestment Risk - Possibility that an investor will be unable to reinvest cash flows received from an investment, such as coupon payments or interest, at a rate comparable to their current rate of return.
  • Inflation Premium
    • Inflation reduces the purchasing power of a unit of currency. Hence this needs to be compensated to investor for expected inflation.
    • The inflation premium is the additional return that investors demand to compensate for the expected erosion of purchasing power due to inflation.
  • Default Risk Premium
    • Probability of not recovery money either on due date or not at all
  • Liquidity Premium
    • Liquidity premium is the additional compensation used to encourage investments in assets that cannot be easily or quickly converted into cash at fair market value. For example, a long-term bond will carry a higher interest rate than a short-term bond because it is relatively illiquid.
  • Maturity Premium
    • The maturity risk premium is the additional compensation investors demand for holding long-term bonds instead of short-term bonds. As bonds have longer maturities, investors take on additional interest rate risk and inflation risk. To compensate, long-term bonds must offer higher yields than short-term bonds.

Cashflow

Cash flow refers to the net balance of cash moving into and out at a specific point in time. Below are few common terms for series of cashflows:
  • Annuity - is a finite set of level sequential cash flow
  • Ordinary Annuity - first cash flow that occurs one period from now (indexed at t=1)
  • Annuity Due - first cash flow occurs immediately (indexed at t=0)
  • Perpetuity - perpetual annuity or a set of level never ending sequential cash flows, with the first cash flow occuring one period from now.
  • r - rate of interest per period
  • FVn - future value of the investment N periods from today
  • PV - present value of initial investment
  • m - frequency of compounding


Future Value

Future value (FV) is the value of a current asset at a future date based on an assumed growth rate. Investors and financial planners use it to estimate how much an investment today will be worth in the future. External factors such as inflation can adversely affect an asset's future value.

  • Single Cashflow
    • Time value associated with a single cashflow or lump sum investment
    • Formula:
      • FVn = PV((1+r)^n)
    • Example:
      • r = 0.05
      • n = 2
      • Original investment = $100
      • Interest for the first year = ($100 * 0.05) = $5
      • Interest for the second year = ($100 * 0.05) = $5
      • Interest for second year based on interest earned in the first year = ($5 * 0.05) = $.25
      • Total sum in 2 years = $100 + $5 + $5 + $.25 = $110.25
    • The interest earned on the interest is known as compounding.
    • n and r must be in same unit if N is in months then r should not be in years, it should also be in months. Else we need to transform the r to make to months.
  • Non Annual Compounding
    • Here the time value of money is calculated on the investments paying interest in different frequencies of compounding.
    • Formula:
      • FVn = PV(( 1 + r/m)^mn)
  • Continous Compounding
    • So far compounding period illustrates discreate compounding, now if the number of compounding periods per year becomes infinite, the the interes is said to compound continously.
    • Formula
      • FVn = PV*(e^rn)
  • Series of Equal Cashflow
    • Equal cash flows can grow significantly over time due to compound interest. The longer the investment period and the higher the interest rate, the greater the future value will be.
    • Formula:
      • Future Value (FV) of annuity due = PMT x [(1 + r)^n - 1] / r
    • Example:
      • Scenario: Imagine you decide to save $100 every month for the next 5 years (60 months) into an investment account that offers a yearly interest rate of 8% (compounded annually). This represents an equal cash flow of $100 each month.
      • Goal: We want to calculate the future value of this series of equal cash flows at the end of 5 years.
      • Solution: There's a specific formula for future value of an annuity due (a series of equal cash flows at the beginning of each period) which is ideal for this scenario. However, for equal monthly deposits, we can slightly adjust the formula to account for monthly compounding. Here's the breakdown:
        • Number of compounding periods (n): In this case, with monthly deposits for 5 years, we have monthly compounding. So, n = total months = 5 years * 12 months/year = 60 months.
        • Interest rate per period (i): Since the interest rate is yearly (8%), we need to convert it to a monthly rate for compounding. So, i = annual rate / number of compounding periods per year = 8% / 12 = 0.667% per month (converted into a decimal).
        • Cash flow per period (PMT): This is the amount deposited each month, which is $100.
      • Formula:
        • Future Value (FV) of annuity due = PMT x [(1 + i)^n - 1] / i
      • Plugging in the values:
        • FV = $100 x [(1 + 0.00667)^60 - 1] / 0.00667
      • Calculation:
        • Using a calculator, you'll find the FV to be approximately $8,103.77.
      • Interpretation:
        • By consistently depositing $100 every month for 5 years at an 8% annual interest rate (compounded monthly), your investment will reach a future value of approximately $8,103.77 at the end of the 5 years.
      • This example demonstrates how equal cash flows can grow significantly over time due to compound interest. Remember, the longer the investment period and the higher the interest rate, the greater the future value will be.
  • Series of Unequal Cashflow
    • Calculating the future value of unequal cash flows requires a slightly different approach compared to equal cash flows.
    • Example:
      • Scenario: You receive three unequal payments from an investment:
        • Year 1: $500
        • Year 3: $1,000
        • Year 5: $2,000
        • The interest rate is 10% annually compounded annually.
      • Goal: Find the future value of all these cash flows at the end of year 5.
      • Solution:
        • For unequal cash flows, we can't use the formula for annuities. Instead, we need to consider each cash flow individually and find its future value at the end of year 5.
        • Future Value of Each Cash Flow:
          • Year 1: FV of $500 at year 5 = $500 x (1 + 0.1)^4 = $500 x 1.4641 = $732.05
          • Year 3: FV of $1,000 at year 5 = $1,000 x (1 + 0.1)^2 = $1,000 x 1.21 = $1,210.00
          • Year 5: The $2,000 is received at year 5 itself, so its future value is simply $2,000.
        • Total Future Value: Now, add the future values of each individual cash flow to find the total future value at year 5.
          • Total FV = $732.05 + $1,210.00 + $2,000 = $3,942.05
      • Interpretation:
        • In this scenario, the total future value of the unequal cash flows at the end of year 5, considering the 10% annual interest, is $3,942.05.


Present Value

Lets look into formulas alone here as its similar to FV.
  • Single Cashflow
    • Formula: PV = FVn((1+r)^-n)
  • Non Annual Compounding
    • Formula: PV = FVn((1+r/m)^-mn)
  • Series of Equal Cashflow
    • Formula: PV = A[ (1 - 1/(1+r)^n) / r]
  • Series of Unequal Cashflow
    • Similar to FV but only the formula changes using the above.
  • Present value of Perpetuity
    • Formula: A/r


Credits and References

https://www.canarahsbclife.com/content/dam/choice/blog-inner/images/what-is-the-time-value-of-money.jpg
https://gemini.google.com/
https://www.investopedia.com

Thursday, 28 September 2023

Rasa Framework - Creating Chatbots

Agenda

  • Overview

  • Natural language processing - NLP

  • Chatbot

    • About

    • Rasa

    • Build Simple College Admission Chatbot

  • Best Practices

  • Conclusion


Overview

“[AI] is going to change the world more than anything in the history of mankind. More than electricity.”— AI oracle and venture capitalist Dr. Kai-Fu Lee, 2018

In today's smart phone world everyone of us have been users of the technology AIML. From the video predictions, shopping, service centres, social media, surveillance, food delivery, transportation, self driving and so on. Interestingly we are as well the producers of these data.

NLP is a discipline of AI, used to helps in understanding, interpret and manipulate human language. 

We would briefly see today what is NLP and its applications. Build a chatbot from scratch using Rasa framework.


Natural language processing
  • Natural language processing helps computers communicate with humans in their own language and scales other language-related tasks. For example, NLP makes it possible for computers to read text, hear speech, interpret it, measure sentiment and determine which parts are important.

  • Today’s machines can analyse more language-based data than humans, without fatigue and in a consistent, unbiased way. Considering the staggering amount of unstructured data that’s generated every day, from medical records to social media, automation will be critical to fully analyse text and speech data efficiently.

  • Sample Applications of NLP

    •  Sentiment Analysis

      •  customer reviews

      •  customer segmentation

      •  anomaly detection

      •  product improvement

    •  Topic Modelling

      • coming up with new topics from the text

      •  using those topics to assign new supervised learning labels

      •  insights that are too difficult to find from manual searching

    •  Text Categorisation

      • categorising animal specials

      • categorising fake news

      • categorising bank transactions


Chatbot

A chatbot is a computer program that simulates human conversation through voice commands or text chats or both. Chatbot, short for chatterbot, is an artificial intelligence (AI) feature that can be embedded and used through any major messaging applications.

Chatbots, also called chatterbots, is a form of artificial intelligence (AI) used in messaging apps.

This tool helps add convenience for customers—they are automated programs that interact with customers like a human would and cost little to nothing to engage with.

Key examples are chatbots used by businesses in Facebook messenger, or as virtual assistants, such as Amazon's Alexa.

Chatbots tend to operate in one of two ways—either via machine learning or with set guidelines.


Rasa

Rasa helps in creating virtual assistants. Used to automate human-to-computer interactions anywhere from websites to social media platforms.

Rasa supplies conversational AI infrastructure for a global community of developers, providing the tools to build chat-based and voice-based contextual assistants.

As Rasa is powered by open source software and runs in production everywhere from startups to Fortune 500s, across industries like healthcare, financial services, retail, and insurance.

Rasa Open Source provides three main functions. Together, they provide everything you need to build a virtual assistant:

  • Natural Language Understanding

    Convert raw text from user messages into structured data. Parse the user’s intent and extract important key details.

  • Dialogue Management

    Machine learning-powered dialogue management decides what the assistant should do next, based on the user’s message and context from the conversation.

  • Integrations

    Built-in integration points for over 10 messaging channels, plus endpoints to connect with databases, APIs, and other data sources.


Installation

The first step before we proceed, lets install rasa.

% python -m venv env

% source env/bin/activate

% pip install rasa 

For UI: pip install rasa-x -i https://pypi.rasa.com/simple [optional]

% rasa init # setup the basic file structure

% update config.yml and endpoint.yml [optional]

% rasa train # train your model

% rasa train nlu

% rasa train core

% rasa shell # run the bot from shell

% rasa run actions # action server

% rasa data validate # validate if the data given in the configuration is good

% rasa run -m models --enable-api --cors "*" --debug # run the rasa as rest http server

Sample UI Installation (not associated with RASA)

% mkdir ui; cd ui

% git clone https://github.com/scalableminds/chatroom.git

% cd chatroom

% yarn install

% yarn build (after any customised changes)

% yarn serve # to run the ui server

% update the index.html with the address of the rasa server running

Open the http://127.0.0.1:8080/index.html in chrome browser. (Note: Chrome is the supported browser for now)

Fig1: Initial page loaded using the chatroom module 


Sample Conversation:

Here is the sample conversation which was written from scratch using Rasa.

Fig2: Sample rasa conversation using rasa shell


Source Code:

Now that we have seen the conversation, lets see how each scenario is written step by step using Rasa framework. We will also see gradually learn the concepts of Rasa from these scenarios.

For each scenarios we would see the output in UI form.


Scenario 1:

College timings

nlu.yml

- intent: timings

examples: |

- I would like to college timings

- college time please

- what would the class start and end time

- when college reopen

stories.yml

- story: college time path

steps:

- intent: timings

- action: utter_timings

domain.yml

intents:

- timings


responses:

utter_timings:

- text: "The college is not open now we are still working through online"

Description:

  • rasa run :

    • rasa train

      • Need to run this after every changes we do the configuration files

    • rasa nlu train

      • If only the nlu is updated, such as nlu.yml, stories.yml and rules.yml

    • Once the train is completed the model will be saved under model/ directory

      • eg: below message will get

        Your Rasa model is trained and saved at '/Users/dev/Desktop/Technical/Blogs/Chatbot/rasa/models/20210925-140501.tar.gz'.

  • nlu.yml

    • NLU(Natural Language Understanding) used to store the training data and extract structured information from user messages. 

    • This usually includes the user's intent and any entities their message contains.

    • From the above example the student can know the timings of the college, so given some sample input for the bot to learn the student's intention.

  • stories.yml

    • Stories helps the bot to learn the dialogue management.

    • Stories can be used to train models that are able to generalise to unseen conversation paths.

    • From the above example, when the user's intention is to know the timing of the college the bot will respond the message back to the student, saying the “colleges are still operating online”.

  • domain.yml

    • Domain is the key file for the Rasa framework.

    • It specifies the intents, entities, slots, responses, forms, and actions your bot should know about. It also defines a configuration for conversation sessions.

    • From the above example, we have specified the intents and our responses here.

Output in UI:

Fig 3: Scenario 1 chat with bot, simple chat


Scenario 2:

Course duration for department

nlu.yml

- lookup: department

examples: |

- civil

- mechanical

- computer

- textile

- printing


- intent: course_duration

examples: |

- what is course duration for [civil]{"entity": "department"}

- would like to know the [computer]{"entity": "department"} course tenure

stories.yml

- story: college course duration

steps:

- intent: course_duration

- action: action_course_duration

domain.yml

intents:

- course_duration


actions:

- action_course_duration


entities:

- department


responses:

utter_course_duration:

- text: "For {department} course is of {duration} months"

actions.py

class ActionCourseDuration(Action):


def name(self) -> Text:

return "action_course_duration"


def run(self, dispatcher: CollectingDispatcher,

tracker: Tracker,

domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:


# It will return array of entities

entities = tracker.latest_message['entities']

print(entities)


course_duration = {

'civil': 10,

'computer': 12,

'mechanical': 14,

'printing': 16,

'textile': 18

}


entity_department = None


# Iterating through the array to retrieve the desired entity

for e in entities:

if e['entity'] == "department":

entity_department = str(e['value']).lower().strip()

duration = course_duration.get(entity_department, 0)


dispatcher.utter_message(

response="utter_course_duration",

department=entity_department,

duration=duration

)


return []

Description:

  • nlu.yml

    • Here we could see something new, it is called as entity and lookup tables.

    • Entity can be said as the information extracted from the intent.

    • Lookup tables are lists of words helps to extract entities.

    • Here we are defining list of departments/courses offered in the college, as it will help the bot to reply better based on the student's department.

  • stories.yml

    • When the user ask about the course duration

    • Reply is not plain uttering the message, rather its an action taken.

  • actions.py

    • Actions file holds the custom action that can run any code you want. This can be used to make an API call, or to query a database for example.

    • Rasa framework as beautifully decoupled actions server, to run it we need to call “rasa run actions”

    • From the example we are returning the course duration saved from the dict, but it could from anywhere.

Output in UI:

Fig 4: Scenario 2 chat with bot, using entities


Scenario 3:

Exam Results

nlu.yml

- regex: rollnumber

examples: |

- \d{10,30}


- intent: get_roll_number

examples: |

- my roll number is [1234567891](rollnumber)

- This is my roll number [1234567891](rollnumber)

- [1234567891](rollnumber)


- intent: request_result

examples: |

- may I know the exam results

- can you please help me to know if I have passed

- am I all clear

rules.yml

- rule: activate result form

steps:

- intent: request_result # intent that triggers form activation

- action: result_form # run the form

- active_loop: result_form # this form is active


- rule: submit form

condition:

- active_loop: result_form # this form must be active

steps:

- action: result_form # run the form

- active_loop: null # the form is no longer active because it has been filled

- action: utter_submit # action to take after the form is complete

- action: utter_slots_values # action to take after the form is complete

- action: action_show_result

domain.yml

intents:

- get_roll_number

- request_result


actions:

- action_show_result


forms:

result_form:

required_slots:

rollnumber:

- type: from_entity

entity: rollnumber


slots:

rollnumber:

type: any


entities:

- rollnumber


responses:

utter_result:

- text: "For {roll}, result is {result} with {score} score"


utter_ask_rollnumber:

- text: "Please provide your roll number"


utter_submit:

- text: "All done!"


utter_slots_values:

- text: "I am going to run a result search using the following parameters:\n

rollnumber: {rollnumber}"

actions.py

class ActionShowResult(Action):


def name(self) -> Text:

return "action_show_result"


def run(self, 

dispatcher: CollectingDispatcher,

tracker: Tracker,

domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:


roll = tracker.get_slot("rollnumber")

print("Rollno: ", roll)

if( roll ):

score = 98

roll = 100

else:

score = -1

roll = 0


result = "Fail"

if score >= 50:

result = "Pass"


dispatcher.utter_message(

response="utter_result",

score=score,

roll=roll,

result=result

)


return []

Description:

  • nlu.yml

    • It would be hard to configure all the possible values at times, to rescue regex is the solution.

    • We could see regex is used to identify the rollnumber

  • rules.yml

    • Rules are a type of training data used to train your assistant's dialogue management model. 

    • Rules describe short pieces of conversations that should always follow the same path.

    • Forms are used, inorder to save the student's roll number.

    • Forms one of the most common conversation patterns is to collect a few pieces of information from a user in order to do something (book a restaurant, call an API, search a database, etc.).

      Note: Don't overuse rules. Rules are great to handle small specific conversation patterns, but unlike stories, rules don't have the power to generalize to unseen conversation paths.

  • domain.yml

    • Slots are your bot's memory. 

    • They act as a key-value store which can be used to store information the user provided (e.g their home city) as well as information gathered about the outside world (e.g. the result of a database query).

    • Here we have save the student's roll number in slot and it is extracted from the entity.

Output in UI:

Fig 5: Scenario 3 chat with bot, using slots


Scenario 4:

Fees Enquiry

  • Provide if only roll number was provided

  • Else ask for the roll number and provide the fees structure


nlu.yml

- regex: rollnumber

examples: |

- \d{10,30}


- intent: get_roll_number

examples: |

- my roll number is [1234567891](rollnumber)

- This is my roll number [1234567891](rollnumber)

- [1234567891](rollnumber)


- intent: fees_enquiry

examples: |

- may I know the fees structure

- how much fees do I need to pay

- do I have any pending fees to be paid

stories.yml

- story: Ask for rollnumber and say fees

steps:

- intent: fees_enquiry

- slot_was_set:

- rollnumber_provided: null

- action: utter_ask_rollnumber

- intent: get_roll_number

- slot_was_set:

- rollnumber_provided: true

- action: action_save_roll_number

- action: action_fees_details

rules.yml

- rule: Only say `fees` if the user provided a rollnumber

condition:

- slot_was_set:

- rollnumber: true

steps:

- intent: fees_enquiry

- action: action_fees_details

domain.yml

intents:

- fees_enquiry


actions:

- action_fees_details


entities:

- department

- rollnumber


slots:

rollnumber:

type: any


responses:

utter_fees:

- text: "For {roll}, fees is {fees} INR."

actions.py

class ActionShowFeesStructure(Action):


def name(self) -> Text:

return "action_fees_details"


def run(self, 

dispatcher: CollectingDispatcher,

tracker: Tracker,

domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:


roll = tracker.get_slot("rollnumber")

print("Rollno: ", roll)

fees = 0

if( roll ):

fees = 10000


dispatcher.utter_message(

response="utter_fees",

fees=fees,

roll=roll

)


return []



class ActionReceiveRollNumber(Action):


def name(self) -> Text:

return "action_save_roll_number"


def run(self, dispatcher: CollectingDispatcher,

tracker: Tracker,

domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:


#text = tracker.latest_message['text']

entities = tracker.latest_message['entities']


roll = None

for e in entities:

if e['entity'] == "rollnumber":

roll = str(e['value']).lower().strip()


dispatcher.utter_message(text=f"I'll remember your rollnumber {roll}!")

return [SlotSet("rollnumber", roll), SlotSet("rollnumber_provided", True)]

Description:
  • All the configuration we have learnt so far would remain the same.

  • But this scenario is written to test the bot's memory (slot)

  • The student here does not require to enter the roll number again.

  • And in story we have written, if the student has not provided the rollnumber it will asked and then the feel structure information will be provided.

Output in UI:

Fig 6: Scenario 4 chat with bot, using slots and conditions


Scenario 5:

Change of department request

nlu.yml

- intent: department_have_been_changed

examples: |

- I have changed from [civil]{"entity": "department", "role": "from"} 

- Have moved from [civil]{"entity": "department", "role": "from"} 


- intent: department_going_to_change

examples: |

- I am going to [civil]{"entity": "department", "role": "to"} department

- I am changing to [civil]{"entity": "department", "role": "to"} department

- Will be moving to [civil]{"entity": "department", "role": "to"} course

stories.yml

- story: The student moving from another department

steps:

- intent: department_have_been_changed

entities:

- department: Civil

role: from

- action: utter_ask_about_experience


- story: The student is going to another department

steps:

- intent: department_going_to_change

entities:

- department: Computer

role: to

- action: utter_wish_luck

domain.yml

intents:

- department_have_been_changed

- department_going_to_change


responses:

utter_ask_about_experience:

- text: "How was your experience with the department."


utter_wish_luck:

- text: "Wish you best luck in the new department."

Description:
  • nlu.yml

    • Here we are using the feature Entity Roles and Groupswhere we need to specify the list the roles and groups of an entity can belong to.

Output in UI:

Fig 7: Scenario 5 chat with bot, using roles


Best Practices
  • Real world test data

  • Test conversation

  • Managing conversation data files modularly as it can be ease for writing and maintaining


Conclusion:

Rasa can help us with quickly build a chatbot for use case. Adding its open source and holds state-of-the-art models in building the chatbot. The scenario given here are just the basics, there is lot more Rasa provides. 


References:


Original Blog Posted in OSFY

https://www.opensourceforu.com/2022/01/using-the-rasa-framework-for-creating-chatbots/

For further research and updates maintaining the blog here.

Scarcity Brings Efficiency: Python RAM Optimization

  In today’s world, with the abundance of RAM available, we rarely think about optimizing our code. But sooner or later, we hit the limits a...