Sunday, September 8, 2019

Setting up ElasticSearch on EC2 - with remote connection configuration

Install Java 8
sudo yum install java-1.8.0

Remove
sudo yum remove java-1.7.0-openjdk   

Get ES
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.rpm

Use the root user
sudo su

Install ES
sudo rpm --install elasticsearch-6.2.4.rpm     

Run ES on system startup
sudo chkconfig --add elasticsearch   

Go inside the elasticsearch directory
cd /usr/share/elasticsearch/

Install the plugins
sudo bin/elasticsearch-plugin install discovery-ec2    sudo bin/elasticsearch-plugin install repository-s3

Change the jvm heap size
sudo nano /etc/elasticsearch/jvm.options
->
-Xms512m
-Xmx512m

Change the configuration to allow remote access
nano /etc/elasticsearch/elasticsearch.yml
Add this
http.host: 0.0.0.0

Run 
sudo /etc/init.d/elasticsearch restart

or 

sudo service elasticsearch start


Then finally, check

Tuesday, August 6, 2019

Working With Date Time - In Depth

A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.

Example


import datetime
x = datetime.datetime.today()
x #> datetime.datetime(2019, 8, 6, 22, 39, 30, 864393)

The output is in the following order: ‘year’, ‘month’, ‘date’, ‘hour’, ‘minute’, ‘seconds’, ‘microseconds’

Parsing a string to datetime

my_date_time = datetime.datetime.strptime('8/3/19', '%m/%d/%y')
my_date_time #> datetime.datetime(2019, 8, 3, 0, 0)

Parsing any string format to datetime

from dateutil.parser import parse
parse('94, December 26, 2010, 10:51pm') #> datetime.datetime(1994, 12, 26, 22, 51)

Formatting datetime

my_date_time = datetime.datetime.strptime('8/3/19', '%m/%d/%y')
my_date_time.strftime('%m/%d/%y') #> '08/03/19'

Adjusting datetime

my_date_time = datetime.datetime.strptime('8/3/19', '%m/%d/%y')
my_date_time - datetime.timedelta(days=2) #> datetime.datetime(2019, 8, 1, 0, 0)

Syntax: datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

Useful datetime functions

# create a datatime obj
dt = datetime.datetime(2019, 2, 15)

# 1. Get the current day of the month
dt.day #> 31

# 2. Get the current day of the week
dt.isoweekday() #> 5 --> Friday

# 3. Get the current month of the year 
dt.month  #> 2 --> February

# 4. Get the Year
dt.year  #> 2019

Get the last day of a month for any given date

import datetime
dt = datetime.date(1952, 2, 12)


import calendar
calendar.monthrange(dt.year,dt.month)[1] #> 29


Pandas date_range


import pandas as pd
import datetime
 
date1 = pd.Series(pd.date_range('2018-1-1 12:00:00', periods=7, freq='M'))
df = pd.DataFrame(dict(date_given=date1))
df



Sunday, August 4, 2019

Get list from pandas DataFrame column headers

The groupby function can be used to concatenate data from multiple rows into one field.


Create a Dataframe

import pandas as pd
import numpy as np

#Create a Dictionary of series
d = {'Name': pd.Series(['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine',
   'Rahul','David','Andrew','Ajay','Teresa']),
   'Age': pd.Series([26,27,25,24,31,27,25,33,42,32,51,47]),
   'Score': pd.Series([89,87,67,55,47,72,76,79,44,92,99,69])}
 
#Create a DataFrame
df = pd.DataFrame(d)
df

the resultant dataframe will be




Now lets get the values as a list by doing:


column_names = df.columns.values.tolist()
column_names

the result will be


Thursday, August 1, 2019

Concatenating Rows in Python Pandas

The groupby function can be used to concatenate data from multiple rows into one field.


Create a Dataframe

import pandas as pd
import numpy as np

#Create a Dictionary of series
d = {'Name': pd.Series(['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine',
   'Rahul','David','Andrew','Ajay','Teresa']),
   'Age': pd.Series([26,27,25,24,31,27,25,33,42,32,51,47]),
   'Score': pd.Series([89,87,67,55,47,72,76,79,44,92,99,69])}
 
#Create a DataFrame
df = pd.DataFrame(d)
df

the resultant dataframe will be




Now lets group by age of the student name


final_df = df.groupby('Age')['Name'].apply(', '.join)

final_df


the result will be


Monday, July 22, 2019

How to split a list inside a Dataframe cell into rows in Pandas

       
temp = {'name' : ['Edmond', 'ALex'], 'cat' : [['Horror', 'Vengeance', 'Justice'], ['Romance', 'Sacrifice']]}

      
df = pd.DataFrame(temp)

      
df.cat.apply(pd.Series)

      
df.cat.apply(pd.Series) \
.merge(df, left_index = True, right_index = True)

df.cat.apply(pd.Series) \
.merge(df, left_index = True, right_index = True) \
.drop(['cat'], axis = 1)

df.cat.apply(pd.Series) \
.merge(df, left_index = True, right_index = True) \
.drop(['cat'], axis = 1) \
.melt(id_vars = ['name'], value_name = "cat") \
.drop(['variable'], axis= 1)

Amount to Words Convert via PostgreSQL

asd



