Friday, 22 January 2016

CFA Level 1 - Part 3 Financial Statement Analysis

Financial Statement
Balance sheet: statement of financial position

Income statement: P&L statement

Other comprehensive income:
Other comprehensive income is the difference between net income as in the Income Statement and comprehensive income, and represents the certain gains and losses of the enterprise not recognized in the P&L Account. It is particularly valuable for understanding ongoing changes in the fair value of a company's assets. Items that are other comprehensive income:

  • Available-for-sale securities fair value changes that were previously written down as impaired
  • Available-for-sale securities unrealized gains and losses
  • Foreign currency transaction gains and losses that are hedges of an investment in a foreign entity
  • Pension or post-retirement benefit plan gains or losses
Other comprehensive income + net income = comprehensive income

Statement of changes in equity:
Statement of changes in equity, often referred to as Statement of Retained Earnings in U.S. GAAP, details the change in owners' equity over an accounting period by presenting the movement in reserves comprising the shareholders' equity.

Cash flow statement
Financial notes
Management discussion and Analysis
Auditor Report

retained earning = opening re + net income - dividend

WC = CA - CL

Accruals
Unearned(deferred) revenue: company receives cash before earning the revenue, it is a liability
Unbilled(accrued) revenue: company earns revenue before receive the cash, it is an asset
Prepaid expense: company pays cash payment before recognizing expense, it is asset

Accrued expense: company incurs expense but not yet paid, as of end of accounting period, it is liability.

Revenue Recognition
Long term contract
  IFRS:
   percentage of completion on prorated basis:
                      spent cost
   revenue = --------------- * sales price
                      total cost

  installment method:
                         profit
   revenue = --------------- * downpayment
                      selling price

if outcome of the contract is not sure
  US GAAP: completed contract method. only at contract completion
  IFRS: cost recovery. revenue = cost spent

Earnings per share:
                                       NI - preferred dividend
  Basic EPS = ---------------------------------------------------------
                       weighted avg number of shares outstanding

                                                     NI
 Diluted EPS = ----------------------------------------------------------------------------------------------
(preferred          weighted avg number of shares outstanding +  converted common shares
shares)

                                                   NI - preferred dividend
 Diluted EPS = -------------------------------------------------------------------------------------------------            
 (stock                weighted avg number of shares outstanding +  (new share issued at option
 option)             exercise  - share purchase with cash received upon exercise)*year proportion

                                          NI + after tax interest - preferred dividend
Diluted EPS = ------------------------------------------------------------------------------------------
(convertible      weighted avg number of shares outstanding +  converted common shares
bond)

Financial Assets:

available for sale, measured at fair value
  unrealised gains/losses recognised in other comprehensive income
held for trading, acquired for purpose of selling
    measured at fair value, unrealised gains/losses are recognised as profit/loss on the income statement

Liquidity ratio (short term)
                            current asset
  current ratio = ---------------------
                            current liabilities
                        cash + AR + marketable securities
  quick ratio = ---------------------------------------------
                            current liabilities
                           cash  + marketable securities
  cash ratio = ----------------------------------------------
                            current liabilities

Solvency ratio (long term)
                                debt
  debt to equity = ------------
                               equity

                                     total assets
  financial leverage = ----------------
                                     total equity

                                    debt
  debt to capital = ------------------
                                 debt +  equity

Cash flow statements
In IFRS:
  interest paid    operating/financing
  interest recd    operating/investing
  dividend paid   operating/financing
  dividend recd   operating/investing

Financial Analyis
  total asset turnover = revenue / avg total assets

  ROA =  NI / avg total assets
                  NI           revenue                  avg total assets
  ROE = ----------- * ------------------- *  -----------------------------
              revenue     avg total assets      avg shareholder equity

Inventory
 LIFO is not allowed in IFRS.
  Transportation cost incurred to ship inv. to customers are an expense, and may not be capitalised in inv. Inventory cost does not include storage cost (unless part of production process), all admin cost, selling cost. These costs are treated as expenses and recognised in the income statement.

IFRS measure inventory as lower of cost or net realisable value
- net realisable value = selling price - cost of selling
IFRS can recover previous write-down inv. but to the original write-down value

COGS = beginning inv + purchase - ending inv

Long-lived Assets
depreciation methods:
  straight-line method
  accelerated method
  units of production method:
                         original cost - residual value
per unit cost =  ----------------------------------
                                total production

year 1: units produced * per unit cost
year 2: units produced * per unit cost

revaluation model
  IFRS only, not allowed under US GAAP
- cost model, historical cost minus accumulated depreciation
- revaluation model, revalue at fair value less accumulated depreciation

 US GAAP
  for machinery
- cost model
 for investment property
- fair value model, no depreciation

revaluation model
Increase in carrying amount goes to other comprehensive income, accumulated in equity under account called "revaluation surplus"
Loss in carrying amount show in income statement

