C 语言中的近似内部收益率(IRR)

CCBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本实验中,我们将学习如何使用C程序来近似计算内部收益率(IRR)。我们将首先读取现金流,现金流代表了在投资的不同时间段投入或收到的资金。然后,我们将使用迭代方法来找到净现值(NPV)近似为零的利率,这将为我们提供估计的IRR。最后,我们将打印计算出的IRR。本实验涵盖了关键的金融数学概念,并展示了它们在C编程中的实现。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("C")) -.-> c/ControlFlowGroup(["Control Flow"]) c(("C")) -.-> c/CompoundTypesGroup(["Compound Types"]) c(("C")) -.-> c/FunctionsGroup(["Functions"]) c(("C")) -.-> c/UserInteractionGroup(["User Interaction"]) c/ControlFlowGroup -.-> c/while_loop("While Loop") c/CompoundTypesGroup -.-> c/arrays("Arrays") c/FunctionsGroup -.-> c/math_functions("Math Functions") c/UserInteractionGroup -.-> c/user_input("User Input") c/UserInteractionGroup -.-> c/output("Output") subgraph Lab Skills c/while_loop -.-> lab-435337{{"C 语言中的近似内部收益率(IRR)"}} c/arrays -.-> lab-435337{{"C 语言中的近似内部收益率(IRR)"}} c/math_functions -.-> lab-435337{{"C 语言中的近似内部收益率(IRR)"}} c/user_input -.-> lab-435337{{"C 语言中的近似内部收益率(IRR)"}} c/output -.-> lab-435337{{"C 语言中的近似内部收益率(IRR)"}} end

读取现金流

在这一步中,我们将学习如何在C程序中读取和存储现金流,以便计算内部收益率(IRR)。现金流代表了在投资的不同时间段投入或收到的资金。

首先,让我们创建一个C文件来实现现金流读取功能:

cd ~/project
nano irr_calculation.c

现在,让我们编写读取现金流的初始代码:

#include <stdio.h>
#define MAX_CASH_FLOWS 10

int main() {
    double cash_flows[MAX_CASH_FLOWS];
    int num_cash_flows;

    printf("Enter the number of cash flows (max %d): ", MAX_CASH_FLOWS);
    scanf("%d", &num_cash_flows);

    printf("Enter cash flows (negative for investments, positive for returns):\n");
    for (int i = 0; i < num_cash_flows; i++) {
        printf("Cash flow %d: ", i);
        scanf("%lf", &cash_flows[i]);
    }

    // 打印输入的现金流以进行验证
    printf("\nEntered Cash Flows:\n");
    for (int i = 0; i < num_cash_flows; i++) {
        printf("Cash flow %d: %.2f\n", i, cash_flows[i]);
    }

    return 0;
}

编译并运行程序:

gcc irr_calculation.c -o irr_calculation
./irr_calculation

示例输出:

Enter the number of cash flows (max 10): 4
Enter cash flows (negative for investments, positive for returns):
Cash flow 0: -1000
Cash flow 1: 300
Cash flow 2: 400
Cash flow 3: 500

Entered Cash Flows:
Cash flow 0: -1000.00
Cash flow 1: 300.00
Cash flow 2: 400.00
Cash flow 3: 500.00
解释
  • 我们定义了最大现金流数量(MAX_CASH_FLOWS)以防止内存使用过多
  • 程序首先要求用户输入现金流的数量
  • 然后提示用户输入每个现金流值
  • 负值代表初始投资
  • 正值代表回报或收入
  • 程序会将输入的现金流打印出来进行验证

使用迭代法找到净现值约为0时的利率

在这一步中,我们将扩展之前的现金流程序,使用迭代数值方法来计算内部收益率(IRR)。

首先,让我们修改现有的C文件:

cd ~/project
nano irr_calculation.c

现在,实现净现值(NPV)和内部收益率(IRR)的计算逻辑:

#include <stdio.h>
#include <math.h>
#define MAX_CASH_FLOWS 10
#define EPSILON 0.0001

double calculate_npv(double cash_flows[], int num_cash_flows, double rate) {
    double npv = 0.0;
    for (int i = 0; i < num_cash_flows; i++) {
        npv += cash_flows[i] / pow(1 + rate, i);
    }
    return npv;
}

double find_irr(double cash_flows[], int num_cash_flows) {
    double rate_low = -0.9;
    double rate_high = 10.0;
    double rate = 0.1;

    while ((rate_high - rate_low) > EPSILON) {
        double npv = calculate_npv(cash_flows, num_cash_flows, rate);

        if (fabs(npv) < EPSILON) {
            return rate;
        }

        if (npv > 0) {
            rate_low = rate;
        } else {
            rate_high = rate;
        }

        rate = (rate_low + rate_high) / 2.0;
    }

    return rate;
}

int main() {
    double cash_flows[MAX_CASH_FLOWS];
    int num_cash_flows;

    printf("Enter the number of cash flows (max %d): ", MAX_CASH_FLOWS);
    scanf("%d", &num_cash_flows);

    printf("Enter cash flows (negative for investments, positive for returns):\n");
    for (int i = 0; i < num_cash_flows; i++) {
        printf("Cash flow %d: ", i);
        scanf("%lf", &cash_flows[i]);
    }

    double irr = find_irr(cash_flows, num_cash_flows);
    printf("\nApproximate Internal Rate of Return (IRR): %.4f or %.2f%%\n", irr, irr * 100);

    return 0;
}

