27 June 2026

Prompting for Human Resources Professionals

 🚀 Stop Guessing, Start Directing: A Non-Tech HR Guide to Mastering AI Prompting

(The Secret Skill Every HR Pro Needs to Master)

👋 Introduction: The HR Superpower You Didn't Know You Needed

In the world of Human Resources, we are experts in communication, empathy, policy, and people. We manage complexity—translating dry legal language into supportive employee guidance, and turning complex organizational goals into actionable strategies.

Recently, a new piece of technology has landed on our desks: Artificial Intelligence (AI) writing tools like ChatGPT.

Many people hear "AI prompting" and immediately panic, assuming it involves code, data streams, or advanced tech degrees. Spoiler alert: It doesn't.

Think of it this way: AI is not a psychic; it is the world's smartest, most enthusiastic, but also most literal intern.

If you give this intern a vague, mumbled request, you get vague, mumbled results. But if you give them crystal-clear directions—the kind you'd give a brilliant intern on their first day—you get gold.

Mastering "prompting" isn't about technology; it’s about communication strategy. It’s learning how to talk to a machine so it understands exactly the sophisticated, nuanced professional you are.

💡 What is Prompting? (The Analogy)

A "Prompt" is simply the instruction or question you give the AI. It’s the verbal blueprint for the work you want done.

Vague Prompting (The Bad Way):

Me to the AI: "Write an email about the new PTO policy."
The AI (Confused): It gives you a generic, bland email that sounds like it came from a 1990s corporate pamphlet.
Effective Prompting (The Good Way):

Me to the AI: "Act as an Internal Communications Specialist. Draft an email to employees announcing the new PTO policy. The tone must be warm and exciting. The goal is to make employees feel appreciated. Structure the email with a 'What's Changing' section, a 'Why' section, and a simple 'Action Steps' list. Keep it under 300 words."
The AI (Impressed): It delivers a structured, empathetic, and ready-to-send email.
The core skill here is providing the three P's: Persona, Purpose, and Parameters.

🛠️ Your Prompting Formula: The HR Cheat Sheet

To write a powerful prompt, you need to give the AI a job description for itself. Follow this formula:

✨ [ROLE] + [CONTEXT] + [GOAL] + [FORMAT & TONE]

Promp

1. The [ROLE] (Persona): Tell the AI who it is.


Example: "Act as a seasoned Chief HR Officer (CHRO)."
 

Why: This forces the AI to use the vocabulary, authority, and perspective of that expert, elevating the quality immediately.
 

2. The [CONTEXT] (Background): Give it the raw ingredients.


Example: "Here is the dry, legal policy text we are updating..."
 

Why: AI doesn't read your mind. You must provide the core data (the policy text, the job description, the meeting notes) so it has something to work with.
 

3. The [GOAL] (Task): State the specific objective.


Example: "Your goal is to translate this legal text into a short, bulleted summary that only focuses on what employees need to know."
 

Why: This prevents the AI from writing an essay when you just need a checklist.
 

4. The [FORMAT & TONE] (Parameters): This is the magic ingredient.


Example: "The tone must be empathetic and reassuring. Use only three bullet points. Write it as a short memo, using headers."
 

Why: This is like setting the boundaries of your request. It controls the length, structure, and emotional feel of the final product.
 

🚀 Putting It Into Practice: 3 HR Scenarios

Here is how this formula transforms your daily routine:

📄 Scenario 1: Simplifying Policy Language (The "Explain It Like I'm 5" Prompt)
 

When a piece of compliance or benefits legislation is dense, don't just ask it to "summarize." Use the full prompt:

The Pro Prompt: "Act as an Internal Communications Specialist. I am pasting our new [Policy Name] policy. Your goal is to translate this complex text into an easy-to-read summary for general employees. The tone must be friendly and reassuring. Use a 'TL;DR' (Too Long; Didn't Read) hook and limit the main points to 3 easy-to-digest bullet points."


📝 Scenario 2: Rewriting Job Descriptions (The "Modern Talent Scout" Prompt)
 

Ditching the "corporate jargon" is key to modern recruitment.

The Pro Prompt: "Act as a Recruitment Marketing Expert. I am pasting a dry job description for a [Job Title]. Rewrite this description to be engaging, inclusive, and focused on outcomes, not just tasks. The tone should be enthusiastic and forward-looking, making candidates feel like they are joining a high-energy team."
 

📨 Scenario 3: Delivering Tough News (The "Empathetic Communicator" Prompt)
 

When layoffs, restructuring, or major benefits changes occur, tone and transparency are everything.

The Pro Prompt: "Act as an empathetic CHRO. We must announce a difficult change: [The Change]. Draft an internal email. The tone must be transparent, supportive, and authoritative. The structure must include: 1) A direct announcement, 2) A clear 'Why' (the business reasoning), and 3) Specific support resources (e.g., Q&A sessions). Do not sugarcoat the message, but cushion it with care."
 

🚫 Before & After: The Power of Specificity
 

To drive this home, let’s look at the difference between a vague request and a powerful one:

❌ Vague Prompt (The Tech Novice)    

"Write an email about the holiday party."  

🎯 Pro Prompt (The HR Strategist) 

 "Act as a Fun Committee Chair. Draft a warm, festive email inviting staff to our annual holiday party on Dec 15th. Include details about an 'ugly sweater' contest and a free raffle. Format key details as a clean, easy-to-read list. The tone should be excited and lighthearted."
 

❌ Vague Prompt (The Tech Novice)     

 "Draft a performance review template."    

  🎯 Pro Prompt (The HR Strategist) 

"Act as an HR Director. Draft a 360-degree performance review template for managers. Format it as a markdown table with three clear columns: 'Competency', 'Self-Evaluation (1-5 Scale)', and 'Manager Feedback Notes'. The tone must be constructive and growth-oriented."
 

Do you see the difference? The second prompt gave the AI not only what to write, but who to be, how to structure it, and what feeling it needed to evoke.

✅ Conclusion: Your Next Assignment

Prompting is a skill, and like any skill, it gets better with practice. Don't think of AI as a replacement for your expertise; think of it as a hyper-efficient, incredibly fast assistant that needs you to be its project manager.

Start today by taking one piece of internal communication that felt difficult, and run it through the formula:

Role + Task + Goal = Perfect Output.

Go ahead—give the machine clear directions, and watch the quality of your work elevate. Happy prompting!

26 June 2026

Using AI Agents to Write A Blog (gemma4:latest)

The Art and Science of Effective Prompting: A Definitive Guide for LLMs

(Draft Complete)


🚀 Quick Summary: Why Prompting Matters Now