Impairment of assets (unexpected decline in asset value)
IFRS:
Impairment is recorded when an long-lived asset's carrying amount exceeds the higher of the asset's value in use (discounted present value of the asset's expected future cash flows) and (fair value - costs to sell).
The impairment loss is measured as the amount by which the asset's carrying amount exceeds its recoverable amount.
If so happens,  reduce carrying amount to the higher of value in use or (fair value - cost to sell)
           can reverse impairment loss

US GAAP: compare carrying amount with undiscounted expected future cash flows
           cannot reverse impairment loss

Should include capitalised interest in the calculation of interest coverage ratio, will provide better picture of solvency, eg,  EBIT / interest payment

Expenditures related to long -lived assets are capitalised as part of cost of the assets if provide benefits beyond one year. Otherwise, expenditures are expensed as incurred.

Interest cost related to acquire or construct asset that is long-lived must be capitalised.

Tangible assets - depreciation
Intangible assets - amortization

Income Taxes

for temporary differences, consider:

deferred tax assets / deferred tax liabilities

deferred tax assets: taxes that have been recognised for tax reporting purposes but not yet on income statement

deferred tax liabilities: taxes that is on income statement but not yet payable under the regulations

- accounting profit on income statement
- taxable income subject to income tax
- tax base of asset / liability is the amount the asset / liability is valued for tax purposes, carrying amount is the amount valued to accounting principles.
- deferred tax assets must be assessed for their recover probability, if will be recovered partly, the carrying amount should be reduced.

For US GAAP, it is done through valuation allowance. For valuation allowance, it happens when there is doubt on company's ability to pay the tax in future.

- higher reported tax expense relative to taxes paid will increase deferred tax liability
- lower reported tax expense compared to taxes paid will increase deferred tax asset
- if the deferred tax asset or liability is reversible (the result of timing difference), the deferred tax is a future asset or liability
- if the deferred tax will not reverse in the future, it should be treated as equity
- tax credit that reduce taxes is permanent difference, it does not cause deferred tax.

Long-Term Liabilities

  • bonds payable
- issued at premium (interest expense reflects interest payment less amortization of the premium),
  carrying amount > face value
  interest expense < coupon payment
- issued at discount(interest expense reflects interest payment plus amortization of the discount),
  carrying amount < face value
  interest expense > coupon payment

eg, 1,000,000 face value, sales proceeds 957,876
  market interest rate 6%, 5% interest payment
interest payment = 5%*1,000,000
interest expense = 6%*957,876

interest payment -> IFRS, operating or financing cash outflow
   US GAAP, operating cash outflow
interest expense -> income statement

debt extinguishment: net carrying amount of the bond (including bond issuance cost) less the amount required to redeem the bond
  • leases
financial(capital) lease
- financial lease is economically similar to borrowing money and buying an asset. lessee reports lease asset and lease payable on balance sheet. On income statement, lessee report interest expense on the debt, and maybe depreciation expense.
- the lessor reports sale of an asset (remove the asset), and record lease receivable

operating lease
- lessee record lease expense on income statement, lease payment on operating cash flow
- lessor record lease rental income on income statement

financial lease has higher debt and expenses than operating lease.

lease payment for interest expense -> operating cash outflow
lease payment for lease liability -> financing cash outflow

Financial Report Quality

  • fraud triangle

1. incentive or pressure
2. opportunities
3. rationalise the behaviour

fraud from cash flow statement:
- securitizing of receivables: boost operating cash flow, gain on income statement.
- tax benefits from stock options:reduce tax return reflecting difference between strike price and market price of option, reduce tax payable, increase equity of balance sheet
- financing payables: use 3rd party to pay the payables, and pay the 3rd party later, re-classify account payable to short term loan
- stretch payables: improve cash flow from operations

Saturday, 9 January 2016

CFA Level 1 - Part 5 Equity and Fixed Income

Markets
Market execution mechanisms
1) quote driven markets
    customers trade with dealers
2) order driven markets
   use rules to match buyers to sellers
3) brokered markets

Equity Indexing
Index weighting
1) Price weighting
               Pi
  wi = ----------
         Σi=1nPi
2) Equal weighting
            1
  wi = -----
            N

3) Market capitalization weighting
               QiPi
  wi = -------------
         Σi=1nQiPi

Need of rebalancing for equal weighting
                                                          total shareholder's equity
book value of equity per share = ----------------------------------
                                                         total  shares outstanding


Need to understand the below:
price return of price weighting
price return of equal weighting
total return of equal weighting
price return of market cap. weighting
total return of market cap. weighting


                                                           365
Day Sales Outstanding (DSO) =--------------------
                                                      credir sales/AR

                                                           365
Day Inventory on Hand (DOH) =-----------------
                                                       Cogs/Avg Inv

                                                                365
Day payable Outstanding (DPO) =-------------------
                                                            purchase/AP


Operating cycle = DSO + DOH
Cash conversion cycle:  DSO + DOH - DPO

Receivable turnover = credit sales / AR
Inventory turnover = cogs / avg inventory
Receivable turnover = purchases / avg payable

cogs = beginning inv + purchase - ending inv

Equity valuation
Present value model
  dividend discount model
                          Dt
  Vi = Σt=1-----------
                         (1+r)t

                          Dt            Pn
  Vi = Σt=1n----------- + --------
                         (1+r)t       (1+r)n

  gordon growth
                   D1
      Vi =   ---------
                  r - g
     g = b * roe
     b: ( 1 - dividend payout ratio)
     g: growth rate
     roe : return on equity

  multi stage dividend growth model

  FCFE model
                          FCFEt
  Vi = Σt=1-------------
                             (1+r)t

