<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: shady shafik</title>
    <description>The latest articles on DEV Community by shady shafik (@shadyshafik).</description>
    <link>https://dev.to/shadyshafik</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F556232%2F63bd49ea-b9bc-4b26-998a-0304a06da9e6.jpg</url>
      <title>DEV Community: shady shafik</title>
      <link>https://dev.to/shadyshafik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shadyshafik"/>
    <language>en</language>
    <item>
      <title>How to Backtest a Trading Bot with BackTrader.</title>
      <dc:creator>shady shafik</dc:creator>
      <pubDate>Mon, 21 Mar 2022 19:15:35 +0000</pubDate>
      <link>https://dev.to/shadyshafik/how-to-backtest-a-trading-bot-with-backtrader-291i</link>
      <guid>https://dev.to/shadyshafik/how-to-backtest-a-trading-bot-with-backtrader-291i</guid>
      <description>&lt;p&gt;Hello there and welcome to another article, today I will get into one of the most important topics in algorithmic trading which is backtesting.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Backtesting&lt;/strong&gt;  is the process of testing your strategy on historical data to get a clear vision about how this strategy would performed and how it will perform in the live trading.&lt;/p&gt;

&lt;p&gt;also, backtesting doesn’t guarantee that the strategy or the trading bot will perform as it performed on historical data for many reasons….&lt;/p&gt;

&lt;p&gt;but still backtesting is a precious tool every algorithmic trader and traders in general need as it can transform the performance by manipulating and testing new strategies.&lt;/p&gt;

&lt;p&gt;In this article we will develop a simple strategy and backtest it using backtrader library and I’ll use jupyter notebook.&lt;/p&gt;




&lt;h2&gt;
  
  
  let’s jump into it.
&lt;/h2&gt;

&lt;p&gt;Import the backtrader library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import backtrader as bt

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Download Bitcoin data from yahoo finance&lt;br&gt;
starting from 2018 we’ll get the daily time frame&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import yfinance as yf
data = yf.download("BTC-USD",start='2018-01-01')
data.head()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjx3ugipr0cbdacuamh3l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjx3ugipr0cbdacuamh3l.png" alt="BTC data "&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Now it’s time to think about the Strategy.
&lt;/h3&gt;

&lt;p&gt;I will develop a Buy the dip strategy which is :&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If prices drop three days in a row we buy&lt;br&gt;
and sell after 2 days.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;create dipStrategy class and pass strategy(a built in object inside backtrader.)&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class dipStrategy(bt.Strategy):
    def log(self, txt, dt=None):

        dt = dt or self.datas[0].datetime.date(0)
        print('%s, %s' % (dt.isoformat(), txt))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;backtrader log function *** datas[0] is the current data row.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def __init__(self):
 self.dataclose = self.datas[0].close
 self.order = None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep a reference to the “close” line in the data[0] dataseries in dataclose&lt;br&gt;
set order to None as there is no order yet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def notify_order(self, order):
       if order.status in [order.Submitted, order.Accepted]:
            return
        if order.status in [order.Completed]:
            if order.isbuy():
                self.log('BUY EXECUTED,%.2f' % order.executed.price)

            elif order.issell():
                self.log('SELL EXECUTED,%.2f' %order.executed.price)

            self.bar_executed = len(self)

        elif order.status in [order.Canceled,
                                       order.Margin,order.Rejected]:
            self.log('Order Canceled/Margin/Rejected')
        self.order = None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;check order status if in submitted or accepted then return.&lt;br&gt;
*** not completed yet but accepted by the broker.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;len(self)&lt;/strong&gt; will return day number&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ex:&lt;/strong&gt; if we bought at day 200 we sould keep track of that number as we’ll sell after 2 days at day 202&lt;/p&gt;

&lt;p&gt;we save the order day in &lt;strong&gt;bar_executed&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Check if an order has been completed&lt;/p&gt;

&lt;p&gt;if buy order completed log the price  &lt;strong&gt;isbuy()&lt;/strong&gt; built in function in backtrader&lt;br&gt;
if order in status in canceled… log this&lt;br&gt;
if sell order completed log the price&lt;br&gt;
set order to None as there is no pending order&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def next(self):

        self.log('Close, %.2f' % self.dataclose[0])
        if self.order:
            return


        if not self.position :

            if self.dataclose[0] &amp;lt; self.dataclose[-1]:

                if self.dataclose[-1] &amp;lt; self.dataclose[-2]:

                    if self.dataclose[-2] &amp;lt; self.dataclose[-3]:

                        self.log('BUY CREATE, %.2f' % 
                                                 self.dataclose[0])
                        self.buy()

                        self.order = self.buy()


        else:

            if len(self) &amp;gt;= self.bar_executed + 2 :
                self.log('SELL CREATE, %.2f' % self.dataclose[0])
                self.order = self.sell()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Simply log the closing price of the series from the reference&lt;br&gt;
Check if there is pending order if yes, &lt;strong&gt;return&lt;/strong&gt;&lt;br&gt;
check if we have position in the market.&lt;/p&gt;

&lt;p&gt;we’ll check if the price drops three days in a row&lt;br&gt;
if today close less than yesterday till three previous days&lt;/p&gt;

&lt;p&gt;*** [-1] is the preious day index and so on till [-3]&lt;/p&gt;

&lt;p&gt;Now it’s time to buy as our conditions met log price&lt;br&gt;
set buy order&lt;/p&gt;

&lt;p&gt;set order to the buy order to keep track and &lt;strong&gt;avoid sending second order&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;self.buy()&lt;/strong&gt; return buy order object&lt;/p&gt;


&lt;h3&gt;
  
  
  Sell conditon
&lt;/h3&gt;

&lt;p&gt;after 2 days we sell our position&lt;br&gt;
and we keep track by bar_executed variable&lt;br&gt;
we’ve finished coding our strategy&lt;br&gt;
Now it’s show time, let’s execute.&lt;/p&gt;
&lt;h3&gt;
  
  
  Backtesting
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cerebro = bt.Cerebro()

cerebro.broker.setcash(100000.0)

cerebro.addsizer(bt.sizers.SizerFix, stake=2)
df = bt.feeds.PandasData(dataname=data)

cerebro.adddata(df)

cerebro.addstrategy(dipStrategy)

print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())

cerebro.run()

print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;cerebro is backtrader engine we’ll create an instance&lt;/p&gt;

&lt;p&gt;set how much cash in your account is set it to 100k&lt;/p&gt;

