I don't know if this is even possible to do on Crystal reports. What I have is a list of how to report results depending on where the result falls. So if the result (raw data) is 1.63 then I have to round it to the nearest 0.1 so it would be 1.6.
Here is the list:
0-1.0 round to nearest 0.05
1-10 round to nearest 0.1
10-40 round to nearest 1
40-100 round to nearest 5
100-400 round to nearest 10
400-1000 round to nearest 50
1000+ round to nearest 100
I thought using ceiling/floor would work but I don't know what I am doing wrong because it is asking for a boolean right after the then. This is the formula I was attempting to use. Our system uses one form of rounding so we were hoping to use the report to help with the rounding issue.
If ({PRM_SxData.nResult} in 0 to 1.0 )
then (Ceiling ({PRM_SxData.nResult}, 0.05)) and (Floor
({PRM_SxData.nResult}, 0.05))
else
IF {PRM_SxData.nResult} in 1.01 to 10
then ((Ceiling ({PRM_SxData.nResult}, 0.1)) and (Floor
({PRM_SxData.nResult}, 0.1)))
else
IF {PRM_SxData.nResult} in 10.01 to 40
then ((Ceiling ({PRM_SxData.nResult}, 1)) and (Floor ({PRM_SxData.nResult},
1)))
else
IF {PRM_SxData.nResult} in 40.01 to 100
then ((Ceiling ({PRM_SxData.nResult}, 5)) and (Floor ({PRM_SxData.nResult},
5)))
else
IF {PRM_SxData.nResult} in 100.01 to 400
then ((Ceiling ({PRM_SxData.nResult}, 10)) and (Floor ({PRM_SxData.nResult},
10)))
else
IF {PRM_SxData.nResult} in 400.01 to 1000
then ((Ceiling ({PRM_SxData.nResult}, 50)) and (Floor ({PRM_SxData.nResult},
50)))
else
IF {PRM_SxData.nResult} > 1000.01
then ((Ceiling ({PRM_SxData.nResult}, 100)) and (Floor
({PRM_SxData.nResult}, 100)))
else " "
It's messy but it is the best I can come up with.
try this:
If ({PRM_SxData.nResult} >= 0 and {PRM_SxData.nResult}<= 1.0 )
then Floor({PRM_SxData.nResult}, 0.05)
else
IF {{PRM_SxData.nResult} >= in 1.01 and {PRM_SxData.nResult} <= 10
then Floor ({PRM_SxData.nResult}, 0.1)
else
IF {PRM_SxData.nResult} >= 10.01 and {PRM_SxData.nResult} <= 40
then Floor ({PRM_SxData.nResult}, 1)
else
IF {PRM_SxData.nResult} >= 40.01 and {PRM_SxData.nResult} <= 100
then Floor ({PRM_SxData.nResult}, 5)
else
IF {PRM_SxData.nResult} >= 100.01 and {PRM_SxData.nResult} <= 400
then Floor ({PRM_SxData.nResult}, 10)
else
IF {PRM_SxData.nResult} >= 400.01 and {PRM_SxData.nResult} <= 1000
then Floor ({PRM_SxData.nResult}, 50)
else
IF {PRM_SxData.nResult} >= 1000.01
then Floor ({PRM_SxData.nResult}, 100)
else 0
Related
I have to use multinomial-dist in order to express the following distribution:
x
P(x)
red
0.5
blue
0.05
green
0.4
black
0.05
Where P(x) refers to the probability of x.
I implemented the following solution in Dr.Racket using Gamble:
(define color '("red" "blue" "green" "black"))
(define (color-probability color)
(cond
[(equal? "red") 0.5]
[(equal? "blue") 0.05]
[(equal? "green") 0.4]
[else 0.05]))
(define my-color (multinomial-dist color color-probability))
(dist-sample my-color)
But it returns an error:
make-multinomial-dist: contract violation
expected: natural?
given: '("red" "blue" "green" "black")
in: the 1st argument of
(->
natural?
(vectorof (>=/c 0))
multinomial-dist?)
I'm new in Racket and i'm still learning the basics and i don't understand what the compiler didn't like!
Thank you all!
The documentation entry for multinomial-dist, viewed from DrRacket by selecting multinomial-dist, right clicking on it, choosing Search in Help Desk for "multinomial-dist" (do this for each new function in what follows) is:
(struct multinomial-dist (n weights))
n : exact-nonnegative-integer?
weights : (vectorof (>=/c 0))
Represents a multinomial distribution. The support consists of vectors of the same length as weights representing counts of n iterated samples from the corresponding categorical distribution with weights for weights.
So a multinomial-dist can be constructed by an expression, for example, like:
(multinomial-dist 100 (vector 49 51))
(the (vector 49 51) could be the result of 100 iterated samples from a
categorical distribution with weights (vector 50 50) eg representing a coin toss)
The P(x) values in the question are categorical distribution (sometimes called
discrete distribution) weights, so start with this:
#lang racket
(require Gamble)
(define color-dist (categorical-dist (vector 0.5 0.05 0.4 0.05)))
To try this out, sample the distribution a few times in DrRacket's interaction area:
> (sample color-dist)
0
> (sample color-dist)
2
> (sample color-dist)
0
>
One way to construct iterated samples in Racket is with build-list:
(define samples (build-list 100 (lambda (x) (sample color-dist))))
> samples
'(3 0 0 2 2 0 2 0 0 0 0 0 2 0 2 0 1 2 2 2 0 2 0 2 1 0 2 0 2 0 0 0 0 2 1 0 0 2 2 2 0 1 2 0 2 2 2 0 2 0 2 2 0 0 0 0 0 0 0 0 0 2 2 0 2 2 0 0 0 2 2 2 2 1 1 0 3 0 2 2 2 0 0 2 0 2 0 0 0 0 0 2 2 2 0 0 0 2 3 0)
>
Counts of these samples are required (when needing a function, one can just type in a likely name and use "Search in Help Desk"...); try it out:
> (count (lambda (n) (= n 0)) samples)
51
>
The weights are required as a vector, so add:
(define weights
(vector (count (lambda (n) (= n 0)) samples)
(count (lambda (n) (= n 1)) samples)
(count (lambda (n) (= n 2)) samples)
(count (lambda (n) (= n 3)) samples)))
> weights
'#(51 6 40 3)
>
(After learning Scheme/Racket basics, one can eliminate the repetition in the definition above)
And then, finally,
> (multinomial-dist 100 weights)
(multinomial-dist 100 '#(51/100 3/50 2/5 3/100))
>
The distribution you're supposed to represent is not a multinomial distribution, which is a distribution over vectors.
I think you need to use discrete-dist instead.
I'm trying to implement recursive procedure for sum of squares of first n odd numbers on Racket
(starting with 1)
e.g., (sum-alt-squares-recursive 0) is 0
(sum-alt-squares-recursive 1) is 1 (1^2)
(sum-alt-squares-recursive 2) is 10 (3^2 + 1^2)
(sum-alt-squares-recursive 3) is 35 (5^2 + 3^2 + 1^2)
This is a recursive procedure that uses a linear iterative process
(define (sum-alt-squares-recursive x (y 1) (z 0))
(if (zero? x)
z ; if x is zero, return accumulator z
(sum-alt-squares-recursive (- x 1) ; x goes down by 1 each iteration
(+ y 2) ; y starts at 1 and goes up by 2 each iteration
(+ z (* y y)) ; z, goes up by y^2 each time
)))
(sum-alt-squares-recursive 1) ; => 1
(sum-alt-squares-recursive 2) ; => 10
(sum-alt-squares-recursive 3) ; => 35
I have a dataset of 100 records , I ran decision tree using the dataset .
On println(model.toDebugString)
Output is :
DecisionTreeModel classifier of depth 3 with 7 nodes
If (feature 0 <= 2.0)
Predict: 0.0
Else (feature 0 > 2.0)
If (feature 1 <= 12354.0)
If (feature 2 <= 14544.0)
Predict: 1.0
Else (feature 2 > 14544.0)
Predict: 0.0
Else (feature 1 > 12354.0)
Predict: 1.0
Is it possible to know how many no of rows are going to If condition and to the Else condition ?
like 40 rows are in If (feature 0 <= 2.0) and 60 rows are in Else
(feature 0 > 2.0)
Unfortunately there is no magical method to compute that for now. You'll need to loop over your condition and filter then count.
example : df.filter([condition1]).count
I need to perform the following function in matlab.
I had tried the following code but somehow my if statement is wrong. I'd like to know how to use the if statement efficiently here. If there is any other method in which i could perform the function please do help. My code is as follows
if (y(i,j) < -0.5, y(i,j) >= -1)
f(i,j) = 0
elseif (y(i,j) < 0, y(i,j) >= -0.5)
f(i,j) = 1
elseif (y(i,j) < 0.75, y(i,j) >= 0)
f(i,j) = 2
elseif (y(i,j) < 1, y(i,j) >= 0.75)
f(i,j) = 3
end
Here y(i,j) is a 1 x 256 matrix. Thanks
You need to use the logical AND operator to tie two Boolean expressions together. You are using a comma which is not correct:
if (y(i,j) < -0.5 && y(i,j) >= -1)
f(i,j) = 0
elseif (y(i,j) < 0 && y(i,j) >= -0.5)
f(i,j) = 1
elseif (y(i,j) < 0.75 && y(i,j) >= 0)
f(i,j) = 2
elseif (y(i,j) < 1 && y(i,j) >= 0.75)
f(i,j) = 3
end
However, it looks like you're using this in a for loop and I wouldn't perform the above in a loop. Use logical indexing instead:
f(y < -0.5 & y >= 1) = 0;
f(y < 0 & y >= -0.5) = 1;
f(y < 0.75 & y >= 0) = 2;
f(y < 1 & y >= 0.75) = 3;
This is assuming that f is the same size as y.
When I do (/ 7 2), what should I do to get the result 3? If I do (/ 7 2.0), I get 3.5, which is as expected.
(floor 7 2)
Ref: http://rosettacode.org/wiki/Basic_integer_arithmetic#Common_Lisp
See FLOOR, CEILING and TRUNCATE in ANSI Common Lisp.
Examples (see the positive and negative numbers):
CL-USER 218 > (floor -5 2)
-3
1
CL-USER 219 > (ceiling -5 2)
-2
-1
CL-USER 220 > (truncate -5 2)
-2
-1
CL-USER 221 > (floor 5 2)
2
1
CL-USER 222 > (ceiling 5 2)
3
-1
CL-USER 223 > (truncate 5 2)
2
1
Usually for division to integer TRUNCATE is used.
You can use the floor function:
(floor 7 2)
3
1
Note that it returns multiple values, and you only need the first one. Since floor returns multiple values, that can be done with multiple-value-bind as follows:
(multiple-value-bind (q r) (floor 7 2) q)
=> 3
Edit: As Rainer notes in his comment, you can just pass the result of floor as an argument if all you need is the quotient.
[1]> (floor 7 2)
3 ;
1
[2]> (+ (floor 7 2) 5)
8
[3]>
I'm leaving the reference to multiple-value-bind in the answer, since it's an important function to be familiar with.
Use the floor function. In SBCL:
* (floor (/ 7 2))
3
1/2
Two values are returned, the integer part and the fractional part.