Community

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
dmg34
Community Member

Problem with a list/sort function for a formula problem

Jump to solution

I was very eager to try applying some of the techniques in  @James ​'s post, Looking up Values in Formula Questions. I'm trying to write problems for a stats class, and I think my case is actually even a little bit easier than the one addressed in the blog post. My list is the areas to the left of z, over a range of z-scores (for this question, the z scores can range from z=0.10 to z=1.50).

I believe I understand everything in the blog post and felt ready to tackle designing my question. I have produced the comma-delimited list (I use Excel for the values and then pasted into Word and cleaned it up). But, when I try to enter it into Canvas per the directions in James' post, I get the following error:

Capture.PNG

I have tried to determine the issue by test-entering a short list, such as list=sort(5,3,4) or even just sort(5,3,4), and various permutations and variations thereof. Nothing works, I always get this error. I also tried the "reverse" function. I am at a loss for why this isn't working, and obviously, it has stopped me dead in my tracks for the result I am trying to accomplish! I will be tickled pink if I can get this question to work the way I want it to (it opens up soooo many possibilities for using Canvas to test this topic!) but I don't know what to do now. Any help would be greatly appreciated!

1 Solution

Accepted Solutions
James
Community Member

That error is one that started popping up when Canvas decided to redo the way they formatted numbers. That is the same fiasco that added a .0 to the end of integers so 5 comes out as 5.0

They are checking for a method called .toPrecision() when they display a number. The problem is that sort() and reverse() don't return numbers, they return an array (list) of numbers of the .toPrecision() method doesn't exist for the list type.

The error is telling you that it doesn't know how to format a list of numbers with the indicated number of decimal places.

That's what the error means, now the real question is what should you do?

Your real issue is that you're missing the at() function, which tells it which item in the list to return. Once you give it a value to return, then it is able to correctly format it and doesn't return the error.

204751_pastedImage_0.png

at() is a 0-based pointer into the list. So my value of x is 1, but starting with 0, 1 is the 2nd number in my list, which is why it returns a 2.

Note that the list still was defined even if the error message shows up. I can't guarantee that, just mentioning it.

Unless you need the list multiple times, I try to just use the at() function around the list itself. Instead of list=sort(1,2,3,4) followed by at(list,x) I just combine them and use at(sort(1,2,3,4),x)

204758_pastedImage_9.png

That's a little harder to read but will avoid the TypeError message.

In my opinion, users shouldn't see this error message. A much more appropriate one would be something like "Not a single value" or "List". It used to be that it showed the list so you could see what was happening, and that was nice too. Their code should probably check to see if there is a .toPrecision() method and not call it when there isn't. The current approach lets someone know that something isn't working - although it's a false positive in this case. That should really be handled in development code, not in production code that is released to the public.

Hopefully it will get fixed in the Modern Quizzing Engine.

View solution in original post

13 Replies
James
Community Member

That error is one that started popping up when Canvas decided to redo the way they formatted numbers. That is the same fiasco that added a .0 to the end of integers so 5 comes out as 5.0

They are checking for a method called .toPrecision() when they display a number. The problem is that sort() and reverse() don't return numbers, they return an array (list) of numbers of the .toPrecision() method doesn't exist for the list type.

The error is telling you that it doesn't know how to format a list of numbers with the indicated number of decimal places.

That's what the error means, now the real question is what should you do?

Your real issue is that you're missing the at() function, which tells it which item in the list to return. Once you give it a value to return, then it is able to correctly format it and doesn't return the error.

204751_pastedImage_0.png

at() is a 0-based pointer into the list. So my value of x is 1, but starting with 0, 1 is the 2nd number in my list, which is why it returns a 2.

Note that the list still was defined even if the error message shows up. I can't guarantee that, just mentioning it.

Unless you need the list multiple times, I try to just use the at() function around the list itself. Instead of list=sort(1,2,3,4) followed by at(list,x) I just combine them and use at(sort(1,2,3,4),x)

204758_pastedImage_9.png

That's a little harder to read but will avoid the TypeError message.

In my opinion, users shouldn't see this error message. A much more appropriate one would be something like "Not a single value" or "List". It used to be that it showed the list so you could see what was happening, and that was nice too. Their code should probably check to see if there is a .toPrecision() method and not call it when there isn't. The current approach lets someone know that something isn't working - although it's a false positive in this case. That should really be handled in development code, not in production code that is released to the public.

Hopefully it will get fixed in the Modern Quizzing Engine.

dmg34
Community Member

 @James , thank you, thank you, thank you! I did as you suggested, and it worked perfectly!  I'm so thrilled. I *love* being able to create meaningful/useful practice AND graded quizzes via Canvas, and this is one that I really wanted to make work! I'm going to take this and run with it now and I foresee lots of awesome Normal Distribution questions in my students' Canvas future! Smiley Wink

I am, officially, tickled pink!

It would be great if the Modern Quizzing Engine would address this - or just have some of the frequently used statistical functions built-in!

dmg34
Community Member

Ack....  @James ​ (or anyone else who knows), now I have a new and different problem. This was going so well - I have a basic P(X<k) problem and a P(X>k) problem, but now I'm trying to do a P(m<X<k) problem. Look at what is happening.

Here is the question text and variable definitions:

Capture.PNG

Obviously, I carefully limited my "upper bound" and "lower bound", so that the upper bound would always be higher than the lower bound. Everything worked find with the example values.

But now look at my generated values:

Capture2.PNG

