IRR Calculate (finance)
by mesias - Saturday September 23, 2023 at 03:17 AM
#1
I Want to share this PostgreSQL Function Big Grin

-- Function: irr_calculate(double precision[])

-- DROP FUNCTION irr_calculate(double precision[]);

-- Just enter cash flow to use this function
CREATE OR REPLACE FUNCTION irr_calculate(cashflows double precision[])
  RETURNS double precision AS
$BODY$
DECLARE
    epsilon double precision := 0.00001; -- The desired level of precision
    max_iterations integer := 100; -- Maximum number of iterations
    guess double precision := 0.1; -- Initial initialization for IRR
    irr double precision := 0.0; -- Initialize IRR to 0
    npv double precision;
    derivative double precision;
BEGIN
    FOR i IN 1..max_iterations LOOP
        npv := 0.0;
        derivative := 0.0;
       
        FOR j IN 1..array_length(cashflows, 1) LOOP
            npv := npv + cashflows[j] / power(1.0 + irr, j - 1);
            derivative := derivative - j * cashflows[j] / power(1.0 + irr, j);
        END LOOP;
       
        irr := irr - npv / derivative;
       
        IF abs(npv) < epsilon THEN
            RETURN irr * 100.0;
        END IF;
    END LOOP;
   
    RAISE EXCEPTION 'The IRR calculation did not found';
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION irr_calculate(double precision[])
  OWNER TO postgres;
 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [FREE] Database Searcher Telegram odanbtw 1,028 91,987 Today, 09:34 AM
Last Post: amhars83
  The Ultimate Cloning Course: Mastering Dump Data & Card Cloning nexo 29 5,449 Yesterday, 05:30 PM
Last Post: kzpxshr
  DarkGPT Tutorial Easy idontknowmyname 190 8,689 Yesterday, 05:28 PM
Last Post: kzpxshr
  ✅ Top 10 Google Dorks For SQL Injections NextSoftGroup 13 638 Yesterday, 04:53 PM
Last Post: sacage_x64
  AI-drived pentest platform sacage_x64 0 96 Yesterday, 04:52 PM
Last Post: sacage_x64

Forum Jump:


 Users browsing this forum: 1 Guest(s)