&lt;p&gt;set size of your order ** how much you’ll buy i set it to 2 shares (bitcoins)&lt;/p&gt;

&lt;p&gt;data feed in our case we have pandas data dataframe&lt;/p&gt;

&lt;p&gt;pass data feed to cerebro with &lt;strong&gt;adddata()&lt;/strong&gt; method&lt;br&gt;
add our strategy to cerebro with &lt;strong&gt;addstrategy()&lt;/strong&gt; method&lt;/p&gt;

&lt;p&gt;print account cash in start&lt;/p&gt;

&lt;p&gt;run the engine start trade&lt;/p&gt;

&lt;p&gt;print cashe at the end&lt;/p&gt;


&lt;h3&gt;
  
  
  Results
&lt;/h3&gt;

&lt;p&gt;we get along list with all trader executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8kpzhdlokjjj03l7748d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8kpzhdlokjjj03l7748d.png" alt="Backtesting Results"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We started with 100k and at the end we have 141k 🔥 👌 that’s not bad isn’t it…😁&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;note: of course this strategy underperformed the market but the aim is to come up with your strategy, optimize and manipulate till your reach a point where you have a strategy that can outperform the market with consistent monitoring, optimization, and idea generation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about visualizing our trades&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cerebro.plot()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fos8wmeqyqyvy5d05tue5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fos8wmeqyqyvy5d05tue5.png" alt="Backtesting plot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  what’s next
&lt;/h3&gt;

&lt;p&gt;I think backtrader is a great tool for traders to test and manipulate their strategies&lt;/p&gt;

&lt;p&gt;In my previous article we developed a &lt;a href="https://dev.to/shadyshafik/algorithmic-trading-how-to-build-a-trading-bot-with-python-and-sqlite-4h55"&gt;crypto trading bot&lt;/a&gt; but without the backteting part, and today we did.&lt;/p&gt;

&lt;p&gt;That’s it for this article I hope you enjoy.&lt;/p&gt;

</description>
      <category>python</category>
      <category>datascience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to build a Trading bot with Python and SQlite.</title>
      <dc:creator>shady shafik</dc:creator>
      <pubDate>Sat, 26 Feb 2022 18:14:58 +0000</pubDate>
      <link>https://dev.to/shadyshafik/algorithmic-trading-how-to-build-a-trading-bot-with-python-and-sqlite-4h55</link>
      <guid>https://dev.to/shadyshafik/algorithmic-trading-how-to-build-a-trading-bot-with-python-and-sqlite-4h55</guid>
      <description>&lt;p&gt;Hello there and welcome to another article about algorithmic trading with python.&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;&lt;a href="https://dev.to/shadyshafik/algorithmic-trading-a-beginners-guide-series-26km"&gt; previous article&lt;/a&gt;&lt;/strong&gt; I gave a light introduction to algorithmic trading, and we developed a simple trading bot using Binance API to get data and execute trades.&lt;/p&gt;

&lt;p&gt;Today we will develop another trading bot, with the use of SQlite to save data and process this data to execute trades.&lt;/p&gt;




&lt;h2&gt;
  
  
  We Start By connecting with Binance and Socket Manager
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhmzmftzqy827bhq2fvjm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhmzmftzqy827bhq2fvjm.jpg" alt="Websocket connection"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A WebSocket is a communication protocol which is a bidirectional connection full duplex communication channel between a client's web browser and a server.&lt;/p&gt;

&lt;p&gt;I am working with Jupyter notebook, python and sqlite.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pandas as pd
!pip install python-binance
from binance import Client
from binance import BinanceSocketManager
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Import pandas, install python binance, import client and socket manager.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;APIKEY= 'Your API Key'
SECRETKEY = 'Your Secret Key'

client = Client(APIKEY,SECRETKEY)
client.API_URL = 'https://testnet.binance.vision/api'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;get an API KEY and SECRET KEY, provide the two to the client function to establish a connection.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;client.API_URL = *&lt;/em&gt;, here we provide the Binance Testnet url to work with test net environment. &lt;/p&gt;

&lt;p&gt;to generate an API key and secret key go to Binance Spot Test Network sign with GitHub and generate HMAC SHA256 key.&lt;br&gt;
finally, replace APIKEY and SECRETKEY variables in the code with actual keys.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sqlalchemy import create_engine
engine = create_engine('sqlite:///SqlDb.db')

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;import create_engine from &lt;strong&gt;sqlalchemy&lt;/strong&gt; ( Python SQL toolkit and Object Relational Mapper ), then define new database &lt;strong&gt;sqlDB.db&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Data with Binance socket manager
&lt;/h2&gt;

&lt;p&gt;I'll start by testing binance socket manager connection by print the received data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; bsm = BinanceSocketManager(client)
 ts = bsm.trade_socket('BTCUSDT')
 await ts.__aenter__()
 msg = await ts.recv()
 print(msg)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;{'e': 'trade', 'E': 1645890024494, 's': 'BTCUSDT', 't': 1271100538, 'p': '39317.92000000', 'q': '0.00051000', 'b': 9560827435, 'a': 9560827538, 'T': 1645890024493, 'm': True, 'M': True}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we can see what we'll receive, it's a dictionary with &lt;strong&gt;'s'&lt;/strong&gt; as &lt;br&gt;
a currency symbol, &lt;strong&gt;'p'&lt;/strong&gt; as price and &lt;strong&gt;'E'&lt;/strong&gt; as time ( in unix format), these three are what we care about.&lt;/p&gt;

&lt;p&gt;Next step is getting a stream of real time data, then send these data to a function in order to produce a dataFrame, and we'll start with the latter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def dframe(msg):

    df = pd.DataFrame([msg])
    df = df.loc[:,['s','E','p']]    
    df.columns = ['Symbol','Time','Price']
    df.Price = df.Price.astype(float)
    df.set_index('Time')

    return df

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I defined a function wich will take the received dictionary, convert it to pandas dataframe slice the Symbol,Time and price.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while True:
   bsm = BinanceSocketManager(client)
   ts = bsm.trade_socket('BTCUSDT')
   await ts.__aenter__()
   msg = await ts.recv()
   frame = dframe(msg)
   frame.to_sql('BTCUSDT',engine,if_exists='append',index=False)
   print(frame)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in order to get a stream of data, I'll request data in an endless loop, after getting the &lt;strong&gt;msg&lt;/strong&gt; dictionary it will be sent to *&lt;em&gt;dframe *&lt;/em&gt; function to update the data frame.&lt;/p&gt;

