Thursday, 13 July 2023

Banks and Risk

 

Introduction

One might think what the bank has to worry as it has all the money, well running a bank is as complex as running a nation. Banks has loads of risk to manage, regulatory to follow, trust to maintain and any impact will directly effect economic and us. Let's try to understand major types of bank and risk a bank faces and how they are addressed, both by banks themselves and by bank regulators.

Banks

Banks are the cornerstone of the global financial system. Banks can be categorized by the functions they perform and the customers it serves. Below are few common category of banks:
  • Commercial Banks: Take deposits and make loans, includes the below:
    • Retail Banks: Primarily serve individuals and small businesses. 
    • Wholesale Banks: Primarily serve large businesses, corporate and institutional customers. The loans and deposits are much larger than the retail banks, as a result administrative costs per dollar of deposits are lower, leading lower rates charged on the loans.
  • Investment Banks: Investment banks are involved in the creation, underwriting, and distribution of securities. Assist in raising capital for their customers and advising in finance matters such as mergers, acquisitions and restructurings. They work with corporations, governments, and other entities to bring new securities to the market. Investment banks may also engage in trading securities for their own accounts and provide various advisory services related to securities and financial transactions and act as a broker-dealer for securities trading. 
  • Custodian Banks: Custodian banks are financial institutions that provide safekeeping and administration of financial assets, including securities, on behalf of institutional investors such as mutual funds, pension funds, and investment managers. They play a crucial role in safeguarding and settling trades, collecting income on investments, and providing various reporting services.
  • Securities Services: Securities services refer to a range of financial services related to the management and handling of securities, which are tradable financial assets such as stocks, bonds, and other investment instruments. Securities services are typically provided by financial institutions, including mentioned above banks and specialized financial service firms. Collectively, these entities and services contribute to the overall infrastructure of the securities industry, facilitating the issuance, trading, and management of securities in financial markets. Securities services are critical for maintaining the integrity and efficiency of capital markets and ensuring that investors can buy and sell financial instruments with confidence.
    • Brokerage Firms: Brokerage firms facilitate the buying and selling of securities on behalf of individual investors and institutional clients. They act as intermediaries between buyers and sellers in the securities markets. Online brokers, full-service brokers, and discount brokers are examples of different types of brokerage firms.
    • Securities Exchanges: Securities exchanges are platforms where buyers and sellers come together to trade securities. Examples include the New York Stock Exchange (NYSE) and the Nasdaq. Exchanges play a crucial role in providing a transparent and regulated marketplace for securities transactions.
    • Depositories: Depositories are institutions that hold and maintain securities in electronic form. They facilitate the transfer of securities between buyers and sellers, ensuring proper settlement and clearing processes. Central depositories, such as the Depository Trust & Clearing Corporation (DTCC), play a central role in the U.S. securities market.
    • Securities Clearing and Settlement Services: Clearinghouses and settlement services help ensure the smooth and efficient completion of securities transactions. They handle the process of clearing trades, confirming ownership, and settling transactions by transferring securities and funds between buyers and sellers.
    • Transfer Agents: Transfer agents are entities responsible for maintaining records of the ownership of securities and for handling the transfer of ownership when securities are bought or sold. They play a role in ensuring accurate and timely recording of changes in ownership.

Risks Faced By Bank

Risk is defined in financial terms as the chance that an outcome or investment's actual gains will differ from an expected outcome or return. Risk includes the possibility of losing some or all of an original investment.  Quantifiably, risk is usually assessed by considering historical behaviours and outcomes. Day to day bank faces several risks, below are few major risks faced by the banks, but these are not the only risks!

Credit Risk

  • Credit Risk refers to the possibility of the borrowers will fail to repay their debts, with loans to corporations, countries and individuals being a significant source of risk to banks.
  • This is one of the greatest risks facing a bank. Loan losses can vary significantly depending on the economic conditions at macro level, and individually at micro level.
  • Banks build expected losses into the interest rate on loans, creating a net interest margin to cover administrative costs and contribute the profits.
    • Example: If a bank's cost of funds is 2% and it can loan money out at 5%, then bank's net interest rate margin is 3%. If the bank anticipates a loss rate of 2% on its loans, then the bank will still have 1% left to cover operation costs and contributions to profits.
    • Formula: Net interest margin = Rate on loaned out money - Cost of funds
  • Simple to say, banks are designed to allocate credit and assume credit risk. A bank does not seek to eliminate credit risk, but instead seeks to profit from credit risk.
  • In order to address credit risk, regulators require banks to hold the most amount of capital. Regulations require banks to maintain enough capital to cover rare or extreme losses. Accounting rule IFRS-9 requires bank to show outstanding principal net estimated expected losses over the next 12 months on their balance sheets. 
  • Example: Default or downgrade of a major borrower, like a country or a large corporation. During COVID-19, economic uncertainties and disruptions to businesses resulted in concerns about credit risk. Companies faced challenges in meeting their debt obligations, and credit spreads widened, reflecting higher perceived credit risk in the market.