Multiplier model
  P/E = p/ (r - g)  p: dividend payout ratio
  P/B
  P/CF
  enterprise value multiple
     EV = debt + preferred stock + common stock - cash
     EV/EBITDA

Asset based model
  estimate the fair value of the companies asset and liabilities
  useful for companies that do not have high level of intangibles or off the book assets

  assets + liabilities
  -------------------------  = asset based value
   shares outsanding


Bonds:
tenor: term to maturity
current yield : annual coupon / bond price
yield to maturity : internal rate of return
   higher the ytm, lower the bond price

bond indenture: the bond legal contract
debentures: a type of bond, can be secured or unsecured

Callable bonds
  american call: call at any time on the first cal date
  european call: only once
  bermuda call: call on specified dates

Putable bonds:
  a put provision has value to bondholders

Commercial paper:
  issue $50,000, 180 day USCP, 5%
  interest : 50,000 * 0.05 * 180/360

Contingency provisions:
   clauses in indenture: call provision, put provision, conversion provision

Bond price <--------> market discount rate
                     inverse

Bond price <---------> coupon
                 low coupon, more price volatile
                 given changes in ytm

Bond price <----------> maturity
               longer term bonds, more price volatile

Bond price, market discount rate is CONVEX relationship

Price and yields:
  PVfull = PVflat + AI ,  PVfull : dirty price, PVflat : clean price

  AI = t/T * PMT  , t/T : fraction of the coupon period that has gone by

  PVfull = PV*(1 + r)t/T

Matric pricing
  use interpolation to find the bond price , from other bond price of tenor and coupon

Z-spread
  yield spread over government spot curve, zero volatility spread
                PMT                   PMT+FV
  PV =  ------------- + --- + ------------
            (1+z1+Z) 1             (1+zn+Z)N

 z1 .. zn : benchmark spot rates

Duration: bond price sensitivity to ytm or yield curve
Macaulay Duration
Modified Duration = MacDur / (1+r)
                                    PV- - PV+
Approx ModDur = ----------------------
                                 2(ΔYield)(PV0)

%ΔPVfull = -AnnualModDur * ΔYield

coupon rate ↑ --> MacDur ↓

time to maturity↑ --> MauDur↑

ytm ↑ --> MacDur ↓


                                       PV- - PV+
Effective Duration = ----------------------
                                    2(ΔCurve)(PV0)

Money Dur = AnnualModDur * PVfull

ΔPVfull = -MoneyDur * ΔYield

PVBP = estimate of change in full price given a 1bp change in ytm
                                      PV- - PV+
                   PVBP = ----------------------
                                           2

                                     PV- - PV+ - 2*PV0 
Approx Convexity   = ----------------------
                                      (Yield)2*(PV0)

ΔPVfull = -MoneyDur * ΔYield + 1/2 * MoneyConv * (ΔYield)2

%ΔPVfull = -AnnualModDur * ΔYield + 1/2 * AnnualModConv * (ΔYield)2


Bonds with embedded option (or floating rate bond) uses effective duration to measure interest rate risk -> effective duration allows for changes in bond's cash flow

Macaulay Dur inversely related to coupon rate, ytm

Macaulay Dur positively related to time to maturity

Convexity is a positive attribute for a bond

more convex --> price ↑ more when interest rate ↓

return impact = -(ModDur * Δspread) 1/2 * ModConv * (Δspread)2

credit migration risk: risk that a bond issuer's creditworthiness may lower

market liquidity risk: risk that the price of investor transact is different from price of market

Friday, 8 January 2016

CFA Level 1 - Part 4 Corporate Finance and Portfolio Management

Corporate Finance

Capital Budgeting

  NPV :  Σt=1nCFt/(i + r)t - outlay

  IRR  : Σt=1nCFt/(i + r)t = outlay

  Payback Period : number of years to recover original investment, based on cashflows

  Discounted Payback Period : number of years to recover original investment, based on discounted cashflows

  NPV profile: shows a project's NPV graph as function of various discount rates

  Profitability Index = PV of future cash flow / initial investment
                                 = 1 +  ( NPV / initial investment )

Cost of Capital
  WACC = wdrd(1 + t) + wprp + were

Cost of Preferred Stock
 Pp = Dp/rp

Cost of Debt
  calculate the ytm of bond

Cost of Equity
  cost of debt + risk premium
  CPAM: ri = rf +  β(rm-rf)
  PV model: P0 = D1/(re-g)

Pure play method
  Unlevered beta of the company:
  βu,comparable or called βassets

  Levered beta of the company:
  βL,comparable or called βequity 

  Find unlevered beta:
  βu,comparable = βL,comparable / [1 + (1 - Tcomparable)Dcomparable/Ecomparable]

  Lever this beta to get βproject
  βL,project βu,comparable [1+ (1 - Tproj)Dproj/Eproj]

Leverage
Degree of Operating Leverage (DOL): measure of operating risk
Operating leverage involves using a large proportion of fixed costs to variable costs in the operations of the firm.


             % change Operating Income
