当我在操场上尝试这个时:
func StockEvolution(S_0:Double, _ down:Double, _ up:Double, _ totalsteps:Int, _ upsteps:Int) -> Double // function being used in calcCall()
{
var S_t:Double = S_0 * pow(up, Double(upsteps)) * pow(down, Double(totalsteps - upsteps))
return S_t
}
func CallPayoff(S:Double, _ K:Double) -> Double // function being used in calcCall()
{
return max(S - K, 0.0)
}
func calcCall(S_0:Double, _ down:Double, _ up:Double, _ r:Double, _ steps:Int, _ K:Double) -> Double //calculate Call-Option
{
var prices = [Double]()
var q = 0.6 //risk-neutral probability factor
var i = 0
while i < steps
{
var payOff = CallPayoff(StockEvolution(S_0, down, up, steps, i), K)
prices.append(payOff)
i += 1
}
var n = steps - 1
while n >= 0
{
var j = 0
while j <= n
{
var value = (1 / r) * (prices[j + 1] * q + (1 - q) * prices[j])
prices.removeAtIndex(j)
prices.insert(value, atIndex: j)
j += 1
}
n -= 1
}
return prices[0]
}
通过做这个:
var checkPrice = calcCall(100, 0.6, 1.5, 1.05, 10, 200)
它给了我这个错误:
执行被中断,原因:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
我似乎无法在我的代码中找到错误。我尝试了不同的输入值,但错误仍然存在。
如果您可以查看我的代码并帮助我解决此问题,那就太好了。感谢您的努力。