本文整理汇总了C#中IFunction.Evaluate方法的典型用法代码示例。如果您正苦于以下问题:C# IFunction.Evaluate方法的具体用法?C# IFunction.Evaluate怎么用?C# IFunction.Evaluate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IFunction
的用法示例。
在下文中一共展示了IFunction.Evaluate方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Mean
static double Mean(IFunction f, double a, double b)
{
double sum = 0;
int N = 20;
double h = b - a;
double dt = h / N;
for (int z = 0; z < N; z++)
sum += f.Evaluate(a + (dt * z)) * dt;
return sum / h;
}
示例2: EvaluateFunction
/// <summary>
/// Evaluates a function and also validates it's return value and parameter data types
/// </summary>
/// <param name="context">The evaluation context instance.</param>
/// <param name="functionInstance">The function to call</param>
/// <param name="arguments">The function arguments</param>
/// <returns>The return value of the function</returns>
public static EvaluationValue EvaluateFunction(EvaluationContext context, IFunction functionInstance, params IFunctionParameter[] arguments)
{
if (context == null) throw new ArgumentNullException("context");
// If the caller is in a missing attribute state the function should not be called
if (context.IsMissingAttribute)
{
context.Trace("There's a missing attribute in the parameters");
return EvaluationValue.Indeterminate;
}
else
{
// Validate function defined arguments
int functionArgumentIdx;
for (functionArgumentIdx = 0; functionArgumentIdx < functionInstance.Arguments.Length; functionArgumentIdx++)
{
// Validate the value is not an Indeterminate value
if (arguments[functionArgumentIdx] is EvaluationValue &&
((EvaluationValue)arguments[functionArgumentIdx]).IsIndeterminate)
{
if (!context.IsMissingAttribute)
{
context.ProcessingError = true;
}
context.Trace("There's a parameter with Indeterminate value");
return EvaluationValue.Indeterminate;
}
// Compare the function and the value data type
if (((functionInstance.Arguments[functionArgumentIdx] != arguments[functionArgumentIdx].GetType(context)) &&
((functionInstance.Arguments[functionArgumentIdx] != DataTypeDescriptor.Bag) && (arguments[functionArgumentIdx] is BagValue))))
{
context.ProcessingError = true;
context.Trace("There's a parameter with an invalid datatype");
return EvaluationValue.Indeterminate;
}
}
//If the function supports variable arguments, the last datatype is used to validate the
//rest of the parameters
if (functionInstance.VarArgs)
{
functionArgumentIdx--;
for (int argumentValueIdx = functionArgumentIdx; argumentValueIdx < arguments.Length; argumentValueIdx++)
{
// Validate the value is not an Indeterminate value
if (arguments[argumentValueIdx] is EvaluationValue && ((EvaluationValue)arguments[argumentValueIdx]).IsIndeterminate)
{
if (!context.IsMissingAttribute)
{
context.ProcessingError = true;
}
context.Trace("There's a parameter with Indeterminate value");
return EvaluationValue.Indeterminate;
}
// Compare the function and the value data type
if ((functionInstance.Arguments[functionArgumentIdx] != arguments[argumentValueIdx].GetType(context)) &&
((arguments[argumentValueIdx] is BagValue) && (functionInstance.Arguments[functionArgumentIdx] != DataTypeDescriptor.Bag)))
{
context.ProcessingError = true;
context.Trace("There's a parameter with an invalid datatype");
return EvaluationValue.Indeterminate;
}
}
}
var sb = new StringBuilder();
// Call the function in a controlled evironment to capture any exception
try
{
sb.Append(functionInstance.Id);
sb.Append("( ");
bool isFirst = true;
foreach (IFunctionParameter param in arguments)
{
if (isFirst)
{
isFirst = false;
}
else
{
sb.Append(", ");
}
sb.Append(param.ToString());
}
sb.Append(" )");
sb.Append(" = ");
EvaluationValue returnValue = functionInstance.Evaluate(context, arguments);
sb.Append(returnValue.ToString());
context.Trace(sb.ToString());
//.........这里部分代码省略.........
示例3: LogAverageFactor
public static double LogAverageFactor(double y, IFunction func, Vector x)
{
return (y == func.Evaluate(x)) ? 0.0 : Double.NegativeInfinity;
}
示例4: HestonCallSimulationOptimizationProblem
public HestonCallSimulationOptimizationProblem(EquityCalibrationData equityCalData, Vector matBound, Vector strikeBound)
: base(equityCalData,matBound,strikeBound)
{
//this.dyFunc = equityCalData.dyFunc;
this.dyFunc = CallEstimator.IstantaneousDividendYield(equityCalData);
this.zrFunc = equityCalData.zrFunc;
// Generates common random numbers
I = (int)Math.Ceiling(cpmd.Maturity[Range.End] / dt);
epsilon = new Matrix(I, 2*N);
NewRandomNumbers();
// Precalculates istantaneous growth rates
growth = new Vector(I);
for (int i = 0; i < I; i++)
{
double t = i * dt;
double zr_t = zrFunc.Evaluate(t);
growth[i] = zr_t + FunctionHelper.Partial(zr_t,zrFunc, new Vector() { t }, 0, dt) * t - dyFunc.Evaluate(t);
}
Console.WriteLine("Heston Simulation Calibration: Paths=" + N);
}
示例5: ToMatrix
internal static Matrix ToMatrix(IFunction func)
{
int n = 20;
double ub=5;
var sup = func as ISupport1D;
if (sup != null)
ub = sup.Support[1];
var mat = new Matrix(n,2);
for (int i = 0; i < n; i++)
{
double x = i*ub / n;
mat[i, 0] = x;
mat[i, 1] = func.Evaluate(x);
}
return mat;
}
示例6: LocVolMatrixFromImpliedVol
private Matrix LocVolMatrixFromImpliedVol(CallPriceMarketData Hdataset, IFunction impVol, out Vector locVolMat, out Vector locVolStr)
{
int nmat = calibrationSettings.LocalVolatilityMaturities;
int nstrike = calibrationSettings.LocalVolatilityStrikes;
double lastMat = Hdataset.Maturity[Range.End];
double lastStr = Hdataset.Strike[Range.End];
locVolMat = Vector.Linspace(Hdataset.Maturity[0], lastMat, nmat);
locVolStr = Vector.Linspace(Hdataset.Strike[0], lastStr, nstrike);
Matrix locVolMatrix = new Matrix(nmat, nstrike);
Integrate integrate = new Integrate(this);
double sigma, dSigmadk, num, y, den, avgGrowthRate;
Vector x = new Vector(2);
for (int i = 0; i < nmat; i++)
{
avgGrowthRate = integrate.AdaptLobatto(0.0, locVolMat[i]);
int j = 0;
x[0] = locVolMat[i];
x[1] = locVolStr[j];
sigma = impVol.Evaluate(x);
dSigmadk = impVol.Partial(x, 1);
num = Math.Pow(sigma, 2) + 2.0 * sigma * x[0] * impVol.Partial(x, 0);
den = 1.0;
locVolMatrix[i, j] = Math.Sqrt(num / den);
// The rest of the cycle.
for (j = 1; j < nstrike; j++)
{
x[1] = locVolStr[j];
sigma = impVol.Evaluate(x);
dSigmadk = impVol.Partial(x, 1);
num = Math.Pow(sigma, 2) + 2.0 * sigma * x[0] *
(impVol.Partial(x, 0) + avgGrowthRate * x[1] * dSigmadk);
y = Math.Log(locVolStr[j] / Hdataset.S0) + avgGrowthRate;
den = System.Math.Pow(1.0 - x[1] * y * dSigmadk / sigma, 2) + x[1] * sigma * x[0] *
(dSigmadk - 0.25 * x[1] * sigma * x[0] * dSigmadk * dSigmadk + x[1] * impVol.Partial2(x, 1));
locVolMatrix[i, j] = Math.Sqrt(num / den);
}
}
return locVolMatrix;
}