DOL =  -------------------------------------
                % change sales
                     Q(P-V)                 P: price
         = ------------------------     V: variable operating cost
                 Q(P-V) - F               F: Fixed operating cost


Degree of Financial Leverage (DOL): measure of financial risk
   Fixed operating cost, such as factory depreciation


               % change net Income
DFL =  --------------------------------------
              % change Operating Income
                     Q(P-V)                 P: price
         = ------------------------     V: variable operating cost
                 Q(P-V) - F -C          C: Fixed financial cost

                  EBIT
DFL = ------------------------
             EBIT - Interest

Since interest is a fixed expense, DFL magnifies returns.
  Fixed financial cost, such as interest payment

DTL  = DOL x DFL
                     Q(P-V)                 P: price
         = ------------------------     V: variable operating cost
                 Q(P-V) - F -C          F: Fixed operating cost

Break-Even Point         Operating Breakeven Point
                  F + C                                   F
    Qbe = ---------------         Qobe = -----------
                  P - V                                   P - V

Dividend Payment Chronology:
  dividend declaration date:
     the day dividend is declared
  ex-dividend date ( ex-date ) :
     the 1st day that a share trades without dividend
  holder of record date:
     two business days after the ex-dividend date, the date that a shareholder listed in the corp. record will be deemed to have ownership of the shares for purpose of receiving the upcoming dividend
  payment date:
     the day company sends out dividend payment

treasury stock: shares that have been issued and then repurchased

money market yield:
    FV - Price               360
    -------------  * ------------------------------------
        Price           number of days to maturity

bond equiv. yield: state bond yields into annual yield
    FV - Price               365
    -------------  * ------------------------------------
        Price            number of days to maturity

Portfolio risk and return:
                                                Pt + Dt
     holding period return: R = ----------  - 1
                                                    Po

                                                    1
     arithmetic/mean return: R = ---- Σt=1T Rt
                                                    T

     geometric mean return:  R = Tπt=1(1+ Rt) - 1

Portfolio Management

For two asset portfolio,

  σp 2= w12σ12+ w22σ2 2 + 2w1wCOV(R1,R2)

  COV(R1,R2) = p12σ1σ2

Efficient Frontier is combinations of 2 stocks

Utilities Theory and Indifference Curves

Capital Allocation line: risk free asset + risky portfolio
Capital Market line: risk free asset + market portfolio

    E(Rp) = Rf + σp/σm [E(Rm) - Rf]

CML with different lending and borrowing rates


systematic risk: risk that cannot be avoided
non-systematic risk: risk that is local

total variance = systematic variance + non-systematic variance

  βi = pimσi/σm        E(Ri) = Rf + βi [E(Rm) - Rf]

Security market line: use systematic risk, applies to any security
CAL, CML: use total risk, applies to efficient portfolio only

Portfolio performance evaluation:
  Sharpe ratio = (Rp-Rf)/σp
  Treynor ratio = (Rp-Rf)/βp
  M-squared(M2) = (Rp - Rf)σm/σf - [E(Rm) - Rf]
  Jensen's Alpha = Rp - [Rf + βp(E(Rm) - Rf)]

Tuesday, 10 November 2015

CFA Level 1 - Part 1 Ethics and Quantitative Methods

Ethics and Professional Standards

Code of Ethics and Standards of Professional Conduct

Guidance for Standards I-VII
  Standards I - Professionalism
    A. Knowledge of the Law
      eg. Applicable law
    B. Independence and Objectivity
      eg. Travel funding
    C. Misrepresentation
      eg. Verify outside information
      eg. Plagiarism
      eg. Work completed for employer
        the firm can use ex-exployee's prior analysis without providing attribution
    D. Misconduct
      eg. background check on prospective employers

  Standards II - Integrity of Capital Markets
    A. Material non-public information
      eg. Mosaic theory
        investment research reports not required to make public
      eg. Firewall elements
      eg. Source of Information
      eg. A prohibition on all types of proprietary activity when a firm has possession of material nonpublic information is not appropriate.
     eg.   CFA member/candidate should encourage the issuing company to make the information public. CFA member/candidate, however, is under no obligation to disseminate the information himself.
    B. Market Manipulation

  Standards III - Duties to Clients
    A. Loyalty, Prudence, Care
      eg. soft commission policies
    B. Fair Dealing
      eg. treat all clients fairly when disseminate investment recommendation or taking investment action. not equally
    C. Suitability
      eg. understand client risk profile
    D. Performance Presentation
      eg. consider the knowledge of audience to whom the presentation is addressed
    E. Preservation of Confidentiality
      eg. members and candidates must keep client info confidential unless client permits disclosure

  Standards IV - Duties to Employers
    A. Loyalty
    B. Additional Compensation Arrangements
      eg. disclose other benefits from third parties
    C. Responsibilities of Supervisors

  Standards V - Investment Analysis, Recommendations and Actions
    A. Diligent and Reasonable Basis
    B. Communication with Clients and Prospective Clients
    C. Record Retention
        eg. up to 7 years

  Standards VI - Conflicts of Interest
    A. Disclosure of Conflicts
    B. Priority of Transactions
      eg. personal trading secondary to trading for clients
    C. Referral Fees

  Standards VII - Responsibilities as a CFA institute member or candidate
    A. Conduct as members and candidates in the cfa program
    B. Reference to cfa institute, the cfa designation and the cfa program