Market Risk

  • Market risk captures risk a bank faces when market factors move in an unfavourable manner, such as stock prices, interest rates, exchange rates, commodity prices and equity prices.
  • In general most of the bank's market risk refers to the potential for losses from a bank's trading activities in financial markets due to movements in market prices. In the U.S. because the proprietary trading is discouraged, most of the market risk stems from the services and products a bank will offer corporate clients and institution investors.
  • Value of market variables is emerges from the bank's by trading in financial markets and can be influenced by various other events. 
  • The risk associated with the probability that securities in a bank’s trading book will decrease in value.
  • Example:
    • In 2016, U.K.'s Brexit vote led to severe fluctuations in value of the British pound.
    • The COVID-19 pandemic had widespread and significant impacts on various types of market risk. 
  • Types of Market Risk:
    • Interest Rate Risk
      • Example: Changes in central bank interest rates, such as the Federal Reserve in the United States adjusting its benchmark interest rates.
    • Equity Price Risk
      • Example: Significant changes in stock prices due to factors like earnings reports, economic data releases, or unexpected geopolitical events.
    • Foreign Exchange Risk / Currency Risk
      • Example: Brexit-related events impacting the British pound or geopolitical tensions affecting currency values.
    • Commodity Price Risk
      • Example: Fluctuations in oil prices due to changes in global supply and demand, geopolitical events, or production disruptions. During COVID-19 oil prices for example, experienced a dramatic decline due to reduced global demand and oversupply concerns.
    • Correlation Risk
      • Correlation risk is a type of market risk that arises from the unpredictability of the relationships between different assets or markets. In financial terms, correlation measures the degree to which the prices of two or more assets move in relation to each other. Correlation risk becomes significant when the expected relationships between assets break down, particularly during times of market stress or crisis.
      • Example: During a financial crisis or a severe market downturn (e.g., a global recession like the 2008 financial crisis or the COVID-19 pandemic in 2020), correlations between various asset classes can change unexpectedly. In times of extreme market stress, investors might experience a "correlation breakdown," where previously uncorrelated or negatively correlated assets start moving in the same direction.
    • Liquidity Risk
      • Example: Sudden illiquidity in a market or asset class, making it challenging for investors to buy or sell securities at desired prices. During COVID-19 investors faced challenges in buying or selling assets, and there were instances of illiquidity in certain markets.
    • Volatility Risk
      • Example: Sharp increases in market volatility, such as during periods of market uncertainty or financial crises. The cryptocurrency market experienced significant volatility, with digital currencies like Bitcoin and Ethereum reaching record highs but also facing sharp declines, raising questions about their role as an asset class.
    • Investment Risk
      • Investment risk can be defined as the probability or likelihood of occurrence of losses relative to the expected return on any particular investment.
 
Operational Risk

  • Operational Risk is defined as the risk of loss resulting from inadequate or failed internal processes, people and systems or from external events.
  • This is also considered one of the biggest risks facing the banks. As generally operational risk comes from low probability and high consequence events. Like in credit risk, regulators use a one-year time horizon to consider losses from operational risk.
  • Categories:
    • Internal fraud: e.g., rogue trading
    • External fraud: e.g., cyberattacks, ddos attacks
    • Employment practices and work place safety: e.g., employee discrimination claims
    • Clients, products and business practice: e.g., money laundering
    • Damage to physical assets: e.g., flooding, terrorism
    • Business disruption and system failures: e.g., software or hardware failures
    • Execution, delivery and process management: e.g., data entry errors
  • Example: During COVID-19, the pandemic forced many businesses to adapt to remote work and digital operations. This shift introduced new operational risks, including cybersecurity threats, technology challenges, and disruptions to traditional business processes.
  • Significant sources of operational risk in banking includes cyber risk, legal risk and compliance risk.


Economic Capital vs. Regulatory Capital

Capital
  • For a bank, capital is a critical component of its financial structure, representing the funds that the bank uses to operate its business and absorb potential losses. 
  • Sufficient capital is important for banks for maintaining financial stability, meeting regulatory requirements, and providing a buffer against unexpected events or risks. 
  • Equity capital: The most important capital is equity capital. Equity capital is needed to shield against possible losses and to maintain solvency. Equity capital can be thought of as going concern since it is meant to cover losses when the bank continuous to operate as a business.
  • Debt capital: Debt capital is an another main category of capital. Debt capital may issue by banks for long-term debt to bolster their capital. In contrast of equity capital, debt capital can be thought as gone concern capital since it is meant to cover losses only once the bank ceases to operate as a business.
  • Banks and their regulators may have different views about how much capital is sufficient in light of the risks a bank face and they are commonly classified into two main categories: regulatory capital and economic capital.
Regulatory Capital
  • Externally imposed, required by regulators to ensure solvency.
  • Targets the Expected Loss.
  • Keeping the total capital of a bank adequately high.
  • Based on standardized rules and calculations.
  • Aims to protect depositors and financial system.
  • Current regulations require banks to maintain enough capital to cover losses that are estimated to occur only once every thousand years.
  • In the United States, for example, the Basel III framework outlines global regulatory standards for bank capital adequacy.
Economic Capital
  • Own models - business perspective, internal measure of capital needed to cover risks.
  • Requires to be additional to regulatory capital.
  • Targets the Unexpected Loss.
  • Based on bank specific model and confidence levels.
  • Aims to optimize capital allocation and returns.
  • The form of economic capital to be held by the banks is decided by regulators though.

Basel Committee Regulations

  • Basel Committee on Banking Supervision was established in 1974, by central banks and supervisory authorities.
  • Basel regulations started with capital requirements for credit risk only and evolved to include market and operation risk.
  • Initially both standarized and internal models developed by banks were used to work out capital requirements.
  • As of now all the three risks - credit, market and operational must be computed using standarized model. However, if a bank is approved by its national regulator, then it may use an internal model for market and credit risks only.
  • After 2007-2009 credit crisis, the Basel Committee allowed less use of internal models and introduced two liquidity ration requirements:
    • Liquidity Coverage Ratio - LCR
      • Ensures bank has enough funding to remain viable for 30days
    • Net Stable Funding Ration - NSFR
      • Controls the maturity mismatches between a bank's assets and liabilities
  • Basel Committee Motivations
    • Financial Stability
      • Ensure banks have sufficient capital to absorb losses promotes financial stability.
    • Risk Management
      • Regulations encourage effective risk management practices and a better understanding of risks.
    • Level Playing Field
      • Common capital requirements create a level playing field for banks operating in different countries.
    • International Cooperation
      • Regulations encourage collaboration and convergence of regulatory standards among global regulators.
    • Public Confidence
      • Promoting transparency and accountability increases public confidence in the banking system.

Deposit Insurance and Moral Hazard

  • To increase public confidence in the banking system and prevent runs on banks, most countries have established systems of deposit insurance.
  • Financial product or Deposit Insurance that protects depositors from losses due to bank failures.
  • Banks pay premiums to the deposit insurance fund based on the amount of deposits they hold.
  • Insurance funds uses these premium to cover losses in the event of a bank failure.
  • But due to this insurance lays moral hazard, like banks may take on risker investments and loans, because they know that depositors are protected by the insurance.
  • In simple words, the existence of deposit insurance allowed banks to follow risky strategies that would not otherwise be feasible.
  • And this also due to deposit insurance may encourage depositors to be less vigilant in monitoring the bank's activities.
  • This can lead to a situation where the bank takes on excessive risk, and if it fails the deposit insurance fund or taxpayers bear the losses.
  • The introduction of risk based deposit insurance premiums has reduced moral hazard to some extent. For example, in recent years poorly capitalized banks have been required to pay higher deposit insurance premiums that well capitalized banks.
  • The moral hazard is also lessened by regulations that ensure that a bank's required capital increases with the risks it takes.

Investment Banking

Investment banks raise money for corporations or governments, in the form of debt, equity or hybrid instruments such as convertible debt. This includes activities such as:
  • Originating securities
  • Underwriting securities
  • Placing the securities with investors