In the very first set of values, the upper bound is BELOW the minimum allowed by my variable definitions! What the what?? There are several others in the generated values which are like this - either UB or LB outside the allowed values - causing the final answer to be negative! This is no good, no good at all. I don't understand why it's doing this. I've never seen the values for the variables in a formula question not adhere to the stated limits. Any thoughts? Did I do something stupid that I'm just not seeing??

Just when everything was going so well.... lol.

dmg34
Community Member

Here is one more piece of weirdness that I don't understand.... in trying to debug the above, I went in to edit the first problem I did. Now I see this:

Capture.PNG

Now, I don't think it's a problem for this particular questions, since I already generated the possible solutions. But if I try to re-generate solutions, I get an error ("The system could not generate any valid combinations for the parameters given"). So I can't make any changes to the question (or I guess I would have to re-enter the "at(sort..." line to do so). I don't know why this would be.

James
Community Member

Did you change something after problem was set up?

I ask because I notice that the question text has [mu], [LB], and [UB] in that order. However, the variable definitions and the table have the order mu, UB, and LB. When I go through and edit the question text and change the order, it changes the order in the Variable Definitions section but the order in the Generate Possible Solutions section remains the same until I generate a new sample and then it matches the variable definitions section.

Your order is different from the text to the definitions but the same in the definitions as the possible solutions, so it looks like something got off somewhere, although I'm not sure where.

You're probably going to have to do some level of recreating the question until it works. If regenerating doesn't fix it, then try redoing the question text and see if it changes the order. You may need to refresh the page or recreate the question.

I imagine it's a one-off rather than an actual error and so you probably don't need to file a bug report. Something probably just got out of sync somewhere.

dmg34
Community Member

Thank you AGAIN! What a great observation. When I reloaded the question, the variable restrictions were in the same order as they are listed in the question, and all seems to have worked properly now. I did have to re-enter the two "at(sort(...." lines due to the other glitchy thing described below, but then when I generated solutions, they all have the correct bounds on the variables! I think I'm back on track!

James
Community Member

I think your formula got cut off. If you look closely, you can see the very last line contains a 0 and nothing else. The formula is incomplete and that's causing it problems. Don't worry about e and d, those aren't your variables, those are the JavaScript variables used to process what you put in.

To double check, I pasted a similar formula, going farther than your 0.9131, and it took the whole thing:

204762_pastedImage_2.png

Now, if you don't put spaces, it limits any one sequence of characters to 1024 characters, so don't try to run the values together without spaces. You didn't do this, I'm just putting the heads up for someone else who tries it.

There are formulas available for approximating the cumulative probability for a standard normal score. You might want to look into one of those. The simplest won't give you 4 decimal place accuracy, but there are several listed on Wikipedia's Normal distribution. Look in the "Numerical approximations for the normal CDF" section. The Zelen and Severo (1964) provides a fifth order polynomial approximation with an error of less than 7.5e-8, which should be more than sufficient for your need.

dmg34
Community Member

Hmmm.... I noticed that it was cut off at the end, but I don't know what's causing that. When I originally enter the line, it is definitely all there - it's when I re-open the question in "edit" mode that it's a problem (which is an annoyance, because I copy the question into the question bank several times and go in and tweak each version; because of this error, I need to delete and re-enter the at(sort... line).

Here is what I entered, from formulas down to the bottom of the list:

Capture.PNG

Capture2.PNG

Then I saved the question, and then clicked edit, and I see this:

Capture3.PNG

So there is definitely something that Canvas is not liking about that line, after I exit and return to the problem. As I said, I can work around it, since it doesn't appear to affect the solutions that I have already generated; but it is still quite curious. It is definitely imposing a 1024 character limit, so that's the problem, though I'm not sure what is triggering it.

Thank you for your tip on the approximation formula. I will try playing around with that as well! And thank you again for all of your time and help! I'm very grateful! Smiley Happy

dmg34
Community Member

Aaaannnnd.... I'm back! I've spent quite a lot of time working on making this work with the approximation formula that  @James ​ directed me to. I ran into a couple of small problems along the way, but was able to work through each one:

1. The formula result was actually TOO precise. Meaning, my students are working from a cumulative standard normal distribution table, which gives the area to 4 decimal places, left of z-values to 2 decimal places. I realized that the results were usually slightly different from the table value, since Canvas was using z to 4 decimal places. Workaround: multiply z*100 and round to integer, then divide by 100. This forces the z to 2 decimal places.

2. The formula is for positive z values only. So I used a zsign variable which is =1 or =-1, compute the area for |z|, and then use an if() statement to get the area, depending on whether the original z is positive or negative.

All of this worked well! And since the formula takes away the limitation on my z-values (using the list approach, I was only allowing -2.5<z<2.5), I can also let sigma be random.

So then I tackled the "between" probability problem, which basically just required 2 lines of each formula - one for z1 and z2. I generated a few sample values to make sure everything was working the way I thought it was. What I found is, occasionally, the final result is off +/- 0.0001! Here is all of my formulas, with example values illustrating the problem:

Capture.PNG

Notice that left2-left1=0.5714 - 0.0384 = 0.5331, should be 0.5330. I saw at least one other example where the result was -0.0001. I guess this is some quirk in Canvas where something in the rounding beyond the 4 decimal places is (sometimes) throwing off the final answer? I suppose I can just set the margin of error to +/- 0.0001. But still puzzled by this! Just when I think I have it allllll figured out! Smiley Wink