&lt;p&gt;Then to_sql() Write records stored in a DataFrame to a SQL database.&lt;/p&gt;

&lt;p&gt;and that's this is the output of the endless loop, a dataframe updated every second with recieved data, and write records to BTCUSDT table in SQl database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Symbol           Time     Price
0  BTCUSDT  1645891574400  39268.11
    Symbol           Time     Price
0  BTCUSDT  1645891576796  39268.89
    Symbol           Time     Price
0  BTCUSDT  1645891578340  39262.33
    Symbol           Time     Price
0  BTCUSDT  1645891579856  39262.33
    Symbol           Time     Price
0  BTCUSDT  1645891581334  39262.32
    Symbol           Time    Price
0  BTCUSDT  1645891582679  39250.4
    Symbol           Time    Price
0  BTCUSDT  1645891584140  39250.4
    Symbol           Time     Price
0  BTCUSDT  1645891585365  39250.39
    Symbol           Time     Price
0  BTCUSDT  1645891586707  39258.25
    Symbol           Time    Price
0  BTCUSDT  1645891587986  39262.4
    Symbol           Time    Price
0  BTCUSDT  1645891589897  39262.4
    Symbol           Time     Price
0  BTCUSDT  1645891591257  39262.39
    Symbol           Time     Price
0  BTCUSDT  1645891593257  39253.68
    Symbol           Time     Price
0  BTCUSDT  1645891594699  39253.68
    Symbol           Time     Price
0  BTCUSDT  1645891596403  39253.68
    Symbol           Time     Price
0  BTCUSDT  1645891597752  39253.68
    Symbol           Time     Price
0  BTCUSDT  1645891599107  39253.69
    Symbol           Time     Price
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;let's make a plot to visualize btc price.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import matplotlib.pyplot as plt

df = pd.read_sql('BTCUSDT',engine)

plt.figure(figsize=(15, 8))
plt.plot(df.Price,color='red',label = "Price", linewidth = 2, marker='*', markerfacecolor='black',markersize=9)

plt.title('BTC Price', color='black',size=20)
plt.ylabel('Price', color='blue',size=16)
plt.xlabel('no of records', color='blue',size=16)
plt.legend()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F56kp52h573gl2fiybsfj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F56kp52h573gl2fiybsfj.png" alt="BTC Price plot "&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Trend Following Strategy
&lt;/h2&gt;

&lt;p&gt;I'll define a simple enough trend following strategy, which is &lt;strong&gt;Buy&lt;/strong&gt; if the price increased with a percentage(0.1%), and &lt;strong&gt;Sell&lt;/strong&gt; if certain percentage up or down (+0.2% , -0.2%) is reached.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def strategy(in_position=False):
    percen = 0.001
    periods = 100


    while True:
        df = pd.read_sql('BTCUSDT', engine)
        df1 = df.tail(periods)
        cumret = (df1.Price.pct_change() + 1).cumprod() - 1
        if not in_position:
            if cumret[cumret.last_valid_index()] &amp;gt; percen:
                buyOrder = client.create_order(symbol='BTCUSDT', 
                                              side='BUY', 
                                              type='MARKET' ,
                                              quantity=0.001)
                print(buyOrder)
                in_position = True
                break
  if in_position:
        while True:
            df = pd.read_sql('BTCUSDT', engine)
            orderPrice = df.loc[df.Time &amp;gt; buyOrder['transactTime']]


            if len(orderPrice) &amp;gt; 1 :
                postionRet = (orderPrice.Price.pct_change() + 1).cumprod() - 1
                lastRet = postionRet[postionRet.last_valid_index()] 

                if lastRet &amp;gt; 0.002 or lastRet &amp;lt; -0.002:
                    sellOrder = client.create_order(symbol='BTCUSDT' ,
                                                   side='SELL' ,
                                                   type='MARKET' ,
                                                   quantity=0.001 ) 

                    print(sellOrder)            
                    break
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;start with calculating the last no of records to calculate the cumulative return, and when the cumulative return exceeds certain percentage then Buy order will be triggered, then break the while loop.&lt;/p&gt;

&lt;p&gt;The buy order will return a dictionary with transaction Time so, we'll slice the dataframe from this time and wait till a cumulative return of the sliced dataframe exceeds or go below sell percentage, then a sell order will be triggered.   &lt;/p&gt;




&lt;h2&gt;
  
  
  Time To Execute a Trade...
&lt;/h2&gt;

&lt;p&gt;it's time to call the strategy function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;strategy()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;we get a Buy order filled at price 39166.91&lt;/p&gt;

&lt;h3&gt;
  
  
  Buy order
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;{'symbol': 'BTCUSDT', 'orderId': 10940581, 'orderListId': -1, 'clientOrderId': 'XCAVbax0xaQxRs0erpds5P', 'transactTime': 1645895549187, 'price': '0.00000000', 'origQty': '0.00100000', 'executedQty': '0.00100000', 'cummulativeQuoteQty': '39.16691000', 'status': 'FILLED', 'timeInForce': 'GTC', 'type': 'MARKET', 'side': 'BUY', 'fills': [{'price': '39166.91000000', 'qty': '0.00100000', 'commission': '0.00000000', 'commissionAsset': 'BTC', 'tradeId': 3562682}]}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;we get a Sell order filled at price 39097.37&lt;/p&gt;

&lt;h3&gt;
  
  
  Sell order
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;{'symbol': 'BTCUSDT', 'orderId': 10945890, 'orderListId': -1, 'clientOrderId': 'txnRsAuc1gcMs3aRiQ6YGc', 'transactTime': 1645896773538, 'price': '0.00000000', 'origQty': '0.00100000', 'executedQty': '0.00100000', 'cummulativeQuoteQty': '39.09737000', 'status': 'FILLED', 'timeInForce': 'GTC', 'type': 'MARKET', 'side': 'SELL', 'fills': [{'price': '39097.37000000', 'qty': '0.00100000', 'commission': '0.00000000', 'commissionAsset': 'USDT', 'tradeId': 3564175}]}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We have a trade with about 0.18 loss, the strategy is working and of course if you know about trading you won't be surprised about the loss because you can't win each time you just try to have a good winning ratio and the overall will result a profit.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next ?!
&lt;/h2&gt;

&lt;p&gt;You can optimize this bot by changing the parameters or manipulate the strategy which will make it better.&lt;/p&gt;

&lt;p&gt;I will share more in the upcoming articles&lt;/p&gt;

&lt;p&gt;and that's it for this article, I hope you the best.&lt;/p&gt;