使用数学库编译程序:

gcc irr_calculation.c -o irr_calculation -lm

使用示例现金流运行程序:

./irr_calculation

示例输出:

Enter the number of cash flows (max 10): 4
Enter cash flows (negative for investments, positive for returns):
Cash flow 0: -1000
Cash flow 1: 300
Cash flow 2: 400
Cash flow 3: 500

Approximate Internal Rate of Return (IRR): 0.2154 or 21.54%
解释
  • calculate_npv() 计算给定利率下的净现值
  • find_irr() 使用二分法找到净现值约为0时的利率
  • 我们使用 EPSILON 定义收敛精度
  • 该算法迭代地缩小内部收益率的范围
  • 最终计算出的内部收益率以小数和百分比形式显示

打印估计的内部收益率

在这最后一步中,我们将增强内部收益率(IRR)计算程序,以提供更详细的输出,并展示不同的投资场景。

让我们修改现有的C文件,以添加更全面的内部收益率分析:

cd ~/project
nano irr_calculation.c

使用额外的输出和分析更新代码:

#include <stdio.h>
#include <math.h>
#define MAX_CASH_FLOWS 10
#define EPSILON 0.0001

double calculate_npv(double cash_flows[], int num_cash_flows, double rate) {
    double npv = 0.0;
    for (int i = 0; i < num_cash_flows; i++) {
        npv += cash_flows[i] / pow(1 + rate, i);
    }
    return npv;
}

double find_irr(double cash_flows[], int num_cash_flows) {
    double rate_low = -0.9;
    double rate_high = 10.0;
    double rate = 0.1;

    while ((rate_high - rate_low) > EPSILON) {
        double npv = calculate_npv(cash_flows, num_cash_flows, rate);

        if (fabs(npv) < EPSILON) {
            return rate;
        }

        if (npv > 0) {
            rate_low = rate;
        } else {
            rate_high = rate;
        }

        rate = (rate_low + rate_high) / 2.0;
    }

    return rate;
}

void print_investment_analysis(double cash_flows[], int num_cash_flows, double irr) {
    double total_investment = 0;
    double total_returns = 0;

    printf("\n--- 投资分析 --- \n");

    // 详细的现金流明细
    for (int i = 0; i < num_cash_flows; i++) {
        printf("时期 %d: $%.2f\n", i, cash_flows[i]);

        if (cash_flows[i] < 0) {
            total_investment += fabs(cash_flows[i]);
        } else {
            total_returns += cash_flows[i];
        }
    }

    // 投资总结
    printf("\n总投资: $%.2f\n", total_investment);
    printf("总回报: $%.2f\n", total_returns);
    printf("净利润: $%.2f\n", total_returns - total_investment);

    // 内部收益率详情
    printf("\n内部收益率 (IRR):\n");
    printf("小数形式: %.4f\n", irr);
    printf("百分比形式: %.2f%%\n", irr * 100);

    // 投资表现解读
    if (irr > 0.15) {
        printf("\n投资表现: 优秀\n");
    } else if (irr > 0.10) {
        printf("\n投资表现: 良好\n");
    } else if (irr > 0) {
        printf("\n投资表现: 中等\n");
    } else {
        printf("\n投资表现: 较差\n");
    }
}

int main() {
    double cash_flows[MAX_CASH_FLOWS];
    int num_cash_flows;

    printf("输入现金流的数量 (最大 %d): ", MAX_CASH_FLOWS);
    scanf("%d", &num_cash_flows);

    printf("输入现金流 (投资为负,回报为正):\n");
    for (int i = 0; i < num_cash_flows; i++) {
        printf("现金流 %d: ", i);
        scanf("%lf", &cash_flows[i]);
    }

    double irr = find_irr(cash_flows, num_cash_flows);
    print_investment_analysis(cash_flows, num_cash_flows, irr);

    return 0;
}

编译程序:

gcc irr_calculation.c -o irr_calculation -lm

使用示例投资场景运行程序:

./irr_calculation

示例输出:

输入现金流的数量 (最大 10): 4
输入现金流 (投资为负,回报为正):
现金流 0: -1000
现金流 1: 300
现金流 2: 400
现金流 3: 500

--- 投资分析 ---
时期 0: $-1000.00
时期 1: $300.00
时期 2: $400.00
时期 3: $500.00

总投资: $1000.00
总回报: $1200.00
净利润: $200.00

内部收益率 (IRR):
小数形式: 0.2154
百分比形式: 21.54%

投资表现: 优秀
解释
  • 添加了 print_investment_analysis() 函数以提供全面的输出
  • 计算总投资、回报和净利润
  • 用描述性类别解释内部收益率表现
  • 提供现金流和投资指标的详细明细

总结

在本实验中,我们首先学习了如何在C程序中读取和存储现金流,以便计算内部收益率(IRR)。现金流代表了在投资的不同时间段投入或收到的资金。然后,我们探索了使用迭代法来找到净现值(NPV)近似为零的利率,即内部收益率。最后,我们学习了如何打印估计的内部收益率。关键的学习要点包括理解现金流数据结构、实现用于找到内部收益率的迭代算法以及展示最终结果。