Financing Arrangements

  • A major activity of a bank's investment banking arm is raising capital for companies in the form of debt, equity or more complicated securities, this process is referred to underwriting.
  • Typically a company will approach the investment bank to discuss its plan to issue securities.
  • Once the plans have been agreed upon, the securities are originated along with the documentation itemizing the rights of investors who purchase the securities.
  • When an investment bank arranges a securities issuance for a customer, it may try to place the entire issue with particular buyer or group of buyers or sell the issue in the public market.
  • To sell securities with help of prospectuses, prospectuses provide information on the company's past performance, future prospects and the major risks (such as lawsuits) faced by the company.
  • Private Placement
    • The investment bank sells securities of the company for raising capital to a small number of large private institutional investors for a fee.
    • Raises capital quickly without the costs and regulatory requirements associated with a public offering.
  • Public Offering
    • The securities of a corporation are offered to the general public through the investment bank.
    • Company typically hires investment banks to underwrite the offerings and help set the offering price based on the company's financial performance and market conditions.
    • Two Ways of Public Offering Arrangements
      • Best Efforts Public Offering
        • Underwriter agrees to use its best efforts to sell the shares to the public but does not guarantee that all the shares will be sold.
        • Underwriter usually charges a fixed fee for each share sold. Mostly based on the historic success of this activity, the fee is determined.
        • Company bears more risk in a best effort offering, as it may not raise the full amount of capital it seeks.
        • Typically used for smaller or risker offerings.
      • Firm Commitment Public Offering
        • Underwriter agrees to purchase all of the shares being offered by the company and then resell them to the public at a higher price.
        • The difference between the price at which it sells the securities and the price it pays the underwriter gets its profit.
        • In case the underwriter bank is unable to sell the securities of the issuer, they own it themselves.
        • Underwriter bears more risk as it may have to hold on to unsold shares or sell them to a loss.
        • Typically used for larger or more established companies.
    • Types of Public Offering
      • Initial Public Offering
        • When the company wishing to issue shares is not publicly traded, the share issue is known as an initial public offering (IPO).
        • Prior to an IPO, shares are typically held by the company's founders, venture capitalists and other who have provided early stage funding.
        • The shares being sold could be the mixture of existing and new shares, which can provide additional capital for the company.
        • Sometimes the founders retain control by arranging for the shares they keep in the company to have better voting rights that other shares.
        • Here the first time a company offers its shares to the public, since the shares are not yet traded, it is a challenging to determine a reasonable post-IPO share price.
        • Usually companies and investment bankers will attempt to set the offering price just below what it believes the market price, will be, thus increasing the likelihood of being able to sell the shares.
        • Company hires an investment bank or group of banks to underwrite the offering by analyzing the value of the issuer, set the offering price and sell the shares to the public.
        • Price is estimated as the value of the firm post-IPO divided by the total number of shares post-IPO.
        • After the IPO, the company becomes publicly traded, with its shares traded on a stock exchange such as the New York Stock Exchange (NYSE).
      • Dutch Auction Approaches
        • An IPO price may also be discovered through a Dutch auction process.
        • Issuer sets a range of prices for the securities being offered.
        • A prospectus is issued and there is road show just like any other regular IPO.
        • Potential buyer submit bids specifying the number of securities they are willing to buy at each price level within the range.
        • Shares are offered to investors in the order of the highest to the lowest bid until all of the shares are sold.
        • Auctioneer set the price at which all the offered securities will be sold based on the bids submitted.
        • In general the price paid by all successful bidders is the lowest bid that leads to a share allocation.
        • All the buyers who bid at or above the clearing price, will pay the same price per security regardless of their bid price.
        • Example:
          • Consider a company which wants to sell one million shares in an IPO through the Dutch auction approach.
          • Bidder

            | Number of shares

            | Price

            A

            100,000

            $30.00

            B

            200,000

            $28.00

            C

            50,000

            $33.00

            D

            300,000

            $29.00

            E

            150,000

            $30.50

            F

            300,000

            $31.50

            G

            400,000

            $25.00

            H

            200,000

            $30.25

          • From the table, based on the bid price as seen from the highest to the lowest, shares are allocated to C, F, E, H, and A, in that order.
          • The price paid by others investors like C, F, E and H is equal to the price bid by the lowest bidder A ($30.00), among whom the shares are allocated.
        • Advantages are here the price that clears the market (30$ in the above example) becomes the market price in the bidding process. Also because it is an auction process, any potential investor and not only the favored clients of investment banker can obtain the shares.
      • Secondary Public Offering
        • If the company is already publicly traded and additional equity financing has to be raised, then as a guide to the issue price, the investment bank looks at the prices at which the company's shares are trading a few days before the issue is to be sold. 
        • New shares will be issued at a target price slightly below the current price. 
        • This leads to a possibility that the price of the company's shares will show a decline before the new shares are sold.