CREATE OR REPLACE FUNCTION dateToWords(the_date TIMESTAMP) RETURNS text AS $$
DECLARE
_month INTEGER;
_day  INTEGER;
_year INTEGER;
_hour INTEGER;
_minute INTEGER;
_year_text TEXT;
_hour_text TEXT;
_day_text TEXT;
_minute_text TEXT;
_exact_time TEXT;
_month_text TEXT;
e TEXT;
BEGIN
_month = EXTRACT(MONTH FROM the_date);
_day = EXTRACT(DAY FROM the_date);
_year = EXTRACT(YEAR FROM the_date);
_hour = EXTRACT(HOUR FROM the_date);
_minute = EXTRACT(MINUTE FROM the_date);


WITH Below20(Word, Id) AS
(
VALUES
  ('Zero', 0), ('One', 1),( 'Two', 2 ), ( 'Three', 3),
  ( 'Four', 4 ), ( 'Five', 5 ), ( 'Six', 6 ), ( 'Seven', 7 ),
  ( 'Eight', 8), ( 'Nine', 9), ( 'Ten', 10), ( 'Eleven', 11 ),
  ( 'Twelve', 12 ), ( 'Thirteen', 13 ), ( 'Fourteen', 14),
  ( 'Fifteen', 15 ), ('Sixteen', 16 ), ( 'Seventeen', 17),
  ('Eighteen', 18 ), ( 'Nineteen', 19 )
),
Below100(Word, Id) AS
(
  VALUES
   ('Twenty', 2), ('Thirty', 3),('Forty', 4), ('Fifty', 5),
   ('Sixty', 6), ('Seventy', 7), ('Eighty', 8), ('Ninety', 9)
)SELECT
CASE
  WHEN (_year % 1000) BETWEEN 1 AND 19 THEN
  CONCAT(
      (Select Word FROM Below20 WHERE ID=_year / 1000), ' Thousand ',
      (Select Word FROM Below20 WHERE ID=_year % 1000)
    )
  WHEN (_year % 1000) BETWEEN 20 AND 99 THEN
  CONCAT(
      (Select Word FROM Below20 WHERE ID=_year / 1000), ' Thousand ',
      (Select Word FROM Below20 WHERE ID=(_year % 1000) / 10), ' ',
      (Select Word FROM Below20 WHERE ID=(_year % 1000) / 10)
    )
  WHEN (_year % 1000) BETWEEN 100 AND 999 THEN
    CASE
        WHEN (_year % 1000) % 100 BETWEEN 1 AND 19 THEN
        CONCAT(
            (Select Word FROM Below20 WHERE ID=_year / 1000), ' Thousand ',
            (Select Word FROM Below20 WHERE ID=(_year % 1000) / 100), ' Hundred ',
            (Select Word FROM Below20 WHERE ID=((_year % 1000) % 100))
          )
        WHEN (_year % 1000) % 100 BETWEEN 20 AND 99 THEN
        CONCAT(
            (Select Word FROM Below20 WHERE ID=_year / 1000), ' Thousand ',
            (Select Word FROM Below20 WHERE ID=(_year % 1000) / 100), ' Hundred ',
            (Select Word FROM Below100 WHERE ID=((_year % 1000) % 100) / 10), ' ',
            (Select Word FROM Below20 WHERE ID=((_year % 1000) % 100) % 10)
          )
    END
END INTO _year_text;


WITH Below20(Word, Id) AS
(
 VALUES
   ('', 0), ('One', 1),( 'Two', 2 ), ( 'Three', 3),
   ( 'Four', 4 ), ( 'Five', 5 ), ( 'Six', 6 ), ( 'Seven', 7 ),
   ( 'Eight', 8), ( 'Nine', 9), ( 'Ten', 10), ( 'Eleven', 11 ),
   ( 'Twelve', 12 )
)
Select
    CASE
      WHEN _hour > 12 THEN (Select Word FROM Below20 WHERE ID= _hour - 12)
      ELSE (Select Word FROM Below20 WHERE ID= _hour)
END INTO _hour_text;



WITH Below20(Word, Id) AS
(
VALUES
  ('', 0), ('First', 1),( 'Second', 2 ), ( 'Third', 3),
  ( 'Fourth', 4 ), ( 'Fifth', 5 ), ( 'Sixth', 6 ), ( 'Seventh', 7 ),
  ( 'Eighth', 8), ( 'Ninth', 9), ( 'Tenth', 10), ( 'Eleventh', 11 ),
  ( 'Twelfth', 12 ), ( 'Thirteenth', 13 ), ( 'Fourteenth', 14),
  ( 'Fifteenth', 15 ), ('Sixteenth', 16 ), ( 'Seventeenth', 17),
  ('Eighteenth', 18 ), ( 'Nineteenth', 19 )
),
Below100(Word, Id) AS
(
   VALUES
    ('Twenty', 2), ('Thirty', 3)
)
Select
CASE
  WHEN _day BETWEEN 1 AND 19 THEN (Select Word FROM Below20 WHERE ID=_day)
  ELSE
  CONCAT(
      (Select Word FROM Below100 WHERE ID= _day / 10), ' ',
      (Select Word FROM Below20 WHERE ID= _day % 10)
   )
END INTO _day_text;