Global Investment Performance Standards (GIPS)

  firms must meet all requirements set forth in the GIPS standards, and cannot claim partial compliance
  third party verification enhances the credibility of the GIPS compliance

Quantitative Methods

Time value of money
  FV = PV(1+r)   - single period
  FV = PV(1+r)n - any number of periods
  FV = PVern         - continuous compounding

effective annual rate
         ear = (1 + period rate)n -1
          n: number of compounding period in a year
         ear = ern -1

annuity - finite                     perpetuity - infinite

ordinary annuity - first cash flow one period from now
annuity due - first cash flow happens immediately (t=0)

Annuity formula
     FV = A[(1+r)n-1]/r

     PV = A[1-(1+r)-n]/r

Perpetuity formula
     PV = A/r

Discounted Cash flow Applications
   bank discount basis (for T-Bill)
       D = F * Rbd * t/360
         F: face value
         D: dollar discount difference between face value of T-Bill and price
         t: remaining days to maturity

  holding period yield
      hpy = (P1 - P0 +D1) / P0

  effective annual yield
      eay = (1 + hpy)365/t -1

  money market yield
      rmm = hpy * 360/t

  bond equivalent yield: double the semiannual ytm
  (annual percentage rate)

  time weighted return - use geometric mean
    return = [(1+hpy1)(1+hpy2)(1+hpy3)] 1/3 - 1
      hpy1 : hpy for year 1

Statistics
statistical inference: make estimates about a larger group from a smaller group that is actually observed

sample statistic: a statistic computed from a sample

measurement scales:
  nominal scales: categorise data, no rank
  ordinal scales: sort and order data
  interval scales: ranking data, differences between scale are equal
  ratio scales:  have a true zero point as the origin

median divides a distribution in half
quartiles into quarters
quintiles into fifths
percentiles into hundredths

mean absolute deviation  = Σni=1 | xi - x̄ | / (n)
population variance  = Σni=1 (xi - x̄)/ (n)
sample variance  = Σni=1 (xi - x̄)/ (n-1)

coefficient of variation : CV =  s/x̄

sample skewness: positively skewed, negatively skewed

lognormal distribution is skewed to the right (positively skewed)

kurtosis: shows the probability of extreme outcomes
               shows peakness of a distribution

Probability
Empirical probability: based on historical data and observation
Subjective probability: based on subjective judgement
A priori probability: based on logical analysis

Conditional Probability
          P(A|B) = P(AB) | P(B)

Joint Probability
          P(AB) = P(A|B)P(B)
          P(AB) = P(BA)
          P(A or B) = P(A) + P(B) - P(AB)

Independent Events
          P(AB) = P(A)P(B)
          P(A|B) = P(A)     P(B|A) = P(B)

Total Probability Rule
          P(A) = P(AS) + P(ASC)
          P(A) = P(AS1) + P(AS2) + ... + P(ASn)
              P(A) is mutually exclusive and exhaustive

Baye's formula
          using the occurrence of event to infer the probability of the scenarios generating it
          update probability based on new info
          P(A) = P(B|C)/P(B) * P(C)

Counting
         labeling problem:  n! / n1!n2! n3! .... nk!
         
         combination: nCr = n!/(n-r)!r!
     
         permutation: nPr = n!/(n-r)!

Odds:
        odds of E = P(E)/[1 - P(E)]
        odds against E = [1 - P(E)]/P(E)


Probability Distributions

Discrete random variable -> probability function

Continuous random variable -> probability density function

Binomial probability
  p(x) = n!/(n-x)!x! * px(1-p)n-x

Binomial distribution
  X ~ B(n,p)

Normal distribution: most used probability density function, kurtosis of 3
  X ~ N(μσ2)

Standard normal distribution (μ = 0, σ= 1)

standardizing a RV X : Z = (x -μ)/σ    standard normal RV, Z(0,1)

Mean variance analysis
    SFRatio = [E(Rp) - RL] / σ p    (safety first ratio)

    RL - safety level

Coefficient of variation , CV = S/

Monte Carlo Simulation
  generate large number of random samples from specific probability distribution to represent the risk

Sampling and Estimation

Sampling methods:

  • simple random sampling
  • systematic sampling: The sampling starts by selecting an element from the ordered sampling frame at random, and then every kth element in the frame is selected
  • stratified sampling: the process of dividing members of the population into homogeneous subgroups before sampling

distribution of sample mean: central limit theorem
  population (μ, σ2) of any distribution
  sample,  mean x̄ , (μ, σ2/n) when sample size is large , n> 30
                                approximate normal distribution

standard error: Sx = S/ √n
                         σx = σ/√n