Advisory Services

  • In addition to handling securities issuances, investment banks also offer advice to corporations on decisions involving mergers and acquisitions, divestments (divestment is the sale of an existing business or an asset class that doesn't perform or meet the expectations of the company or a country) and restructurings.
  • The exchange during M&A can consider any of the below type of offer that should be made:
    • Cash offer
    • Share for share exchange
    • Combination of a cash offer and share for share exchange
  • The initial offer is not final offer, and the investment banks must use its experience to develop a reasonable plan for the price negotiations.
  • The companies targeted by takeover attempts are also advised by investment bankers. Sometimes a company often with the advice of an investment banks, will take steps to avoid being taken over. These are known as poison pills.
  • Examples of poison pills are:
    • Granting key employees attractive stock options that can be exercised in the event of takeover - this could discourage a potential acquirer from proceeding because the key employees will almost certainly leave.
    • Adding a provision to the company's charter making it impossible for a new owner to fire the existing directors for a period of time.
    • Issuing preferred shares that automatically get converted to regular shares in the event of a takeover.
    • Allowing existing shareholders to purchase shares at a discount in the event of a takeover.
    • Changing the voting structure so that management has sufficient votes to block a takeover.
    • Adding a provision that allows remaining shareholders to sell shares to the new owner at a 50% premium in the event of a successful takeover.
  • Poison pills are illegal in many countries, but they are permitted in the U.S, however majority of shareholders approvals are required.

Potential Conflicts of Interest

  • If a bank or a bank holding company provides commercial banking, investment banking and securities services, then several conflicts of interest may arise.
  • Investment Banking vs. Investors
    • While selling advisory services to investors, a bank may promote securities that its investment banking division is trying to sell. 
    • The bank's advisory services division may be incentivized to recommend securities from the investment banking division, even if they may not be the best fit for the investors, potentially leading to biased advice.
  • Investment Banking vs. Brokers
    • Assume investment bankers are handling a new issue for a client, but the issue has been received poorly by investors.
    • To sell more shares and earn a larger commission, the investment banker may turn to the bank's broker and ask them to recommend the securities to their clients.
    • Creates a conflict of interest because the investment banker is pressuring the brokers to recommend securities they may not have recommended otherwise.
  • Investment Banking vs. Clients or Beneficiaries:
    • When a bank has the fiduciary responsibility, the bank can allocate the difficult-to-sell securities into this account.
    • Fiduciary responsibility refers to the legal and ethical obligation to act in the best interests and has a duty to manage and safeguard the bank's clients or beneficiaries. 
    • Difficult-to-sell securities typically refer to financial instruments that may have limited liquidity in the market, making them challenging to sell quickly without affecting their market value.
    • The idea is that by isolating these securities, the bank may be able to address their unique characteristics without negatively impacting the overall portfolio or the interests of the clients or beneficiaries.
  • Investment Banking vs. Commercial Banking
    • A commercial banking or investment banking division, may acquire nonpublic confidential information about the company when negotiating a loan or arranging a securities issueance may be misused. 
    • This is done by sharing information with its mergers and acquisitions arm to render advice to another one of its clients on potential takeover opportunities. 
    • The potential conflict of interest arises from the fact that the bank is leveraging the confidential information it gained through its lending relationship to benefit its M&A advisory services for another client. 
  • Investment Banking vs. Securities Services
    • The research side of the securities division might be enticed to rate a company's share as a “buy” to please the company's management and get more investment banking business.
    • Research analysts are expected to provide objective and unbiased assessments of securities to aid investors in making informed decisions. 
    • If the research side is influenced by the desire to attract more business from the company's management, it may compromise the integrity of the research and harm the interests of investors who rely on unbiased information.
  • Possible Solutions to Conflict of Interest
    • Separation of banking activities: Also known as Chinese walls. Involves separating and preventing the exchange of information among commercial banking, securities services and investment banking divisions. Glass-Steagall Act of 1933 in the U.S. states the investment banks were not allowed to take deposits and make commercial loans.
    • Strict regulation: Ensures banks operates at the best interests of their client rather their own.
    • Disclosure requirements or Transparency: It's important for financial institutions to manage and disclose any potential conflicts of interest to clients and regulators so that investors are fully informed when making informed decisions based on the advice provided by the bank.
    • Whistleblow protections: Protections can be implemented to encourage whistleblowers to report any suspicions without any fear.
    • Ethical guidelines and training: Banks can adopt ethical guidelines and provide training to employees to help them recognize and avoid/report conflicts of interest.

Banking Book vs. Trading Book

  • When calculating regulatory capital, it is important to distinguish between the trading book and the banking book.
  • Trading Book
    • Trading book includes all the assets and liabilities the bank has as a result of its trading operations; i.e., instruments the bank intends to trade.
    • The values of these assets and liabilities are marked to market daily. The value of the book is adjusted daily to reflect changes in market prices.
    • Marked to Market
      • "Marked to market" is an accounting practice that involves adjusting the value of an asset or liability to reflect its current market value. This adjustment is made regularly to ensure that the recorded value in the financial statements accurately reflects the asset or liability's fair market value at the current moment.
    • The regulatory capital is maintained based on the emphasis of market risk.
    • The holding period is short term and generally lower liquidity risk.
    • Examples: Derivatives, FX
  • Banking Book
    • Banking book includes loans made to corporations and individuals.
    • When a borrower makes principal and interest payments of a loan on time, the loan is recorded in the bank's books at the principal amount owed plus accrued interest.
    • If payments due are more than 90 days past due, the loan is classified as a non-performing loan. The bank does not then accrue interest on the loan when calculating its profit.
    • When it becomes likely that principal of the loan will not be repaid, the loan is as classified a loan loss.
    • Present value:
      • "Present value" is a financial concept that refers to the current value of future cash inflows or outflows, taking into account the time value of money.
      • Present value calculations play a vital role in various aspects of banking, helping banks make informed decisions about the valuation, profitability, and risk associated with their assets and liabilities in the banking book.
    • The regulatory capital is maintained based on the emphasis of credit risk.
    • The holding period is long term and generally higher liquidity risk.
    • Examples: Loans, Mortgages, Treasury Bills

Originate To Distribute Model

  • In the originate-to-distribute (OTD) model, the bank originates the loans but does not keep the loans in its books. Instead portfolios of loans are packaged into tranches which are then sold to investors.
  • Three government sponsored entities exist in the United States to facilitate this originate-to-distribute model for mortgages.
    • Government National Mortgage Association (GNMA) or "Ginnie Mae"
    • Federal National Mortgage Association (FNMA) or "Fannie Mae"
    • Federal Home Loan Mortgage Corporation (FHLMC) or "Freddie Mac"
  • OTD Model
    • Loan origination
      • Banks assess borrowers' credit worthiness and lend money by creating loans, such as mortgages or other types of consumer credit.
    • Securitization
      • Loans are pooled together and transformed into securities, such as mortgage-backed securities. These securities are divided into tranches based on their level of risks.
    • Distribution
      • Banks sell the securities to investors, transferring the credit risk from the originating bank to the investors. In return, investors receive periodic payments generated by the underlying loans.
    • Fee based income
      • Instead of relying on interest income from holding loans on their balance sheets, banks earn revenue through origination, servicing and underwriting fees.
  • In addition to the residential mortgage market, this model has been applied in the other areas such as student loans, credit card balances, commercial loans and mortgages.
  • Benefits
    • Increased Liquidity, by securitizing loans, banks can keep them off their balance sheet, which frees up funds to enable it to make more loans and support economic growth.
    • Wider access of capital, securitization frees up capital that can be used to cover risks being taken elsewhere in the bank. For instance, by distributing risk, the model can make credit more widely available to borrowers, including those with lower credit scores.
    • Yield profits, a bank earns a fee for originating a loan and a further fee if it services the loan after it has been sold.
    • Risk diversification, spreads credit risk among multiple investors, reducing the potential impact of single institution.
    • Investor opportunities, provides investors with various investment options based on their risk appetite and return expectations.
  • Drawbacks
    • Lack of incentive for quality underwriting, the originating bank may not hold onto the loans it originates. As the focus now shifts to quantity to quality, may lead risk.
    • Transfer of risk, while the transfer of risk in the OTD model provides advantages such as risk diversification and improved liquidity, it also poses challenges related to moral hazard, adverse selection, and potential reduced diligence. For instance, when the poor loans are packaged and sold, it transfers the prepayment risk from the bank to the investors, eventually leading to moral hazard impacting the brand value as well.
    • Market dynamics and liquidity risk, depending on market conditions, the ability to sell loans to investors may be constrained. In illiquid markets or during financial downturns, banks relying heavily on the OTD model may face challenges in offloading loans, leading to liquidity risk.
    • It has led banks to loosen lending standards, and this was one of the factors that led to the credit crisis in the United States from 2007 to 2009.

Credits and References

  • https://bsmedia.business-standard.com/_media/bs/img/article/2023-03/27/full/1679923530-8258.jpg
  • https://www.youtube.com/watch?v=G0GHQ1NHnII&list=PLIYnk9FMYcktiidMmEwazO4LChWNvkPfM
  • GARP, Schweser and Bionic Turtle Notes
  • https://www.investopedia.com/terms/r/risk.asp

Thursday, 22 June 2023

IoT with Python


What is IoT 

IoT - Internet of Things, we would encounter this technology on a daily basis - automatic AC or lights switch on and off based on presence, adjust the fan speed or AC temperature based on the climate outside, self-driving vehicle, surveillance and security, smart electricity meter produces monthly bills directly send to email, manufacture production lines, in agriculture while harvesting, real time monitoring of our health such calories, steps and so on where the tech is connected with internet. Kevin Ashton created the term "Internet of Things" in 1999.


IoT is defined as a network of electronic devices with sensors, actuators or accessories having processing ability using software, which can connect, control and exchange data with other systems over internet or other communication networks in real time making improved decision making.

Why Python

Python has wide usage in the field of IoT because of its simplicity and versatility. Let's highlight some of the key reasons here.
  • Easy to learn and use, as the syntax is simple like the English language. Also there is a lighter version of Python called MicroPython making great fit for small computing resources. MicroPython is a lean and efficient Python 3 programming language implementation that includes a small part of the Python standard library and is optimised to work on microcontrollers and in limited contexts.
  • Cross platform compatibility, can run on multiple OS such as Linux, Windows, MacOS or Raspberry Pi without worrying about compatibility and mostly python is preinstalled.
  • Existing large community, has many users contributing to the python community building several tools and support for further advancements.
  • Interoperability, can be easily integrated with other languages and protocols such as MQTT, HTTP, BLE and so on.
  • Scalability, the modular architecture can be used for both large or small scale IoT applications.
  • Extensive library support, several tools available for machine learning, deep learning, data intensive application, data analysis, data inference, visualization and also the lighter version of them has been the key reason to use Python in IoT. Having a huge set of development libraries, tools and frameworks making development faster and compatible with IoT.
  • Opensource, zero cost involved as python is opensource framework that is easily available to download.

IoT with Python

Python has various modules that can be utilised for IoT programming. Let us look at 30 modules that aid in the development of IOT at various stages.
  • IoT devices
    • Arduino and Raspberry Pi are the two most popular controller boards available for use in among the various hardware projects.
    • Raspberry Pi
      • MicroPython is a full Python 3 implementation that works directly on embedded hardware such as the Raspberry Pi Pico. 
      • You get an interactive prompt (the REPL) and a built-in filesystem, as well as the ability to run commands directly through USB Serial. 
      • MicroPython's Pico port offers modules for interacting with low-level chip-specific hardware.
      • Very good documentation with all the hand holding steps are provided here : https://www.raspberrypi.com/documentation/microcontrollers/micropython.html
    • Arduino
      • Arduino is an open-source platform made up of hardware and software that enables the quick production of interactive electronics projects. 

      • Arduino employs its own programming language, akin to C++, however, for applications that require integration with sensors and other physical devices, Arduino can be used with Python. 

      • Firmata protocol is the most standard way to control Arduino from PC among the other various options. This protocol facilitates serial communication with microcontrollers from software on a host computer, allowing it to receive digital and analogue inputs and transmit data to digital and analogue outputs.

      • Real python has brilliant material to get started, visit this https://realpython.com/arduino-python/

  • IoT system and sensors interaction
    • PySensors : pip install PySensors
      • lm_sensors (Linux monitoring sensors) is a free and open-source application that provides temperature, voltage, and fan monitoring tools and drivers.
      • The PySensors Python module is used for ctypes bindings for libsensors.so from the lm-sensors project. 
      • Reference: https://pypi.org/project/PySensors/
    • esptool : pip install esptool
      • esptool is a Python-based, open source, platform-independent software for communicating with the Espressif SoC's ROM bootloader.
      • The toolbox for working with Espressif chips includes esptool.py, espefuse.py, and espsecure.py. 
      • They can, for example, accomplish the following:
        • Binary data saved on flash can be read, written, erased, and verified.
        • Read chip characteristics and other data relating to the chip, such as the MAC address or flash chip ID.
        • The one-time-programmable efuses can be read and written.
        • Make binary executable images that are ready for flashing.
        • Binary images can be analysed, assembled, and merged.
      • Reference: https://pypi.org/project/esptool/
    • pyusb : pip install pyusb
      • pyusb makes it simple to communicate with USB devices in Python. 

      • It should function without any additional code in any Python >= 3.6 environment with ctypes and a pre-built USB backend library.

      • Reference: https://pypi.org/project/pyusb/

    • pyserial : pip install pyserial
      • pyserial is a Python library is responsible for serial port access. 

      • It provides the same class-based interface for Python running on Windows, OSX, Linux, BSD (potentially any POSIX compliant system), and IronPython on all supported platforms.

      • This allows it to communicate serially with devices like voltmeters, oscilloscopes, strain gauges, flow metres, actuators, and lights.

      • Reference: https://pypi.org/project/pyserial/

    • pybluez : pip install pybluez2
      • pybluez, is bluetooth Python extension module that allows Python developers to access Bluetooth resources on the system. 
      • PyBluez is compatible with GNU/Linux, macOS, and Windows.
      • Reference: https://pypi.org/project/pybluez2/
    • gpiozero: pip install gpiozero
      • gpio provides a straightforward interface to GPIO devices on the Raspberry Pi.
      • Component interfaces are provided to make getting started with physical computing as simple as possible. 
      • One may rapidly connect your components together with very little code.
      • Reference: https://pypi.org/project/gpiozero/
    • mraa
      • mraa is a Python-compatible structural GPIO library. 
      • Eclipse Mraa (Libmraa) is a C/C++ library with Java, Python, and JavaScript bindings for interacting with I/O pins and buses on various IoT and Edge systems, providing an organised and reasonable API where port names/numbering match the board that you are on. 
      • The use of libmraa does not bind you to any particular hardware. Because board detection is done at runtime, you can write portable code that works on all supported platforms. This most appealing aspect of this is that there is only one library for each individual device. 
      • The goal is to make it easy for developers and sensor manufacturers to map their sensors and actuators on top of compatible hardware and to allow high level languages and structures to govern low level communication protocols.
      • Reference: https://github.com/eclipse/mraa/tree/master/examples/python
  • IoT communication protocols
    • sockets : pip install sockets
      • TCP/IP and UDP are communication transport layer protocols.
      • sockets package facilitates TCP/IP and UDP communication, which are used to enable networking in IoT devices.
      • Reference: https://pypi.org/project/sockets/
    • paho-mqtt : pip install paho-mqtt
      • The MQTT - Message Queue Telemetry Transport protocol is a publish/subscribe messaging transport that is intended for machine-to-machine (M2M)/Internet of Things connectivity enabling high speed data exchange with low payload.
      • When a minimal code footprint is necessary or bandwidth on the network is at a premium, it is beneficial for communications with faraway locations.
      • Applications can connect to a MQTT broker to publish messages, subscribe to topics, and receive published messages thanks to the client class provided by the Poho library code. 
      • Additionally, it offers certain assistance functions to make it simple to publish one-time messages to MQTT servers.
      • Reference: https://pypi.org/project/paho-mqtt/
    • smtplib
      • Protocol to handle sending and routing emails between mail servers.
      • It is part of python's standard library.
      • Reference: https://docs.python.org/3/library/smtplib.html
    • asyncio : pip install asyncio
      • The asyncio module provides architecture for building single-threaded concurrent programming with coroutines, multiplexing I/O access through sockets and other resources, running network clients and servers, and other primitives.
      • Using this will enhance the performance of the IoT applications.
      • Reference: https://pypi.org/project/asyncio/
  • IoT database
    • MySQLdb : pip install MySQL-python
      • mysqldb is a popular go-to relational database that aids in the building of remote storage for IoT systems.
      • Reference: https://pypi.org/project/MySQL-python/
    • SQLite
      • SQLite is a C library that provides a lightweight disk-based database that does not require a separate server process and may be accessed using a nonstandard variation of the SQL query language. 
      • SQLite can be used by IoT to store internal data. 
      • The sqlite3 module installation is not required, as since Python 2.5, it has been included in the standard library.
      • Reference: https://docs.python.org/3/library/sqlite3.html
  • IoT graphical user interface
    • TKinter
      • Python provides numerous GUI (Graphical User Interface) development choices. Tkinter is the most commonly used of all the GUI techniques. 
      • It is a standard Python interface to the Python-supplied Tk GUI toolkit. 
      • Python with tkinter is the quickest and most straightforward approach to construct GUI apps.
      • Reference: https://docs.python.org/3/library/tkinter.html
  • IoT backend
    • flask : pip install Flask
      • flask is a simple micro framework for building web applications, to build http requests.
      • IoT applications require a web-based interface in order to provide monitoring and controlling devices.
      • Reference: https://pypi.org/project/Flask/
    • websockets : pip install websockets
      • websockets is a Python package for creating WebSocket servers and clients that prioritises consistency, simplicity, resilience, and performance.
      • The WebSocket API is a cutting-edge technology that allows a two-way interactive communication session to be established between a user's browser and a server. This API to send messages to a server and receive event-driven responses without polling the server for a response.
      • Reference: https://pypi.org/project/websockets/
    • requests : pip install requests
      • requests module allows to send HTTP requests, the HTTP request generates a Response Object that contains all of the response data (content, encoding, status, and so on).
      • Supports multipart file uploads and streaming downloads, connection pooling and keep-alive.

      • Automatic decoding and decompression of content.

      • Reference: https://pypi.org/project/requests/

    • aiohttp : pip install aiohttp
      • aiohttp, provides asynchronous HTTP client or server framework, including websockets support.

      • Reference: https://pypi.org/project/aiohttp/

    • pushsafer : pip install python-pushsafer
      • pushsafer can send and receive push notifications in real-time to iOS, Android, and Windows devices (mobile and desktop), as well as browsers such as Chrome, Firefox, Opera, and others.
      • Reference : https://pypi.org/project/python-pushsafer/
  • IoT data analysis and visualization
    • numpy : pip install numpy
      • numpy, package enables an power N-dimensional array computing.
      • Its features are utilised in IoT to read sensor bulk data from the system's database inherent functions.
      • Reference: https://pypi.org/project/numpy/
    • pandas : pip install pandas
      • pandas is a Python module that provides quick, versatile, and expressive data structures that are intended to make working with "relational" or "labelled" data simple and natural. 
      • It aspires to be the basic high-level building block for doing realistic, real-world data analysis in Python which is very much suitable for IoT applications.
      • Reference: https://pypi.org/project/pandas/
    • matplotlib : pip install matplotlib
      • Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.
      • Reference: https://pypi.org/project/matplotlib/
    • JSON 
      • JSON is a data storage and exchange syntax.
      • JSON is text that has been written in JavaScript object notation.
      • Python includes a library named json that may be used to work with JSON data.
      • Reference: https://docs.python.org/3/library/json.html
  • IoT machine learning and deep learning (AI)
    • tensorflow : pip install tensorflow
      • TensorFlow is a high-performance numerical computing open source software package.
      • Its adaptable architecture enables simple computing deployment over a wide range of platforms (CPUs, GPUs, TPUs), from PCs to clusters of servers to mobile and edge devices for handling non-linear datasets.
      • Reference: https://pypi.org/project/tensorflow/
    • opencv : pip install opencv-python
      • OpenCV is a large open-source library for computer vision, machine learning, and image processing, and it currently plays an essential part in real-time operation. 
      • Reference: https://pypi.org/project/opencv-python/
    • pycaret : pip install pycaret
      • PyCaret - An open source, low-code machine learning library in Python.
      • When compared to other open-source machine learning libraries, PyCaret is a low-code library that may be used to replace hundreds of lines of code with only a few lines. Experiments become significantly faster and more efficient as a result, making it apt use for IoT apps.
      • Reference: https://pypi.org/project/pycaret/
    • lightgbm : pip install lightgbm
      • LightGBM is an abbreviation for Light Gradient Boosting Machine, which provides plenty of memory-efficient yet quick computational power and is capable of handling massive volumes of data.

      • Reference: https://pypi.org/project/lightgbm/

  • IoT cloud integration
    • azure-cli : pip install azure-cli
      • azure-cli provides python tools for azure
      • Reference: https://pypi.org/project/azure-cli/
    • aws
      • Client devices can interface with AWS IoT and AWS IoT Greengrass core devices using the Python programming language by using the AWS IoT Device SDK for Python.
      • Reference: https://docs.aws.amazon.com/greengrass/v1/developerguide/IoT-SDK.html

Challenges of using Python for IoT

Python has many advantages, but no programming language is flawless.

  • Python is slower than other programming languages because it is dynamically typed and run line by line.
  • Python's key limitations are its performance and speed, memory management, and concurrency and parallelism support. When compared to other languages, the runtime speed is poor. Due to the amount of memory Python needs, projects with many objects active in RAM may encounter challenges when using Python.
  • While Python is a good server-side language, it is rarely used on the client-side as working with mobile applications and apps is slow and inconvenient.

Conclusion

IoT is evolving, and in order to fulfill the demands of the technology's users, it is necessary to examine which tool can match those demands. Several programming languages have shown to be effective in IoT development. As previously said, IoT using Python has shown to be a useful tool for prototype, development, and operation of various IoT devices and systems. Python's speed of development, minimal learning curve, and vast library set make it indispensable for IoT.


Credits and References:

https://www.datamation.com/wp-content/uploads/2021/04/IOT-4.png
https://www.deepseadev.com/en/blog/iot-with-python-reasons-to-use-it/
https://edu.varistor.in/python-in-iot/
https://www.oodlestechnologies.com/blogs/role-of-python-in-iot-development/
https://svitla.com/blog/internet-of-things-with-python

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...