我创建了一个抽象类,并且我有一个抽象方法,用于计算用于计算期权价格的随机数向量。以下是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Project1
{
public abstract class Random_Numbers
{
public Random_Numbers()
{
newSteps = 0;
newTrials = 0;
newStockPrice = 0;
newSigma = 0;
newMu = 0;
newStrike = 0;
newTenor = 0;
}
public Random_Numbers(int Steps, int Trials, double Strike,double StockPrice, double Mu, double Tenor,double Sigma)
{
newSteps = Steps;
newTrials = Trials;
newStrike = Strike;
newStockPrice = StockPrice;
newMu = Mu;
newTenor = Tenor;
newSigma = Sigma;
}
public int getSteps()
{
return newSteps;
}
public void setSteps(int Steps)
{
newSteps = Steps;
}
public double getTrials()
{
return newTrials;
}
public void setTrials(int Trials)
{
newTrials = Trials;
}
public double getStockprice()
{
return newStockPrice;
}
public void setStockprice(double StockPrice)
{
newStockPrice = StockPrice;
}
public double getSigma()
{
return newSigma;
}
public void setSigma(double Sigma)
{
newSigma = Sigma;
}
public double getMu()
{
return newMu;
}
public void setMu(double Mu)
{
newMu = Mu;
}
public double getTenor()
{
return newTenor;
}
public void setTenor(double Tenor)
{
newTenor =Tenor;
}
public double getStrike()
{
return newStrike;
}
public void setStrike(double Strike)
{
newStrike = Strike;
}
public abstract double GenerateRandom();
public int newSteps;
public int newTrials;
public double newStockPrice;
public double newSigma;
public double newMu;
public double newTenor;
public double newStrike;
}
}
//My derived class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Project1
{
class PriceSimulation:Project1.Random_Numbers
{
public override double GenerateRandom()
{
double[] r = new double[newSteps + 1];
double[] p = new double[newSteps + 1];
double[] v = new double[newSteps + 1];
double[] d = new double[newSteps + 1];
double[] l = new double[newSteps + 1];
double[,] Z = new double[newTrials + 1, newSteps + 1];
int i = 0;
int j = 0;
Random rnd = new Random();
for (j = 1; j <= newTrials + 1; j++)
{
for (i = 1; i <= newSteps + 1; i++)
{
do
{
r[i] = rnd.Next(-1, 1);
p[i] = rnd.Next(-1, 1);
v[i] = 2 * r[i] - 1;
d[i] = 2 * p[i] - 1;
l[i] = Math.Pow(v[i], 2) + Math.Pow(d[i], 2);
}
while (l[i] >= 1);
}
Z[i, j] = Math.Sqrt(-2 * Math.Log(l[i]) / l[i]) * v[i];
}
return Z[i, j];
}
public double CPriceCalc()
{
double[,] Prices = new double[newTrials, newSteps];
double[,] CPayoff = new double[newTrials, 1];
double[,] Exercise = new double[newTrials, 1];
Exercise[newTrials, 1] = newStrike;
Prices[0, 0] = newStockPrice;
double dt = newTrials / newSteps;
int i = 0;
int j = 0;
for (j = 1; j <= newTrials; ++j)
{
for (i = 1; i <= newSteps; ++i)
{
Prices[i, j] = Prices[i, j - 1] * Math.Exp((newMu - .5 * (Math.Pow(newSigma, 2)) * dt) + newSigma * Math.Sqrt(dt) * Z[i, j]);
CPayoff[newTrials, 1] = Math.Max(Prices[newSteps, j] - Exercise[newTrials, 1], 0);
}
}
return CPayoff[newTrials, 1];
}
public double PPriceCalc()
{
double[,] Prices = new double[newTrials, newSteps];
double[,] PPayoff = new double[newTrials, 1];
double[,] Exercise = new double[newTrials, 1];
Exercise[newTrials, 1] = newStrike;
Prices[0, 0] = newStockPrice;
double dt = newTrials / newSteps;
int i = 0;
int j = 0;
for (j = 1; j <= newTrials; ++j)
{
for (i = 1; i <= newSteps; ++i)
{
Prices[i, j] = Prices[i, j - 1] * Math.Exp((newMu - .5 * (Math.Pow(newSigma, 2)) * dt) + newSigma * Math.Sqrt(dt) * Z[i, j]);
PPayoff[newTrials, 1] = Math.Max(Exercise[newTrials, 1] - Prices[newSteps, j], 0);
}
}
return PPayoff[newTrials, 1];
}
public double COptionPrice()
{
for (int k = 1; k <= CPayoff.Length; k++)
{
Sum += CPayoff[k, 1];
}
return CallPrice = (Sum / CPayoff.Length) * Math.Exp(-newMu * newTenor);
}
public double POptionPrice()
{
double PutPrice;
for (int k = 1; k <= CPayoff.Length; k++)
{
Sum += PPayoff[k, 1];
}
return PutPrice = (Sum / PPayoff.Length) * Math.Exp(-newMu * newTenor);
}
//Member variables and arrays.
public double[,] Z;
public double[,] CPayoff;
public double[,] PPayoff;
public double Sum;
public double CallPrice;
public double PutPrice;
}
}
我的问题是我收到警告“Field'Project1.PriceSimulation.Z'永远不会被分配给,并且总是将其默认值设为null”。
答案 0 :(得分:3)
我认为你有一个局部变量
double[,] Z = new double[newTrials + 1, newSteps + 1];
遮蔽你的场地
public double[,] Z;