If you’ve been using large language models (LLMs) like ChatGPT or Claude by simply asking questions—“What is quantum computing?”—you’ve been treating them like a sophisticated search engine. That’s fine for basic retrieval, but it only scratches the surface.

Real power in AI doesn't come from the model itself; it comes from you, the prompt engineer.

Effective prompting is the difference between getting a vague, generic answer and receiving a perfect, structured, actionable output—whether it’s a piece of code, a market analysis, or a deeply persuasive essay. This guide is your masterclass in moving from basic querying to advanced, programmatic instruction.

🧠 Principle 1: Stop Asking Questions. Start Giving Orders.

The fundamental mindset shift you must make is recognizing that an LLM is not a Google search bar; it is a runtime, instruction-following engine. It excels at mimicking specific roles and adhering to rigid rules.

Instead of asking, "Write about prompt engineering," you should be giving an order: "Act as a leading educational content writer. Your task is to explain prompt engineering to a high school student in no more than three paragraphs. The tone must be witty and highly engaging."

The more specific and structured your instructions, the more predictable and accurate the output will be.

🎭 Principle 2: Give the AI a Role (The Persona)

The single most powerful technique is role assignment. By telling the AI who it is, you are forcing it to activate a specific knowledge framework, tone, and style that aligns with that persona.

❌ Weak Prompt: "Explain blockchain." ✅ Strong Prompt: "Act as a venture capitalist who has never dealt with crypto. Explain the core concept of blockchain technology to me in a maximum of 200 words, using analogies that relate to physical industries, like supply chains or banking."

🔑 Takeaway: The AI will automatically adopt the specialized vocabulary, focus, and level of detail appropriate for a VC—not just a general Wikipedia entry.

🗺️ Principle 3: Define the Boundaries (Context and Constraints)

A model’s genius is also its chaos. If you don't set boundaries, it will wander. Constraints are the guardrails for your AI output. They are arguably more important than the prompt itself.

Ask yourself:

  1. Format: Does the output need to be a JSON object, a Markdown table, a bulleted list, or a poem? Specify the format.
  2. Length: Max 500 words. Three paragraphs. Exactly 12 bullet points. Specify the length.
  3. Tone: Professional, sarcastic, academic, urgent, etc. Specify the tone.
  4. Negative Constraints: What must it avoid? (e.g., "Do not use any corporate jargon," or "Do not exceed a Grade 8 reading level.")

🧱 Principle 4: Show, Don't Just Tell (Few-Shot Learning)

If you are asking the model to perform a complex or nuanced task (like classifying sentiment or rewriting text in a specific style), don't just describe it—show it. This is called Few-Shot Learning.

You provide the model with 2-3 complete examples of the desired input/output pattern before you provide the actual data.

Example Structure:

  • Input 1: The customer left a 2-star review saying, "The shipping was slow but the product is amazing."
  • Output 1: Sentiment: Mixed. Focus: Delivery speed vs. Quality.
  • Input 2: "I waited two weeks and the package never came."
  • Output 2: Sentiment: Negative. Focus: Reliability/Logistics.
  • Input 3: [The new data you want analyzed]
  • Output 3: [The model fills this in]

This immediately anchors the model in the exact pattern you expect.

💡 Principle 5: The Master Key—Chain-of-Thought (CoT)

This is the most powerful technique available today. Instead of asking the model for the final answer, you instruct it to show its work.

Instead of: "Who was the key figure in the Cuban Missile Crisis?" (Might give a single name.) Try: "First, identify the three main players involved. Second, list the timeline of key actions. Third, based on the above points, argue which player bore the most responsibility for the tense negotiations. Show your reasoning step-by-step."

By forcing the model to "think out loud" (i.e., generating the chain of thought), it engages its internal reasoning circuits, drastically reducing the chance of logical leaps, factual errors, or shallow analysis.


👑 The Golden Prompt Template (Putting It All Together)

For the absolute best performance, combine all the techniques into a single, highly structured prompt. Use delimiters (like markdown or XML tags) to clearly separate sections.

Here is the template:

# 🚀 ROLE / PERSONA
Act as a [Insert specific expert role, e.g., Senior Software Architect / Academic Editor / Marketing Director].

# 🎯 CONTEXT & TARGET AUDIENCE
- Background: [Provide necessary context or system state].
- Target Audience: [e.g., Non-technical executives, expert peers, beginners].

# 💡 OBJECTIVE
Your singular goal is to: [Clearly state the unambiguous goal].

# 🚧 CONSTRAINTS
- Format: [e.g., Must be valid JSON / Markdown table with 3 columns].
- Tone: [e.g., Objective, persuasive, highly casual].
- Negative Constraints: [Do not use vague language, avoid all metaphors, keep it under 300 words].

# 🧠 FEW-SHOT EXAMPLES (Optional but recommended)
<example-1-input>[Input sample]</example-1-input>
<example-1-output>[Desired output sample]</example-1-output>

# 🚶 CHAIN-OF-THOUGHT INSTRUCTIONS
Before generating the final output, you MUST perform these steps:
1. Analyze the raw input against the constraints.
2. Draft a step-by-step reasoning chain explaining your logic.
3. Critically review the drafted output.
4. Output ONLY the final, refined result within the specified format block.

# 📥 INPUT DATA
"""
[Insert raw data, text, or query here]
"""

✨ Conclusion: The Prompt is the Product

Mastering prompting is not about memorizing commands; it's about learning how to think like a machine: systematically, logically, and with absolute clarity.

View your prompts not as simple requests, but as high-performance API calls. The better the API call (your prompt), the more predictable, powerful, and reliable the system response (the AI output) will be.

Go forth, prompt wisely, and unlock the true potential of generative AI.


🏷️ Alternate Titles

  1. Prompt Engineering for Non-Engineers: A Guide to Perfect AI Output
  2. Beyond the Query: Mastering Structured Prompting for LLM Excellence
  3. The Prompt Playbook: How to Turn Generic AI Answers into Expert-Level Insights

🐦 Tweet-Length Hooks

  1. 🛑 Stop asking AI questions. Start issuing orders. The difference between vague answers and perfect output is mastering the structured prompt. Read our definitive guide! #AI #PromptEngineering
  2. Your prompts are weak. Here’s how to fix them. Learn the 5 techniques—from Role Assignment to CoT—that turn general AI use into programmatic magic. ✨ (Full Guide in comments!)

25 March 2020

[Investing] Time to Double Down for Dollar-Cost Averaging?

Hi there,

I wrote a couple of posts on dollar-cost averaging, and with the current economic climate, with the STI shed more than 30% in value over 2 weeks, I thought it might be a good opportunity to relook at this investing strategy.

