Documentation
¶
Overview ¶
Example (BondPrice) ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
c := engine.New()
c.FinN = 5
c.Y = 7
c.X = 6
c.BondPrice()
fmt.Printf("Bond price: $%.2f\n", c.X)
}
Output: Bond price: $97.74
Example (DateAdd) ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
c := engine.New()
c.Flags.Dmy = false
c.Y = 3.012025
c.X = 90
c.DateAdd()
fmt.Printf("Date after 90 days: %.6f\n", c.X)
}
Output: Date after 90 days: 5.302025
Example (DateDays) ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
c := engine.New()
c.Flags.Dmy = false
c.Y = 1.012025
c.X = 6.152025
c.DaysBetween()
fmt.Printf("Days between: %.0f\n", c.X)
}
Output: Days between: 165
Example (DepreciationSL) ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
c := engine.New()
c.FinN = 10
c.FinPV = 50000
c.FinFV = 5000
c.X = 1
c.DepreciationSL()
fmt.Printf("Annual depreciation: $%.2f\n", c.X)
}
Output: Annual depreciation: $4500.00
Example (FutureValue) ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
c := engine.New()
c.FinN = 20
c.FinI = 8
c.FinPV = -10000
c.FinPMT = 0
c.SolveFV()
fmt.Printf("Future value: $%.2f\n", c.X)
}
Output: Future value: $46609.57
Example (Irr) ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
c := engine.New()
c.FinCF0 = -100000
c.FinCFj[0] = 40000
c.FinCFj[1] = 50000
c.FinCFj[2] = 30000
c.FinCfCnt = 3
c.Flags.StackLift = true
c.ComputeIRR()
fmt.Printf("IRR: %.2f%%\n", c.X)
}
Output: IRR: 10.13%
Example (Mortgage) ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
c := engine.New()
c.FinN = 30 * 12
c.FinI = 6.0 / 12.0
c.FinPV = 300000
c.FinFV = 0
c.SolvePMT()
fmt.Printf("Monthly payment: $%.2f\n", -c.X)
}
Output: Monthly payment: $1798.65
Example (Npv) ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
c := engine.New()
c.FinCF0 = -100000
c.FinCFj[0] = 30000
c.FinCFj[1] = 40000
c.FinCFj[2] = 50000
c.FinCFj[3] = 60000
c.FinCfCnt = 4
c.X = 10
c.Flags.StackLift = true
c.ComputeNPV()
fmt.Printf("NPV: $%.2f\n", c.X)
}
Output: NPV: $38877.13
Example (Statistics) ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
c := engine.New()
c.X, c.Y = 10, 1
c.StatAdd()
c.X, c.Y = 15, 2
c.StatAdd()
c.X, c.Y = 22, 3
c.StatAdd()
c.X, c.Y = 18, 4
c.StatAdd()
c.X, c.Y = 25, 5
c.StatAdd()
c.MeanX()
meanX := c.X
c.SDev()
sdev := c.X
fmt.Printf("Mean: %.1f, StdDev: %.1f\n", meanX, sdev)
}
Output: Mean: 3.0, StdDev: 1.6
Index ¶
- Constants
- Variables
- func DepDB(cost, salvage, life, period, factorPct float64) (dep, remaining float64)
- func DepSL(cost, salvage, life, period float64) (dep, remaining float64)
- func DepSOYD(cost, salvage, life, period float64) (dep, remaining float64)
- func FV(rate, n, pv, pmt float64, when Timing) float64
- func IRR(cashflows ...float64) (float64, error)
- func NPV(rate float64, cashflows ...float64) float64
- func NPer(rate, pmt, pv, fv float64, when Timing) (float64, error)
- func NaNf64() float64
- func PMT(rate, n, pv, fv float64, when Timing) float64
- func PV(rate, n, pmt, fv float64, when Timing) float64
- func Rate(n, pmt, pv, fv float64, when Timing) (float64, error)
- func SolveTVM(n, i, pv, pmt, fv float64, when Timing) (float64, error)
- type AngleMode
- type Engine
- func (e *Engine) Abs()
- func (e *Engine) Acos()
- func (e *Engine) Add()
- func (e *Engine) Amortize()
- func (e *Engine) Asin()
- func (e *Engine) Atan()
- func (e *Engine) BST()
- func (e *Engine) BondPrice()
- func (e *Engine) BondYield()
- func (e *Engine) Chs()
- func (e *Engine) ClearFin()
- func (e *Engine) ClearPgm()
- func (e *Engine) ClearReg()
- func (e *Engine) ClearStats()
- func (e *Engine) Clx()
- func (e *Engine) ComputeIRR()
- func (e *Engine) ComputeNPV()
- func (e *Engine) Cos()
- func (e *Engine) DateAdd()
- func (e *Engine) DaysBetween()
- func (e *Engine) DepreciationDB()
- func (e *Engine) DepreciationSL()
- func (e *Engine) DepreciationSOYD()
- func (e *Engine) Div()
- func (e *Engine) Enter()
- func (e *Engine) Exp()
- func (e *Engine) Exp10()
- func (e *Engine) Fact()
- func (e *Engine) Frac()
- func (e *Engine) Goto(line int)
- func (e *Engine) Intg()
- func (e *Engine) LastXRecall()
- func (e *Engine) LinEst()
- func (e *Engine) Ln()
- func (e *Engine) Log()
- func (e *Engine) MeanX()
- func (e *Engine) MeanY()
- func (e *Engine) Mul()
- func (e *Engine) Pct()
- func (e *Engine) PctChg()
- func (e *Engine) PctTotal()
- func (e *Engine) Pi()
- func (e *Engine) Push()
- func (e *Engine) Recall(idx int)
- func (e *Engine) Recip()
- func (e *Engine) Restore(s EngineState)
- func (e *Engine) RollDown()
- func (e *Engine) RollUp()
- func (e *Engine) SDev()
- func (e *Engine) SST()
- func (e *Engine) SetFV()
- func (e *Engine) SetI()
- func (e *Engine) SetN()
- func (e *Engine) SetPMT()
- func (e *Engine) SetPV()
- func (e *Engine) Sin()
- func (e *Engine) Snapshot() EngineState
- func (e *Engine) SolveFV()
- func (e *Engine) SolveI()
- func (e *Engine) SolveN()
- func (e *Engine) SolvePMT()
- func (e *Engine) SolvePV()
- func (e *Engine) Sqr()
- func (e *Engine) Sqrt()
- func (e *Engine) StatAdd()
- func (e *Engine) Store(idx int)
- func (e *Engine) Sub()
- func (e *Engine) Tan()
- func (e *Engine) ToDeg()
- func (e *Engine) ToH()
- func (e *Engine) ToHMS()
- func (e *Engine) ToPolar()
- func (e *Engine) ToRad()
- func (e *Engine) ToRect()
- func (e *Engine) Tuck()
- func (e *Engine) WeightedMean()
- func (e *Engine) XY()
- func (e *Engine) YPowX()
- type EngineState
- type Flags
- type Instruction
- type Registers
- type Stack
- type Timing
Examples ¶
Constants ¶
const MaxDigits = 10
Variables ¶
var ( // ErrNoConvergence is returned by Rate/IRR when the iterative solver fails // to converge (e.g. a cash-flow stream with no valid internal rate). ErrNoConvergence = errors.New("engine: solution did not converge") // ErrNoSolution is returned when the inputs admit no finite answer. ErrNoSolution = errors.New("engine: no solution for the given inputs") )
Functions ¶
func DepDB ¶
DepDB returns declining-balance depreciation for the given period and the remaining book value (above salvage) after that period. factorPct is the declining-balance factor as a percent (200 == double-declining balance); zero defaults to 200%.
func DepSL ¶
DepSL returns straight-line depreciation for the given period and the remaining depreciable value after that period.
Example ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
// $50,000 asset, $5,000 salvage, 10-year life, first year.
dep, remaining := engine.DepSL(50000, 5000, 10, 1)
fmt.Printf("Year 1 depreciation: $%.2f, remaining: $%.2f\n", dep, remaining)
}
Output: Year 1 depreciation: $4500.00, remaining: $40500.00
func DepSOYD ¶
DepSOYD returns sum-of-the-years'-digits depreciation for the given period and the remaining depreciable value after that period.
func FV ¶
FV returns the future value of a series of cash flows.
Example ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
// $10,000 invested for 20 years at 8% (outflow now, so PV is negative).
fv := engine.FV(0.08, 20, -10000, 0, engine.End)
fmt.Printf("Future value: $%.2f\n", fv)
}
Output: Future value: $46609.57
func IRR ¶
IRR returns the internal rate of return (as a fraction) of a cash-flow stream, i.e. the rate at which NPV is zero. cashflows[0] is the period-0 flow. It returns ErrNoConvergence if the solver does not converge.
Example ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
irr, _ := engine.IRR(-100000, 40000, 50000, 30000)
fmt.Printf("IRR: %.2f%%\n", irr*100)
}
Output: IRR: 10.13%
func NPV ¶
NPV returns the net present value of a cash-flow stream discounted at rate (a fraction). cashflows[0] is the period-0 flow (typically the initial outlay); cashflows[k] occurs at the end of period k.
Example ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
npv := engine.NPV(0.10, -100000, 30000, 40000, 50000, 60000)
fmt.Printf("NPV: $%.2f\n", npv)
}
Output: NPV: $38877.13
func NPer ¶
NPer solves for the number of periods. It returns ErrNoSolution when the cash flows never reach the target future value.
func PMT ¶
PMT returns the periodic payment for a loan or annuity.
Example ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
// A $300,000 mortgage at 6% APR over 30 years, paid monthly.
pmt := engine.PMT(0.06/12, 360, 300000, 0, engine.End)
fmt.Printf("Monthly payment: $%.2f\n", -pmt)
}
Output: Monthly payment: $1798.65
func Rate ¶
Rate solves for the periodic interest rate (as a fraction) using Newton-Raphson iteration. It returns ErrNoConvergence if no rate is found.
Example ¶
package main
import (
"fmt"
"github.com/shanehull/dozen/engine"
)
func main() {
// What rate turns $1,000 today into $2,000 after 12 periods?
r, _ := engine.Rate(12, 0, -1000, 2000, engine.End)
fmt.Printf("Rate: %.3f%%\n", r*100)
}
Output: Rate: 5.946%
Types ¶
type Engine ¶
type Engine struct {
LastX float64
Mem [20]float64
StatsN, StatsSx, StatsSy, StatsSxx, StatsSyy, StatsSxy float64
StatsLx, StatsLy, StatsWx, StatsWxx float64
PgmLen int
PgmPC int
Program [200]Instruction
Flags Flags
Stack
Registers
}
func (*Engine) ClearStats ¶
func (e *Engine) ClearStats()
func (*Engine) ComputeIRR ¶
func (e *Engine) ComputeIRR()
func (*Engine) ComputeNPV ¶
func (e *Engine) ComputeNPV()
func (*Engine) DaysBetween ¶
func (e *Engine) DaysBetween()
func (*Engine) DepreciationDB ¶
func (e *Engine) DepreciationDB()
func (*Engine) DepreciationSL ¶
func (e *Engine) DepreciationSL()
func (*Engine) DepreciationSOYD ¶
func (e *Engine) DepreciationSOYD()
func (*Engine) LastXRecall ¶
func (e *Engine) LastXRecall()
func (*Engine) Restore ¶
func (e *Engine) Restore(s EngineState)
func (*Engine) Snapshot ¶
func (e *Engine) Snapshot() EngineState
func (*Engine) WeightedMean ¶
func (e *Engine) WeightedMean()
type EngineState ¶
type EngineState struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Z float64 `json:"z"`
T float64 `json:"t"`
Last float64 `json:"last"`
Mem [20]float64 `json:"mem"`
FinN float64 `json:"finN"`
FinI float64 `json:"finI"`
FinPV float64 `json:"finPV"`
FinPMT float64 `json:"finPMT"`
FinFV float64 `json:"finFV"`
FinCF0 float64 `json:"finCF0"`
FinCFj [10]float64 `json:"finCFj"`
FinNj [10]int `json:"finNj"`
FinCfCnt int `json:"finCfCnt"`
AmortN float64 `json:"amortN"`
AmortInt float64 `json:"amortInt"`
AmortPrin float64 `json:"amortPrin"`
Begin bool `json:"begin"`
Dmy bool `json:"dmy"`
Angle AngleMode `json:"angle"`
}
func UnmarshalState ¶
func UnmarshalState(data []byte) (EngineState, error)
func (EngineState) Marshal ¶
func (s EngineState) Marshal() ([]byte, error)