Maybe is a dumb question but I'm figthing with a rating calculation. I've a global rating which is between 0 and 1, the number of ratings, the user rating.
How can I calculate the new rating with :
the old global rating
the user rating
the new and old number of ratings
Any idea ?
Thank you.
Multiply the previous average rating by the previous number of
ratings.
Add the new rating.
Divide by the previous number of ratings plus
one to get the new average.
Careful with your choice of data types or you will lose a little accuracy each time you do this calculation.
Pseudocode:
newRating = ((oldRating * previousNumberOfRatings) + userRating) / (previousNumberOfRatings + 1)
Related
I do have a table songs with several fields; among them: songs[title]), with songs titles, obviously, and songs[votes], with the number of votes every song has received.
I'd like to create a measure using RANKX() in order to add it to a new table (a PowerBI widget, I mean, not a table from my data model) with those both fields (songs[title]) and songs[votes]) so it tells me the rank position of every song based on the number of votes it has.
I have tried:
songs_ranking = RANKX(ALL(songs), MAX(songs[votes]))
However, all songs end up ranked #1, as if ALL() were not able to remove row context for each song:
Any hint? Thanks in advance.
_rank:=RANKX(ALL('fact'),CALCULATE(MAX('fact'[Vote])),,ASC,Dense)
I have this model
class Exemple(models.Model):
from_date = models.DateField()
until_date = models.DateField()
person = models.ForeignKey(Person, on_delete=models.CASCADE)
I have a number per year, exemple 100 and I must to decrease that number from the sum of days of that person. I must to calculate day on every row of that person and then to make sum and then 100 - sum of days
Considering persons contains your persons, you could do something like that :
for person in persons:
sum = 0
for exemple in Exemple.objects.filter(person=person):
sum += max(1, exemple.until_date - exemple.from_date)
Explanation :
1) You do the computation person per person
2) For each person, you browse every exemple
3) You sum every "until - from". The max() is here to return 1 if until_date = from_date is equal to 0 (because you said you don't want it to be 0)
I don't know if you want to store it somewhere or if you just want to do it in a method so I just wrote this little sample of code to provide you the logic. You'll have to adapt it to suit your needs.
However this might not be the prettier way to achieve your goal.
I'm using coupons in a storefront to offer discounts. Some coupons are for a flat dollar amount for orders greater than a specific value. Like, $10 off an order of $40 or more. Other coupons give a discounted rate, say, 20% off your first order this month (storefront is handling the limit, so can ignore). I want to use authorize.net to process the transactions, and send receipts to customers.
My first thought was to modify the unit price of things that are discounted. This would work fine for rate discounts, though doesn't show all the information. The problem would be for flat discounts. Where do you take the $10 off if there are a few kinds of items.
My second thought was to add a line item with a negative value/price to the order receipt. Authorize doesn't seem to accept negative values for anything, so that was a failure.
We're using the AIM transaction libraries for Java.
Order anetOrder = Order.createOrder();
anetOrder.setInvoiceNumber(sanitize(order.getOrderNumber(), 20));
anetOrder.setShippingCharges(shippingCharges);
anetOrder.setTotalAmount(total);
for (OrderProductIf op : order.getOrderProducts()) {
OrderItem item = OrderItem.createOrderItem();
item.setItemTaxable(true);
item.setItemId(sanitize(op.getSku(), 31));
item.setItemName(sanitize(op.getName(), 31));
item.setItemDescription(sanitize(op.getModel(), 255));
item.setItemPrice(op.getPrice());
item.setItemQuantity(new BigDecimal(op.getQuantity()));
anetOrder.addOrderItem(item);
}
sanitize is a function that limits the length of strings.
Transaction transaction = merchant.createAIMTransaction(TransactionType.AUTH_CAPTURE, total);
transaction.setCreditCard(creditCard);
transaction.setCustomer(customer);
transaction.setOrder(anetOrder);
transaction.setShippingAddress(shippingAddress);
transaction.setShippingCharges(shippingCharges);
Result<Transaction> result = (Result<Transaction>) merchant.postTransaction(transaction);
return getPaymentResult(result);
I'm out of ideas here.
One way would be to calculate the total amount with the discount without modifying the line items, for a $60 sale with a $10 discount below:
<transactionRequest>
<transactionType>authCaptureTransaction</transactionType>
<amount>50</amount>
Then add
<userFields>
<userField>
<name>Discount</name>
<value>$10.00</value>
</userField>
The userField value is arbitrary, make it -$10.00, if you like it better.
The line items are not totaled. SO
Add line item for credit in your system.
When you submit to Authorize.net, check for negative numbers and change description to indicate it is a credit, then change value to positive
Make sure the TOTAL you submit is correct. Authorize.net will not check that your line items add up to the correct total
Let's assume we have the following two models:
class Player(Model):
alliance = ForeignKey("Alliance")
points = PositiveIntegerField()
class Alliance(Model):
points = PositiveIntegerField()
Every player and every alliance have a specific amount of points. Total alliance points are count alliance.points + Sum(player_set__points).
What I want to do is to fetch all the alliances ordered by the amount of total points.
The problem is that I do not seem to be able to do Sum + Sum in aggregation.
You can denormalize your DB. Add a field sum on an Alliance, an update it on post_save and post_delete of Alliance and Player. So you'll have ready-to-use value and sort on it easily.
These are my models
class Review(models.Model):
reviewHeader = models.CharField(null=False,default="",max_length=200)
class ReviewRate(models.Model):
review = models.ForeignKey(CompanyReview,null=False,default=0)
isPositive = models.BooleanField(blank=True)
I would like to sort the reviews by the number of positive votes.
Review.objects.all().annotate(ss = Sum('rev__reviewrate__isPositive')).order_by('-ss')
This sorts the reviews by which has the maximum positive votes. But if a review has 2 positive and 2 negative votes, it comes before the review which has only one positive vote.
But I would like to sort them by (positive-negative) count.
How can I achieve this ?
Thanks
for this kind of things i think you need to add new field to your model like num_of_pos_votes then you can sort with this field