</description>
      <category>python</category>
      <category>cryptocurrencies</category>
      <category>sql</category>
      <category>datascience</category>
    </item>
    <item>
      <title>Algorithmic Trading: A Beginners Guide series.</title>
      <dc:creator>shady shafik</dc:creator>
      <pubDate>Sun, 13 Feb 2022 18:31:52 +0000</pubDate>
      <link>https://dev.to/shadyshafik/algorithmic-trading-a-beginners-guide-series-26km</link>
      <guid>https://dev.to/shadyshafik/algorithmic-trading-a-beginners-guide-series-26km</guid>
      <description>&lt;h2&gt;
  
  
  What is algorithmic Trading ?!
&lt;/h2&gt;

&lt;p&gt;In simple words, algorithmic trading is the use of programs to make systematic trades based on the strategy implemented by the programming language.&lt;br&gt;
algorithmic trading can be fully-automated, semi-automated, or give signals to be executed manually.&lt;/p&gt;



&lt;p&gt;In this article, we will develop a simple semi-automated strategy using Binance API to get data and execute trades.&lt;br&gt;
I am working with Jupiter notebook, using python, pandas, and NumPy&lt;/p&gt;

&lt;p&gt;Starting with importing the libraries needed install python-binance to connect to Binance API.&lt;/p&gt;

&lt;p&gt;I am using &lt;strong&gt;binance testnet API&lt;/strong&gt; to make anyone able to go and execute the code even if you don’t have binance account.&lt;/p&gt;



&lt;p&gt;first, you have to have an API KEY and SECRET KEY to provide to the client function to establish a connection&lt;br&gt;
to generate an API key and secret key go to Binance Spot Test Network sign with GitHub and generate HMAC SHA256 key.&lt;br&gt;
finally, replace APIKEY and SECRETKEY variables in the code with actual keys.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import pandas as pd
import numpy as np
!pip install python-binance
from binance import Client

APIKEY = 'Your API KEY'
SECRETKEY = 'Your Secret KEY'
client =Client(APIKEY,SECRETKEY)
client.API_URL = 'https://testnet.binance.vision/api'
acc = client.get_account()
acc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;after establishing connection let’s see our TEST account balance Info with client.get_account() function, and here is the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
---


{'makerCommission': 0,
 'takerCommission': 0,
 'buyerCommission': 0,
 'sellerCommission': 0,
 'canTrade': True,
 'canWithdraw': False,
 'canDeposit': False,
 'updateTime': 1644598822834,
 'accountType': 'SPOT',
 'balances': [{'asset': 'BNB',
   'free': '1000.00000000',
   'locked': '0.00000000'},
  {'asset': 'BTC', 'free': '1.01300000', 'locked': '0.00000000'},
  {'asset': 'BUSD', 'free': '10000.00000000', 'locked': '0.00000000'},
  {'asset': 'ETH', 'free': '100.00000000', 'locked': '0.00000000'},
  {'asset': 'LTC', 'free': '500.00000000', 'locked': '0.00000000'},
  {'asset': 'TRX', 'free': '500000.00000000', 'locked': '0.00000000'},
  {'asset': 'USDT', 'free': '9449.55739000', 'locked': '0.00000000'},
  {'asset': 'XRP', 'free': '50000.00000000', 'locked': '0.00000000'}],
 'permissions': ['SPOT']}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Now it’s time to get crypto data stream&lt;/p&gt;

&lt;p&gt;we have more than one option about how to get data from Binance, in this article I want to make it simple enough so I am using&lt;/p&gt;

&lt;p&gt;client.get_historical_klines() function&lt;/p&gt;

&lt;p&gt;Parameters provided to this function are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;symbol&lt;/strong&gt;: which is the cryptocurrency symbol you want to trade in our case its BTCUSDT&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time Frame&lt;/strong&gt;: time frame which you’ll trade in our case I chose a 1minute time frame.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Period&lt;/strong&gt;: How many past periods to get in our case I chose 20 minutes.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
def dataorg(symbol):
    data = pd.DataFrame(client.get_historical_klines( symbol ,
                                                      '1m',                                               
                                            '20 min ago UTC'))
    df = data.iloc[:,:6]
    df.columns = ['Time', 'Open', 'High' , 'Low',
                                         'Close','Volume']
    df = df.set_index('Time')
    df.index = pd.to_datetime(df.index,unit='ms')
    df = df.astype(float)
    return df


dataorg('BTCUSDT')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;I defined a function that takes a symbol argument to organize data,&lt;br&gt;
and make a data frame out of it &lt;/p&gt;

&lt;p&gt;here is the resulting data frame containing:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time , Open Price , Low Price , Close Price and Volume.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ksupm0uznbjvwngw5dy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ksupm0uznbjvwngw5dy.png" alt="dataframe"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;Here we want to calculate the cumulative return of the last 20 minutes to start work on the trading strategy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cumulative return formula&lt;/strong&gt;: (1+ 1min return)*(1+cumlative return of last min)-1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;pct_change()&lt;/strong&gt;: calcluate the change in price in the last period.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;cumprod()&lt;/strong&gt;: calculate the second half of the formula.&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test = dataorg('BTCUSDT')
ret = (test.Open.pct_change() + 1).cumprod() -1
ret
Time
2022-02-11 16:52:00         NaN
2022-02-11 16:53:00    0.000279
2022-02-11 16:54:00    0.000974
2022-02-11 16:55:00    0.001551
2022-02-11 16:56:00    0.001645
2022-02-11 16:57:00    0.002303
2022-02-11 16:58:00    0.002782
2022-02-11 16:59:00    0.002060
2022-02-11 17:00:00    0.001818
2022-02-11 17:01:00    0.000858
2022-02-11 17:02:00    0.001216
2022-02-11 17:03:00    0.002534
2022-02-11 17:04:00    0.002666
2022-02-11 17:05:00    0.003242
2022-02-11 17:06:00    0.002548
2022-02-11 17:07:00    0.001416
2022-02-11 17:08:00    0.002460
2022-02-11 17:09:00    0.002109
2022-02-11 17:10:00    0.002550
2022-02-11 17:11:00    0.003376
Name: Open, dtype: float64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Strategy implementation
&lt;/h2&gt;