Recap: Dollar Cost Averaging (DCA)

This is a simple investing strategy where one invests a fixed amount of money periodically. The volume bought varies with the going market price, but the total value is fixed by the amount of money invested.

For example, if $100 was invested every month to buy a stock that was going for $2 each, 50 units ($100/$2) of that stock would have been bought. If the price of the stock increased to $2.20 each, 45 units ($100/$2.20) would have been bought.

Over time, a number of stocks will be accumulated and the average price can then be computed. This average price tends to fixed value a long period of time, assuming there are no changes to the periodic investment amount.

The simplest application is the Regular Savings Plan on a STI ETF like the Nikko AM STI ETF (G3B).


Recap: What Happened?

COVID-19 has created much uncertainty in the markets, as countries implement lockdowns and restrict movement.

As a result, everything looks insanely cheap. For example, DBS is now about $18 compare to about $25 a month ago. STI is now at about 2,440 points, compare to 3,158 a month ago. In terms of ETF, this means the price of STI ETF has fallen from approx. $3.15 to approx. $2.44, or about 22% decrease.


Two Different Approaches to DCA

There are two approaches to DCA: a passive one and a active one.

The passive approach is a fire-and-forget strategy. One just sets the amount to invest periodically, and forgets about it. No watching of the market. No worries.

The active approach is to increase the investment amount every time the market price is lower than the average price of the portfolio. In this way, more units could be bought when the prices are low. That in turn will reduce the average price in the portfolio.

The analyses do not consider re-investment of dividends.


Comparison

I compared the two strategies using monthly data from Yahoo! Finance, starting from 1 March 2009, since the inception of G3B into SGX.

For the passive strategy, I used a monthly investment amount of $100 and determined the number of units I would be able to buy with that amount. I then computed the maximum portfolio value, total number of units accumulated and the average price per unit.

A similar approach was used for the active strategy, except that when the going price was below the average price, the investment amount would be increased by a factor. For the simplest case, I set the factor to 2 - if the going price was below the average price, I doubled the investment amount to $200. I would simulate factors 3, 5, 10, and 100 also.


Results





Total Units Accumulated

  • Passive = 4,299
  • Active = 4,830

Toal Invested
  • Passive = $13,203
  • Active = $14,701
Average Price
  • Passive = $3.07
  • Active = $3.04

The passive approach is generally fine. The average prices is generally below the price. The passive DCA is a sound strategy.

The difference in the average prices between the passive and active strategies may not look significant, but the number of units accumulated is. The active approach accumulates 12% more units, at about 11% more costs.

This additional volume has another advantage. Since each unit is entitled to dividends, more units means more dividends received. Over time, the difference will be significant.

Now, lets look at the effect if the factor is increased to 3.


Factor = 3

This approach triples the amount invested when the average price is below the market price.

Total Units Accumulated
  • Passive = 4,299
  • Active = 5,362
Toal Invested
  • Passive = $13,203
  • Active = $16,202
Average Price
  • Passive = $3.07
  • Active = $3.02
Total units accumulated in an active approach is abt 25% more than the passive one, even though the active approach would have invested about 22% more.


A More Radical Approach?

A variant of the active approach is to buy the number of stocks already accumulated, that is double the portfolio size, when the price is below the average price. The factor applied is 1.

The results:

Total Units Accumulated
  • Passive = 4,299
  • Radical = 35,262
Toal Invested
  • Passive = $13,203
  • Radical = $91,165
Average Price
  • Passive = $3.07
  • Radical = $2.58
This approach significantly reduces the average price, but it also involves significant investments.


Conclusion

DCA is a passive fire-and-forget investment strategy. It leverages on time to amass large number of units. It by itself is a sound investment strategy.

However, if the market remains bullish for an extended period of time, the averaged price will usually tends toward a higher side, unless there is a prolonged bear market.

It is possible to optimise the strategy by taking a more active approach. When the market price is lower than the average price, more units could be bought so that the average price is lowered. In this post I used multiples of 2 and 3, doubling and tripling the investment amount when the condition is fulfilled. I also shared a more radical strategy where we double the portfolio size when the condition is fulfilled.

The idea of active intervention, regardless radical or not, is to amass as many units as possible when prices are low (compared to the average).

Perhaps now is the favorable time to re-look at your porfolio and see if you are able to make full use of this opportunity. Personally, I have managed to averaged down my portfolio to $2.85 (from $3.31).

If you have not started investing. This is one of the best times to start. Of course, work out your insurance and make sure you have enough liquidity first.

Start small but stay invested.


~ZF

13 March 2020

[Investing] The Time for Value Investing Is Here

The Straits Times Index shed 15% from 3220 a month ago to 2637 today. Many stocks are on discount.

Take for example, DBS was in the $25 range. Now it is $19.35. That is more than 20% discount!

At the current price,

  • DBS has a market cap of about $52B (it was $56 when I started monitoring it when the index started to fall). I've learnt its probably more prudent to buy larger cap companies.
  • DBS has a P/BV of 1.01 and ROE of 12% and ROA(ssets) of about 1%.
  • Its profit margin is 42% (woah!)
  •  Dividend yield is 4.2% (5-yr average)
With those stats, I think the current price of DBS is very attractive. I valuated it to be about $22 then. I bought some at $21.60 (including fees).

Has the stock reach the bottom? I do not know. 

But this is not the point of this post. The point of this post is to re-visit the principles of stocks-picking, especially at times like this where prices are low and people are panicking because the prices are low.

Not for a value-investor. This is the time to hunt. The basic principles apply. A good stock has:
  • Profitable business
  • Good financial strength
  • Good cashflow
It boils down to two words: Strong and Efficient. The pre-requisite, however, is consistency.

I aim to find stocks that exhibit characteristics of the basic principles. I cited DBS because I think it fulfill those principles (correct me if I am wrong!).

It is easier to judge whether a stock possesses the basic principles. But is is harder to know at what price should one buy the stock if it has been determined that is a  steal?

I used to think:
  • What if the stocks drop further, so I can get a better bargain?
  • What if the stocks increase and I missed the chance?
I learnt that I can mitigate this by:
  1. Split the investment funds into half. 
  2. For the first half buy at the price which I determined to be a good value*.
  3. For the second half buy only when the price is substantially lower than the value I determine**.
This way I can lock down my optimal value. If the stock price goes up, at least I have gotten into a position that I think is favorable. If the stock price goes down, I can wait longer and then get into another position again, so than on average the stock price I buy at is lower than the price that I valued. This is my form of managing risk I suppose.

