• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

b1f6c1c4 / ProfessionalAccounting / 300

25 Jul 2023 12:04AM UTC coverage: 58.835% (+22.3%) from 36.498%
300

push

appveyor

b1f6c1c4
improv yr

5970 of 10147 relevant lines covered (58.84%)

130.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/AccountingServer.Shell/Plugins/YieldRate/YieldRateSolver.cs
1
/* Copyright (C) 2020-2023 b1f6c1c4
2
 *
3
 * This file is part of ProfessionalAccounting.
4
 *
5
 * ProfessionalAccounting is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU Affero General Public License as
7
 * published by the Free Software Foundation, version 3.
8
 *
9
 * ProfessionalAccounting is distributed in the hope that it will be useful, but
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
12
 * for more details.
13
 *
14
 * You should have received a copy of the GNU Affero General Public License
15
 * along with ProfessionalAccounting.  If not, see
16
 * <https://www.gnu.org/licenses/>.
17
 */
18

19
using System;
20
using System.Collections.Generic;
21
using System.Linq;
22
using AccountingServer.Entities.Util;
23

24
namespace AccountingServer.Shell.Plugins.YieldRate;
25

26
/// <summary>
27
///     收益率求解器
28
/// </summary>
29
internal class YieldRateSolver
30
{
31
    /// <summary>
32
    ///     日期
33
    /// </summary>
34
    private readonly double[] m_Delta;
35

36
    /// <summary>
37
    ///     现金流
38
    /// </summary>
39
    private readonly double[] m_Fund;
40

41
    /// <summary>
42
    ///     期数
43
    /// </summary>
44
    private readonly int m_N;
45

46
    public YieldRateSolver(IEnumerable<double> delta, IEnumerable<double> fund)
×
47
    {
×
48
        m_Delta = delta.ToArray();
×
49
        m_Fund = fund.ToArray();
×
50
        m_N = m_Delta.Length;
×
51
        if (m_Fund.Length != m_N)
×
52
            throw new ArgumentException("数组大小不匹配");
×
53
    }
×
54

55
    /// <summary>
56
    ///     试算净值
57
    /// </summary>
58
    /// <param name="b">收益率+1</param>
59
    /// <param name="val">净值</param>
60
    /// <param name="der">净值对收益率的偏导数</param>
61
    private void Value(double b, out double val, out double der)
62
    {
×
63
        val = 0D;
×
64
        der = 0D;
×
65
        for (var i = 0; i < m_N; i++)
×
66
        {
×
67
            var v = Math.Pow(b, m_Delta[i] - 1);
×
68
            val += v * b * m_Fund[i];
×
69
            der += v * m_Delta[i] * m_Fund[i];
×
70
        }
×
71
    }
×
72

73
    /// <summary>
74
    ///     采用牛顿下山迭代法求解收益率
75
    /// </summary>
76
    /// <returns>收益率</returns>
77
    public double Solve()
78
    {
×
79
        var lambda = 1D; // learning rate
×
80

81
        var credit = 1000000; // avoid dead loop
×
82
        while (credit-- > 0)
×
83
        {
×
84
            var b = 1D; // the searching variable
×
85

86
            var dir = 0; // search direction
×
87
            while (credit-- > 0)
×
88
            {
×
89
                Value(b, out var v, out var d);
×
90
                if (v.IsZero())
×
91
                    return b - 1;
×
92

93
                var del = v / d; // Newton
×
94
                if (dir > 0)
×
95
                {
×
96
                    if (del < 0)
×
97
                        break;
×
98
                }
×
99
                else if (dir < 0)
×
100
                {
×
101
                    if (del > 0)
×
102
                        break;
×
103
                }
×
104
                else
105
                    dir = del > 0 ? 1 : -1;
×
106

107
                b -= lambda * del;
×
108
            }
×
109

110
            lambda /= 2;
×
111
        }
×
112

113
        return double.NaN;
×
114
    }
×
115
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc