Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I've been using the Lotka-Volterra equation to estimate population sizes and need to find the MAD (mean absolute deviation), MSD (mean squared deviation) and mape (mean absolute percentage error) for my estimated population data vs the actual population data. I have 14 years of data. Important part of code I've been working on is below. However when I run this I get wrong/strange output. Any ideas? The sum code I'm using for MSD is likely the one that needs to be fixed and is at the very end. Thanks.

MyLVPredatorError[myPreyPopList_, myPredatorPopList_, myalpha_, myc_, myd_, myh_] := 
    Module [{n, myPredatorPopValueList, myPredatorPopErrorList, 
    myLastN, myLastP, CurrentP, MSD, MSDError, MSDSqr, 
    numberOfValues, DebugFlag = False},

    (* Step #3: Calculate the Predator Population Values  *)                                 
    If[DebugFlag, Print["Entering While loop"]];
               (*************)                                    
    myPredatorPopValueList = myPreyPopList;
    myLastN = myPreyPopList[[1]];
    myLastP = myPredatorPopList[[1]];
    n = 1;

    (* Calculate the prey population for varying number of predators *) 
    While[n <= numberOfValues,
            CurrentP = myLastP + (((-myd*myLastP) + (myc*myalpha*myLastN*myLastP))*myh);
          myPredatorPopValueList[[n]] = CurrentP;
          myLastN = myPreyPopList[[n + 1]];
          myLastP = myPredatorPopList[[n + 1]];                                          
          myPredatorPopValueList[[n]] = CurrentP;
          MSDError = myLastP - CurrentP;
          MSDSqr = MSDError*MSDError;   

          If[DebugFlag, Print["n = ", n]];                                          
          If[DebugFlag, Print["numberOfValues =", numberOfValues]];                                           
          If[DebugFlag, Print["myPredatorPopErrorList =", myPredatorPopErrorList]];

          n = n + 1 
       (* end of While loop *) 
    ];                                
    If[DebugFlag, Print["leaving While loop"]];                                             
    MSD = Sum[MSDSqr, {n, numberOfValues}]/14;

    Return[MSD];  
    (* end of the MyLVPredatorSoln module *)                              
];
share|improve this question
    
Hard to tell. You should define "wrong/strange". Here I see only a portion of code, called with unknown parameters, and I have no idea what the data looks like, so I cannot tell if you just forgot a 14 in MSD = Sum[MSDSqr, {n, numberOfValues}]/14 and you find strange result because your test data is not 14 years long... Moreover, procedural style code is not the best way to do things in MMA (but I have to concede sometimes it is). –  Peltio Apr 29 at 2:08
    
This is the population data i'm using (myList = predator, myList2 = prey) myList = {87, 91, 77, 79, 69, 61, 54, 52, 55, 29, 41, 58, 41, 56}; myList2 = {7373, 11221, 9254, 10608, 6847, 9284, 5923, 6556, 9118, 4562, 10836, 10838, 9573, 8474}; This is my input: myLionAndWWError = MyLVPredatorError[myList2, myList, 0.00198, 0.01685, 0.2, 1] And this is the output i'm getting: (-60.6322 + {87, 91, 77, 79, 69, 61, 54, 52, 55, 29, 41, 58, 41, 56}[[ 15]])^2 I limited the parameters in parts of the code I didn't include but i'm almost certain the parameters are not the problem :/ –  user13307 Apr 29 at 18:24
    
You are taking the n+1'th part. So when n == 14, you are trying to take the 15th part. But your list is not that long. Perhaps you need a strictly less than in your While condition? –  wxffles Apr 29 at 21:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.