However there are many subjectivity. What is good value? What is substantially lower? This will require some education, experience and emotion-management. 
  • On education. Reading helps. I have a curated a reading list in this blog. Read about how stocks are valuated, but do not get sucked into fancy valuation models; simple is great. Learn about risk management. Valuation models that do not address risks should be avoided. Valuation is also subjective; nobody can really pin-point the true value of a stock. And because of this, the value of a stock can also be assessed relative to another stock in terms of efficiency. If stock A is 3% return on Assets and stock B is 5%. Which is more valuable? I prefer to think of it as fair price and efficiency.
  • On experience. I watch the stock market perhaps once every quarter? But I do take note of news because I know how the media can affect the markets. I personally buy STI ETF on a RSP basis. Knowing the market helps - how much a stock would max at. Some of my friends use the middle-point of the 52-week range to gauge whether a stock is selling at a discounted price. It is probably a good start. P/BV is probably also another good starting point. DCF too. But that said one should not be sucked into the mania of the media and the markets.
This sale has just started. So it is a good time to do some evaluation and buy stocks when the prices are right.

~ZF
Warren Buffett and Charlie Munger are my heroes.
 

26 February 2020

[Investing] Keppel DC Reit Downgraded to "Hold"

Some REITs (Real Estate Investment Trusts) have always been on my radar. One of them is Keppel DC Reit (AJBU).

AJBU is a data centre Reit, and has footprints also in Australia, Germany, Ireland, Italy, Malaysia, The Netherlands and United Kingdom. It was IPO-ed in 2014 at a price of $1.

As of writing, the price is $2.50. I hesitated to buy when it was $1.60.

Regardless, the recent developments around me made me start revising investment concepts. So I was revising some Reit evaluation methodology I learnt.

Based on my calculation, AJBU has

  1. A gearing ratio of 31.4%
  2. Distribution Yield of 3%
  3. Cost of debt at 1.8%
  4. Price/NAV at 2.19X
  5. Fees/Distributable Income is 14.1%
  6. Fees/Total Assets is 0.5%

Most of the items looks OK (perhaps the fees is slightly on the high side), but its Price/NAV is more than 2 times.

What does this mean? It means that for every dollar of net assets, an investor is paying $2 for it. That leaves little or no room for growth at all. Assets (mainly real estate) are the main source of income for Reits, but a high valuation like ABJU makes it not really a preferred choice now. 

To put this into perspective, it takes about 28 years of compounding a dollar at 3% to reach the value of $2.20. An investor would have already made 250% in capital gain if he had invested during IPO, of course. 

Not that I know of any other way Reits can generate revenue beside rental income, anyway.

Perhaps when the Price/NAV goes down, either because the price goes down (unlikely) or the Reit acquire more assets (likely). 

I think perhaps this is why DBS downgraded this Reit to "Hold". 


I also analysed Reits like the Mapletree Commercial Trust and the Fraser Centrepoint Trust, and shall share them in the future posts.

Stay tuned.

~ZF



16 February 2020

Posting After 2 Years' of Hiatus!

Checked my last post and realise it was dated in 2018. I have not been updating here for more than 2 years!

During this time, I changed my job and am applying my data skills into analysing people. In a cool way, I am a People Analyst now. I will blog about this more.

But the change is not after I brushed up my Statistics and Python skills. I tried to read more about Human Resources. And yes, I wanted to say continuous learning has enabled me some mobility in my career.

I am still learning and re-learning Python almost every day.

I am still passionate in investing, so I will still blog about this. With the Novel Coronavirus happening, I believe there will be much to talk about and opportunities to find.

Till then.

~ZF


27 July 2018

[Investing] 3 Lessons To Learn from the Hyflux Saga

I first heard of Hyflux in 2006 while still doing my internship in a SME in a similar industry - water treatment, when Hyflux IPO-ed, if my memory does not fail me. It was a reputable company with much potential. Apparently, the MD of the SME was from the same batch/company as Olivia Lum, the CEO of Hyflux, and he was pretty envious of the latter. I suppose it is a thing with bussinessmen,  to bring their businesses to public, or IPO.

I was not too sure about that. I just started to learn to invest then. I knew water was (and still is) an important resource in Singapore, so I reckon anything to do with water treatment should not be too bad a deal. I could not read the financial statement then.

Hyflux opened at S$1.50 on the day its stock debut, if I remember correctly. I could not participate in any of those actions because of the lack of capital and knowledge then, since I was just a student. Even when I started investing in 2009, I never quite considered Hyflux because I thought it was too expensive but I still watch stock once in a while for my interest in water-related stocks.

Fast forward 7 years to 2016, Hyflux introduced a 6% perpertual securities. A friend asked if it was a good deal. 6% is really a good deal, but I think it was too good to be true and did a quick check on the financials. I concluded that Hyflux could not sustain the 6% pay out; it was probably a "stunt" to raise capital. I hoped the friend heeded my suggestion and stayed far far away from the perps.

There are some interesting articles on Hyflux and its recent financial troubles. I shall list some here:
  1. Once a star company, Singapore's Hyflux faces major challenges (CNA, May 2018)
  2. Hyflux offers up to $300m perpertuals at 6% a year (ST, May 2016)
But what happened? I think it can be summarised as such: Tuaspring, loss, loss, loss and loss. Tuaspring is the second and largest desalination project which Hyflux won the bid in 2011. Today, Hyflux is trying to sell that plant because it is the cause of all the troubles, or so it claimed. I would also attribute it to bad management (I could hear Buffett screaming that too).

What has happened may or may not be important - I am not affected anyway, since I did not buy their stocks, but the lessons we can learn from are valuable. (I am writing this also as an application of what I know and also hope to serve as a reminder to those who are risking their money for a better retirement, etc. Imagine buying Hyflux at $1.20 a piece and see it down to S$0.20 a piece over 4 years.)

Lesson 1 - Profitability is always the First, with Consistency as its Prefix 
As a stock investor, profitability of a company is a must, and it must be consistent. By consistent, I mean that revenues and profitability must grow in most years over a long period (at leat 5 years). It is ok to have a small dip in one of the years, as long as the company is profitable (not making a loss). I do not consider any company further if I see more red or bracketed numbers in the net income. 
For the case of Hyflux, profitability has always been a challenge it has yet to overcome. Referring to the Income Statement from SGX for 2014 to March 2018, Hyflux profit has been flat, except for 2016 there was a 100% increase, then it started to go negative in Year 2017. EBITDA is negative for 3 out of the 5 periods. EPS too. 
Lesson 2 - Costs will erode any good performance
Costs thin profit margins, and hence minimize profitability. Controlling costs is probably the easiest way to improve profitability. It is also a measure of the management's competency and efficiency. Read the annual report and see if anything is being done to keep costs in check at least. High costs is always a red flag that should be scrutnised. 
Hyflux is considered a high tech company and there I will not be surprise if its operating costs was high. The next question is: did the management do anything about it? I did not investigate further because I think I made my point for Lesson 2.