Confidence Interval
  given probability 1-α , degree of confidence that interval contains the parameter is 100(1-α)%

   ± Z α/2 σ/√n   --> 100(1-α)%  (confidence interval for population mean)

    Z α/2  -- standard normal distribution

  90% - Z0.05 = 1.65
  95% - Z0.025 = 1.96
  99% - Z0.005 = 2.58

   ± Z α/2 σ/√n   (population variance is unknown, large sample size)

   ± t α/2 σ/√n   (population variance is unknown, large sample size or small sample size but population is normally distributed)

Data mining bias (data snooping bias): finding models by repeatedly searching for patterns
Sample selection bias : survivorship bias
look ahead bias:
time period bias: time period used makes data time period specific

Hypothesis Testing

1. stating the hypothesis
    null hypothesis, H0, the hypothesis to be tested, H0 is considered true unless evidence proves it to be false
    alternative hypothesis, Ha, the hypothesis accepted when His rejected

two sided hypothesis
H0 : θ=θ0
Ha : θ\neq θ0

one sided hypothesis
H: θ\leq \!\,θ0
Ha : θ>θ0

2. define test statistics
       x̄ - μ
      ------------
        s/√n

choose test distribution:  t-test
                                            Z-test
                                           X2 test
                                            F-test

3. specify the significance level
    reflect how much sample evidence to reject H0

reject false H0
reject true H0                -- type I error
do not reject false H-- type II error
do not reject true H0

4. state the decision rule
    statistically significant           ----> reject H0
    not statistically significant    ----> do not reject H0

eg. two sided test, 0.05 significant level, H: θ=θ0
   Z0.025 = 1.96   ,  Z-0.025 = -1.96
   Z < -1.96 or Z > 1.96   ---> reject H0

eg. one sided test, 0.05 significant level, H: θ\leq \!\,θ0
   Z0.05 < 1.645   ,
   Z > 1.645    ---> reject H0

5. collect data, calculate test statistic

6. make statistical decision

7. make investment decision


p-value, smallest level of significant that Hcan be rejected

p-value < sig. level --> reject H0

p-value close to zero, Hshould be rejected

single mean : t-test, Z-test
         x̄ - μ
      -------------
        s/√n             (n-1) df

difference between means: t-test

single variance:  X2-test
       (n - 1) s2
X2= -----------------
          σ2               (n-1) df

difference between variance: F-test
           s12                  (n1-1) df1
 F= -------------
          s22               (n2-1) df2


Technical Analysis


  • Chart Pattern

  head and shoulders: price target = neckline - ( head - neckline)
  inverse head and shoulders: price target = neckline + (neckline - head)


  • Technical Indicator

  moving average:
    golden cross: short term moving average cross underneath a long term moving average
    death cross: short term moving average cross above a long term moving average

  bollinger bands: upper boundary band, lower boundary band

  momentum indicators:
    momentum oscillators: overbought, oversold
      M = V/Vx * 100    V - last closing price
                                     Vx - closed price x days ago
      M = (V - Vx) * 100

    relative strength index

    stochastic oscillator  :
      > 70 overbought
      < 30 oversold

    moving average convergence/divergence (MACD)
      difference between short term and long term moving average of price


  • Sentiment Indicator

    flow of funds indicators
      arms index (TRIN) = advanced number / declined number
                                         ------------------------------------------------
                                         advanced volume / declined volume


  • Cycles

  Kondratieff wave: (K wave)
    54 year economic cycle


  • Elliott wave theory

  5 up waves, impulse wave
  3 down waves, corrective wave
  1.618 golden ratio -> fibonacci ratio
  relationship among wave heights is fibonacci ratio
 

Wednesday, 14 October 2015

Device Tree in Linux

A device tree is the tree data structure that describes the physical devices in the system hardware. In Linux, the device tree source (DTS) files are located in arch/<xxx>/boot/dts

  •   dts for board level files
  •   dtsi for Soc level definitions

A device tree compiler (DTC) compiles the source into binary files. The DTC source code is in scripts/dtc.c.

The device tree blob (DTB) is produced by the DTC compiler. It is loaded by the bootloader and parsed by kernel at boot time.

Let's look at an real example where a platform driver is using two clocks.

In xxx_probe(struct platform_device *pdev)
    my_port->aaclk = clk_get(&pdev->dev, "aa");
    my_port->bbclk = clk_get(&pdev->dev, "bb");

The corresponding device tree has to reflect the two clocks.

uart0: serial@3000 {
compatible = "xxx,uart";
reg = <0x3000 0x100>;
interrupt-parent = <&gic>;
interrupts = <11 12>;
clocks = <&oscclk 0>,<&oscclk 1>;
clock-names = "aa","bb";
};

The two clock-names needs to have two matching clocks source.

Let's look at at an advanced example of device tree usage:


This example shows an oscillator clock being fed to pll1, pll2 and pll3. The hw3_clk can be derived from either pll2, pll3 or osc clock. 

To model the hardware using device tree. We declare the following entries.

osc: oscillator {
#clock-cells = <0>;
compatible = "fixed-clock";
clock-frequency = <20000000>;
clock-output-names = "osc20M";
};

pll2: pll2 {
 #clock-cells = <0>;
compatible = "abc123,pll2-clock";
clock-frequency = <23000000>;
clocks = <&osc>;
reg = <0x05 0x10>;
};