&lt;p&gt;We will develop a trend following strategy which as simple as:&lt;br&gt;
&lt;strong&gt;If&lt;/strong&gt; the price of Bitcoin increases by more than a certain percentage &lt;strong&gt;(0.2%)&lt;/strong&gt; &lt;strong&gt;then BUY&lt;/strong&gt; order and after buy &lt;strong&gt;If&lt;/strong&gt; price increased or decreased by certain percentage &lt;strong&gt;( 0.2% , -0.2%)&lt;/strong&gt; then SELL.&lt;/p&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ey0i87xypz961ampgo9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ey0i87xypz961ampgo9.png" alt="BTC chart"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def strategy(symbol,qty,inPosition=False):
    while True:
        df = dataorg(symbol)
        cumret = (df.Open.pct_change()+1).cumprod()-1
        if not inPosition:
               if cumret[-1] &amp;gt; 0.002:
                    ordertime = df.index[-1]
                    order = client.create_order(symbol=symbol,  
                    side='BUY',type='MARKET', quantity=qty)

                    print(order)
                    inPosition=True
                    break
                else:
                    print('No trade excuted')
     if inPosition:
        while True:
            df=dataorg(symbol)
            afterbuy = df.loc[df.index &amp;gt; pd.to_datetime(
            order['transactTime'],
            unit='ms')]

            if len(afterbuy) &amp;gt; 0:
                afterbuyret =  (df.Open.pct_change()+1).cumprod()-1                    
                print(afterbuyret)
                if afterbuyret[-1]&amp;gt;0.002 or afterbuyret[-1]&amp;lt; -0.002:

                    sellorder = client.create_order(symbol=symbol,
                                        side='SELL',type='MARKET',
                                                 quantity=qty)
                    print(sellorder)
                    break
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;&lt;strong&gt;it’s time to see the prices in the last 20 mins in a plot.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test.Open.plot()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwpx7hgi0mzqh2h5g9iao.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwpx7hgi0mzqh2h5g9iao.png" alt="BTC Price plot "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It seems like we have an uptrend .&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Trade execution
&lt;/h2&gt;

&lt;p&gt;Our final step is to call &lt;strong&gt;strategy()&lt;/strong&gt; function and wait for a trade to be filled,&lt;/p&gt;

&lt;p&gt;In this case we have a buy order filled then list of cumulative return after a trade and when the return exceed our threshold  &lt;strong&gt;(+0.2% or -0.2%)&lt;/strong&gt; then a sell trade been filled&lt;br&gt;
and it’s a profitable trade 😀&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;strategy('BTCUSDT', 0.001)
{'symbol': 'BTCUSDT', 'orderId': 4982968, 'orderListId': -1, 'clientOrderId': 'oappYtNU1466jYiXCV3URd', 'transactTime': 1644599469117, 'price': '0.00000000', 'origQty': '0.00100000', 'executedQty': '0.00100000', 'cummulativeQuoteQty': '43.65218000', 'status': 'FILLED', 'timeInForce': 'GTC', 'type': 'MARKET', 'side': 'BUY', 'fills': [{'price': '43652.18000000', 'qty': '0.00100000', 'commission': '0.00000000', 'commissionAsset': 'BTC', 'tradeId': 1240296}]}
Time
2022-02-11 16:52:00         NaN
2022-02-11 16:53:00    0.000279
2022-02-11 16:54:00    0.000974
2022-02-11 16:55:00    0.001551
2022-02-11 16:56:00    0.001645
2022-02-11 16:57:00    0.002303
2022-02-11 16:58:00    0.002782
2022-02-11 16:59:00    0.002060
2022-02-11 17:00:00    0.001818
2022-02-11 17:01:00    0.000858
2022-02-11 17:02:00    0.001216
2022-02-11 17:03:00    0.002534
2022-02-11 17:04:00    0.002666
2022-02-11 17:05:00    0.003242
2022-02-11 17:06:00    0.002548
2022-02-11 17:07:00    0.001416
2022-02-11 17:08:00    0.002460
2022-02-11 17:09:00    0.002109
2022-02-11 17:10:00    0.002550
2022-02-11 17:11:00    0.003376
2022-02-11 17:12:00    0.003520
Name: Open, dtype: float64

{'symbol': 'BTCUSDT', 'orderId': 4983220, 'orderListId': -1, 'clientOrderId': 'OWLlWGmI044UlKEFaURffy', 'transactTime': 1644599521374, 'price': '0.00000000', 'origQty': '0.00100000', 'executedQty': '0.00100000', 'cummulativeQuoteQty': '43.65542000', 'status': 'FILLED', 'timeInForce': 'GTC', 'type': 'MARKET', 'side': 'SELL', 'fills': [{'price': '43655.42000000', 'qty': '0.00100000', 'commission': '0.00000000', 'commissionAsset': 'USDT', 'tradeId': 1240505}]}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;what's next&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ok guys, that's it for this article of course this isn't the best trading bot to make profits and it lacks a lot,&lt;br&gt;
especially data anlysis and backtesting ,&lt;br&gt;
I'll provide more insights in my upcoming article,&lt;br&gt;
I hope you enjoy.&lt;/p&gt;

</description>
      <category>algorithmic</category>
      <category>trading</category>
      <category>python</category>
      <category>datascience</category>
    </item>
    <item>
      <title>Machine learning: Cost function and Gradient Descent For Linear Regression.</title>
      <dc:creator>shady shafik</dc:creator>
      <pubDate>Tue, 02 Nov 2021 11:06:40 +0000</pubDate>
      <link>https://dev.to/shadyshafik/machine-learning-cost-function-and-gradient-descent-for-linear-regression-1kg4</link>
      <guid>https://dev.to/shadyshafik/machine-learning-cost-function-and-gradient-descent-for-linear-regression-1kg4</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Gradient descent is one of the most popular optimization algorithms in machine learning and deep learning,&lt;/p&gt;

&lt;p&gt;it used to find the local minimum of a differentiable function.&lt;/p&gt;

&lt;p&gt;In machine learning, gradient descent is used to find the values of parameters that minimize the cost function (loss function).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“A gradient measures how much the output of a function changes if you change the inputs a little bit.” — Lex Fridman&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;In this article, we’ll go through&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What is gradient in the first place ?!&lt;/p&gt;

&lt;p&gt;hypothesis function(st-line equation).&lt;/p&gt;

&lt;p&gt;Cost function for linear regression.&lt;/p&gt;

&lt;p&gt;Gradient descent for linear regression.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/shadyshafik/machine-learning-from-scratch-linear-regression-step-by-step-3pgm"&gt;the previous article, we went through Linear regression, what is regression, hypothesis function, and cost function&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a gradient in the first place ?!
&lt;/h2&gt;