Lesson 3 - Debt is like a cancer 
As long as cash is not generated from the business operations, I would consider a debt. Money borrowed from banks = debt. Money raised from IPO, stock issuance and whatever = debt. Accounts Receivable = debt, since money is not "generated" into the companies account yet. It is a gross over generalisation from the conventional debt = banks, equity = shares, and the definition of liability. But I personally find defining debt my way is easier to assess the financial health of the company. 
Some extent of debt is healthy. It helps with business operations and also cashflow. But when the company's operation is nto earning enough to service the debt, that is where it runs into trouble. It means the business model has a problem, it could be a cost or efficiency issue, or it could be that the business is not profitable to begin with (quite true for all the start-ups scene that pop up recently). It may also have issues with claiming money, or the companies that they have receivables with also run into financial problems (remember Keppel?). Or if they overextend their dividend payout (consider a company with a dividend payout ratio of more than 100% every year; it will run out of juice really fast). 
As long as a company issues bond or new shares or the like, it is important to scrutinise the intent of it. Hyflux did the 6% perps, which I found that they probably wont have the ability to fulfill that commitment. True enough.
'A big shock': Retail investors in Singapore caught out by Hyflux woes (CNA, Jun 2018) 
I think it is safe to assume that no company will want to give out money freely. Afterall, stocks is a way to raise money. Financial statements are marketing tools to do that.
While picking stocks, I look for three things in the financial statements - Profitability, Financial Strength and Sustainability, before diving further into analysis.