WITH Below20(Word, Id) AS
(
VALUES
  ('', 0), ('One', 1),( 'Two', 2 ), ( 'Three', 3),
  ( 'Four', 4 ), ( 'Five', 5 ), ( 'Six', 6 ), ( 'Seven', 7 ),
  ( 'Eight', 8), ( 'Nine', 9), ( 'Ten', 10), ( 'Eleven', 11 ),
  ( 'Twelve', 12 ), ( 'Thirteen', 13 ), ( 'Fourteen', 14),
  ( 'Fifteen', 15 ), ('Sixteen', 16 ), ( 'Seventeen', 17),
  ('Eighteen', 18 ), ( 'Nineteen', 19 )
),
Below100(Word, Id) AS
(
   VALUES
    ('Twenty', 2), ('Thirty', 3),('Forty', 4), ('Fifty', 5),
    ('Sixty', 6)
)
Select
CASE
  WHEN _minute BETWEEN 1 AND 19 THEN (Select Word FROM Below20 WHERE ID=_day)
  ELSE
  CONCAT(
      (Select Word FROM Below100 WHERE ID= _minute / 10), ' ',
      (Select Word FROM Below20 WHERE ID= _minute % 10)
   )
END INTO _minute_text;



SELECT
CASE
WHEN _hour BETWEEN 0 AND 12 THEN 'Morning'
WHEN _hour BETWEEN 13 AND 17 THEN 'Afternoon'
ELSE 'Evening'
END INTO _exact_time;



WITH nameOfTheMonth(monthAsText, Id) AS
(
VALUES
  ('January', 1), ('February', 2),( 'MARCH', 3), ( 'April', 4),
  ( 'May', 5), ( 'June', 6), ( 'July', 7), ( 'August', 8),
  ( 'September', 9), ( 'October', 10), ( 'November', 11), ( 'December', 12)
)
select monthAsText FROM nameOfTheMonth WHERE ID=_month INTO _month_text;


SELECT CONCAT(
      _day_text, ' day of', ' ',
      _month_text,', ',
     _year_text,', ',
     _hour_text,' ',
     _minute_text, ' in the ',
  _exact_time)
    INTO e;


return e;
END; $$
LANGUAGE PLPGSQL;


select dateToWords('May 15, 2018 5:38 PM')


Monday, April 15, 2019

MongoDB OSX Setup



1.) Install brew. Go to https://brew.sh/
     Then copy the installation script then paste it to terminal
     /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2.) Install node.
     brew install node

3.) Install mongodb.
     brew install mongo

4.) Test it. Go to terminal and write
      mongod

You should be able to see 'waiting for connections on port 27017' at end of the logs
   



Two ways of starting the mongodb server

1.) By calling the mongo itself in the terminal
      mongod


2.) By starting the services itself. This will automatically start mongodb at any time.
      brew services start mongo

Sunday, January 20, 2019

Apache Spark Code Collection

Read 'csv' File

lines = sc.textFile("..../u.data")

Get the first line of  an RDD file type

lines = sc.textFile("..../u.data")

firstRow=lines.first()

Count the number by Appearance and Show the Results

lines = sc.textFile("/Users/edmondlegaspi/Desktop/Datasets/u.data")

ratings = lines.map(lambda x: x.split()[2])

results = ratings.countByValue()

sortedResults = collections.OrderedDict(sorted(results.items()))

for key, value in sortedResults.items():
    print(key, value)



Monday, December 24, 2018

Install Apache Spark 2.0 on Amazon EC2 - Quick Setup


1.) Install java
     sudo yum install java-1.8.0-openjdk

2.) Download the apache spark
     wget http://mirror.rise.ph/apache/spark/spark-2.4.0/spark-2.4.0-bin-hadoop2.7.tgz

3.) Unzip it
    tar -xvf spark-2.4.0-bin-hadoop2.7.tgz

4.) Create a symbolic Link
     ln -s spark-2.4.0-bin-hadoop2.7 spark

5.) Edit the bashprofile
     sudo nano .bashrc
    
    Add the following codes:
    export SPARK_HOME=/home/ec2-user/spark 
    export PATH=$PATH:$SPARK_HOME

6.) Save changes with .bashrc
      . .bashrc

7.) Test spark
      spark-shell

Thursday, November 8, 2018

Installing Apache Kafka

You can set up Apache Kafka by using brew or downloading the binary.

Installing via Binary Download

1.) Go to apache kafka site

2.) Under Binary downloads, download
    "Scala 2.12  - kafka_2.12-2.0.0.tgz (asc, sha512)"

3.) Open a terminal, move the downloaded file to the root directory then extract
     mv Downloads/kafka_2.12-2.0.0.tgz .
     tar -xvf kafka_2.12-2.0.0.tgz

