Drew Yallop

I need to convert the following C++ (little knowledge) method to
C# (good knowledge). I am having trouble with the memcpy function. It is required here because the intial positions of the vectors must be fixed for the method to work properly. Could you kindly help with this

Thank you,

Drew Yallop


double monte (
int n , // Number of trading opportunities
double *raw , // Raw return of each
int *pos , // Position of each; 1=long; -1=short; 0=neutral
int nreps , // Number of replications, generally at least 1000
double *nonpar , // If not NULL, only signs used for nonparametric test
int *work // Work vector n long avoids shuffling input data
)
{
int i, irep, k1, k2, count, nonpar_count, temp ;
double cand_return, nonpar_cand_return, trial_return, prod ;
memcpy ( work , pos , n * sizeof(int) ) ; // Preserve input positions
/*
Compute the return of the candidate model
If requested, do the same for the nonparametric version
*/
cand_return = 0.0 ;
for (i=0 ; i<n ; i++)
cand_return += posIdea * rawIdea ;
if (nonpar != NULL) { // Do the same using only signs if requested
nonpar_cand_return = 0.0 ;
for (i=0 ; i<n ; i++) {
prod = posIdea * rawIdea ;
if (prod > 0.0)
nonpar_cand_return += 1.0 ;
else if (prod < 0.0)
nonpar_cand_return -= 1.0 ;
}
}
/*
Do the Monte-Carlo replications
*/
count = 0 ; // Counts how many at least as good as candidate
nonpar_count = 0 ; // Ditto for ignoring signs (if requested)
for (irep=0 ; irep<nreps ; irep++) {
k1 = n ; // Shuffle the positions, which are in 'work'
while (k1 > 1) { // While at least 2 left to shuffle
k2 = (int) (unifrand () * k1) ; // Pick an int from 0 through k1-1
if (k2 >= k1) // Should never happen as long as unifrand()<1
k2 = k1 - 1 ; // But this is cheap insurance against disaster
temp = work[--k1] ; // Count down k1 and swap k1, k2 entries
work[k1] = work[k2] ;
work[k2] = temp ;
} // Shuffling is complete when this loop exits
trial_return = 0.0 ;
for (i=0 ; i<n ; i++) // Compute return for this randomly shuffled system
trial_return += workIdea * rawIdea ;
if (trial_return >= cand_return) // If this random system beat candidate
++count ; // Count it
if (nonpar != NULL) { // Do the same using only signs if requested
trial_return = 0.0 ;
for (i=0 ; i<n ; i++) { // Compute return for this randomly shuffled system
prod = workIdea * rawIdea ;
if (prod > 0.0)
trial_return += 1.0 ;
else if (prod < 0.0)
trial_return -= 1.0 ;
}
if (trial_return >= nonpar_cand_return) // If this random system beat candidate
++nonpar_count ; // Count it
}
}
if (nonpar != NULL)
*nonpar = (double) nonpar_count / (double) nreps ;
return (double) count / (double) nreps ;
}


Re: Visual C# General memcpy to C#

Mattias Sjogren

Have you tried using Array.Copy (or Array.CopyTo)






Re: Visual C# General memcpy to C#

Drew Yallop

Thanks Mattias. i think this will work.

Drew