pll3: pll3 {
 #clock-cells = <0>;
compatible = "abc123,pll3-clock";
clock-frequency = <23000000>;
clocks = <&osc>;
reg = <0x15 0x10>;
};

hw3_clk: hw3_clk { 
    #clock-cells = <0>; 
    compatible = "abc123,hw3-clk"; 
    clocks = <&pll2>,<&pll3>; 
    clock-output-names = "hw3_clk"; 
}; 

In the source code, to register hw_clk3 as a mux, and show the parent relationship of both pll2 and pll3, we declare the below.

of_property_read_string(node, "clock-output-names", &clk_name); 
parent_name[0] = of_clk_get_parent_name(node, 0); 
parent_name[1] = of_clk_get_parent_name(node, 1); 

clk = clk_register_mux(NULL, clk_name, parent_name, 
      ARRAY_SIZE(parent_name), 0, 
      regs_base , offset_bit, one_bit, 0, NULL);

Wednesday, 19 November 2014

[iOS App] 'NSInvalidArgumentException', reason : unrecognized selector sent to Instance

I am going through iOS App development recently. I use modal segue to move from parent ViewController to child ViewController, as shown in the picture below.


When i return from child ViewController to parent ViewController.  There is an error.


2014-11-20 09:00:48.229 Heritage[64395:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[XYZContinentTableViewController topViewController]: unrecognized selector sent to instance 0x8c60050'

The error shows up when i have 3 level of ViewController, and specifically, from second level ViewController to root ViewController.

I found out the root cause. It is because the unwind segue

- (IBAction)unwindToRegion:(UIStoryboardSegue *)segue
{  }

is mixed up with prepareForSegue.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    ....
}
   
To fix the error, in prepareForSegue, add the segue identifier checking. Make sure prepareForSegue is only processing the forward segue (In our case, the UserShowSegueCountry).

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    
    if ([[segue identifier] isEqualToString:@"UserShowSegueCountry"])
    {
    UINavigationController *nav = segue.destinationViewController;
    XYZCountryTableViewController *vcToPushTo = (XYZCountryTableViewController *)nav.topViewController;
        
    }
    

}


Wednesday, 15 October 2014

IP address conflict and its detection

We have an IP network, with devices getting their IP address from DHCP server. A problem could arise when a device is off the network, then reconnect to the network, and its IP address is the same as other device which has just joined the network. This could be due to DHCP server thinks that the DHCP lease has expired, but the device doesn't. This is a problem of IP address conflict.

To detect IP address conflict, we can use two methods

1) ARP probe: It is an ARP request. The sending device fills the source MAC address with the hardware address of its interface, and the source IP address must be all ZEROes. The destination MAC address should be all ZEROes. The destination IP address is the address being probe. If the sending device receives any ARP on the interface where probe is being performed, where the packet's source IP is the IP being probed for, then this IP is used by other host. So it is an IP conflict.

2) Gratuitous ARP: It could mean both gratuitous ARP request or gratuitous ARP reply. Gratuitous in this case means a request/reply is not required, but could be used in some cases. In a gratuitous ARP request,  the source and destination IP are both set to the IP of the device issuing the packet, and the destination MAC is the broadcast address ff:ff:ff:ff:ff:ff. Ordinarily, no reply packet will occur. A gratuitous ARP reply is a reply to which no request has been made. When a device receives an ARP request containing a source IP that matches its own, then it knows there is an IP conflict.

Tuesday, 2 September 2014

Java Serializer

In Computer Science, serialisation is a process of translating data structures or objects into a byte stream that can be stored (in a file or memory buffer), or can be transmitted across a network link, and the byte stream can be re-read and re-constructed into the same clone.  Serialisation is a way to prevent the byte ordering (endianness), memory layout problem.

In Java, the java.io.serializable implement the serialisation. You can write a Java class to implement the Serializable interface.

class IsSerializable implements Serializable  
{  
    private String plug = "Plugin to Java!";  
    public IsSerializable()  
    {  
        try  
        {  
            FileOutputStream out = new FileOutputStream("/mnt/sdcard/yes-out.txt");  
            ObjectOutputStream oos = new ObjectOutputStream(out);  
            oos.writeObject(this);  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }  

}  

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IsSerializable is = new IsSerializable();  
        ...
}

If you run this code, there is an error message:

java.io.NotSerializableException: com.example.xxxxx.MainActivity.

The reason is that you can't get the whole class to be serialised.

Solution:
If we replace oos.writeObject(this); with oos.writeObject(plug);  the problem is fixed.

Monday, 25 November 2013

View the Android App database contents

So you created your Android App with SQLite database, and you are interested to view the database offline. You can do it with the adb command.

1) in command line, issue "adb shell" command, and then you will go to android prompt:
root@android:/ #

2) cd data/data, then ls, and you will see a lot of the App package:
com.android.bluetooth
com.android.browser
com.android.calculator2
com.android.calendar
com.android.certinstaller
com.android.contacts
com.android.defcontainer
....

3) cd  to your App package, say for example "com.victor.example"

4) cd databases, and ls to find out the databases name

5) run the command "sqlite3 mydb.s3db"
root@android:/data/data/com.victor.example/databases # sqlite3 mydb.s3db
sqlite3 mydb.s3db
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"