&lt;p&gt;In mathematics, a gradient is another name for the slope of the st-line or how steep is the st-line.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tv83wU0q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l82lvlt496rdcgqa0hid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tv83wU0q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l82lvlt496rdcgqa0hid.png" alt="Gradient (slope)" width="880" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Gradient = slope = derivative of a function&lt;/p&gt;

&lt;h2&gt;
  
  
  Hypothesis function and plotting different values of theta.
&lt;/h2&gt;

&lt;p&gt;let’s start by taking an example,&lt;br&gt;
we have 3 sample points ( 2, 2 ), ( 4, 4 ), ( 8, 8 )&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kR6arFLX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xn05hfuinwhu63alqfy5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kR6arFLX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xn05hfuinwhu63alqfy5.png" alt="plot" width="600" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the straight-line equation (Hypothesis function)&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y_FKrBDB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8zy4es4ugjiuz2i8dyg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y_FKrBDB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8zy4es4ugjiuz2i8dyg.jpg" alt="hypothesis function" width="496" height="102"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;it’s also written as: &lt;strong&gt;h(x)= y = θ1 + θ2 x&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;let’s make it simple enough and make θ1= 0.&lt;/p&gt;

&lt;p&gt;the equation will be:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;h (x) = θ2.X&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;now we want to draw a straight line h(x) = θ2 x to best fit the data&lt;/p&gt;

&lt;p&gt;then predict (y) values of training data (x,y) to see how good the line fits the data by calculating the cost function ( loss function )&lt;/p&gt;

&lt;p&gt;of course in this example, it's obvious that we want to choose θ2= 1 to best fit the line but with too many samples ( thousands or millions ) it’s not that easy&lt;/p&gt;

&lt;p&gt;we have to use Gradient descent or other algorithms to choose the best parameters,&lt;br&gt;
even if this is a very simple example but it will clear the vision about how the parameters are chosen and how gradient descent works.&lt;/p&gt;

&lt;p&gt;I’m choosing two values for θ2 ( 1.5 and 0 )&lt;br&gt;
( that’s the Gradient descent rule to choose the best parameters but we are choosing different values to understand how it goes)&lt;/p&gt;

&lt;p&gt;so now the blue line equation will be &lt;strong&gt;h(x) = 1.5 x&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;and the red line equation &lt;strong&gt;h (x) = 0x = 0&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;so the slope = tan ( θ ) = tan( 0 ) = zero degree.&lt;/p&gt;