I find that Sustainability is very important. If companies overeextend payouts (be it executives' salary, dividends, etc) or the business operation is too costly, I do not foresee they will remain profitable, for long.

I think it helps to always asks:"Where is the money coming from?" when analysing companies.

If it interests you, you can also read about my thoughts on the Netlink Trusts (CJLU) here. The stock is now at S$0.78 (was S$0.815 at IPO), down as I would have expected it.


~ZF





19 May 2018

[Investing] Book Review - The Warren Buffett Way (3rd Edition) by Robert G. Hagstrom

As the title suggests, this book is about Warren Buffett and his approach and wisdom on investing. It is well-known that Buffett's investing approach goes something like this.

  1. Regard buying a stock as owning part of a business. That is, treat stock investing as you would running a business.
  2. Buy stocks of businesses that are easy to understand, within your level of competency, has a competitive edge and run by companies' with competent management.
  3. Buy stocks cheap.
The book actually sums up the above 3 points in a form of a framework call the Tenets of the Warren Buffett Way. It has four categories, namely Business Tenets, Management Tenets, Financial Tenets and Market Tenets.

The book also discusses the academic forms of finance/investing that gave birth to the modern portfolio theory (the idea of covariance of stock prices as risks, the Captial Asset Pricing Model and the Market Efficient Hypothesis). I found a part pretty meaningful and I shall quote it here:

Today, investors are caught in an intellectual and deeply emotional crossroads. To the left lies the pathway of modern portfolio theory. The theory has a 50-year history full of academic papers, neat formulas, and Nobel Prize winners. It seeks to get investors from Point A to Point B with as little price volatility as possible, thereby minimizing the emotional pain of a bumpy ride. Believing the market is efficient, hence price and intrinsic value are one and the same, adherents to modern portfolio theory focus on price first and asset value later -- or sometimes not at all.
To the right lies the pathway that Warrent Buffett and other successful investos have taken. It has a 50-year history that is full of life experiences, simple arithmetic, and long-term business owners. It seeks to get investors from Point A to Point B not by providing a smooth short-term price ride but by orchestrating an investment approach that seeks to maxmize, on an economic risk-adjusted basis, the intrinsic-value rate of growth. Proponents of the Buffett approach do not believe the market is always efficient. Instead, they focus on asset values first and prices later -- or sometimes not at all

I have read much on value investing (see my reading list!) and I consider myself  a value investor. I think this book is worth a read.

~ZF

13 February 2018

[Data Science] Thoughts After Completing Coursera's Deep Learning 5-Course Specialisation

I have just completed Coursera's 5-Course Specialisation in Deep Learning taught by Andrew Ng. I have been looking forward to this Deep Learning course after taking Ng's Machine Learning course, also in Coursera, and learning a little more about Deep Learning.

Course Structure

The specialisation consists of 5 courses:

1. Neural Networks and Deep Learning 
This course introduces Neural Networks and Deep Learning. Since I have taken the Machine Learning course, the first half of the course was not totally new to me. Deep Learning is an neural network on steroids:  more hidden layers, which can be multi-dimensional. The exciting part about this course is I get to code Deep Learning Algorithms by hand using Python and Numpy - no Tensorflow or Keras, yet.
2. Improving Deep Neural Networks: Hyperparameter tuning, Regularisation and Optimization 
One of the powerful features of Deep Nets is the ability to learn complex relationship. However, the compromise to this power is the need for many parameters to the algorithm, or hyperparameters, to tune. This course is specifically for tuning hyper paramters. It can be quite dry. The only respite is when the Ng started to touch on Optimisation algorithms. I find this part very interesting, and I get to understand the workings behind when I type things like opt = AdamOptimizer(...) 
3. Structuring Machine Learning Projects 
This is one of the shorter module, and also another drier one. I shall not describe too much about it. The next two courses are the "specialisations" to the course.
4. Convolutional Neural Networks 
Or CovNets for short. It is used for image recognition (or computer vision). Wonder how a security camera is able to pick up faces, verify objects or identify suspicious articles? It is like the works of CovNets. It took me a while to have a fuzzy understanding of this powerful application of Deep Nets.
5. Sequence Models 
What happens if a time dimension is included into data? The data becomes a time series, or sequences. The Recurrent Neural Network is an application of Deep Nets on time series or sequence data. This is also a short course, but a pretty heavy one. It deals with mostly Natural Language Processing (NLP) and Machine Translation. There is also a side project on Jazz music improvisation, which I find interesting too.
The programming exercises are coded in Python using Jupyter Notebooks. They are interesting! Along the way, there will be some opportunity to use Deep Learning frameworks like Keras and Tensorflow. Keras is a "simpler" but less flexible derivative of Tensorflow, by the way.


Take-Away

The reasons I took this specialisation up are:

  1. Deep Learning is a building block of artificial intelligence (AI), a topic which I have grown to be very interested in. I always believe AI is going to provide an opportunity to provide more equity to mankind, if managed properly.
  2. My current work involves in analysis tasks which I think can be automated. With the knowledge of Deep Learning, I believe I can implement/build something, or at least a proof-of-concept, AI-ish to improvement work efficiency. If complicated tasks like image recognition, jazz improvisation, and even translation can be accomplished by Deep Learning, I believe my work can benefit from it too; I do not think the work I do is more complicated than what Deep Learning can accomplish, honestly.
  3. Well, the name "Andrew Ng" has become somewhat a household name for machine learning.

I gained better understanding in Deep Learning and its related concepts. However I come to realise a few "inconvenient"  aspects of learning from online resources, based on my person experience:

  1. In online courses, data are provided - cleaned, transformed and ready to be deployed. In real life, data can be a pain to obtain, let alone clean and validated, which will consume much time.
  2. The application of the concepts, however, requires much self-study is needed to learn about using Keras and Tensorflow. This mirrors much in real life. Stackoverflow remains my best friend.

Nevertheless, the learning continues.

~ZF

19 January 2018

[Investing] Colex (567) Vs 800 (5TG)


I have been collating and filtering stocks data and Colex Holdings Ltd (567) has been appearing in the top 10 stocks for me to KIV. I have noted the stock for and been watching it appreciate from 30-ish Singapore cents to 40-ish cents. Pain, I know.

Recently a friend discussed with me about the stock again, and he was also recommended 800 Super Holdings Ltd (5TG), which is  also a similar company.

First, why Colex ((567) caught my eye:
Profile:
------
Stock Symbol = 567
Price = 0.481
Dividends Yield = 0.021
Price/Sales = 0.800
Price/BV = 1.500
Price/Cash Flow = 5.590
 
Profitability
------
Earnings per Share (EPS): 0.048
Net Profit Margin % : 7.960
Return on Equity % = 15.890
Return on Capital % = 12.640
Return on Assets % = 12.640
Inventory Turnover % = nan
Assets Turnover % = 1.590
 
Financial Strength
------
Book Value = 0.280
Current Ratio = 4.050
Quick Ratio = 3.900
Interest Coverage = 305.870
Debt-to-Equity Ratio = nan
The data is collated from [1] using a Python script I wrote. What drew my attention about Colex is its efficiency, namely that its Return on Capital (or Assets) is 12.6%, that is, for every dollar of capital, Colex is able to bring 12.6% of earnings. It also has a very strong current ratio of 4X! Oh, and that interest coverage too! Not shown here is that the P/E of Colex is 16x.

Next, why 800 (5TG) is a potential alternative:
Profile:
------
Stock Symbol = 5TG
Price = 1.207
Dividends Yield = 0.025
Price/Sales = 1.310
Price/BV = 2.400
Price/Cash Flow = 6.160
 
Profitability
------
Earnings per Share (EPS): 0.096
Net Profit Margin % : 10.800
Return on Equity % = 21.170
Return on Capital % = 11.120
Return on Assets % = 11.120
Inventory Turnover % = 31.630
Assets Turnover % = 1.030
 
Financial Strength
------
Book Value = 0.480
Current Ratio = 1.490
Quick Ratio = 1.420
Interest Coverage = 27.940
Debt-to-Equity Ratio = 0.470

Indeed, 800 (5TG) seems to have a better valuation. Its Return on Equity is much higher, its Return on Capital (or Asset) is almost on-par with Colex's. Financial strength-wise it is not as impressive compare to Colex. 800 (5TG) has a P/E of about 12X. Based on this metric, it seems like it is somewhat under-valued.

Just by comparing the data I just shown, it is difficult to decide which to buy. At 11-12% Return on Capital it is equally enticing at any price. I have to dwell deeper into the financials. I did not refer to the respective Annual Reports, but rely on [2]. Again, I will be doing the Profitability, Financial Strength and Cash Flow analysis, quick and hopefully not too dirty.


Profitability

Comparing the Net Income for the past 5 years, Colex has a increasing net income, whereas 800 seems to have plateau or at least not showing a solid consistency.


Financial Strength

In terms of cash stash, 800 has more (25M Vs 14M). 800 also has a comparatively larger amount of receivables. I learnt that receivables, although is in the Asset account, can be a potential write-off risk. Total Assets for 800 and Colex are 159M and 44M respectively.

However, if we compare liabilities, based on the data [2], Colex has close to 9M, whereas 800 has close to 77M.

Both companies' retained earnings are growing.


Cashflow Analysis

In this analysis I took the Operating Cashflow and added back depreciation and minus of CAPEX. Then I divide by the outstanding number of stock. This yielded 0.62 for 800 and 0.74 for Colex. Stock to stock, Colex stock can generate more cash. Colex stock is at 40cents but each stock can churn 74 cents of cash! (OK this part I need to review the calculations, again).


Conclusion

When I look for stock, I look for efficiencies based on ratios such as Returns on Capital and the Free Cashflow generated per share. At this point, Colex at approximately 12% Returns on Capital and a better cashflow number makes a better choice between the two.

Please feel free to comment!

*** Note: This is my personal analysis and is not a recommendation for a stock buy or a stock tip. 

~ZF


References:
[1] MSN Money
[2] Yahoo! Finance

11 January 2018

[Investing] Comparing Dollar-Cost Averaging and Buy-Low-Sell-High (B.Lo.S.Hi) Strategies

In my previous post, I applied dollar-cost averaging (DCA) on Nikko AM STI ETF (G3B) on a 5-year time-frame.

Then a friend showed me an article about the fallacy of dollar-cost averaging.

Indeed, I mentioned in my previous post that dollar-cost averaging depends much on the point where the prices are; if one started low, the accumulation will be fast.

But the result of averaging is not average. At times when prices are low, units accumulation takes place. When prices are high, the additional units also contribute to the total value. Units are the one generating dividends too. In good times (prices are high), dollar-cost averaging suppresses costs. In bad times when prices are low, dollar-cost averaging helps consolidate units.

Dollar-cost averaging is a lazy-man style of investing in auto-pilot. It makes sense if investing costs are low. Obviously, it cannot do better than the Buy-Low-Sell-High (BLoShi) strategy, as I will show later. But the DCA has its merits over the BLoShi too, which I will also share my thoughts later.


DCA Vs BLoSHi. Fight!

I have extracted the maximum data (month) from Yahoo! Finance and coded up the following charts using Python. The hypothetical investing strategy is that I am investing $100 per month since 2009, when G3B debutted into the market.



As shown in the above chart, the average costs increases over time, but the rate of increase slows as time advances because of the large amount of units accumuated (not shown here). The points that are marked with a red 'X' are points when the actual price that is below the average price. These will be points in consideration for the BLoSHi approach.


The BLoShi Approach

In the BLoShi approach, funds are accumulated since the start and maximum units will be bought with all the funds accumulated when the price is below the average price of the DCA method. As shown, there are 5 points that mark this criteria. I shall assume that I enter at only two of these - the first (31-Aug-2011) and the last (31-Jan-2016).

The expected result is that this approach would definitely fare better than the DCA. As we shall analyse in the next chart.


DCA Vs BLoSHi. Performance

The following chart shows compares the value of the units held for the whole period in consideration.



The relevant numbers are:
With DCA,
Total Amount invested = $10,800 
Total Units Bought = 3839 
Total Value = $13,669 
Total Yield = 26.565% 

BLoSHi (Without DCA)
Total Amount invested = $8,400 
Total Units Bought = 3280 
Total Value = $11,678 
Total Yield = 39.027%
Due to the perpertual nature of the DCA, the amount accumulated in DCA is definitely more than the approach without - 600 more units as shown. The costs and holding value will also be higher.

Note that the analysis does not include the dividend yield.


A Simple Principle in Investing

"Buying Low and Selling High" is one of the principles of investing. "Buy a dollar for 50-cents". How low is low and how high is high? There are many metrics to determine that. For the lows, some use the 52-week low to gauge, the same is true for the highs. This form some-sort of a price resistance (I am not a trading pro BTW). Some use moving average over a certain period - if the price is below that MA, buy; if the price is above that MA, sell! Simple enough. Of course there is also the almost-mythical intrinsic value.

We may determine the intrinsic value, but we are able to make a best guess of it. Personally, I would say that numbers that are backed by fundamentals. i.e. there is a basis, is probably the more reliable one, because I cannot find satisfactory answers to question like:"How do we know if the price is going to break the 52-week high (or low)?", "How do we know if the prices will go lower even after falling below the MA?". To the latter question, one way is to keep buying, of course.


The Problem with BLoSHi

BLoSHi seems like a very sound investment strategy, I just wait out for the price to be low enough to buy and sell when the price is high enough. However, the investment strategy is oversimplified. Let's look at the statement again, this time I will italics the key words:
I just wait out for the price to be low enough to buy and sell when the price is high enough.
In essence, there is uncertainty in the price movement and the time. Firstly, I need to determine the price - how low/high is adequately low/high to buy/sell? In the above analysis, it is retrospective. It is easy to pin-point prices that are lower than a certain value (an yearly average, for example) on a historical price chart like the above. Again we can use various metrics (discussed above) to determine the price to buy with some confidence.

Next, when prices are live, and when it shows that the price is trending down after a peak, not many of us will be able to overcome the psychological barrier (fear, that is) or the discipline to buy even if it is near the low that we are waiting for. That is because nobody is certain where the price is going - is it going to bottom out? What if it crash?

Finally, holding out cash to wait out for the price to reach buying-low is not efficient at all. There are much opportunity costs to this approach. In the example above, it is more than 5 years between the two purchases (Aug 2011 and January 2016). Consider the amount that could have been paid out. Assuming that the price remains stable at $2.50, a monthly contribution of $100 will allow me to buy 40 units. In a year I would have accumulated 480 units, and 2400 units in 5 years. If each unit pays out 2% or 0.05cents dividends every year, I would have missed out on $360  of dividends in total, that is more than 3 months' worth of contribution.

With 2% dividend, the yields presented above will be about 29% and 39% for the DCA and Non-DCA cases respectively. As mentioned, the BLoSHi has forgone the compounding effect of any dividends.


Conclusion

Dollar-cost averaging is not a perfect investment strategy. Honestly, in my opinion, it cannot beat the market per se, but it helps to suppress costs when prices are high and accumulate units when prices are low. Here 'highs' and 'lows' are relative to the average price. It also helps one stay invested without the psyhchological barrier when prices dip, and it optimizes the effect of compounding. It also helps negate the need for large capital outlay. This way, we still can get back a fair share from the stock market even though the outcome may not be as stellar as the BLoSHi approach, which I shared that there are many barriers to apply efficiently. However, this is all in the pretext that the investing costs are low.

Perhaps, to optimize the results, one way is to increase the contribution when the prices are low. Maybe I shall leave it for another time.

Do feel free to air your comment!

~ZF







07 January 2018

[Data Science] Machine Learning - Part 3, Logistic Regression

In a Classification task, the learning algorithm allocates a number 1 if an instance of the object is to a class (True), and 0 otherwise. When predicting, the outcome will be a number between 1 and 0, which can be interpreted as the probability of the object belonging to a certain class.

The common algorithm to do so is the Logistic Regression. The hypothesis function of algorithm is
\[\hat{y} = \frac{1}{1 + e^{-Z}}\]

known as the sigmoid or logistic function, or the S-curve. $Z$ can also be any function with respect to the features and the corresponding weights, for example, $Z = w_1X_1 +w_2 X_2 + w_3X_3$. In the learning algorithm, the weights, $w_1, w_2, w_3$ are determined through optimization, such as Gradient Descent, of the cost function.

The output of the sigmoid function will always be between 0 and 1, as the chart below shows for $Y = \frac{1}{1 + e^{-Z}}$

I shall apply Logistic Regression on the Iris data set. We can call out the dataset in Scikit-Learn using this code:
from sklearn import datasets 
iris = datasets.load_iris()
The data is a dictionary, so we can see the keys using iris.keys(). There are three species of the flower with 50 instances each. Hence there are 150 instances of data.

Next, the feature and label data, X and y respectively.
X = iris['data'][:, 3:] #petal width 
y = (iris['target']==2).astype(np.int) #1 if Iris-Virginica
As shown, only the petal width will be used to identify if the species is Iris-Virginica in this example.

It is always a good idea to split the data into training and test set randomly. I will split 30% the data as test data (that is 45 instances). This can be done easily using Scikit-Learn:
from sklearn.model_selection import train_test_split 
X_train, X_test = train_test_split(X, test_size = 0.3, random_state = 123) 
y_train, y_test = train_test_split(y, test_size = 0.3, random_state = 123)
 Now, we can apply Logistic Regression on the data!
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
The model's attributes such as the intercept and coefficient can be called out using model.intercept_ and model.coef_ respectively. Note the underscore '_'!
Model Coefficient = [[ 2.17900148]] 
Model Intercept = [-3.67603174]
Hence the model is:
\[\hat{y} = \frac{1}{1+e^{2.179X -3.3676}}\]

We can use the model to make predictions.
pred = model.predict(X_test)

To determine how the model fare, we can use a Confusion Matrix to see the number of right classifications. 
from sklearn.metrics import confusion_matrix 
print(confusion_matrix(y_test, pred))
The confusion matrix will come in a form:

[[ TN, FP]
 [FN, TP] ]

The result will be:

[[28  0]
 [ 2 15]]

Hence,

TP = 15
TN = 28
FP = 0
FN = 2

The rows of the matrix are the True classes; the columns are the Predicted classes. Hence there are 15 + 28 instances where the model classfied correctly (15 True predicted as True, or also known as the True Positive; 28 Negative as Negative, or True Negative).

From the confusion matrix, the True Positive Rate, or Precision, the proportion of positives predicted by the model that are truly positive, can be determined.
\[precision = \frac{TP}{TP + FP}\]

Another useful metrics is the proportion of positive instances that are correctly predicted by the model. This is known as the Recall.
\[recall = \frac{TP}{TP + FN}\]

The precision and recall can be computed using the following code:
from sklearn.metrics import precision_score, recall_score 
print('Precision Score = {:0.3f}'.format(precision_score(y_test, pred))) 
print('Recall Score = {:0.3f}'.format(recall_score(y_test, pred)))

The result:

Precision Score = 1.000 (15/15+0)
Recall Score = 0.882 (15/15+2)

If we were to calculate the accuracy, the instances that were predicted correctly, it will be 33% (15/15+28+2). This is why accuracy is never used in classification tasks.

Here's the graphical representation of the things were carried out.

This marks the end of the Logistic Regression example. The logistic regression learning algorithm is one of the important learning algorithm. It is also frequently used as a building block to understand Artificial Neural Networks, or Neural Nets for short, which I hope to touch on soon.

I hope this has been useful in providing a bigger picture to the Logistic Regression learning algorithm. I have deliberately left out the mathematics behind the learning algortihm because there are many resources that can do a better job than I. By the way, the reference quoted below is awesome!


~ZF



References:
[1] Hands-On Machine Learning with Scikit-Learn and Tensor Flow, Aurelien Geron, O'reilly

01 January 2018

[Data Science] Machine Learning - Part 2, Essentials of Machine Learning

In my previous post, I briefly described what machine learning is. I shall attempt to dwell into more (but enough) details in this posts.

As mentioned, machine learning is about deploying algorithms on a computer to apply statistical methods in data analytics. An algorithm is a set of instructions to perform a specific tasks, so a machine learning algorithm is a set of instruction for the computer/programme to learn patterns from a set of given data.

Types of Machine Learning Algorithms

There are many machine learning algorithms. The common ones are:

  1. Linear Regression
  2. Logistic Regression
  3. Nearest Neighbours
  4. Support Vector Machines
  5. Decision Trees (and Random Forests)
  6. Neural Networks
  7. Clustering
  8. Dimensionality Reduction (e.g. Principle Component Analysis, or PCA)

Supervised and Unsupervised Learning Algorithms

Machine learning algorithms are categorized as supervised learning or unsupervised learning - basically it is data with a known outcome, or labels, for the former (supervised learning) and data without labels for the latter (unsupervised learning).

The outcome of supervised learning is to find a general pattern of the data that validates the labels. Items 1-6 in the list above are supervised learning algorithms.

The outcome of unsupervised learning is to find and group general characteristics within the data. Items 7-8 are unsupervised learning algorithms.


Data Types and Coresponding Objectives

Data can be quantitative (numbers, or measurables on a standard scale), or qualitative (description, or non-measureables because not on a standard scale). Qualitative data is also known as categorical data.

Quantitative data is generally used for projection. For example, what would be next year's sales of a shopping mall be like, given the number of visitors to the shopping mall this year?

Qualitative data is generally used for classification, which can be broken down into two tasks: object verification (if a cat is a cat), and object identification (if an object is a cat).

In practice, the two types of data co-exists. It is unlikely to have a pure data of either form. Thus extra care has to be made to handle data because categorical data is usually assigned a number to represent groups or level, and usually this might give algorithms a false sense of scale if adequate consideration is not taken.


Hypothesis Function

Each machine learning algorithm has an assumption about the data, perhaps except neural networks. This assumptions are normally described as a function, called the hypothesis function. For example, the linear regression assumes linear relationship between the data and the label that we are interested in.

For example, the hypothesis function for linear equation is:
\[\hat{Y} = mX + c\]

By convention, $\hat{Y}$ is the approximation (to the real label, $Y$). It is related to the input data $X$ by $m$ and $c$, the parameters of the model; that is, a different set of $m$ and $c$ yields different value of $\hat{Y}$.

Cost Function

The desired $\hat{Y}$ is one that it is closest to $Y$. This means that $Y - \hat{Y}$ is minimum. Any deviation of $\hat{Y}$ from $Y$ is akin to a costs, hence $Y - \hat{Y}$ is the cost function of the algorithm. However, the mean square error is commonly used as the cost function:
\[\min \frac{1}{m}\sqrt{\sum (Y-\hat{Y})^{2}}\]


Optimization

This can be done by finding the optimal set of $m$ and $c$ to satisfy the above condition. We could have also taken the derivative $\frac{dY}{dX}$  and set it equals to 0 to find the minima or the maxima. However, this is not possible when the dimensions of $X$ increases, meaning there are $X_1, X_2, X_3, ..., X_n$ to be considered, in the case of Multivariate Regression. Fortunately there are optimization algorithms to help us find the optimal set of $m$ and $c$, the most common being Gradient Descent.


An Example

For the set of data given below (Figure 1), we can fit a line (in red) that generalizes the pattern (Figure 2).
Figure 1

Figure 2

We can calculate the gradient $m$ and intercept $c$ by hand in this case to find the equation of the line that generalizes the pattern of the data. Or we can use Python and Scikit-Learn to do so. Here's the code:

import numpy as np
import pandas as pd
import sklearn
from sklearn.linear_model import LinearRegression
lm = LinearRegression()
lm.fit(x, y)
print('The coefficient, m = {}'.format(lm.coef_))
print('The intercept, c = {}'.format(lm.intercept_))

This has the following outputs: $m = 2.03364$ and $c = 2.30736$

Graphically,
Data and the fitted Linear Regression model.


As I have showned, I first deployed the statistical approach - fitting a straight line to a model, and then the computer science approach - run a code to perform Linear Regression. I could have computed the gradient ($m$) and intercept ($c$) manually, using derivatives and all but coding negate the hassle - all in 3 lines of code.

I hope this give some idea about machine learning and also the merits of learning to code. Data analytics gets complicated when the data becomes massive - with multi-dimensions and examples.

Till then~

~ZF