6) check the table name
sqlite> .tables
.tables
android_metadata  employees
sqlite> select * from employees;
select * from employees;
1|mitt|01234567|01234567|20
2|mitt|01234567|01234567|20
3|mitt|01234567|01234567|20
4|mitt|01234567|01234567|20
5|mitt|01234567|01234567|20
sqlite>

7) That's it. You successfully view the App database content using the adb shell command.

Sunday, 24 November 2013

Export Excel data to MySQL database


We will show you how to export Excel data to MySQL db.

The steps are:

1) saved the excel data to csv format, such as below
BK001,Introduction to science,976,85.0
BK002,Human Anatomy,985,200.0
BK003,Concepts in Health,765,100.5

2) create a db table that matches the csv format
mysql> create table book_info
    -> (book_id varchar(20), book_name varchar(40), isbn_no int(11), book_price, int(10));

3) run the mysql command
mysql> load data infile 'c:\\Book1.csv' into table book_info fields terminated by ',' lines terminated by '\n';
Query OK, 3 rows affected (0.07 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

After that, the db contains the Excel data, To verify, issue the command:

mysql> select * from book_info;
+---------+-------------------------+---------+------------+
| book_id | book_name               | isbn_no | book_price |
+---------+-------------------------+---------+------------+
| BK001   | Introduction to science |     976 |         85 |
| BK002   | Human Anatomy           |     985 |        200 |
| BK003   | Concepts in Health      |     765 |        101 |
+---------+-------------------------+---------+------------+
3 rows in set (0.00 sec)

Friday, 25 October 2013

Facebook App Development on Android

On Facebook
Firstly, we talk about the tasks and tools available on Facebook developers.

Now, login to your FB account, go to "Manage Apps"

Choose "Edit App" or "Create New App".

Here are the basic settings of the App:

Then, choose "Use Graph API Explorer".
Enter the POST action path: "me/victory-test-now:gin"

victory-test-now is the namespace, gin is the Action. You may have to add the Action in the "Open Graph" section beforehand.


The food url is from "Open Graph->Types":

After clicking submit button in the Graph API Explorer, go to the Activity Log of the user. You can see the Action in the Activity Log.

You can add a place field to the POST Action.

The place id can be obtained using the following method.

Click on the profile picture, right click, choose "Copy link address"

For example, the link address is below.
https://www.facebook.com/photo.php?fbid=10150753663333147&set=a.438257763146.238406.145768288146&type=1&source=11

The place id is "145768288146".

The Action can be seen in the Activity Log again.

On Android
Secondly, we highlight how the Android App is able to access the data in Facebook through the Facebook App.

Preparation steps:

  • Download the Facebook SDK for Android
  • Import the SDK into Eclipse
  • Compile and build it

After that, you can start to create an Android App in Eclipse. The good tutorial from FB is listed below:
https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/

After you have created the Android App, we go back to the App Dashboard on Facebook Developers, and register the Android package and activity name in the "Native Android App" settings. As an example, the detail info is shown below.

To get the Key Hash, add the code to the OnCreate() method of the MainActivity class.
    // Add code to print out the key hash
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "com.facebook.samples.hellofacebook", 
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
Publish to Facebook Feed

In the Facebook tutorial below,
https://developers.facebook.com/docs/android/scrumptious/publish-open-graph-story/
it talks about publishing to Open Graph story.

You could modify the code to make it publish to Facebook Feed. Sounds interesting, isn't it? The essential changes are:

1) populate the Bundle with the description, link, name, caption
2) change the post action path to "me/feed"

The obvious changes are not difficult. However, you may have to retrieve the user info from List<GraphUser>.

                        public String announceString;
                        ...
                        private List<GraphUser> selectedUsers;
                        ...
                     for (GraphUser user : selectedUsers) {
                         announceString += user.getName();
                     }

The changes to AsyncTask:

AsyncTask<Void, Void, Response> task =
    new AsyncTask<Void, Void, Response>() {

    @Override
    protected Response doInBackground(Void... voids) {
        // Create an eat action
        EatAction eatAction =
        GraphObject.Factory.create(EatAction.class);
        // Populate the action with the POST parameters:
        // the meal, friends, and place info
        for (BaseListElement element : listElements) {
            element.populateOGAction(eatAction);
        }
        // Set up a request with the active session, set up
        // an HTTP POST to the eat action endpoint
         // Request request = new Request(Session.getActiveSession(),
         //         POST_ACTION_PATH, null, HttpMethod.POST);
        Bundle postParams = new Bundle();
        postParams.putString("name", "Gin food");
        postParams.putString("caption", "Gin food");
        postParams.putString("description", announceString);
        postParams.putString("link", announceUrl);
        Request request = new Request(Session.getActiveSession(),
        "me/feed", postParams, HttpMethod.POST);
       
        // Add the post parameter, the eat action
        request.setGraphObject(eatAction);
        // Execute the request synchronously in the background
        // and return the response.
        return request.executeAndWait();
    }

    @Override
    protected void onPostExecute(Response response) {
        // When the task completes, process
        // the response on the main thread
        onPostActionResponse(response);
     }
};