4.) Verify if it works
      cd kafka_2.12-2.0.0
      bin/kafka-topics.sh

    4.1) (If it doesn't work)
          brew tap caskroom/versions
          brew cask install java8

Installing Kafka via Brew

1.)  Open a terminal,
      brew install kafka
      (That's it)

Note: The one notable difference between installing via brew vs binary is that if you install by brew,
          there's no need to add the .sh extension to run kafka commands

Wednesday, August 1, 2018

Advanced Indexing from Multi-Index/Pivoted Dataframe

In this tutorial we will learn how to select row x column  in python multi-level dataframe using loc() function .  Lets see with an example

Consider the following data:


d = {
    'app' : ['J', 'J', 'J', 'J', 'J', 'J', 'J', 'J', 'J', 'B'],
    'geo':
        ['US', 'US', 'US', 'Asia', 'Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'US'],
    'device':
        ['iPhone', 'iPad', 'Android', 'iPhone', 'iPad', 'Android', 'iPhone', 'iPad', 'Android', 'iPhone'],
    'cost':
        [4500, 4000, 4500, 2000, 2000, 2500, 500, 500, 500, 250]}


df = pd.DataFrame(d)





















and a pivot out of it


df_pivoted =  pd.pivot_table(df, index = 'app', columns=['geo', 'device'], values=['cost'], aggfunc=np.sum)

and you want to select the cost under row 'B' with geo 'Asia'


df.loc[['J'], ('cost', 'Asia')]




Friday, February 16, 2018

Lorenz Equations with Python


import matplotlib.pyplot as plt
import numpy as np

vSigma = 10
vBeta = 8/3
vRho = 28


def f1(t,x,y,z):
    f1 = vSigma*y - vSigma*x
    return f1

def f2(t,x,y,z):
    f2 = vRho*x - y - x*z
    return f2

def f3(t,x,y,z):
    f3 = -vBeta*z + x*y
    return f3


print("Sample input: lorenzEquation(0,20,200,5,5,5)")

def lorenzEquation(first,second,N,alpha1,alpha2, alpha3):
    h = (second-first)/N
    t = first

    w1 = alpha1
    w2 = alpha2
    w3 = alpha3

    A = [w1]
    B = [w2]
    C = [w3]
    tempTime = alpha1
    time = [alpha1]
    
    A1 = [w1]
    B1 = [w2]
    C1 = [w3]


    for num in range(1,N):
        k1x = h*f1(t,w1,w2,w3)
        k1y = h*f2(t,w1,w2,w3)
        k1z = h*f3(t,w1,w2,w3)
        
        k2x = h*f1(t + (h/2), w1 + (k1x/2), w2 + (k1y/2), w3 + (k1z/2))
        k2y = h*f2(t + (h/2), w1 + (k1x/2), w2 + (k1y/2), w3 + (k1z/2))
        k2z = h*f3(t + (h/2), w1 + (k1x/2), w2 + (k1y/2), w3 + (k1z/2))
        
        k3x = h*f1(t + (h/2), w1 + (k2x/2), w2 + (k2y/2), w3 + (k2z/2))
        k3y = h*f2(t + (h/2), w1 + (k2x/2), w2 + (k2y/2), w3 + (k2z/2))
        k3z = h*f3(t + (h/2), w1 + (k2x/2), w2 + (k2y/2), w3 + (k2z/2))
        
        
        k4x = h*f1(t + h, w1 + k3x, w2 + k3y, w3 + k3z)
        k4y = h*f2(t + h, w1 + k3x, w2 + k3y, w3 + k3z)
        k4z = h*f3(t + h, w1 + k3x, w2 + k3y, w3 + k3z)
        
        w1 = w1 + (1/6)*(k1x + 2*k2x + 2*k3x + k4x)
        w2 = w2 + (1/6)*(k1y + 2*k2y + 2*k3y + k4y)
        w3 = w3 + (1/6)*(k1z + 2*k2z + 2*k3z + k4z)

        A.append(w1)
        B.append(w2)
        C.append(w3)
        tempTime = alpha1 + num*h
        time.append(tempTime)

    w1 = alpha1 + 0.001
    w2 = alpha2
    w3 = alpha3


    for num in range(1,N):
        k1x = h*f1(t,w1,w2,w3)
        k1y = h*f2(t,w1,w2,w3)
        k1z = h*f3(t,w1,w2,w3)
        
        k2x = h*f1(t + (h/2), w1 + (k1x/2), w2 + (k1y/2), w3 + (k1z/2))
        k2y = h*f2(t + (h/2), w1 + (k1x/2), w2 + (k1y/2), w3 + (k1z/2))
        k2z = h*f3(t + (h/2), w1 + (k1x/2), w2 + (k1y/2), w3 + (k1z/2))
        
        k3x = h*f1(t + (h/2), w1 + (k2x/2), w2 + (k2y/2), w3 + (k2z/2))
        k3y = h*f2(t + (h/2), w1 + (k2x/2), w2 + (k2y/2), w3 + (k2z/2))
        k3z = h*f3(t + (h/2), w1 + (k2x/2), w2 + (k2y/2), w3 + (k2z/2))
        
        
        k4x = h*f1(t + h, w1 + k3x, w2 + k3y, w3 + k3z)
        k4y = h*f2(t + h, w1 + k3x, w2 + k3y, w3 + k3z)
        k4z = h*f3(t + h, w1 + k3x, w2 + k3y, w3 + k3z)
        
        w1 = w1 + (1/6)*(k1x + 2*k2x + 2*k3x + k4x)
        w2 = w2 + (1/6)*(k1y + 2*k2y + 2*k3y + k4y)
        w3 = w3 + (1/6)*(k1z + 2*k2z + 2*k3z + k4z)

        A1.append(w1)
        B1.append(w2)
        C1.append(w3)
    #
    plt.plot(time, A)
    plt.plot(time, A1)
    plt.legend(['Initial condition [5,5,5]', 'Initial condition [5.001,5,5]'], loc='upper left')
    plt.show()

    plt.plot(A, B)
    plt.show()
    
    plt.plot(A, C)
    plt.show()

    return


Lotka Voltera Equations with Python



import matplotlib.pyplot as plt
import numpy as np

a = 1.2
b = 0.6
c = 0.8
d = 0.3

def f1(t,x,y):
    f1 = a*x - b*x*y
    return f1

def f2(t,x,y):
    f2 = -c*y + d*x*y
    return f2


print("Sample input:  predatorPrey(0, 30, 300, 2, 1)")


def predatorPrey(first,second,N,alpha1,alpha2):
    h = (second-first)/N
    t = first
    
    w1 = alpha1
    w2 = alpha2
    
    A = [w1]
    B = [w2]
    tempTime = alpha1
    time = [alpha1]
    
    for num in range(1,N):
        k1x = h*f1(t,w1,w2)
        k1y = h*f2(t,w1,w2)
        
        k2x = h*f1(t + (h/2), w1 + (k1x/2), w2 + (k1y/2))
        k2y = h*f2(t + (h/2), w1 + (k1x/2), w2 + (k1y/2))
        
        k3x = h*f1(t + (h/2), w1 + (k2x/2), w2 + (k2y/2))
        k3y = h*f2(t + (h/2), w1 + (k2x/2), w2 + (k2y/2))
        
        
        k4x = h*f1(t + h, w1 + k3x, w2 + k3y)
        k4y = h*f2(t + h, w1 + k3x, w2 + k3y)
        
        w1 = w1 + (1/6)*(k1x + 2*k2x + 2*k3x + k4x)
        w2 = w2 + (1/6)*(k1y + 2*k2y + 2*k3y + k4y)
        
        A.append(w1)
        B.append(w2)
        tempTime = alpha1 + num*h
        time.append(tempTime)
        
    
    plt.plot(time, A)
    plt.plot(time, B)
    plt.legend(['x, prey', 'y, predator'], loc='upper left')
    ax.grid()
    ax.set_xlabel("Time (h)")
    plt.show()
    
    plt.plot(A, B)
    plt.show()
    
    return


Regula Falsi Or Method of False Position with Python


Regula Falsi or Method of False Position


     The regula falsi method iteratively determines a sequence of root enclosing intervals, $(a_n, b_n)$, and a sequence of approximations, which shall be denoted by $p_n$. Similar to the bisection method, the root should be in ther interval being considered. During each iteration, a single point is selected from $(a_n, b_n)$ to approximate the location of the root and serve as $p_n$. If $p_n$ is an accurate enough approximation, the iterative process is terminated. Otherwise, the Intermediate Value Theorem is used to determine whether the root lies on the subinterval $(a_n, p_n)$ or the subinterval $(p_n, b_n)$. The entire process is then repeated on that subinterval. It was developed because the Bisection method converges at a fairly slow rate.

Let f be a continuous function on the interval $[a,b]$ s.t. $f(a) \cdot f(b) < 0$, locate the point $(p1,0)$ where the line joining the points $(a, f(a))$ and $(b,f(b))$ crosses the x-axis. Hence,
       $$p_1 = b -  \frac{f(b)(b-a)}{f(b)-f(a)} = \frac{af(b) - bf(a)}{f(b) - f(a)}$$




Algorithm


To find a solution to $f(x) = 0$ given the continuous function $f$ on the interval $[a, b]$, where $f(a)$ and $f(b)$ have opposite signs:

INPUT endpoints a, b; tolerance TOL; maximum number of iterations $N_0$.

STEP 1 Set $i = 1$
                     $FA = f(a)$.

STEP 2 While $i \le N_0$ do Steps 3-6.

        STEP 3 Set $p = \frac{af(b) - bf(a)}{f(b) - f(a)}$
                               $FP = f(p)$

        STEP 4 If $FP = 0$ or |f(p)| < TOL  then
                         STOP
                     else OUTPUT(P)
                 
        STEP 5 Set $i = i + 1$

       STEP 6 If $FA \times FP > 0$ then set $a = p$;
                          $FA = FP$
                     else set $b = p$.

STEP 7 OUTPUT("Method failed after $N_0$")


Sample Problem:


Use Regula Falsi method to approximate the solution of $f(x) = x^3 + 2x^2 - 3x - 1 = 0$ within $[1, 2]$ that is accurate to at least within $10^-4$.


For the approximation, see the outpout below:

    n                    $a_n$                                $b_n$                        $p_n$                                  $f(p_n)$
         
   1                     1                                   2                       1.1                                  -0.549            

   2                    1.1                                 2                       1.1517436                      -0.27440072      

   3                    1.1517436                     2                       1.1768409                      -0.13074253      

   4                    1.1768409                     2                       1.1886277                      -0.060875863      

  5                    1.1886277                      2                       1.1940789                      -0.028040938      

  6                    1.1940789                      2                       1.1965821                      -0.01285224      

  7                    1.1965821                      2                       1.1977278                       -0.0058772415    

  8                    1.1977278                      2                       1.1982513                       -0.0026848163    

  9                    1.1982513                      2                       1.1984904                       -0.001225881      

 10                   1.1984904                     2                        1.1985996                       -0.0005596125    

 11                   1.1985996                     2                        1.1986494                        -0.00025543669    

 12                   1.1986494                     2                        1.1986721                        -0.0001165895    


Python Code:


import math
import numpy as np



def f(x):
    f = math.pow(x,3) + 2*math.pow(x,2) - 3*x - 1
    return f
 
 
print("Sample input: regulaFalsi(1,2,10**-4, 100)")
 
def regulaFalsi(a,b,TOL,N):
    i = 1
    FA = f(a)
    
    print("%-20s %-20s %-20s %-20s %-20s" % ("n","a_n","b_n","p_n","f(p_n)"))
     
    while(i <= N):
        p = (a*f(b)-b*f(a))/(f(b) - f(a))
        FP = f(p)
         
        if(FP == 0 or np.abs(f(p)) < TOL):
            break
        else:
             print("%-20.8g %-20.8g %-20.8g %-20.8g %-20.8g\n" % (i, a, b, p, f(p)))
        
         
        i = i + 1
         
        if(FA*FP > 0):
            a = p
        else:
            b = p
     
    return


Regula Falsi or Method of False Position with Scilab


Regula Falsi or Method of False Position


     The regula falsi method iteratively determines a sequence of root enclosing intervals, $(a_n, b_n)$, and a sequence of approximations, which shall be denoted by $p_n$. Similar to the bisection method, the root should be in ther interval being considered. During each iteration, a single point is selected from $(a_n, b_n)$ to approximate the location of the root and serve as $p_n$. If $p_n$ is an accurate enough approximation, the iterative process is terminated. Otherwise, the Intermediate Value Theorem is used to determine whether the root lies on the subinterval $(a_n, p_n)$ or the subinterval $(p_n, b_n)$. The entire process is then repeated on that subinterval. It was developed because the Bisection method converges at a fairly slow rate.

Let f be a continuous function on the interval $[a,b]$ s.t. $f(a) \cdot f(b) < 0$, locate the point $(p1,0)$ where the line joining the points $(a, f(a))$ and $(b,f(b))$ crosses the x-axis. Hence,
       $$p_1 = b -  \frac{f(b)(b-a)}{f(b)-f(a)} = \frac{af(b) - bf(a)}{f(b) - f(a)}$$




Algorithm


To find a solution to $f(x) = 0$ given the continuous function $f$ on the interval $[a, b]$, where $f(a)$ and $f(b)$ have opposite signs:

INPUT endpoints a, b; tolerance TOL; maximum number of iterations $N_0$.

STEP 1 Set $i = 1$
                     $FA = f(a)$.

STEP 2 While $i \le N_0$ do Steps 3-6.

        STEP 3 Set $p = \frac{af(b) - bf(a)}{f(b) - f(a)}$
                               $FP = f(p)$

        STEP 4 If $FP = 0$ or |f(p)| < TOL  then
                         STOP
                     else OUTPUT(P)
                 
        STEP 5 Set $i = i + 1$

       STEP 6 If $FA \times FP > 0$ then set $a = p$;
                          $FA = FP$
                     else set $b = p$.

STEP 7 OUTPUT("Method failed after $N_0$")


Sample Problem:


Use Regula Falsi method to approximate the solution of $f(x) = x^3 + 2x^2 - 3x - 1 = 0$ within $[1, 2]$ that is accurate to at least within $10^-4$.


For the approximation, see the outpout below:

    n                    $a_n$                              $b_n$                    $p_n$                        $f(p_n)$
         
    1                    1                                 2                    1.1                        -0.549            
    2                    1.1                              2                    1.1517436            -0.27440072      
    3                    1.1517436                  2                    1.1768409            -0.13074253      
    4                    1.1768409                  2                    1.1886277            -0.060875863      
    5                    1.1886277                  2                    1.1940789            -0.028040938      
    6                    1.1940789                  2                    1.1965821            -0.01285224      
    7                    1.1965821                  2                    1.1977278            -0.0058772415    
    8                    1.1977278                  2                    1.1982513            -0.0026848163    
    9                    1.1982513                  2                    1.1984904            -0.001225881      
   10                   1.1984904                  2                    1.1985996            -0.0005596125    
   11                   1.1985996                  2                    1.1986494            -0.00025543669    
   12                   1.1986494                  2                    1.1986721            -0.0001165895


Scilab Code:


clear
clc
 
function f = f(x)
    f = x^3 + 2*x^2 - 3*x -1 
endfunction
 
 
disp("Sample input: regulaFalsi(1,2,10^-4, 100)")
 
function regulaFalsi(a,b,TOL,N)
    i = 1
    FA = f(a)
    finalOutput = [i, a, b, a + (b-a)/2, f(a + (b-a)/2)]
     
    printf("%-20s %-20s %-20s %-20s %-20s \n","n","a_n","b_n","p_n","f(p_n)")
    
    while(i <= N),
        p = (a*f(b)-b*f(a))/(f(b) - f(a))
        FP = f(p)
         
         
        if(FP == 0 | abs(f(p)) < TOL) then
            break
        else
             printf("%-20.8g %-20.8g %-20.8g %-20.8g %-20.8g\n", i, a, b, p, f(p))
        end
         
        i = i + 1
         
        if(FA*FP > 0) then
            a = p
        else
            b = p
        end
    end
     
    //disp(finalOutput)
     
endfunction



Bisection Method with Python


The Bisection Method


     Suppose $f$ is a continuous function defined on the interval $[a, b]$, with $f(a)$ and $f(b)$ of opposite sign. The Intermediate Value Theorem implies that a number p exists in (a, b) with $f( p) = 0$. Although the procedure will work when there is more than one root in the interval $(a, b)$, we assume for simplicity that the root in this interval is unique. The method calls for a repeated halving (or bisecting) of subintervals of $[a, b]$ and, at each step, locating the half containing p.


Algorithm


To find a solution to $f(x) = 0$ given the continuous function $f$ on the interval $[a, b]$, where $f(a)$ and $f(b)$ have opposite signs:

INPUT endpoints a, b; tolerance TOL; maximum number of iterations $N_0$.

STEP 1 Set $i = 1$
                     $FA = f(a)$.

STEP 2 While $i \le N_0$ do Steps 3-6.

        STEP 3 Set $p = a + (b - a)/2$
                               $FP = f(p)$

        STEP 4 If $FP = 0$ or $(b-a)/2 < TOL$ then
                         STOP
                     else OUTPUT(P)
                   
        STEP 5 Set $i = i + 1$

       STEP 6 If $FA \times FP > 0$ then set $a = p$;
                          $FA = FP$
                     else set $b = p$.

STEP 7 OUTPUT("Method failed after $N_0$")


Sample Problem:


Show that $f(x) = x^3 + 4x^2 - 10 = 0$ has a root in $[1, 2]$, and use the Bisection method to determine an approximation to the root that is accurate to at least within $10^-4$.

Solution: Because $f(1) = -5$ and $f(2) = 14$ the Intermediate Value Theorem ensures that this continuous function has a root in $[1, 2]$.

For the approximation, see the outpout below:

    n                    $a_n$                        $b_n$                          $p_n$                        $f(p_n)$
           
    1                    1                           2                           1.5                       2.375                    

    2                    1                           1.5                        1.25                     -1.796875        

    3                    1.25                      1.5                        1.375                   0.16210938        

    4                    1.25                      1.375                    1.3125                 -0.84838867      

    5                    1.3125                  1.375                    1.34375               -0.35098267      

    6                    1.34375                1.375                    1.359375             -0.096408844      

    7                    1.359375              1.375                    1.3671875            0.032355785      

    8                    1.359375              1.3671875            1.3632812            -0.032149971      

    9                    1.3632812            1.3671875            1.3652344            7.2024763e-05    

    10                  1.3632812            1.3652344            1.3642578            -0.016046691      

    11                  1.3642578            1.3652344            1.3647461            -0.0079892628    

    12                  1.3647461            1.3652344            1.3649902            -0.0039591015    

    13                  1.3649902            1.3652344            1.3651123            -0.001943659      


Python Code:


def f(x):
    f = x**3 + 4*x**2 - 10
    return f
 
 
print("Sample input: bisectionMethod(1,2,10**-4, 100)")
 
def bisectionMethod(a,b,TOL,N):
    i = 1
    FA = f(a)
    
    print("%-20s %-20s %-20s %-20s %-20s" % ("n","a_n","b_n","p_n","f(p_n)"))
    print("%-20.8g %-20.8g %-20.8g %-20.8g %-20.8g\n" % (i, a, b, a + (b-a)/2, f(a + (b-a)/2) ))
    
     
    while(i <= N):
        p = a + (b-a)/2
        FP = f(p)
         
        if(FP == 0 or (b-a)/2 < TOL):
            break
        else:
             print("%-20.8g %-20.8g %-20.8g %-20.8g %-20.8g\n" % (i, a, b, p, f(p)))
        
         
        i = i + 1
         
        if(FA*FP > 0):
            a = p
        else:
            b = p
     
    return


Final Note: 


The Bisection method, though conceptually clear, has significant drawbacks. It is relatively
slow to converge (that is, N may become quite large before $| p − p_N|$ is sufficiently
small), and a good intermediate approximation might be inadvertently discarded. However,
the method has the important property that it always converges to a solution, and for that
reason it is often used as a starter for the more efficient methods

Bisection Method with Scilab


The Bisection Method


     Suppose $f$ is a continuous function defined on the interval $[a, b]$, with $f(a)$ and $f(b)$ of opposite sign. The Intermediate Value Theorem implies that a number p exists in (a, b) with $f( p) = 0$. Although the procedure will work when there is more than one root in the interval $(a, b)$, we assume for simplicity that the root in this interval is unique. The method calls for a repeated halving (or bisecting) of subintervals of $[a, b]$ and, at each step, locating the half containing p.


Algorithm


To find a solution to $f(x) = 0$ given the continuous function $f$ on the interval $[a, b]$, where $f(a)$ and $f(b)$ have opposite signs:

INPUT endpoints a, b; tolerance TOL; maximum number of iterations $N_0$.

STEP 1 Set $i = 1$
                     $FA = f(a)$.

STEP 2 While $i \le N_0$ do Steps 3-6.

        STEP 3 Set $p = a + (b - a)/2$
                               $FP = f(p)$

        STEP 4 If $FP = 0$ or $(b-a)/2 < TOL$ then
                         STOP
                     else OUTPUT(P)
                   
        STEP 5 Set $i = i + 1$

       STEP 6 If $FA \times FP > 0$ then set $a = p$;
                          $FA = FP$
                     else set $b = p$.

STEP 7 Display("Method failed after $N_0$")


Sample Problem:


Show that $f(x) = x^3 + 4x^2 - 10 = 0$ has a root in $[1, 2]$, and use the Bisection method to determine an approximation to the root that is accurate to at least within $10^-4$.

Solution: Because $f(1) = -5$ and $f(2) = 14$ the Intermediate Value Theorem ensures that this continuous function has a root in $[1, 2]$.

For the approximation, see the outpout below:

    n       $a_n$                   $b_n$                $p_n$                   $f(p_n)$

    1.      1.                   2.                  1.5                  2.375    
    1.      1.                   2.                  1.5                  2.375    
    2.      1.                   1.5                1.25                - 1.796875
    3.      1.25               1.5                1.375              0.1621094
    4.      1.25               1.375            1.3125            - 0.8483887
    5.      1.3125           1.375            1.34375          - 0.3509827
    6.      1.34375         1.375            1.359375        - 0.0964088
    7.      1.359375       1.375            1.3671875      0.0323558
    8.      1.359375       1.3671875    1.3632812      - 0.0321500
    9.      1.3632812     1.3671875    1.3652344      0.0000720
    10.    1.3632812     1.3652344    1.3642578      - 0.0160467
    11.    1.3642578     1.3652344    1.3647461      - 0.0079893
    12.    1.3647461     1.3652344    1.3649902      - 0.0039591
    13.    1.3649902     1.3652344    1.3651123      - 0.0019437


Scilab Code:


clear
clc

function f = f(x)
    f = x^3 + 4*x^2 - 10
endfunction


disp("Sample input: bisectionMethod(1,2,10^-4, 100)")

function bisectionMethod(a,b,TOL,N)
    i = 1
    FA = f(a)
    finalOutput = [i, a, b, a + (b-a)/2, f(a + (b-a)/2)]
    
    disp("   n      a_n          b_n          p_n         f(p_n)")
    
    while(i <= N), 
        p = a + (b-a)/2
        FP = f(p)
        
        if(FP == 0 | (b-a)/2 < TOL) then
            break
        else
             finalOutput = [finalOutput; i, a, b, p, f(p)]
        end
        
        i = i + 1
        
        if(FA*FP > 0) then
            a = p
        else
            b = p
        end
    end
    
    disp(finalOutput)
    
endfunction

Final Note: 


The Bisection method, though conceptually clear, has significant drawbacks. It is relatively
slow to converge (that is, N may become quite large before $| p − p_N|$ is sufficiently
small), and a good intermediate approximation might be inadvertently discarded. However,
the method has the important property that it always converges to a solution, and for that
reason it is often used as a starter for the more efficient methods

Present and Accumulated Values of an Annuity-Immediate

Problem 15.1
Consider an investment of $5,000 at 6% convertible semiannually. How much can be withdrawn each half−year to use up the fund exactly at the end of 20 years?

Solution.

Problem 15.2
The annual payment on a house is $18,000. If payments are made for 40
years, how much is the house worth assuming annual interest rate of 6%?

Solution.

Problem 15.3
If , calculate .

Solution.

Problem 15.4
Calculate the present value of 300 paid at the end of each year for 20 years
using an annual effective interest rate of 8%.

Solution.

Problem 15.5
If and , express as a function of and .

Solution.

Problem 15.6
(a) Given: , , . Find .
(b) You are given that and . Determine .

Solution.

Equations of Value and Time Diagram

Problem 12.1
In return for payments of $5,000 at the end of 3 years and $4,000 at the
end of 9 years, an investor agrees to pay $1500 immediately and to make an additional payment at the end of 2 years. Find the amount of the additional payment if .

Solution.

Problem 12.2
At a certain interest rate the present values of the following two payment
patterns are equal:
(i) 200 at the end of 5 years plus 500 at the end of 10 years;
(ii) 400.94 at the end of 5 years.
At the same interest rate 100 invested now plus 120 invested at the end of 5 years will accumulate to P at the end of 10 years. Calculate P.

Solution.

Problem 12.3
An investor makes three deposits into a fund, at the end of 1, 3, and 5 years. The amount of the deposit at time is . Find the size of the fund at the end of 7 years, if the nominal rate of discount convertible quarterly is .

Solution.

Problem 12.4
Brian and Jennifer each take out a loan of . Jennifer will repay her loan
by making one payment of 800 at the end of year 10. Brian will repay his
loan by making one payment of 1,120 at the end of year 10. The nominal
semi-annual rate being charged to Jennifer is exactly one-half the nominal
semi-annual rate being charged to Brian. Calculate .

Solution.

Problem 12.5
Fund A accumulates at 6% effective, and Fund B accumulates at 8% effective. At the end of 20 years the total of the two funds is 2,000. At the end of 10 years the amount in Fund A is half that in Fund B. What is the total of the two funds at the end of 5 years?

Solution.

Problem 12.6
Louis has an obligation to pay a sum of $3,000 in four years from now and a sum of $5,000 in six years from now. His creditor permits him to discharge these debts by paying in two years from now, $1000 in three years from now, and a final payment of in nine years from now. Assuming an annual effective rate of interest of 6%, find .

Solution.

Problem 12.7
Every Friday in February (7, 14, 21,28) Vick places a 1,000 bet, on credit,
with his off-track bookmaking service, which charges an effective weekly interest rate of 8% on all credit extended. Vick looses each bet and agrees to repay his debt in four installments to be made on March 7, 14, 21, and 28. Vick pays 1,100 on March 7, 14, and 21. How much must Vick pay on March 28 to completely repay his debt?

Solution.

Problem 12.8
A borrower is repaying a loan by making payments of 1,000 at the end of
each of the next 3 years. The interest rate on the loan is 5% compounded
annually. What payment could the borrower make at the end of the first
year in order to extinguish the loan?

Solution.

Problem 12.9
An investor purchases an investment which will pay 2,000 at the end of one year and 5,000 at the end of four years. The investor pays 1,000 now and agrees to pay at the end of the third year. If the investor uses an interest rate of 7% compounded annually, what is ?

Solution.

Solving for Unknown Time

Problem 14.1
The present value of a payment of $5,000 to be made in years is equal to the present value of a payment of $7,100 to be made in years. If find $t4.

Solution.