&lt;p&gt;that will make the line lie on the x-axis as you see&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xHFLZoZm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wd7wpv6ah35qt1ldjz1f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xHFLZoZm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wd7wpv6ah35qt1ldjz1f.png" alt="plot" width="600" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and the last line we will draw of course will be with θ = 1&lt;br&gt;
the green st-line equation will be&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;h(x) = x&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;and that will make the line pass through each point of the samples. &lt;strong&gt;(which is not good by the way and called overfitting but in this case let’s assume it's a good thing)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4_42LdFf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7g62xlwf6d7of1t0bea.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4_42LdFf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7g62xlwf6d7of1t0bea.png" alt="plot" width="600" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cost function and Plotting different values for theta.&lt;br&gt;
Now we have three models to choose from the best model which makes the best predictions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;h(x) = 1.5 x&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;h(x) = 0&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;h(x) = x&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We want each equation of those to make predictions of output h(x) based on a given value of the input (x) by using our training examples&lt;/p&gt;

&lt;p&gt;Then we calculate the cost function ( model accuracy ) to choose the best model of them.&lt;/p&gt;

&lt;p&gt;We have 3 training examples of (x,y)&lt;br&gt;
(2,2 ), (4,4),(8,8)&lt;/p&gt;

&lt;p&gt;we’ll start by:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;h(x) = 1.5x&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;for &lt;strong&gt;x = 2 , h(x)=1.5*2 = 3&lt;/strong&gt;&lt;br&gt;
for &lt;strong&gt;x = 4 , h(x)=1.5*4 = 6&lt;/strong&gt;&lt;br&gt;
for &lt;strong&gt;x = 8, h(x)=1.5*8 = 12&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--55bHWkYM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3vxlynwtiegzxzllxya.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--55bHWkYM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3vxlynwtiegzxzllxya.png" alt="predicting values " width="880" height="678"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s time to calculate the cost function (model accuracy)&lt;/p&gt;

&lt;h2&gt;
  
  
  The cost function for linear regression.
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7LVuahFW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o5i2xrggv40thu6eyzcy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7LVuahFW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o5i2xrggv40thu6eyzcy.png" alt="cost function" width="880" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;where&lt;/p&gt;

&lt;p&gt;&lt;em&gt;θ: equation parameters&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;i: index&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;m: number of training examples( in our case we have 3 training examples)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;h(x): predicted value&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;y: expected value&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;we’ll calculate J(1.5 ) , J(0) , J(1)&lt;/p&gt;

&lt;p&gt;let’s substitute our predicted values of h(x)&lt;/p&gt;

&lt;p&gt;for J(1.5)&lt;br&gt;
h(2) = 3, h(4)= 6 and h(8)= 12&lt;/p&gt;

&lt;p&gt;so, &lt;strong&gt;J(1.5) = 1/2m ( (3–2 )² +(6–4)² +(12–8 )²)&lt;br&gt;
J(1.5) = 1/2*3 ( 1² + 2² + 4²) = 1/6 ( 21)&lt;br&gt;
J(1.5) = 3.5&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We will do the same for J(0)&lt;br&gt;
h(x) = 0 so, all outputs will be equal 0&lt;br&gt;
the cost will be as follow&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;J(0) = 1/2m ( (0–2 )² +(0–4)² +(0–8 )²)&lt;br&gt;
J(0) = 1/6* (84) = 14&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;the last cost will be for θ = 1&lt;br&gt;
**h(x) = x&lt;br&gt;
h(2) = 2 , h(4)= 4 and h(8)= 8&lt;br&gt;
J(1) = 1/2m ( (2–2 )² +(4–4)² +(8–8 )²)&lt;/p&gt;

&lt;p&gt;J(1) = 1/6 ( 0 )&lt;br&gt;
J(1) = Zero**&lt;/p&gt;

&lt;p&gt;which means that h(1) make perfect predictions&lt;/p&gt;

&lt;p&gt;So, we’ll choose θ= 1 for the best predictions,&lt;/p&gt;

&lt;p&gt;what we just did in the previous steps is what the Gradient descent algorithm does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gradient Descent For Linear Regression (How it works).
&lt;/h2&gt;

&lt;p&gt;Now we want to see the relationship between J(θ) and θ&lt;/p&gt;

&lt;p&gt;we have 3 points to plot &lt;strong&gt;( θ, J(θ) )&lt;/strong&gt; with J(θ) on the y-axis and θ on the x-axis&lt;/p&gt;

&lt;p&gt;Points : (0, 14) , (1, 0) and (1.5, 3.5)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eVTILSqc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2gvtm1g8tq8v83rz5z7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eVTILSqc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2gvtm1g8tq8v83rz5z7.png" alt="J(θ) and θ plot" width="880" height="651"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;the perfect choice of parameters will make J(θ) = zero.&lt;/p&gt;

&lt;p&gt;but, a perfect model with 100% accuracy working with a lot of data is rare if not impossible at least for now.&lt;/p&gt;

&lt;p&gt;gradient descent algorithm starts by initializing starting point (x,y)&lt;br&gt;
and then tries to find the &lt;strong&gt;Global minimum ( the value of θ gives minimum value to J(θ))&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;which in our graph the point &lt;strong&gt;( 1, 0 )(Global minimum)&lt;br&gt;
when θ=1, J(θ) = 0.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--evm45QID--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76ig4bj4ju1a3de5jd4p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--evm45QID--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76ig4bj4ju1a3de5jd4p.png" alt="Gradient" width="880" height="683"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But how the heck the gradient descent algorithm does that ?!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now it’s time to see the gradient descent general formula&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pGsFhpYq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fzh3ywtjq8xv95mn3kh1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pGsFhpYq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fzh3ywtjq8xv95mn3kh1.jpg" alt="gradient descent " width="452" height="136"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Where&lt;/p&gt;

&lt;p&gt;&lt;em&gt;θ : parameter ( also called weight )&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;j = 1 or j = 2 ( number of parameters θ)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;α : learning rate&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;∂ / ∂θ : partial derivative ( slope of straight line )&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;J(θ1,θ2) : cost function&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In our example &lt;strong&gt;θ1 = 0&lt;/strong&gt;, so we just have θ2 to deal with&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PSDAxftF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fl7e1emky8s8lbc8abeb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PSDAxftF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fl7e1emky8s8lbc8abeb.png" alt="gradient descent" width="373" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;gradient descent algorithm goal is to find values for equation parameters (θ2) which make J(θ2) is minimum&lt;/p&gt;

&lt;p&gt;it starts with an initial value of θ then update this value until converge ( reach the minimum cost )&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;θ2 ( new value ) = θ2 (initial value ) - α( number )* ∂/∂θ2 ( slope of the st- line )&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;gradient descent may do this step thousands of times or more with one or more parameters.&lt;/p&gt;

&lt;p&gt;now let’s see how the slope of the straight line work in this equation&lt;/p&gt;

&lt;h4&gt;
  
  
  how it converges if the slope is positive
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8QWoXx8t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oh9i5jcs08li6oo7ixz5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8QWoXx8t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oh9i5jcs08li6oo7ixz5.png" alt="gradient descent" width="880" height="835"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  how it converges if the slope is negative
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fpWKL4tc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dh5c1wxm5m1mg693as5u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fpWKL4tc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dh5c1wxm5m1mg693as5u.png" alt="gradient descent" width="880" height="835"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>datascience</category>
      <category>algorithms</category>
      <category>ai</category>
    </item>
    <item>
      <title>Machine Learning From Scratch: Linear Regression step by step.</title>
      <dc:creator>shady shafik</dc:creator>
      <pubDate>Sat, 23 Oct 2021 13:21:08 +0000</pubDate>
      <link>https://dev.to/shadyshafik/machine-learning-from-scratch-linear-regression-step-by-step-3pgm</link>
      <guid>https://dev.to/shadyshafik/machine-learning-from-scratch-linear-regression-step-by-step-3pgm</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you want to get into machine learning, I can’t think of a better way than start by learning Linear regression.&lt;br&gt;
In this article, you will get a clear vision on what is linear regression, hypothesis function, and cost function.&lt;br&gt;
Linear Regression is one of the most straightforward algorithms.&lt;br&gt;
It is easy to understand and known by all data scientists and machine learning engineers.&lt;br&gt;
also, it’s a very good starting point to learn other machine learning algorithms.&lt;br&gt;
yet, linear regression is a very useful and efficient algorithm,&lt;br&gt;
when used in the right problem with the right dataset.&lt;br&gt;
In this article, we will go step by step to understand how linear regression is working.&lt;br&gt;
So let’s not waste more time and get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a regression in the first place ?!
&lt;/h2&gt;

&lt;p&gt;In statistical modeling, regression is the process of forecasting a dependent variable (Y). based on one or more of an independent variable (X).&lt;br&gt;
in other words, a regression model is a statistical tool used to find the relation between the output (Y). and the input (X).&lt;/p&gt;

&lt;h1&gt;
  
  
  Linear Regression Algorithm.
&lt;/h1&gt;

&lt;p&gt;The linear regression model is the basic form of regression analysis,&lt;br&gt;
it estimates that the relation between the dependent and independent variables is linear.&lt;br&gt;
We know from linear algebra that the straight-line equation :&lt;br&gt;
y = mx+ b&lt;/p&gt;

&lt;p&gt;we will use this equation to predict the output variable (y) with the relation to &lt;strong&gt;x( feature), m ( the slope of a straight line ), and b ( intersection of st-line with y-axis ).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wLPEqkIo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/moe1r4t91xdkzelboh2o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wLPEqkIo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/moe1r4t91xdkzelboh2o.png" alt="straight line graph"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;equation of blue line &lt;strong&gt;y = x + 2&lt;/strong&gt;&lt;br&gt;
equation of red line &lt;strong&gt;y = 2x + 0&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  hypothesis equation
&lt;/h2&gt;

&lt;p&gt;hypothesis equation is the st-line equation used in the machine learning algorithm with optimized parameters to best fit the data.&lt;/p&gt;

&lt;p&gt;it’s a model that maps inputs to outputs and is used to make predictions.&lt;/p&gt;

&lt;p&gt;the basic idea here or in any machine learning problem is:&lt;br&gt;
feed the model with data.&lt;/p&gt;

&lt;p&gt;test the model by predicting values you already know.&lt;br&gt;
optimize the model for better predictions, and now you have a working machine learning model that can predict outputs.&lt;br&gt;
let’s change the parameters of the st-line equation &lt;strong&gt;m → θ2, b → θ1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zo5uLUOD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8jbqgdvgdb7joche08s.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zo5uLUOD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8jbqgdvgdb7joche08s.jpg" alt="hypothesis function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's give different values for theta (θ1,θ2) and see how the line changes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t61JX1nd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2555u769mjslymerqq7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t61JX1nd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2555u769mjslymerqq7.png" alt="different values for theta 1 and 2 "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we see that as θ2 changes the angle (slope) of the st-line changes and as θ1 changes the line intersection with the y-axis changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accuracy
&lt;/h2&gt;

&lt;p&gt;when our models make predictions, it’s not 100% accurate&lt;br&gt;
in classification problems where the prediction is binary ( 0 or 1 )&lt;/p&gt;

&lt;p&gt;the ratio between right predictions and wrong predictions is the model accuracy&lt;/p&gt;

&lt;p&gt;if our model predicts 7 out of 10 right predictions so, the accuracy of the model equal to 70%&lt;/p&gt;

&lt;h2&gt;
  
  
  Cost function
&lt;/h2&gt;

&lt;p&gt;Now we have a model that makes predictions by the hypothesis equation and we want to know how well are those predictions&lt;br&gt;
in simple words, the cost function tests how well the model performs,&lt;br&gt;
if the cost function is equal to zero, then the model is 100% accurate.&lt;/p&gt;

&lt;p&gt;different from accuracy cost function is not a percentage&lt;br&gt;
but, calculated by the error difference between the predicted value (ŷ) and expected value ( True value) (y).&lt;/p&gt;

&lt;h2&gt;
  
  
  Mean Squared Error
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F0p1CUTV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9hm0rf60gsq3sctjr0f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F0p1CUTV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9hm0rf60gsq3sctjr0f.png" alt="mean squared error formula"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;MSE = Mean Squared Error&lt;br&gt;
m = number of training examples ( samples ).&lt;br&gt;
i = index of sample&lt;br&gt;
ŷ = predicted value&lt;br&gt;
y = expected value&lt;/p&gt;

&lt;p&gt;the predicted value ŷ is the output of the hypothesis while the expected value y is the value of training examples.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z15xrSXA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6o2yc2fehitx38ama2q5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z15xrSXA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6o2yc2fehitx38ama2q5.png" alt="regression example."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;let’s use this dummy plot to get an intuition about how the MSE works.&lt;/p&gt;

&lt;p&gt;the orange line height is the difference between the predicted value and the expected value (blue dot ).&lt;/p&gt;

&lt;p&gt;let’s predict Y for these three values of X (3,4,6)&lt;/p&gt;

&lt;p&gt;when&lt;br&gt;
x =3 the predicted value &lt;strong&gt;ŷ = 3.9&lt;/strong&gt; while the expected value &lt;strong&gt;y = 2&lt;/strong&gt;&lt;br&gt;
x =4 the predicted value &lt;strong&gt;ŷ = 4.7&lt;/strong&gt; while the expected value &lt;strong&gt;y = 6&lt;/strong&gt;&lt;br&gt;
x =6 the predicted value &lt;strong&gt;ŷ = 5.8&lt;/strong&gt; while the expected value &lt;strong&gt;y = 4&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;now we calculate the cost with the MSE formula&lt;/p&gt;

&lt;p&gt;we just have 3 training examples &lt;strong&gt;m = 3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;MSE = 1/ 2* 3 ( (3.9–2)² + (4.7–6)² + (5.8–4)² )&lt;/p&gt;

&lt;p&gt;MSE = 1/6 ( (1.9)² + (-1.3)² + ( 1.8)² ) = 1.42&lt;/p&gt;

&lt;p&gt;of course, if we have a large dataset the cost will be much higher than that number.&lt;/p&gt;

&lt;p&gt;I hope you’ve got an intuition about how cost function works, if not then am sure that the little project in the next section will give you a better understanding of the whole process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Weight prediction based on height.
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2TJpzwC0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9xoyqpks8ufus8n1zir.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2TJpzwC0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j9xoyqpks8ufus8n1zir.png" alt="weight and height samples"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I picked 20 samples of weights and heights from a dataset so we can have a better understanding of the linear regression algorithm&lt;/p&gt;

&lt;p&gt;first, we’ll &lt;strong&gt;plot the data&lt;/strong&gt; then draw a &lt;strong&gt;regression line&lt;/strong&gt; to make our predictions and see our model working.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JhSA0zaR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kx8xpeg2okpyl85s6kgb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JhSA0zaR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kx8xpeg2okpyl85s6kgb.png" alt="weights and heights plot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Regression Line
&lt;/h2&gt;

&lt;p&gt;Now we have a plot with height (x-axis)(independent variable) and weight (y-axis)(dependent variable).&lt;/p&gt;

&lt;p&gt;it’s time to draw the regression line using the hypothesis equation&lt;/p&gt;

&lt;p&gt;y = θ1 + θ2. x&lt;/p&gt;

&lt;p&gt;But, how do we choose the θ1 and θ2 parameters?&lt;/p&gt;

&lt;p&gt;Ok, that’s where &lt;strong&gt;Gradient Descent&lt;/strong&gt; comes to play, it’s a learning algorithm used to choose the best values for parameters to best fit the data, therefore, minimize the cost function (prediction error ).&lt;/p&gt;

&lt;p&gt;we’ll talk about gradient descent in another article and for now, let’s say we have chosen the optimized parameters to draw the regression line.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PQVRnkcG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6euosyaucpvlb0mnccs4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PQVRnkcG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6euosyaucpvlb0mnccs4.png" alt="Plot with after regression line"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and here comes the interesting part, let’s make a prediction,&lt;br&gt;
I want our model to predict the weight of a 70 inches, let’s do it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bxPuw_gr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tae5hxrbscrzpqu93kvg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bxPuw_gr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tae5hxrbscrzpqu93kvg.png" alt="predict y with regression live"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;it predicted ~135 pounds for 70 inches, if we look in our dataset table you’ll find 70.1 inches with 136.4 pounds.&lt;br&gt;
so, we can say that our simple model has good accuracy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recap
&lt;/h3&gt;

&lt;p&gt;In this article, we went through the main idea behind linear regression and how it works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Regression&lt;/strong&gt; is the process of finding the relation between input and output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linear regression&lt;/strong&gt; algorithm estimates that the relation between the dependent and independent variables is linear.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The hypothesis function&lt;/strong&gt; is the straight-line equation used in the machine learning model to map the input to output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cost function&lt;/strong&gt; tests the accuracy of the hypothesis function by calculating the average difference between the predicted value and the actual values from the dataset.&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
