One-to-one techniques for improving ability to sing in tune

Question: There are a number of questions on this site from singers who want to find software so they can work, on their own, on their ability to sing in tune.
However, I am hoping to do some one-to-one (and face-to-face) work with a choir member who has significant problems pitching accurately. At present I cannot tell whether he can hear that the note is incorrect but cannot mimic the right note because of a lack of awareness of muscularity, or whether he does not hear that it is incorrect.
What techniques for working one-to-one with a student would you recommend? I have heard, for example, that one possible starting place is to have them sing a note and for the teacher to match it, vocally, and to then gradually work on awareness of moving up and down from that note.
I have also heard that it is easier for a student in this situation to match another voice rather than a piano. This makes sense to me, but I am concerned that the fact that we are different genders may complicate matters - if I sing exactly the same pitch as him, it will necessarily sound low in my voice whereas it would sound high in his; conversely if I sing in my own octave so the 'feeling' of high is matched, he may incorrectly try to mimic me using falsetto. So any related thoughts in working across gender are welcome.
Answer: If you need somewhere to start, I would make sure he can hear differences in pitch. This can be as simple as playing a note, playing another note, and then asking if it was higher, lower, or the same. Then you can start applying that skill to his own voice, though, it sounds likely that he has some production issues to work through first. The ear isn't going to be worth much if he can't control his own voice to begin with.
Young children have a few tendencies that may apply to this situation (you didn't say how old your choir member was): They can more easily match pitch when given a contour than when given a single note, and they have trouble transposing octaves.
So if you do sing for him, make sure it's in whichever octave he will be singing in. I wouldn't worry about whether it sounds high or low in your voice. On the other hand, it's easier to hear pitch in a piano than it is in a voice for various reasons—in my experience choristers would rather I not sing their part for them, opting to have it played on the piano.
Oh, and it's highly unlikely that he will be unable to hear high and low—that level of tone-deafness would show up in a person's speech patterns. You'll most likely need to work on controlling the high and low of his voice, starting with siren patterns and detaching pitch from volume.
Comment: the author of the post says "I have also heard that it is easier for a student in this situation to match another voice rather than a piano."  And while you have experience of choristers preferring to take their pitches from a piano, I most recently had the opposite experience.  The chorister I was working with would sing perfect fourths and fifths lower than whatever piano note I played but when I sang the pitch for him he had a much easier time picking out the (nearly) right pitch.
Comment: Interesting; good information. Every student is different, obviously, but I make that claim because between different notes on a piano, the decay is going to stay constant and the tone will be clear, making it easier to isolate pitch as a changing quality. In contrast, the human voice is _generally_ not so static, and will vary greatly (both in pitch _and_ quality) from person to person and between different ranges, vowels, volume levels, diameter of vibrato, etc.
Comment: All good points, I will mention that tuning across timbres is more challenging and that's where tuning to a piano can be difficult.  Also when doing this tuning exercise with my chorister, I sang at a constant dynamic level (or as close as possible) and held it until the chorister could match it.  It usually took longer than the sustain on a piano for the chorister to match (that's without taking into account that he couldn't actually match the piano pitch)
Comment: I see; that is then something the OP will have to take into consideration, since her voice timbre is going to be significantly different from her male chorister's.


Applying Bayes Theorem in a word problem involving drawing balls in succession

Question: I just got introduced to Bayes theorem, and tried solving the following question:

Two balls are drawn in succession from a jar containing 3 red balls and 4 white balls. What is the probability that the first ball was white given the second ball was red.

I used the formula given in the book implicitly using the following tree diagram,

And, then I got:
$$\frac{\frac{1}{2} \cdot \frac{2}{6}}{\frac{1}{2} \cdot \frac{2}{6} + \frac{1}{2} \cdot \frac{3}{6}}$$
where $\frac{1}{2}$ represents the probability of either picking a white or red ball when we draw the first ball, $\frac{2}{6}$ represents the probability of drawing a red ball when we draw the second ball, and $\frac{3}{6}$ represents the probability of drawing a white ball when we draw the second ball.
Did I do it correctly?
Comment: This is not quite right. Your expression seems to have the right format but the probabilities are off. Can you tell us what each of the probabilities in the fraction represents and how you determined them?
Comment: Sure, I'll add it into the question
Answer: No since there are no longer 7 balls after you take the first ball out.
$$\begin{split}P(W^1|R^2)&=\frac{P(W^1R^2)}{P(W^1R^2)+P(R^1R^2)}\\
&=\frac{\color{green}{\frac 47}\cdot \frac 3 {\color{red}6}}{\color{green}{\frac 47}\cdot \frac 3 {\color{red}6}+\color{blue}{\frac 37\cdot\frac 26}}\end{split}$$
The blue part is if you draw red on the first draw.
Answer: There are $7$ balls, $4$ white and $3$ red. So, it should be,
$ \displaystyle \frac{P(WR)}{P(WR) + P(RR)} =  \frac{\frac{4}{7}\cdot\frac{3}{6}}{\frac{4}{7} \cdot \frac{3}{6} + \frac{3}{7} \cdot \frac{2}{6}}$
The denominator is probability of second ball being red - which can be either both balls red or first ball White and second ball red.
The numerator is probability of first ball being white and second being red.
Answer: No.  You need :  $$\dfrac{\mathsf P(R_2\mid W_1)\mathsf P(W_1)}{\mathsf P(R_2\mid W_1)\mathsf P(W_1)+\mathsf P(R_2\mid R_1)\mathsf P(R_1)}$$
So you have the right structure but the wrong numbers.

If the second ball is red the first ball will have been one among the other balls: two of which were red, four of which were white.
The probability that the first ball is white given that the second ball is red is equal to the probability that the second ball is white given that the first is red.


Filter dataframe for longest sequence of repeated numbers by row in r

Question: I am trying to create a "filter-by" matrix, which I can use to isolate rows of data in my data frame, such that each row contained only the values that correspond to the longest consecutive sequence of the same number, while the rest are kept zero. After searching around, I think rle is the function to use, but that does not give me what I am after. Here is an example of my code and results. Suggestions and solutions would be very much appreciated. Thank you!
SAMPLE DATA:
    a<- c(1,0,1,1,1,1,0,0)
    b<- c(0,0,0,1,1,1,0,1)
    c<- c(0,0,1,1,0,0,0,1)
    d<- c(1,0,0,1,1,1,1,0)
    e<- c(1,0,0,1,0,0,1,1)
    f<- c(0,0,0,1,1,1,0,1)
    g<- c(0,0,1,1,0,0,0,1)
    test.data <- data.frame(cbind(a,b,c,d,e,f,g))

    # > test.data
    #   a b c d e f g
    # 1 1 0 0 1 1 0 0
    # 2 0 0 0 0 0 0 0
    # 3 1 0 1 0 0 0 1
    # 4 1 1 1 1 1 1 1
    # 5 1 1 0 1 0 1 0
    # 6 1 1 0 1 0 1 0
    # 7 0 0 0 1 1 0 0
    # 8 0 1 1 0 1 1 1

SAMPLE CODE FOR ATTEMPTED SOLUTION:
result <- data.frame(lapply(test.data, function(x) {
  r <- rle(x)
  r$values[r$lengths!=max(r$lengths)]==1
  r2=inverse.rle(r)
  r2
}))

RESULT I GET (looks like exact copy of what went in?):
# > result
#    a b c d e f g
# 1  1 0 0 1 1 0 0
# 2  0 0 0 0 0 0 0
# 3  1 0 1 0 0 0 1
# 4  1 1 1 1 1 1 1
# 5  1 1 0 1 0 1 0
# 6  1 1 0 1 0 1 0
# 7  0 0 0 1 1 0 0
# 8  0 1 1 0 1 1 1

THIS IS THE RESULT I WANT TO GET (T/F can be used instead of 1 and 0, if easier):
# > result
#    a b c d e f g
# 1  0 0 0 1 1 0 0
# 2  0 0 0 0 0 0 0
# 3  0 0 0 0 0 0 0
# 4  1 1 1 1 1 1 1
# 5  1 1 0 0 0 0 0
# 6  1 1 0 0 0 0 0
# 7  0 0 0 1 1 0 0
# 8  0 0 0 0 1 1 1

PLEASE ADVISE!
Answer: I think is what you are after...
test.data[] <- t(apply(test.data,1,function(x) {y<-rle(x)
           y$values[y$lengths==1] <- 0
           y$values[y$lengths!=max(y$lengths[y$values==1])] <- 0
           return(inverse.rle(y))}))

test.data
  a b c d e f g
1 0 0 0 1 1 0 0
2 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0
4 1 1 1 1 1 1 1
5 1 1 0 0 0 0 0
6 1 1 0 0 0 0 0
7 0 0 0 1 1 0 0
8 0 0 0 0 1 1 1

Comment: Thanks Andrew! That is closer to what I am after. Is there a way to keep the row 7 1-1, since I would like to keep any sequence of at least 2 or more consecutive 1, but if there are several sequences in same row, then the one that is longest or if there are 2 of the same length, then the first occurring one. Please let me know. Thank you!
Comment: @Mary I've amended the answer to fit your logic. It throws a couple of warnings which can be ignored - the answer is right!
Comment: @ Andrew - Thank you!
Answer: library(magrittr)

val <- 1

test.data %>%
    apply(1, function(x){
      rle(x) %$% {
        if(all(values != val)) rep(0, length(x))
        else {
          m      <- max(lengths[values == val])
          # Get only longest sequences
          values <- (lengths == m & values == val)*values*(m > 1)
          # Get only one of them
          values[seq_along(values) != which(values == val)[1]] <- 0
          rep(values, lengths)
        }
    }}) %>% t

#      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
# [1,]    0    0    0    1    1    0    0
# [2,]    0    0    0    0    0    0    0
# [3,]    0    0    0    0    0    0    0
# [4,]    1    1    1    1    1    1    1
# [5,]    1    1    0    0    0    0    0
# [6,]    1    1    0    0    0    0    0
# [7,]    0    0    0    1    1    0    0
# [8,]    0    0    0    0    1    1    1

Comment: Thanks Ryan, this works, except if I have two of the same sequences in the same row.
Comment: a<- c(1,0,1,1,1,1,1,0)
    b<- c(0,0,0,1,1,1,1,1)
    c<- c(0,0,1,1,0,0,0,1)
    d<- c(1,0,0,1,1,1,1,0)
    e<- c(1,0,0,1,0,0,1,1)
    f<- c(0,0,0,1,1,1,0,1)
    g<- c(0,0,1,1,0,0,0,1)
    test.data <- data.frame(cbind(a,b,c,d,e,f,g))
Comment: in row 7 there are two occurrences of 1,1 - so how can I keep only the first?


Why Angular2 (click) event is not firing on a ?

Question: I found a strange behaviour with Angular2:
(click) isn't firing on this:

test

But it works here:

test

Can anyone explain this behaviour? Why there is a need to set position style in order for (click) to fire?
Am I missing anything?
Comment: Please reproduce it. I'm pretty sure that problem is in your markup and styles
Comment: Can you reproduce this issue on plunker?
Comment: where is your **component.ts** module declarations and so on. with one line of HTML nothing can be helped.
Comment: nowadays angular use stopropagation by default, and remember to put a id o key on the element selected
Answer: Your code snippet looks all good!
The issue is in your CSS styles. Your
 probably either inherits a different position value or simply - goes behind another element which element blocks your
 (does not allow it to be clicked).
By changing the position to relative it works, most probably because this position enables z-index and moves your
 on top to the other element that's blocking it.
This should be enough for you to figure it out. But if you want more detailed answer - please share your CSS too.
Comment: Thanks. It is an issue with the css. I am using Angular-cli, on a fresh project all works fine. When I add bootstrap4 I see this behavior probably due to bootstrap's default css.
Comment: I am getting the same issue with Angular 4 and Bootstrap 4.
Answer: Faced the same issue, and the problem was that I named an HTML element and the method the same.
It was such:
mycomponent.html

mycomponent.ts
doSomething() {
    // ...
}

Answer: I was also facing the similar issue mostly time on Iphone. basically the pointer-event causing the stuck on the components. So I just applied the below fix at all the events where i applied the (click) property on the tag.
    a,
    div,
    ion-item,
    .item-md,
    .item-ios,
    .segment-button,
    .select-md
    .select-ios,
    .button-ios,
    .button-md,
    .searchbar-md,
    .searchbar-ios {
        pointer-events: auto !important;
    }

Now it is working fine in both Android and IOS platform.
Answer: I add a tag inside div tag

{{ item.curriculumName }}

{{ item.description }}

Answer:

<my-component></my-component>

^ this worked for me


Detecting absolute pitch by ear

Question: I've been playing classical guitar for a few years and my theory is not bad. I also think that I am really good at detecting relative pitch and chords. I can play most of the things (solo or chords) I hear by ear, and say if a chord is major, minor etc.
The problem is that, I cannot detect the absolute pitch easily. Sometimes I remember a song that I was listening some time ago, and while it plays in my mind I can play it on my guitar, too. And when I start playing the song on my computer, what I see generally is that I just transposed whole the song without knowing. Sometimes it is only a half step, sometimes half a scale. Also, I can say the scale of a song, but not the key.
Is there any special exercise that I can do to improve this?
Comment: @jjm - just trawled through a dozen references to PP and AP, nearly all refer to them as the same. Is one a misnomer? Most times they are interchangeable. AP is becoming the more acceptable term for knowing a note name, but where did the PP reference come from? I thought that was 'able to sing in tune'.
Comment: I've never been aware of any difference between the terms "absolute pitch" and "perfect pitch" (wikipedia even claims the later is another popular term for the former), and I'm not sure I understand the distinction you're making here. But I do agree that "absolute pitch" is a more clear term, especially when opposed to "relative pitch".
Comment: I changed perfect pitch to absolute pitch in your question. Absolute pitch is that a song is in a particular, single key. Perfect pitch is a person's ability to recognize a note as being a particular pitch.
Comment: Thanks! At first I made it absolute but on a few Google searches I saw the term perfect, so I thought it was the actual term.
Comment: Perfect Pitch is the ability to match pitch by ear (such as singing along to a song). Absolute Pitch is the ability to produce any pitch without external reference or context.
Comment: If I understand the distinction you're making, "perfect pitch," would basically mean "not tone deaf" -- which pretty much anyone can be. I don't think I've ever heard this usage in practice. I've usually heard both terms used to refer to the second thing (and it's inverse: naming a given pitch without reference).
Comment: See also [the history of pitch standards](https://en.wikipedia.org/wiki/Concert_pitch#History_of_pitch_standards_in_Western_music): a piece with a score in a given key can sound at a different absolute pitch because of a historical tuning (A not at 440Hz). Note also that music is sometimes transposed to other keys for various reasons.
Answer: I used to have the same problem.
I don't know if there is actually an "official" absolute pitch training, but I bet it's a long journey.
Now, here's an easy thing you can do as a good start:

Pick up a few "reference" tunes that you know by heart, and for which you know you can remember the pitch perfectly without any help. It could be songs you know since you're little, or ones you actually listen to very regularly. (for example I use the first notes of Bach's Toccata in D minor)
Remember the scale of these reference tunes. (easier if the scale is in the name: the toccata in D minor is actually in D minor!)
Whenever you're listening to some new song you plan to cover, shut your ears and play one of your reference tunes in your mind, then relatively determine the scale of the new song by comparing it with the scale of this tune you know by heart. Remember it.

Then when you try to cover the song with your instrument a few minutes later, you will know the scale of your song in advance, and there won't be any bad surprise when you compare your version to your computer's one anymore.
Comment: Thanks for your comment! I can keep some absolute pitches in my brain, but when I listen even a short portion of a song, they suddenly become relative again. :)
Answer: As eyam said, a song you know really well from when you were young is good to play and then check by listening to it. Problem being that as soon as you know how to play it/ knowing which key/frets/fingering you start in, you are no longer testing your absolute pitch.
Might be better to wake up every morning and have that note playing in your head, then go to your guitar and play the note and see if its the same. Or even better, humm the tune into your guitar tuner because if you are getting good / within a semitone you will need to see how far out you are.
EDIT: the morning part when you wake up is because you have no musical influence then. You could argue that if you hear a song on the radio that for the rest of the day you are just using relative pitch. I don't have absolute pitch just relative pitch and if I hear a song I like on the radio it can play in my head until the evening and still be in key.
Comment: Thank you! In the morning, I can tune a guitar almost perfect. After listening a few songs? It is a terrible experience. I'll do the humming and also I got a pitch pipe in key of A.
Answer: Even though I' not a musician, I've always had the ability to identify notes on the guitar and piano from their pitch. By the way I recommend Mnemonics for Perfect Pitch if someone wants to acquire some level of absolute pitch.
Answer: The ability to aurally remember a song in the correct key (or within two semitones) is called the Levitin Effect. It is discussed in Sometimes I naturally sing songs in the right key. Do I have perfect (absolute) pitch?

Detecting absolute pitch by ear


Is using GL_NEAREST_MIPMAP_* or GL_LINEAR_MIPMAP_* for GL_TEXTURE_MIN_FILTER considered erroneous if MIP maps have not been generated?

Question: I have a feeling this is one of those situations that will depend on the driver implementation since this scenario is not described in the OpenGL specification. However, I'm hoping that someone will know if using GL_(LINEAR/NEAREST)_ MIPMAP_* without generating MIP maps would be treated the same as using GL_(LINEAR/NEAREST)?
Answer: Define "without any active MIP textures"? I'm not sure how you would, since there's no such thing as a "mip texture" (a texture contains mipmaps, but mipmaps are not textures).
If you mean that you did not create a full mipmap pyramid, then it depends on how you set up your GL_TEXTURE_BASE/MAX_LEVELs. If you properly used these to tell OpenGL you only had one mipmap level, then you'll be fine. If you used glTexStorage to create your texture's storage, and you only told it to create one mipmap, again you'll be fine.
But if none of those apply, then OpenGL defaults to the least useful thing (per its usual idiocy): it sets the GL_TEXTURE_MAX_LEVEL level to 1000. Which means it's going to expect you to have defined mipmaps.
According to (desktop) OpenGL, a texture with mipmap filtering where the BASE/MAX_LEVELs refer to mipmaps that are not defined is not "texture complete". OpenGL defines that the value you get for a texture access against a non-complete texture is (0, 0, 0, 1).
In short, set your BASE/MAX_LEVEL properly and you won't have to worry. ALWAYS DO THIS.
Comment: @user8709: "*I think I got it.*" **No, you did not.** You do not *have* to call `glGenerateMipmap`. You can manually upload mipmaps if you want. The point is to ***set your `BASE/MAX_LEVEL` correctly always***. Every time you create a texture and bind it, *immediately* make two glTexParameter calls to set the `BASE/MAX_LEVEL` to what you intend for them to be.
Comment: Yeah *I got it*. FYI, OpenGL ES 2.0 requires a full MIP map pyramid if you use MIP mapping. It does not support the `GL_TEXTURE_BASE_LEVEL` parameter. Support was added in ES 3.0.
Comment: Sorry for the bad terminology. Basically, what I meant was, if you use  GL_LINEAR_MIPMAP_* without a call to `glGenerateMipMap`, will OpenGL treat it like GL_LINEAR. I think I got it. Thanks!
Answer: In my experience, all the non-generated mipmap levels will default to black.
So if you have a texture displayed at 1:1 resolution, and progressively zoom out, it will fade to black as soon as the 1rst mipmap is used.
I think this is the behaviour at least on NVIDIA and AMD; I never tried on powervr (since you're mentioning opengl-es).


Android: Bold Button Text

Question: I have a button in my application. the text in the button goes as "Type: Location" something like that.
I'm wondering whether its possible to change the text on the button as "Type: Location"
i.e Bold the text partially on the button??
Thanks for yoru time in advance.
Answer: we have a more better choice like this :android:textStyle="bold"
android api support bold
Comment: this will bold the whole text in the button :(
Comment: I ask my master ,he say that Html.fromHtml is better and more easlier control.
Answer: Simply put your string in strings.xml and change it like this,
 <string name="hello">Hello World, fh!</string>

and set this text to your button like this
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAllCaps="false"
    android:text="@string/hello"
    />

Sometimes the above approach will not be helpful when you might have to use Dynamic Text. So at that case SpannableString comes into action.
  String tempString="Copyright";
  Button button=(Button)findViewById(R.id.button);
  SpannableString spanString = new SpannableString(tempString);
  spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
  button.setText(spanString);

Comment: Don't forget add android:textAllCaps="false" (it doesn't work without that thing)
Answer: You can set it using Html.fromHtml() and give as a string, a string resource with HTML elements. Hope this helps!
Answer: Using spans:
SpannableStringBuilder builder = new SpannableStringBuilder("Type: your type here!");
StyleSpan boldStyle = new StyleSpan(Typeface.BOLD);
builder.setSpan(boldStyle, 0, 5, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
((Button) findViewById(R.id.button)).setText(builder);

Answer: You can use basic markup directory in strings, e.g.
"Type: Location"
See Styling with HTML markup
Answer: If want to set text programmatically then use this method
Button
First you have to set button's property in XML
android:textAllCaps="false" // very important without this property might be it won't show effect

public SpannableString setSpanableString(String textString, int start, int end){

    SpannableString spanString = new SpannableString(textString);
    spanString.setSpan(new StyleSpan(Typeface.BOLD), start, end, 0);

    return spanString;
}

Button btn; // get your button reference here

String text = "Hi, Dharmbir";
btn.setText(setSpanableString(text, 4, text.length));// set here your index

TextView
TextView tv; // get your TextView reference here

String text = "Hi, Dharmbir";
tv.setText(setSpanableString(text, 4, text.length));

Output
Hi, Dharmbir

Android: Bold Button Text


How can proper scoring rules optimize the probabilistic prediction compared to improper scoring rules?

Question: I understand the fundamentals in the decision theory about accuracy being an improper scoring rule compared to other proper scoring rules like Brier score & log loss. And, that threshold setting in binary outcomes is highly subjective.
To give some background, the following questions actually emerged initially from my previous question about setting thresholds for binary predictions of a fire occurring. Here, I did not know the cost of false-positive and false-negative in case of fire and was, therefore, advised to use proper scoring rules. I understand one should use these proper scoring rules when one does not know the cost of misclassifying. But in my head, utilizing a proper scoring rule does not change the fact that there still is a probability of misclassifying a fire as no fire and vice versa, for instance.
(1) So, how can one be sure/argue that 1's are more likely to be predicted as 1's and vice versa just because a proper scoring rule is applied rather than an improper?
(2) How come the semi-proper scoring rule AUC  is sometimes suggested to be the evaluation parameter, as here, and completely seen as bogus other times?
(3) Is the confusion matrix and everything coming with it actually mostly used as it is comprehensible and easy to report to others?
Comment: I interpret Harrell to mean that AUC is an adequate diagnostic of a particular model to check if it is any good at making predictions; it is easier to interpret $AUC=0.93$ as an $A$ grade than to interpret a Brier score of $0.18$. (It is this same reason why accuracy initially seems appealing, I believe.)
Comment: I don't understand what you mean in #3. You have evaluated model performance using log-loss.
Comment: Sorry, I was just told that:

"The deviance criterion in `cv.glmnet()` (the default for logistic regression) is equivalent to a strictly proper log-loss scoring rule". So, I thought that by using `deviance`, one already had a scoring rule and did not need to evaluate the probabilistic prediction differently then. Does it makes sense?
Comment: The scoring rule is an evaluation of the probabilistic prediction.
Comment: True, and then we are back to my questions. E.g. my `Brier score` is low `(0.00086)`, so the probabilistic prediction is doing well. In my mind, the improper scoring rule `accuracy` shows similar high probabilistic predictive performance with certain thresholds. How can I then be sure that 1's are predicted as 1's when using e.g. `Brier score` compared to other scoring rules?
Comment: Related: [Is accuracy an improper scoring rule in a binary classification setting?](https://stats.stackexchange.com/q/359909/1352)
Comment: Yep, definitely @StephanKolassa. But, I still do not see, how that page then relates to especially Q1? We will still get some misclassified fires and non-fires in my case with using proper scoring rules.
Comment: One quote from the linked page that is claimed to call AUC "bogus" says it "is good for describing a single model. It is not sensitive enough to use for choosing models or comparing even as few as two models." That doesn't make it "bogus," and there is no question that it's superior to accuracy, F1 score, etc. There just are better choices for comparing models.
Comment: Okay, maybe not complete 'bogus' then, but challenged as there are better choices as you mention. As mentioned above, AUC is also seen as an evaluation parameter which I have heard before. Thank you.
Comment: @Dave, more specifically you would still choose Brier over AUC?  And, in my own personal case with highly imbalanced data (ratio: 99.5:0.5). Isn't it evident that my Brier score would also tend to be low (~0.0005) just like the accuracy always becomes high for both cases always predicting 0's?
Comment: @Thomas I mean that it is easy to say that a model with a Brier score of $0.18$ is better than a model with a Brier score of $0.28$; however, is a model with a Brier score of $0.18$ any good? I have no intuition. I'm not so sure that I have an intuition for AUC values, either, but it least it feels more familiar (which could be dangerous).
Comment: Got it, and it does indeed make a lot of sense. I also saw your [answers here](https://stats.stackexchange.com/questions/476424/what-are-the-worst-commonly-adopted-ideas-principles-in-statistics) about similar tendencies as you mentioned above.  Hey, thank you for taking your time as well, appreciate it!
Answer: I won't be able to answer all your questions, but here goes.

So, how can one be sure/argue that 1's are more likely to be predicted as 1's and vice versa just because a proper scoring rule is applied rather than an improper?

You can't be sure, but you can argue.
A scoring rule is a function $S$ that takes a probabilistic prediction or classification $\hat{f}$ and a corresponding actual observation $y$ and maps these to a loss value, $S(\hat{f},y)\in\mathbb{R}$.
Now, both $\hat{f}$ and $y$ are random. For $y$, this is obvious, and for $\hat{f}$, this is due to the fact that we typically sample predictors and corresponding noisy actuals and build our model based on this.
So it makes sense to consider the expectation of our scoring rule, $E\big(S(\hat{f},y)\big)$. Let's only denote the unknown distribution $f$ of $y$ in this expectation for convenience: $E_{y\sim f}\big(S(\hat{f},y)\big)$.
A scoring rule is called proper if this expectation is minimized over all $\hat{f}$ by the true distribution $f$:
$$ E_{y\sim \hat{f}}\big(S(\hat{f},y)\big) \leq E_{y\sim f}\big(S(\hat{f},y)\big). $$
(There is also the opposite convention, where scoring rules are positively oriented and maximized in this situation. We will stick with this one.)
Thus, if we have two competing probabilistic predictions $\hat{f}$ and $\hat{g}$, and one of them is the true distribution $f$, we expect the scoring rule to give us a lower (or at least not-higher) value for this one compared to the other.
And the arguing you asked about happens when we flip this around: one prediction $\hat{f}$ gives us a lower scoring rule than another one $\hat{g}$, so it stands to reason that $\hat{f}$ is "closer" to the true $f$ than $\hat{g}$. But of course, since we are only talking about expectations, it may well be that for our particular sample, a wrong prediction gave us a lower score than the true distribution.
(Also, I'll admit that we are committing a similar error in flipping implications as when people misinterpret $p$ values as probabilities for hypotheses.)
And if we do the exercise with an improper scoring rule, then the problem just is that this improper rule has no reason to be minimized by the true distribution - if it did, it would not be improper any more, but proper.

ow come the semi-proper scoring rule AUC is sometimes suggested to be the evaluation parameter, as here, and completely seen as bogus other times?

I'll be honest: I don't have a handle on this. It might be a good separate question.

Is the confusion matrix and everything coming with it actually mostly used as it is comprehensible and easy to report to others?

Well... People think they understand it. Just as they think they understand accuracy. Easily "understood" falsehoods often have an advantage over harder to understand truths.
(From your comment):

We will still get some misclassified fires and non-fires in my case with using proper scoring rules.

Yes, certainly. Proper scoring rules are not magic silver bullets that will give you perfect predictions. After all, they evaluate probabilistic predictions. If your prediction is 80% for class A, and this is the correct probability, then there is still a 20% chance for non-A.
Proper scoring rules have the advantage that they work in expectation. As above, they may not give you the best result in each and every instance. But they will work better than alternatives in the long run.
Finally, if you get bad predictions even with a proper scoring rule, then of course you need to revisit your model. Was there some predictor you didn't include, because you simply didn't know it? Very bad (probabilistic) predictions can be a source of a lot of learning.
Comment: [This answer](https://stats.stackexchange.com/a/193141/28500) from @Sycorax gets to Point 2. AUC isn't "bogus" but has been noted to be less sensitive to detecting differences among models than strictly proper scoring rules; see my comment on the question above. My sense is that AUC doesn't well represent the calibration of the probability model, as it just evaluates the rank ordering of probabilities.
Comment: [This answer](https://stats.stackexchange.com/a/339993/28500) from @usεr11852 provides a helpful heuristic explanation of different classes of scoring rules, including the uses and limits of AUC as a semi-proper scoring rule.
Comment: "Also, I'll admit that we are committing a similar error in flipping implications as when people misinterpret p values as probabilities for hypotheses." That seems like a huge deal.
Comment: Once again, thank you @Stephan. I believe, in my own personal case, the highly imbalanced dataset (ratio of 99.5 to 0.5 %) also affects the higher number of false positive and false negative detected with my model. Nothing really to do about that.


Chrony on RHEL7.2 can sync to windows ntp server, But Chrony on RHEL7.4 can't sync it

Question: according to redhat link:
<code>https://access.redhat.com/solutions/3425701
</code>
we can see that - Chrony on RHEL7.2 can sync to windows ntp server, But Chrony on RHEL7.4 can't sync it.
in my case we have redhat 7.5
how to know if the problem with chrony is the same also on redhat 7.5 or maybe already solved ?
Comment: Your question links to subscriber-only content, and doesn't contain enough information to diagnose your problem.  If you'd like help, please provide the exact configurations of your RHEL & Windows time sync services, and exact package version numbers.
Comment: I only refer to redhat article case , since redhat not supply  more info I cant understand if this case is only to redhat 7.4 or maybe also to redhat 7.5
Answer: It is possible to register for free a developer subscription to gain access to these RedHat articles.
Saying the above there are two proposed solutions there:

Use NTP server on RHEL instead of Windows.
Add maxdistance parameter to greater than 3 in the /etc/chrony.conf

More details also in https://bugzilla.redhat.com/show_bug.cgi?id=1525833


jQuery is it possible to concatenate two selector variables?

Question: Lets say I have two variables defining separate selectors, for example...
var parent = $('.parent');
var child = $('.child');

And I want to create a something like the following...
$(parent + child).click();

Which should be equivalent to doing this (if it was correct syntax)...
$('.parent .child').click();

This may not be best practice, however I am curious if it is possible. Thanks for your help in advance.
Comment: So, what exactly are you trying to select?
Comment: I think you mean `.parent, .child` to match both right?
Comment: Just posting on my phone.. have you tried using `||` which usually donates OR in JavaScript.
Comment: I am trying to select the child element.. But I am wondering if it is possible to do by combining variables?
Answer: You can pass a jQuery object to use as context in another selector, using $( selector [, context] ):
var parent = $('.parent');
var child = $('.child');
$(child, parent).click(function () {

});

$(child, parent) is the equivalent of $(".parent .child").
DEMO.
Comment: `$(child, parent)` is the same as `parent.find(child)`, but slightly cleaner looking :-)
Comment: Yeah that is what I am looking for this works. For some reason I thought I had to use +
Answer: Try using .find.
parent.find(child).click();

Comment: much better solution ;-) +1 - and now I realise my comment at the top won't work, considering they're jQuery objects and not strings!
Answer: The title indicates that the OP wanted to know whether it is possible to concatenate two selections which have been bound to variables (at least others landing here will be looking for that) a la jQuery's .add(). Assuming the implied nesting relationship of child and parent is a red herring, you can use the concatenation like:
parent.add(child).click();


xA=0 sufficient condition for zero determinant?

Question: Let A be a symmetric n by n matrix and x be a 1 by n vector. If I find one x such that xA=0, does it mean A is singular?
Comment: If $x\ne 0$ yes. What role plays symmetry?
Answer: A matrix is singular if and only if its kernel contains a nontrivial element, i.e. if there exists $x\in\mathbb R^n\setminus\{0\}$ such that $Ax=0$. Transpose to get $x^T A^T = x^T A = 0$ where I have chosen $x$ to be a column vector (as is usual). So yes under the additional assumption that $x\ne 0$. Notice that the symmetry is irrelevant because $A$ is invertible iff $A^T$ is invertible.


EF Core 7 with multiple dynamics where conditions

Question: Good morning everyone, first of all sorry for my bad English. I use EF Core 7 and I need to filter the data of a SQL database by setting a sequence of conditions whose number I don't know a priori.
These conditions apply to a single column.
If I have a single condition I use:
trova = (from tr in db.T_ClientiCOLs
         where tr.Azienda.Contains(t) select tr).ToList();

but if I want to filter with different conditions, how can I do it?
I tried
string[] x = t.Split(' ');
string pSql = "";

foreach (string s in x)
{
    if (string.IsNullOrEmpty(s))
    {
        continue;
    }
    else
    {
        if (pSql.Length > 0)
        {
            pSql += " AND ";
        }

        pSql = pSql + " [Azienda] like N'%" + s + "%' ";
    }
}

FormattableString sql = $"SELECT * FROM [dbo].[T_Clienti] WHERE {pSql}";
trova = db.T_Clientis.FromSql(sql).ToList();

but it does not work...
Comment: Use methods instead of query form and use different `.Where(...)` calls. Eg `if(x) { query=query.Where(...);`. You can append multiple `Where` clauses and effectively get an `AND` filter
Comment: *ON THE OTHER HAND* `LIKE '%x%'` is very slow because it can't use any indexes. If you want to search for specific words it's a LOT faster to use your database's full-text-search indexing features.
Comment: Which database are you using? SQL Server and PostgreSQL for example have full-text-search indexes and functions that can be used in EF Core using provider-specific functions, like [EF.Functions.FreeText](https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.sqlserverdbfunctionsextensions.freetext?view=efcore-7.0) for SQL Server
Comment: I use SQL Server
As you may have understood, I should search for parts of text in the company name: for example the customer could search for "com er" or "com er pi tar nel", so I don't know how many where I will have to use.
Can I add it in a dynamic way? And how?
Answer: I figured out how to modify the code:
string[] x = t.Split(' ');
var query = db.T_Clientis.AsQueryable();
foreach (string s in x)
{
    if (string.IsNullOrEmpty(s)) { continue; }
    else
    {
        query = query.Where(x => x.Azienda.Contains(s));
    }
}
trova = query.ToList();


I have difficulty interpreting ANOVAs results

Question: I have a dataset of genotype performance (that's my independent variable) in different growing environments. I want to know if there are interactions between genotypes and the different environments and if the genotypes can be distinguished from each other regardless of the environment.
I'm using statsmodels in python with the functipon anova.anova_lm
The results of the ANOVA (1) with genotype*environment interaction are as follows:
             sum_sq       df     F          PR(>F)
genotype     0.408288   103.0    3.334366   3.181776e-15
ENV          2.170352     8.0  228.204205   8.152433e-175
genotype:ENV 0.654210   824.0    0.667842   1.000000e+00
Residual     4.277378  3598.0   NaN          NaN

This is interpreted as the genotype and environment contributions being significant in the response variable, but the genotype and environment interaction with a p-value of 1 is not significant.
However, if I run another ANOVA (2) using only genotypes as a variable, I get the following result:
           sum_sq     df     F        PR(>F)
genotype   0.167824  103.0  0.8209   0.905164
Residual   8.683694  4375.0  NaN      NaN

The p-value of 0.9 indicates that there are no significant differences between different genotypes. Why do genotypes not contribute to the response variable here but do in ANOVA 1?
Finally, if I run another ANOVA (3) considering only the interaction between genotype and environment, but not their individual terms, I get:
              sum_sq     df     F       PR(>F)
genotype:ENV  4.860024  935.0  4.372   3.325465e-215
Residual      4.277378  3598.0 NaN     NaN

The interpretation is that there are significant differences between genotype and environment interactions, which seems to contradict the results obtained in ANOVA 1.
All I want to answer is whether there are significant differences in the response of different genotypes.
Comment: Please edit the question to specify what software, package, and function you used for the ANOVAs. If designs aren't perfectly. balanced, some `anova()` functions can produce misleading results, and I'm not sure how `anova()` would be interpreted with an interaction-only model that omits "main effects." Please provide that information by editing the question, as comments are easy to overlook and can be deleted.
Comment: You may want to start by looking at your data (plot the individual data points and/or distributions).
Answer: Under the traditional backward selection procedure, you should try the models in this order:

fit $y$ related to genotype + ENV + genotype:ENV (ANOVA-1)
drop the genotype:ENV interaction because it is not significant
fit $y$ related to geneotype + ENV (this was not shown)
fit $y$ related to intercept + genotype (ANOVA-2)
genotype is not significant on its own (but might have been in the presence of ENV if it was tested).

I guess that, in your case, ENV would have been significant in step 3, and possibly genotype.  You'll have to show us.
Why do genotypes not contribute to the response variable here but do in ANOVA 1?
We can't tell the answer from what you gave in the question, but your analysis of ANOVA-1 is incorrect.  You don't know if the genotype main effect is significant in ANOVA-1 because you should only evaluate the interaction in ANOVA-1 under the normal process of backward selection.  You have to move to step 3 to then evaluate the main effect.
ANOVA-3 does not contradict ANOVA-1
In ANOVA-3 you only tested the interaction which was significant.  In ANOVA-1, the model says that the interaction is not significant when in the presence of the main effects.  These are completely different statements and do not contradict.
Answer: First, it looks like the default in statsmodels.stats.anova.anova_lm( ) is to perform "Type I" ANOVA, as it is with the base anova() function in R.
Those results can be misleading unless the design is balanced. See this page and its links. That's true even with a simple additive model without an interaction term.
In your situation there probably isn't a big problem with that, as both the genotype and ENV associations with outcome are strong in the full model with the interaction, ANOVA(1). But that can be a big hidden "gotcha" in other circumstances.
With respect to model ANOVA(2), you are evidently suffering from omitted-variable bias. Leaving out one predictor that's associated with outcome can bias the estimates for the remaining predictor. It can even go so far as in your situation, where the remaining predictor appears to be "insignificant."
With respect to model ANOVA(3), it can be hard to interpret interaction terms when you omit the corresponding individual predictors. See this answer for example.


how to take something from another class in python?

Question: I made two different classes Oranges and Witches and one more MyGame in my draw function in MyGame class I need to take position from the Oranges class and add it to the position in my Witches class. for example the new position of the witch = position of the orange - position of the witch. How can I do this?
class Orange(object):
    def __init__(self, position, image):
        self.image = image
        self.position = (random.randint(350, 500), random.randint(250, 350))

    def draw(self, surface):
        surface.blit(self.image, self.position)

class Witch(object):
    def __init__(self, position, image):
        self.image = image
        self.position = (random.randint(0, 760), random.randint(0, 0))
        self.position1 = (random.randint(0, 0), random.randint(0, 520))
        self.position2  = (random.randint (0, 760), random.randint(480, 520))
        self.position3 = (random.randint (730, 760), random.randint(0, 520))

    def draw(self, surface):
        surface.blit(self.image, self.position)

class MyGame(object):
    def draw(self):

Comment: new_orange=Orange(...input parameters...),
    new_witch=Witch(...input parameters...)
    new_witch.position=new_orange.position-new_witch.position....What is the problem in doing this?
Comment: Doublecheck your indentation. You probably posted from a text editor set to use tabs for indentation, which is usually not a great choice for SO code samples. (Unfortunately this being Python correcting it for you would involve making a guess.)
Comment: Also: it's recommended to simplify code samples to include only what's relevant to your problem. Here all the drawing code etc is mostly clutter.
Comment: @millimoose: I wholeheartedly agree with you. Posting such a long code will delay the answering process. It is always wise to dissect the question and just ask what is needed rather than to provide too much detail which is generally annoying.
Answer: Your code is really long but just to subtract the position of the orange and that of witch class, following should suffice. You have to create an object for orange as well as witch class and just do the following. I don't know if you are asking some other things but from your question, this is all I can tell.
new_orange=Orange(...input parameters...)
new_witch=Witch(...input parameters...)
new_witch.position=new_orange.position-new_witch.position

Note: The two positions you have is tuple, so I recommend you look into map feature and do something like this:
new_witch.position=tuple(map(lambda x,y:x-y,new_orange.position,new_witch.position))

This will give you the iterable object and you can convert it to tuple which I want to leave it for you as a motivation to go and look for lambda and iterable objects.
Comment: I don't know how it can be answered when the code is so long and question is so vague. He shows some effort for sure but might be confused about what he really wants. I made my way in programming like this as a hobbyist and feel the pain but at the meantime, the code is too much and question is too small. Don't know what he truly wants apart from """the new position of the witch = position of the orange - position of the witch"""
Comment: well he has to do a little more than that ... but conceptually yeah (+1)
Comment: their tuples ... so subtracting them raw like that will blow up but hopefully he can figure out what he needs to do
Comment: Take a look at the updated answer and please visit and look about map, lambda and iterables. It will help you.


How does Drupal 7's SQL call for 'node' and retrieve an image?

Question: I have the following query shown in one of my views:
SELECT node.nid AS nid, node.title AS node_title, node.created AS node_created,
node.sticky AS node_sticky,
'node' AS field_data_field_featured_image_node_entity_type,
'node' AS field_data_body_node_entity_type,
'node' AS field_data_field_premium_content_node_entity_type,
'node' AS field_data_field_article_type_node_entity_type,
'node' AS field_data_field_video_upload_node_entity_type
    FROM
    {node} node
    INNER JOIN {field_data_field_sports_type} field_data_field_sports_type ON node.nid = field_data_field_sports_type.entity_id AND (field_data_field_sports_type.entity_type = 'node' AND field_data_field_sports_type.deleted = '0')
    WHERE (( (node.status = '1') AND (node.type IN  ('article')) AND (field_data_field_sports_type.field_sports_type_tid = '1884') ))
    ORDER BY node_sticky DESC, node_created DESC
    LIMIT 8 OFFSET 1

What does the 'node' in this query return, for instance when I'm calling:
...'node' AS field_data_field_featured_image_node_entity_type,

It seems to me that this should just return the string "node" but in the preview it's actually showing the image. When I do the SQL query in the console it's just returning the string 'node'. I'm confused because I don't see any JOIN statements on things like the field_featured_image table in this query, which I would have expected if the query actually was pulling the image.
How is the image getting shown via this query?
Answer: It's utilizing a loaded entity to fill in these placeholder columns. Per the views_handler_field_field::query() docs:

By default, the only columns added to the query are entity_id and
  entity_type. This is because other needed data is fetched by
  entity_load(). Other columns are added only if they are used in
  groupings, or if 'add fields to query' is specifically set to TRUE in
  the field definition.

In the post-execute phase, during views_handler_field_field::post_execute(),  you can see where the field values are dropped in from the entity values.


Output of chisq.test in r

Question: I have a question regarding the below output from a chi-squares test, which I find to be confusing and contrary to my expected results - my chi-squared value is infinity here :)
I have two questions here

I made a data frame showing the relation between smoking and working out. In the column workoutideal, I have tried to convey that smokers don't work out and non smokers work out. In the column workoutmixed, it's any random data.

I expected it to show a strong relation between smoke and workoutideal (I was expecting chi square to be 0), but a weak relation between smoke and workoutmixed (I was expecting any integer value for chi square here). However, what I observe is the exact opposite. Please see my output below:
<code>mydata = data.frame(smoke = c('no','yes','no','no','yes')
                    workoutideal = c('yes','no','yes','yes','no')
                    workoutmixed = c('no','no','yes','yes','yes') )

table(smoke, workoutideal)
     workoutideal
smoke no yes
  no   0   3
  yes  2   0
table(smoke, workoutmixed)
     workoutmixed
smoke no yes
  no   1   2
  yes  1   1
chisq.test(smoke,workoutideal)

    Pearson's Chi-squared test with Yates' continuity correction

data:  smoke and workoutideal
X-squared = 1.7014, df = 1, p-value = 0.1921

Warning message:
In chisq.test(smoke, workoutideal) :
  Chi-squared approximation may be incorrect

chisq.test(smoke, workoutmixed)

    Pearson's Chi-squared test with Yates' continuity correction

data:  smoke and workoutmixed
X-squared = 0, df = 1, p-value = 1

Warning message:
In chisq.test(smoke, workoutmixed) :
  Chi-squared approximation may be incorrect
</code>

While deciding whether null hypothesis should be accepted or rejected in R, should I look at the X-squared value and accept null hypothesis if it is less than the critical value for it's degrees of freedom and reject otherwise. OR, should I look the p-value and accept null hypothesis if it is higher than 0.05, the significance level and reject otherwise.
Answer: You are confused about the nature of hypothesis testing, test statistics, and p-values.
What you might expect from your "ideal" case is that the p-value would be 0, not that the chi-squared test statistic would be 0.  Your test statistic would be very large.  (The reason why it isn't very large, and your p-value isn't very low, is just that you have few data.)
On the other hand, for your "mixed" case, the opposite should be true:  That is, the test statistic should be very low, and the p-value should be close to 1.  Which we in fact see, low N notwithstanding.
Regarding question 2, using the critical value for the chi-squared test statistic or using whether the p-value is < alpha will always yield the same decision.  This is because the critical value corresponds to the spot where the p-value drops below alpha.
Answer: The data seems right. However, doing a chisq test on data with cell counts less than 4 is not recommended. That could be why you got bad results.
Comment: The problem here is not the low expected counts.

Output of chisq.test in r


True /False : If $P^*$ is a refinement of $P$, then $U(P^* ,f,a) \le U (P,f,a)$

Question: I have  confusion in  Baby Rudin  theorem  $6.4$:
Let $P$ be a partition of $[a,b]$, that is $a=x_0 \le x_1 \le \ldots \le x_n = b$,  $f$ is the function that we want to integrate, $\alpha$ is an increasing function on $[a,b]$, $M_i = \sup_{x_{i-1}\le x \le x_i} f(x)$ and $\Delta \alpha_i = \alpha(x_i) - \alpha(x_{i-1})$.
Define $$U(P, f, \alpha) = \sum_{i=1}^n M_i \Delta \alpha_i$$
Theorem: If $P^*$ is  a refinement   of  $P$, then $U(P^* ,f,\alpha) \le  U (P,f,\alpha)$
I think this statement is N0T  true
My attempt :  Let $f(x)= x$ .Take    $P=\{0,\frac{1}{4} ,1\} $ and  $P^*=\{0,\frac{1}{4} ,\frac{1}{2} ,1\} $
$w_1 = \sup \{f(x) | x \in [0,\frac{1}{2}]\}=\frac{1}{2}$
$w_2 = \sup \{f(x) | x \in [\frac{1}{2},1]\}=1$
$M_i = \sup \{f(x) | x \in [0,1]\}=1$
Then  $U(P^*,f)- U(P,f)$  =  $w_1  + w_2 - M_i =\frac{1}{2} +1 -1  \ge 0 \implies  U(P^*,f)\ge U(P,f)$
Comment: Btw, it is quite unlikely that a theorem (with proof!) in a textbook from Rudin is wrong.
Comment: Please include what $U$, $P$, $f$ and $a$ are. In the current form it takes a lot of guess work (or a copy of Rudin) to understand your question.
Comment: Can you elaborate how you computed the difference $U(P^*,f)- U(P,f)$? I wonder why the point $x=1/4$ does not occur in the result.
Comment: Actually, that was my mistake  in thinking @MartinR
Answer: $$w_1 = \sup \{f(x) | x \in [\frac14,\frac{1}{2}]\}=\frac{1}{2}$$
$$w_2 = \sup \{f(x) | x \in [\frac{1}{2},1]\}=1$$
$$M_i = \sup \{f(x) | x \in [0,1]\}=1$$
Then  if $\alpha$ is an increasing function,\begin{align}&U(P^*,f)- U(P,f) \\ &=  w_1 (\alpha(1) - \alpha(0.5))  + w_2(\alpha(0.5) - \alpha(0.25)) - M_i(\alpha(1)-\alpha(0.25)) \\&=\frac{1}{2}(\alpha(1) - \alpha(0.5)) +1(\alpha(0.5) - \alpha(0.25)) -(\alpha(1)-\alpha(0.25))  \\
&=\frac{1}{2}(\alpha(1) - \alpha(0.5)) +1(\alpha(0.5) - \alpha(0.25)) -(\alpha(1)-\alpha(0.5) + \alpha(0.5) - \alpha(0.25))  \\
&=-\frac12(\alpha(1) - \alpha(0.5)) \le 0 \end{align}
I think your fallacy is to assume that we can have
$$\alpha(1)-\alpha(0.25) = \alpha(1)-\alpha(0.5)=\alpha(0.5)-\alpha(0.25)=1$$
Comment: you can still edit to include $w_1$ and $w_2$. :) good to have a reference to the book and at the same time, try to make it self-contained too.
Comment: Thanks for Edit .Actually ,i forgot to edit it@siang thye  Goh
Answer: I don't understand your computation. Why don't you compute directly
$$\frac{11}{16}=\frac{1}{4}\frac{1}{4} + \frac{1}{4}\frac{1}{2} + \frac{1}{2}1=U(P^* ,f,a) \le  U (P,f,a)= \frac{1}{4} \frac{1}{4} + \frac{3}{4}1 =\frac{13}{16} $$
By the way, I don't have Rudin's proof at hand of the theorem, but it is rather clear and simple.


How can I disable the form confirmation message?

Question: I'm referring to the green box that informs the user that they have successfully submitted their form.
In particular, I'm wondering if I can remove it without writing any php. Is there anything in the admin interface?
Comment: Is there a way in your form to show a "success" page?
Comment: After submission, the page is redirected to a "success page" where we have written a "thank you for submitting" message. So, the green confirmation box is redundant.
Comment: Can you / Do you want to edit the theme?
Comment: I can't personally -- that would have to go through a vendor. But at that point I would probably just use php, right?
Comment: Why redirect to a success page if webform has an option to just reload the current page with the thank you message?
Comment: create a template(page.tpl.php) file of your page where your form rendered,then comment the line   in your template file
Answer: There is no option in Drupal's core that gives you the ability to disable messages but there is a module for that: disable_messages.
Still, I like this approach, which uses drupal_get_messages function implemented in hook_form_alter:
<code>/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, &$form_state, $form_id) {
  // Define the $form_id for the form which you want to suppress messages for.
  if ($form_id == 'hide_messages_form_id') {
    $form['actions']['submit']['#submit'][] = 'my_module_submit';
  }
}

/**
 * My custom submit handler.
 */
function my_module_submit($form, &$form_state) {
  // Hide drupal 'status' messages. Equally use 'error', 'warning' or 'NULL'.
  drupal_get_messages('status');
  // Or display your own message.
  // drupal_set_message(t('My message.'));
}
</code>


Contains em Lista de Classe

Question: Tenho minha classe, que tem 2 classes dentro dela.
public class ProfEsp {
public Especilidade Especialidade {get;set;}
public Profissional Profissional {get;set;}

}
public class Especialidade {
public int Id {get;set;}
public string Nome {get;set;}
}

public class Profissional {
public int Id {get;set;}
public string Nome {get;set;}
}

Então tenho minha lista
var lista = new List<ProfEsp>();

Eu quero verificar se existe uma especialidade dentro dessa lista
Tentei usando var lista.Any(x => x.Especialidade == Esp);
Com contains não tem como também
Alguém tem uma ideia?
Comment: quais são os atributos de Especilidade?
Comment: Acabei de editar a pergunta Tuyoshi
Answer: Relativamente ao .Contains()
O método .Contains() da List<T> tem uma particularidade que é usar o comparador retornado pelo EqualityComparer.Default. Segundo a documentação:

A propriedade Default verifica se T implementa a interface System.IEquatable e, em caso afirmativo, retorna um EqualityComparer que usa essa implementação. Caso contrário, ele retorna um EqualityComparer que usa as substituições de Object.Equals e Object.GetHashCode fornecido por T.

Ou seja pode implementar a interface IEqualityComparer<T> na sua classe ou então fazer o override do método base.Equals() e vai ser esse o método de comparação utilizado.
Mas porque implementar IEqualityComparer<T>?
Quando usa o .Equals() da interface, existe a garantia que os tipos do objecto original e o objecto a comparar são iguais. Não só torna mais segura a programação, como tem um ligeiro impacto na performance, dado que, ao contrário do base.Equals() não ocorre boxing/unboxing na comparação.
Este cenário torna-se mais importante quando existe uma extensa utilização de comparações onde os custos da operação de boxing/unboxing deixe de ser negligivel.
Any Vs Contain
Uma análise interessante é a comparação de complexidades entre o .Any() e o .Contains().
O .Contains() é um método da instância e por isso a complexidade está dependente da colecção em si. No caso de uma List<T> a complexidade é O(n), enquanto que num HashSet a complexidade seria O(1).
Já o .Any() é uma extensão cuja complexidade é sempre O(n), dado que vai percorrer toda a colecção, aplicar o delegate passado a cada elemento e retornar quando encontrar um elemento cujo delegate devolva true.
Por fim, o .Any() devido ao delegate, é mais fléxivel que o .Contains() que apenas aceita um objecto.
Answer: O Any funciona, entretanto você provavelmente você está comparando objetos de especialidade diferentes, mas que parecem ser iguais, por terem os mesmos valores nas propriedades.
Tente comparar uma propriedade que identifique Especialidade:
var lista.Any(x => x.Especialidade.Id == Esp.Id)

Ou então implemente o operador == para o tipo Especialidade:
public static bool operator ==(Especialidade x, Especialidade y)
{
    return x.Id == y.Id;
}

E também o método Equals... para manter a consistência com o operador acima.
Ai neste caso poderia fazer da forma que você estava tentando com o Any:
var hasAny = lista.Any(x => x.Especialidade == Esp);

Answer: Você pode procurar pelo nome ou Id no exemplo abaixo estou procurando pelo nome
var ProfEsp = lista.SingleOrDefault(x => x.Especialidade.Nome == "Programador" );
if(ProfEsp!=null){
  // achou especialidade!
}

Answer: Neste caso existe a necessidade de você implementar uma customização deste operador com regras especificas para comparação entre objetos, caso não exista é comparado o HashCode de cada objeto, um exemplo de comparação:
public class Especilidade
{
    public string Nome  { get; set; }
    public string Categoria { get; set; }

    public static bool operator ==(Especilidade a, Especilidade b)
    {
        // mesma instancia, retorna true.
        if (System.Object.ReferenceEquals(a, b))
        {
            return true;
        }

        // Caso um deles sejam nulos, retorna false
        if (((object)a == null) || ((object)b == null))
        {
            return false;
        }

        // Retorna true se o campos forem iguais
        return a.Nome == b.Nome && a.Categoria == b.Categoria;
    }

    public static bool operator !=(Especilidade a, Especilidade b)
    {
        return !(a == b);
    }
}

Veja esta documentação, vai te ajudar com comparações entre objetos:
http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx
Answer: Eu testei e funcionou assim:
var listaProfEsp = new List<ProfEsp>();

     var lista1 = new ProfEsp();
     lista1.Especialidade = new Especialidade
     {
         Id = 1,
         Nome = "teste"
     };

    listaProfEsp.Add(lista1);
    var exist = listaProfEsp.Any(x => x.Especialidade.Id == lista1.Especialidade.Id);

    if (exist)
    {
       Console.Write("exist");
       Console.ReadLine();
    }
    else
    {
       Console.Write("não exist");
       Console.ReadLine();
    }

Comment: Concordo alterei lá ficou até mais fácil para exemplificar...
Comment: Funciona mas não existe procura pelo item. A unica coisa que está a fazer é confirmar que existe pelo menos uma especialidade não nula dentro de `listaProfEsp`.
Comment: Mas pelo que entendi ele quer ver se existe um entidade dentro por isso que ele queria usar o contains, se for para verificar se uma em especifico existi dai ele procuraria por Id, nome ... etc
Comment: Correcto, ele queria ver se existe uma especialidade especifica dentro da lista, não se existe uma qualquer. Concordo que a pergunta possa parecer que ele quer ver apenas se existe algo lá dentro, mas dado que o OP diz `var lista.Any(x => x.Especialidade == Esp);`, foi assumido que ele queria procura algo em especifico.
Comment: Acabei de ler novamente e você esta certo alterei lá...
Comment: A unica coisa que acrescentaria é para, em vez de usar `idEspecialidade`, acesse directamente a propriedade `ID` de `lista1`.

Contains em Lista de Classe


what is total order - explanation please

Question: sorry for the dumbest question ever, but i want to understand total order in an intuitive way,
this is the defition of total order:
i) If $a ≤ b$ and $b ≤ a$ then $a = b$ (antisymmetry);
ii) If $a ≤ b$ and $b ≤ c$ then $a ≤ c$ (transitivity);
iii) $a ≤ b$ or $b ≤ a$ (totality).
totality means that any pair of the total ordered pair is mutually comparable. i dont understand what they mean under comparable, what is the defition of comparability? i can also compare the elements of partial order, where is problem? why is partial order not not mutually comparable?
can someone explain me please in simple words :(
Comment: OK, suppose you can compare elephants with each other to see which one is bigger, and you can compare laptops with each other to see which one is more powerful. So the set $E$ of elephants is totally ordered and the set $L$ of laptops is also totally ordered. You can look at the set $L\cup E$ of all elephants and all laptops. Then it is only partially ordered.
Comment: in a partial order you can't necessarily compare any two elements. For instance, suppose you only have two elements in your partially ordered set, say a and b, and that neither a<=b nor b<=a. How then can you compare a and b?
Comment: i understand under compare this: i cannot compare elephant with laptop, because they are total different things, but i can compare numbers always, but the result is not always defined.
Comment: @DanShved thanks, now i got the clue.
Answer: Two distinct elements are called "comparable" when one of them is greater than the other. This is the definition of "comparable". When you have a partially ordered set, some pairs of elements can be not comparable. i.e. you can have two elements $x$ and $y$ such that $x\leqslant y$ is false and $y \leqslant x$ is also false.
For example, consider the set $\mathbb{R}^2$ and a partial order defined like this:
$$
    (x_1,x_2) \leqslant (y_1,y_2) \quad\textrm{iff}\quad x_1\leqslant y_1 \,\textrm{and}\,x_2 \leqslant y_2.
$$
With this partial order, elements $(0,0)$ and $(1,2)$ of $\mathbb{R^2}$ are comparable, because $(0,0)\leqslant(1,2)$. But elements $(0,1)$ and $(1,0)$ are not comparable, because both statements "$(0,1)\leqslant(1,0)$" and "$(1,0)\leqslant(0,1)$" are false.
Comment: great Dan, this is the right explanation. Thanks!
Comment: I don't understand how you would define "greater than." Suppose a relation is defined on the power set of some finite set. Then how would one element be "greater than" another?
Answer: If $\le$ is a partial order on a set $A$, two elements $a,b$ of $A$ are comparable if either $a\le b$ or $b\le a$; otherwise they are not. For instance, in the partial order $\subseteq$ on $\wp(\Bbb N)$ the sets $\{0,2\}$ and $\{0,1\}$ are not comparable: neither is a subset of the other. On the other hand, the sets $\{0,2\}$ and $\{0,1,2\}$ are comparable: $\{0,2\}\subseteq\{0,1,2\}$.
Another name for a total order is linear order. It expresses the intuitive idea very well: you can picture a linear order $\le$ on a set $A$ as arranging the elements of $A$ in a line. The usual order $\le$ on $\Bbb R$ is a linear (or total) order: if $x,y\in\Bbb R$ are any real numbers, either $x\le y$, or $y\le x$. To put it another way, if $x$ and $y$ are any real numbers, at least one of the statements $x\le y$ and $x\ge y$ must be true. Contrast that with subsets of $\Bbb N$. If $A$ and $B$ are subsets of $\Bbb N$, it's not the case that at least one of $A\subseteq B$ and $A\supseteq B$ must be true: we just saw that $A=\{0,2\}$ and $B=\{0,1\}$ are a counterexample. The linear order $\le$ lines up the real numbers as a single linear arrangement; the partial order $\subseteq$ does not line of the subsets of $\Bbb N$ in a linear arrangement.
Comment: Great, Brian, thanks a lot. very nicely explained!
Comment: @doniyor: You're welcome.
Answer: As you know that $(D, \leq )$ is a POSET (partially ordered set) if it fulfills three properties of,

*R*eflexivity    $a\leq a$
*A*ntisymmetry   $a\leq b$ and $b\leq a$  implies $a=b$
*T*ransitivity   $a\leq b$ and $b\leq c$  implies $a\leq c$

In addition to these, if all elements in your set $D$ also are comparable with each other, for instance $D$ has $\{a,b,c,d\}$ and you can compare $a\leq b$, $a\leq c$, $a\leq d$, $b\leq c$, $c\leq d$ and so on, then it can be called as a total order. i.e. when you can have the relation $\leq$ totally on all elements.
Answer: Suppose we have a set that is the union of members of EvilCorp and Skynet. An order on this set is " $a\leq b$ if a has equal or lower rank than b in their company ". This satisfies i) and ii) but not iii) - we can not compare a and b if they are from different companies. So this is not a total order, even though it is quite a natural order.
Comment: Yeaah, you are right. thanks, very good explanation!


Evaluating a definite integral with a complex exponent

Question: How is it possible to evaluate the following integral:
$$
\int_0^{2\pi}\frac{1}{|1-ae^{-it}|^2}dt
$$
with $|a| < 1$.
The answer should be something like $\frac{1}{1-|a|^2}$.
Answer: $$\frac1{|1-ae^{-it}|^ 2} = \frac1{(1-ae^{-it})(1-\bar ae^{it})} = \sum_{n = 0}^{\infty}\sum_{m=0}^\infty a^n\bar a^m e^{(m-n)it}$$ Since $|a| < 1$ we can intervert sum and integral $$\int_0^{2\pi} \frac1{|1-ae^{-it}|^ 2} \mathrm dt =  \sum_{n = 0}^{\infty}\sum_{m=0}^\infty a^n\bar a^m \underbrace{\int_{0}^{2\pi} e^{i(m-n)t}\mathrm dt}_{\left\{\begin{array}{cc}0 & \text{if $n\neq m$} \\ 2\pi & \text{if $n=m$}\end{array}\right.} = \sum_{n=0}^{\infty} 2\pi (a\bar a)^n = \frac{2\pi}{1-|a|^2}$$
Comment: Thank you very much, the solution is very elegant!
Comment: You are welcome!
Answer: Hint. We have that $e^{it}=\cos(t)+i\sin(t)$ and
$$I=\int_0^{2\pi}\frac{dt}{|1-ae^{-it}|^2}=2\int_0^{\pi}\frac{dt}{1+a^2-2a\cos(t)}$$
Now let $s = \tan(t/2)$. Then
$$\cos(t) = \frac{1-s^2}{1+s^2}\quad,\quad dt=\frac{2 ds}{1+s^2}$$
and you are left to integrate a rational function,
$$I=4\int_0^{+\infty}\frac{ds}{(1-a)^2+(1+a)^2s^2}.$$
You may also take a look here: How do you integrate $\int \frac{1}{a + \cos x} dx$?
Comment: Thank you very much for your help! It all makes perfect sense to me now!
Answer: Note that $e^{it}$ for $t\in[0,2\pi]$ parameterizes a unit circle drawn anti-(counter-) clockwise about $0$, so with substitution $z = e^{it}$ and
$dz = ie^{it}\,dt = iz\,dt$,
\begin{align}\int_{0}^{2\pi} \frac{dt}{|1-ae^{-it}|^2}
= \oint_C \frac{dz \,/ (iz)}{|1-a\overline{z}|^2}
&= \frac{1}{i} \oint_C \frac{dz}{z(1-a\overline{z})\overline{(1-a\overline{z})}}\\
&= \frac{1}{i} \oint_C \frac{1}{z-a} \underbrace{\frac{1}{1-\overline{a} z}}_{\equiv \;f(z)}\;dz\\
&\overset{(*)}{=} \frac{1}{i} 2\pi i \; f(a)
= \frac{2\pi}{1-|a|^2}
\end{align}
where $(*)$ is Cauchy Integral Formula, which is valid as $f$ is holomorphic, because $|\overline{a}z| = |a||z| < (1)(1) = 1$ so $|1-\overline{a}z| \neq 0$ in the disk.


Poisson related homework question

Question: The number of new cars sold by a dealer each week is distributed as a Poisson variable
with mean 10. Suppose that 80% of the customers need a loan to pay for the car,
and of these 62.5% are likely to take the loan from the dealer. Suppose that the loan
amounts are on average 22,000, with a standard deviation of 5,000. Find the
approximate probability that the total new car loan issued by the dealer in one week will exceed 600,000.
Here is where I am. Let $N$ be the number of cars sold and $N~P(10)$. Let $L$ be a random variable representing the loan amount. We want to compute $P(0.8\times0.625\times N\times L>600,000)$. $N$ and $L$ times together. How to resolve this? Please help!!!
Comment: are the loan amounts normal? "the loan amounts are on average 22000 wihth sd of 5000"
Comment: @Jellyfish This is the part I am struggling with. There is no info. about L expect mean and std:(
Answer: The number of loan takers N will be distributed $N\sim\text{Poisson}(10\cdot .8 \cdot .625)=\text{Poisson}(5)$.
The cost of the loan (assuming a normal distribution) will be $L|N\sim\text{Normal}(22000N, (5000N)^2)$.
The probability that the loans will exceed 600000 is
$$\begin{split}P(L>600000)&=\sum_{N=0}^\infty P(L>600000|N)P(N)\\
&=\sum_{N=0}^\infty P(Z>\frac{600000-22000N}{5000N})P(N)\\
&=\sum_{N=0}^\infty \Phi(\frac{22000N-600000}{5000N})\frac{e^{-5}5^N}{N!}\end{split}$$
The prob stabilizies after about N=25 to be $2.369295e-07$

Poisson related homework question


Separated integers with comma delimiters and sscanf

Question: I need help with sscanf.
I have a data file. I read it line by line.
One line is look like this: 23,13,45;
I want to read the integers.
I try this code:
unsigned char a,b,c;
sscanf(line,"%d,%d,%d;",&a,&b,&c);

But this is not working, only the first number read, the others is 0.
Answer: Correct format specifier for unsigned char is %hhu.
Other than that I don't see any problem as long as line does contain the string in the format you expect.
Comment: For `%d` sscanf() expects a `signed int*` but you are passing pointers to `unsigned char`, which is [undefined behaviour](http://en.wikipedia.org/wiki/Undefined_behavior).
Answer: This is because %d expects a pointer to a 4-byte integer, and you are passing a pointer to a 1-byte char. Because variables a, b and c are stored in the order of decreasing memory addresses, sscanf first fills a with 23, at the same time filling 3 other bytes of stack memory with zeros (this is a memory violation, BTW). Then it fills b with 13, also filling a and two other bytes with zeros. In the end it fills c with 45, also filling a and b and one other byte with zeros. This way you end up with zeros in both b and a, and an expected value only in c.
Of course this is only one possible scenario of what can happen, as it depends on the architecture and compiler.
A proper way to read 4 integers would be to use int instead of unsigned char, or change the format specifier.


Does Mock call Real Objects?

Question:
How are mock objects better than real objects in unit testing?
When you create a mock object, what exactly it means? For example let's say you create
A a = Mockito.mock(A.class);
Does it create an object of class A? or something else?
When you create a mocked object and use it in calling a method, say a.method1(), how does the mocked object knows that it should call method1 itself?

Kindly let me know about these.
Comment: Possible duplicate of [What is the purpose of mock objects?](http://stackoverflow.com/questions/3622455/what-is-the-purpose-of-mock-objects)
Answer: What is a unit-test
A unit-test suggests you only want to see your functionality in a small environment, typically a class. But this class often talks to other classes and those talk to other classes... which can mean a lot of work, just to get this one class going. Also, you just want to test your unit. This is, where mocks/stubs come in. I will explain the distinction later.
What you do, is - instead of recreating your whole app - to make assumptions. A mock is basically an assumption. Everything your class needs, is being mocked or stubbed. This can also be the case for already tested parts of your unit. You assume, then you test one specific (real) part of your unit. Then mock this one, if necessary, and so on.
Now what is the difference between mocks and stubs.
Both are assumptions, but mocks are also expectations. So, if you use a mock, your testing-framework should complain, if it hasn't been used/called.
A stub on the other hand, is just for the case. Like a random call to the Internet, lookups to database, which are not important for the test per se.
What your framework actually creates, depends on the framework and the language. But it will/should not call the original method, as explained above.  Usually you'd have to tell this explicitly to the framework, which sometimes might make sense.

Does Mock call Real Objects?


Como juntar as duas tabelas nesse caso?

Question: Tenho duas tabelas uma "alunos" com a matrícula e inscrição e outra "faz_teste" com as notas dos alunos em determinados testes. Utilizei o comando pivot na tabela "faz_teste" para transformar o nome das provas em coluna e por as notas em linhas, como mostra a query a seguir:
SELECT  inscricao,[1_BIM],[2_BIM],[3_BIM],[4_BIM]
 FROM (
    SELECT inscricao, idteste, nota_num
    FROM [EasyNovo].[dbo].[faz_teste]
    WHERE idteste='1_BIM' OR idteste='2_BIM' OR idteste='3_BIM' OR idteste='4_BIM'
  )tt

PIVOT (SUM(nota_num)
FOR idteste IN ([1_BIM],[2_BIM],[3_BIM],[4_BIM]))pvt

No resultado dessa query tenho a inscrição e as notas dos alunos em cada prova, como mostra a imagem:

Mas eu preciso por, além da inscrição, o nome. Como fazer nesse caso?
Comment: Ja tentou `SELECT inscricao, idteste, nota_numn, NOME` e no , `PIVOT (SUM(nota_num) , NOME` ..... `SELECT  inscricao,[1_BIM],[2_BIM],[3_BIM],[4_BIM], NOME` ?
Comment: a Use o JOIN também
Answer: Faz o join após ter feito o pivot... da mesma forma que fez um select dentro do from:
SELECT
alunos.nome,
pvt.*
FROM (SELECT  inscricao,[1_BIM],[2_BIM],[3_BIM],[4_BIM]
 FROM (
    SELECT inscricao, idteste, nota_num
    FROM [EasyNovo].[dbo].[faz_teste]
    WHERE idteste='1_BIM' OR idteste='2_BIM' OR idteste='3_BIM' OR idteste='4_BIM'
  )tt

PIVOT (SUM(nota_num)
FOR idteste IN ([1_BIM],[2_BIM],[3_BIM],[4_BIM]))) pvt
INNER JOIN alunos on alunos.inscricao = pvt.inscricao

Answer: Tente fazer.
SELECT  inscricao,[1_BIM],[2_BIM],[3_BIM],[4_BIM], Nome
 FROM (
    SELECT inscricao, idteste, nota_num, a.Nome
    FROM [EasyNovo].[dbo].[faz_teste] ft
    join [EasyNovo].[dbo].[alunos] a
    on a.idaluno = ft.idaluno
    WHERE idteste='1_BIM' OR idteste='2_BIM' OR idteste='3_BIM' OR idteste='4_BIM'
  )tt

PIVOT (SUM(nota_num)
FOR idteste IN ([1_BIM],[2_BIM],[3_BIM],[4_BIM]))pvt

veja a ligação da tabela on a.idaluno = ft.idaluno
Comment: Tentei dessa forma, mas não funcionou.
Comment: qual erro deu? .
Comment: Aparece só o título das colunas, o conteúdo não.
Comment: você fez a união correta no select "Os IDs "  `on a.idaluno = ft.idaluno`
Comment: Sim, veja como ficou:

 FROM (
    SELECT a.nome, ft.inscricao, ft.idteste, ft.nota_num
    FROM [EasyNovo].[dbo].[faz_teste] ft
    join [EasyNovo].[dbo].[aluno] a
    on a.inscricao = ft.inscricao

 WHERE .....
Comment: Esse select interno está retornando alguma coisa?
Comment: Vou por toda Query pra você ver:

`SELECT  nome, inscricao,[1_BIM],[2_BIM],[3_BIM],[4_BIM]
 FROM (  SELECT a.nome, ft.inscricao, ft.idteste, ft.nota_num
 FROM [EasyNovo].[dbo].[faz_teste] ft
join [EasyNovo].[dbo].[aluno] a
    on a.inscricao = ft.inscricao
    WHERE idteste='1_BIM' OR idteste='2_BIM' OR idteste='3_BIM' OR idteste='4_BIM'
  )tt

PIVOT (SUM(nota_num)
FOR idteste IN ([1_BIM],[2_BIM],[3_BIM],[4_BIM]))pvt`
Comment: tente ver o que retorna em . `SELECT a.nome, ft.inscricao, ft.idteste, ft.nota_num FROM [EasyNovo].[dbo].[faz_teste] ft join [EasyNovo].[dbo].[aluno] a on a.inscricao = ft.inscricao WHERE idteste='1_BIM' OR idteste='2_BIM' OR idteste='3_BIM' OR idteste='4_BIM'`
Comment: Vamos [continuar esta discussão no chat](http://chat.stackexchange.com/rooms/36093/discussion-between-jeferson-leonardo-and-marconcilio-souza).
Comment: Não retorna nada.
Comment: bom ai você vai ter que ver a relação entre essas tabelas .
Comment: Conseguiu alguma coisa?
Comment: Ainda não. Vou voltar a mexer hoje, não tive muito tempo. Se eu conseguir alguma solução posto aqui.
Comment: você tem que rever seu `join` , faça um select que retorne os dados das duas tabelas.

Como juntar as duas tabelas nesse caso?


How to sort list depending on values in sublists?

Question: So I have a list that looks something like this:
example = [['b',1],['b',2],['a',2]]

And it needs to be sorted to become:
example = [['b',1],['a',2],['b',2]]

I.e. sorted numerically by the number in the [1] position. The program needs to recognise when there are two numbers that are the same, and then sort these elements alphabetically.
Any ideas?
Edit:
and how would I go about sorting the list so that the highest number is printed first?, i.e:
example = [['a',2],['b',2],['b',1]]

Answer: You can use list.sort, its key function, and operator.itemgetter:
>>> from operator import itemgetter
>>> example = [['b',1],['b',2],['a',2]]
>>> example.sort(key=itemgetter(1,0))
>>> example
[['b', 1], ['a', 2], ['b', 2]]
>>>

You could also use a lambda instead of operator.itemgetter:
example.sort(key=lambda x: (x[1], x[0]))

but that would be slower.
Comment: Could you explain how this works? Lets say I now wanted to sort the list from the highest number to the lowest (but still sorting elements with the same number alphabetically a-z), how would you change this code to allow for that?
Comment: @HayleyvanWaas, I've got examples for that in my answer
Answer: >>> lis = [['b',1],['b',2],['a',2]]
>>> sorted(lis, key=lambda x:x[::-1])
[['b', 1], ['a', 2], ['b', 2]]

To sort the list in-place use lis.sort(...).
Comment: In Python 2.x, we can simply do `print(sorted(lis, key=sorted))` :)
Answer: Here is a neat trick
>>> example = [['b',1],['b',2],['a',2]]
>>> sorted(example, key=sorted)
[['b', 1], ['a', 2], ['b', 2]]

Only works for Python2 though

There are two ways to sort by numbers from highest to lowest
>>> sorted(example, key=lambda x: (-x[1], x[0]))
[['a', 2], ['b', 2], ['b', 1]]

or
>>> from operator import itemgetter
>>> sorted(sorted(example), key=itemgetter(1), reverse=True)
[['a', 2], ['b', 2], ['b', 1]]


Many-to-Many Relationships in EF7 - Select query for a list including related data

Question: I am building a blog with the following Many-to-Many relationship on the content -> category tables. I have used the following code to generate the tables in EF7:
public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Categorization> Categorization { get; set; }
}

public class Categorization
{
    public int ContentId { get; set; }
    public Content Content { get; set; }

    public int CategoryId { get; set; }
    public Category Category { get; set; }
}

public class Content
{
    [Key]
    public int Id { get; set; }
    public string Title { get; set; }
    public string Excerpt { get; set; }
    public string Body { get; set; }

    ...

    public virtual ICollection<Categorization> Categorization { get; set; }
}

However, when I attempt to load a list of categories, including the content associated with an EF7 query as follows, I get an error.
categories = await _db.Categories
                      .Include(c => c.Categorization)
                      .ThenInclude(c => c.Content)
                      .OrderByDescending(c => c.Categorization.Count)
                      .ToListAsync();

This is the error page that I get when this is called.

A database operation failed while processing the request.
AggregateException: One or more errors occurred.  AggregateException:
  One or more errors occurred.  AggregateException: One or more errors
  occurred.  AggregateException: One or more errors occurred.
  SqlException: Invalid column name 'Id'.  There are pending model
  changes for ApplicationDbContext Scaffold a new migration for these
  changes and apply them to the database from the command line:
dnx ef migrations add [migration name]
dnx ef database update

Interesting note: When i remove the line
 .Include(c => c.Categorization).ThenInclude(c => c.Content)

It will sometimes work correctly, as in load the categorization, however this does not ALWAYS happen, sometimes the categorization doesn't load and I only have null references for Categorization in each Category class in the generated enumerable.
Comment: Using Include is normally a bad adiea, as the performance impact is quite heavy. if you have 10 Categories and 10 Categorization and 10 Content, you will retrieve 1000 (10*10*10) rows from the database. When getting them in 3 separate calls, you will only retrieve 30 (10+10+10) rows.
Comment: Sure, i understand the overhead of include, I am just using it to figure out the issue with the relationship. I am only using this to get a list of all the categories, and an associated count of the content in each category. Is there a clean way for doing this, perhaps a computed column for the count?
Comment: Assuming [Categorization.ContentId, Categorization.CategoryId] is unique  `await _db.Categories.Select(c => new {c.Id, ContentCount = c.Categorization.Count()}.ToDictionaryAsync(c => c.Id, c => c.ContentCount).ConfigureAwait(false)`
Comment: Fantastic! You inspired me to my final solution. I created a new POCO class to contain the count and the category, then simply ran a select to polulate my list. as follow: categories = await _db.Categories.Select(c => new CategoryApi { Category = c, Count = c.Categorization.Count }).ToListAsync()
Comment: Alas! Still receiving the SqlException: Invalid column name 'Id' when loading this line now. I have updated the model and the migrated the database several times. Also, the system continually detects that a change in the model has occurred yet, when a migrations add, it generates the exact same migration code as the previous migration, then still thinks there is a change. Odd!
Answer: The error message says it all. SqlException: Invalid column name 'Id'
Your database is different than the models on your project. If you have database migration enabled then run the commands
dnx ef migrations add [migration name]
dnx ef database update

If you don't want to use database migration, you have to update the models on your database to match your models.
Comment: I am using migrations, and the database is up to date with the migrations in my code, there seems to be something wrong with the model, as it continuously thinks there is pending change on the model, that doesn't exist in the migrations. I have ran the migration commants repeatedly, it simply generates the exact same migration code, and applies the same changes over and over, still thinking there is a change im my model afterwards.


How to execute a list of checks with Gnu Make?

Question: I have a Makefile that should execute some simple commands on a set of imput files. Each does not have an output, just the success of the command should be checked:
# Makefile
check: test1 test2 test3

test1: one.xml
    xmllint --schema my.xsd $@
test2: two.xml
    xmllint --schema my.xsd $@
test3: three.xml
    xmllint --schema my.xsd $@

I would like to have this more generic, that I don't have to list identical rules for each file. Which is especially inconvenient when I for example use $(wildcard ...):
# Makefile (sketch only)
XMLS:=$(wildcard *.xml)

check: xmllint

xmllint: %.xml   # this will not work will it?
    xmllint --schema my.xsd $@

The last rule isnor correct, of course, since the % is on the right side:

$ make check
*** No rule to make target '%.xml', needed by 'xmllint'.  Stop.

How do I connect the $(XMLS) input files with the xmllint: ... rule, so that on make check the program xmllint is executed on all xml input files?
Answer: .PHONY: all check

# The files to be tested
XMLS:=$(wildcard *.xml)

# Turning the filenames into *test* names
XMLTESTS:=$(patsubst %.xml,%.test,$(XMLS))

# "make check" should run all tests
check: $(XMLTESTS)

# Each test depends on the XML file
# Now we have a pattern on both sides of the generic rule
%.test: %.xml
    xmllint --schema my.xsd $<

Note that, for some (to me) unknown reason, putting .PHONY after the declarations and adding $(XMLTESTS) to it results in "nothing to be done for target 'all'". I have no idea why.
EDIT: .PHONY: does not work for implicit rules and pattern rules.


How to calculate sine, cosine in Less?

Question: Trying to convert the following php method to use in a .less stylesheet:
<code><?php
    $deg2radians = pi() * 2 / 360;
    $rad = $degree * $deg2radians;
    $costheta = cos($rad);
    $sintheta = sin($rad);
?>
</code>
In Less, how I might one implement a sine/cosine method without using language-specific cos()/sin() functions?
<code>.rotate(@deg) {
    // css transform capable browsers properties...

    // IE <= 8
    @deg2radians: 3.1416 * 2 / 360;
    @rad: @degree * @deg2radians;
    @sintheta: sin(@rad);             // How to sin()?
    @costheta: cos(@rad);             // How to cos()?

    // filter: transform matrix method...
}
</code>
Comment: Sine and cosine are transcendental functions.  Although they can be defined by power series, there is no exact formula in terms of finitely many arithmetic operations, but approximations of any desired accuracy can be defined using arithmetic.  It's definitely a "reinvent the wheel" kind of thing, but if you're interested I'll give you some pointers on how to do it.
Comment: @hardmath I find your name to be somewhat humorous in this situation.
Comment: as a note, it would be ideal to not use any language-specific Math sine/cosine methods to solve this. If it can be done with basic math, that would make it work in separate implementations like dotless (asp.net), lessjs (JavaScript), etc...
Answer: Well, it is not quite language-independent, but since it is just Javascript it should work in all the LESS implementations, unless I'm not thinking about this clearly.
That being said, you can use Javascript to calculate sine and cosine:
<code>.rotate(@deg) {
    // css transform capable browsers properties...

    // IE <= 8
    @deg2radians: 3.1416 * 2 / 360;
    @rad: @degree * @deg2radians;
    @sintheta: ~`Math.sin(@{rad})`;
    @costheta: ~`Math.cos(@{rad})`;

    // filter: transform matrix method...
}
</code>
The backticks are used to evaluate Javascript, and you can actually access the DOM as well. For example, the following is perfectly allowed:
<code>`document.write('Hello!')`
</code>
The tilde is used for escaping, and the @{} signifies variable interpolation. For example:
<code>@input: 10;

`document.write(Math.sin(@{input}))`;
</code>
Check out the LESS usage guide for more info.
Comment: ah see, I was wondering if it was the entirety of Less that was written in JavaScript, or if it was just less.js that was the JavaScript version of less. I'll test that out in dotless (asp.net's implementation of less) right now
Comment: ah so it turns out that the escape syntax is different in less.js
@var: #f00;
prop: e(%("stuff here %d"), @var);
is prop: "stuff here #f00";
but in all other implementations it is ~foobar@{var};
Comment: @tester The syntax that you gave for less.js doesn't seem to interpolate Javascript, though. I'll try to test it later, if I have time, but it seems that the usage guide on their web site is out of date.
Comment: here's an example of the differences between string/var concat in lessjs vs dotless (or the less standard): https://github.com/dancrew32/lesslib/blob/master/mixins.less#L69-72 . =/
Comment: (just noticed you updated the answer) ah, so that's how/when to implement @{varName}. Thanks for clearing that part up.
Answer: Back-ticks do not seem to be working in dotless. You will get "[script unsupported]" in the output. The way I came up with solving the cos/sin problem in dotless was to implement function extensions. You can see an example of a function extension here: https://groups.google.com/forum/?fromgroups=#!topic/dotless/3fVydO8mTHw. Wouter Boevink had a question with configuring this in Asp.Net, which he himself answers further down in the discussion. Also look at the implementation for dotless.Core.Parser.Functions.AbsFunction to see how to implement a simple numeric function.

How to calculate sine, cosine in Less?


How do I override the title with the username?

Question: I am creating a view that will be visible under /user/%/blog, and I am trying to override the view's title with the username (e.g. kiamlaluno's blog), but the description of the field doesn't tell me what I can use to override the title.

I only get I can use Twig syntax, but that doesn't help much.
I used Content: Authored by as contextual filter. I also added author as relationship, but I didn't see any change.
How can I override the title with the name of the user authoring the nodes being shown from the view?
Answer:
Make Fields the format used by the view

Alternatively, if you are using the rendered entity row plugin check the Force using fields in Format | Settings

In Relationship, add Content author

Checkmark ☑ Require relationship

In Fields, add Name

Checkmark ☑ Exclude from display
Uncheck ☐ Link to the user

For Title put {{ name }}'s blog

You could put it as contextual filter; even though {{ name }} is not listed as a replacement pattern, it works (as long as you do step 1, 2, and 3)

Result

Bonus
If you want to capitalize the first letter of the user name use {{ name|capitalize }}'s blog.
You can also add a Rendered Entity as a field.
Comment: No need to delete this! The approach is still technically valid, it could help someone out. I'm sorry if you feel like I've pressured you to get rid of this, that wasn't my intention. I just wanted to clarify the points, I spent about 2 hours looking into this last night so I'm very familiar with how this part of Views works now
Comment: @Clive `unintuitive` if you're used to solving everything by code not unintuitive at all if you're used to coming up with non coding solutions.
Comment: @Nope, read my edited answer.
Comment: @Clive `already have a "Rendered Entity"` I do not since I am using [Fields as my format...](http://i.stack.imgur.com/ipuOQ.jpg) not content, when you use fields there is no rendered entity unless you add it as a field.
Comment: But why would you intentionally restrict yourself to that when the core devs have already given you a proper way to do it? The user_uid argument plugin doesn't convert the title to username by accident, the OP's use case is the reason it exists. Don't use a screwdriver to hammer a nail. Especially when you have s a hammer in the other hand :)
Comment: @Clive I approached it from a non User ID way, IMO `user/1` looks ugly, I like using path auto so it's `user/admin`, hence your answer is better if people want to use UID, but if they don't, you're answer is no good for them.
Comment: I meant user Name has `{{ arguments.name }}` ok I see
Comment: This answer is useful in the case somebody isn't able to write a module or doesn't want to do so. There are users who prefer doing things from the UI, and this answer helps them.
Comment: This approach is quite limited, and unintuitive. It relies on using the field row plugin, which means it doesn't work with the rendered entity row plugin, or any others.
Comment: Nope. Read the first paragraph of my answer. No code involved, problem solved without restrictions
Comment: ...which is unintuitive, when you already have a "Rendered Entity" row style.  Not to mention more effort, and more to maintain.Thanks for proving my point.
Comment: I don't understand - the OP's requirement is for user **ID**, not name, in the path. You're arguing that user name could be used, fine, everyone's got an opinion. But yet instead of making that change you've abused the field plugin to force a token into scope for the user uid argument/contextual filter? You haven't switched the contextual filter to be based on the user name field itself (see the first revision of my answer), which would actually impose the change you've suggsted rather than doing nothing at all? Can you not see how flawed that logic is?
Comment: And just to confirm, the first revision of my answer covered the _you're answer is no good for them._ part of your objection. The OP explicitly stated, as you can still see in the comments, that's incorrect for their requirements. So I removed it, as it was wrong.
Comment: User name has `{{ arguments.uid }}` ok I see.
Comment: Awesome trick! Thank you! @Clive To use this with the rendered entity row plugin check the `Force using fields` in format settings.
Answer: You can switch the node's uid argument plugin to Drupal\user\Plugin\views\argument\Uid, and get the same behaviour from that field instead. It would take a small amount of code in a custom module.
function MYMODULE_views_data_alter(&$data) {
  $data['node_field_data']['uid']['argument']['id'] = 'user_uid';
}

Alternatively, if passing the author's username in the view URL is not a problem for you, since you have the author relationship set up, you can use the User: User ID field for your filter instead of the one you're currently using. Because of the way its handler treats requests for titles, using {{ arguments.uid }} in the "Override title" field will actually provide you with the username(s) instead of the ID(s).
Comment: And `{{ raw_arguments.uid }}` gives me the user ID, if I really need it.
Comment: If I set _User: Name_ as contextual filter, the view expects I use it for /user/kiamlaluno/blog, while Drupal uses /user/1 for the user profiles.
Comment: Hmmm, understood. So what you need is a contextual filter that works off the user ID, but makes the associated username available in the arguments. Sounds reasonable. I'll have a dig in the code
Comment: @kiamlaluno Took a while but got something, updated


pfSense on failover won't route with GW on a completely different subnet

Question: I'm trying to install a pfSense with an IP failover (requirement from online.net) but I can't get the server to route the requests.
Last time I got it working with the following commands:
route add -inet 188.8.131.52/32 -link -iface em0
route add default 220.127.116.11

then download ShellCmd and add these commands (for pfsense reboot)
But it doesnt work now.
Useful informations :
ESX IP : 126.96.36.199
ESX gateway : 188.8.131.52
IP Failover 220.127.116.11

WAN interface (em0)
Status up
MAC address 00:50:56:00:2d:d9
IPv4 address 22.214.171.124
Subnet mask IPv4 255.255.255.255
Gateway IPv4 WANGW 18.104.22.168
IPv6 Link Local fe80::250:56ff:fe00:2dd9%em0
ISP DNS servers 127.0.0.1
Media 1000baseT <full-duplex>
In/out packets 0/3300 (0 bytes/258 KB)
In/out packets (pass) 0/3300 (0 bytes/258 KB)
In/out packets (block) 0/0 (0 bytes/0 bytes)
In/out errors 0/0
Collisions 0

LAN interface (em1)
Status up
MAC address 00:0c:29:bd:28:8c
IPv4 address 10.0.0.1
Subnet mask IPv4 255.255.255.0
IPv6 Link Local fe80::20c:29ff:febd:288c%em1
Media 1000baseT <full-duplex>
In/out packets 2140/2505 (189 KB/968 KB)
In/out packets (pass) 2140/2505 (189 KB/968 KB)
In/out packets (block) 0/0 (0 bytes/0 bytes)
In/out errors 0/0
Collisions 0

ping 126.96.36.199 (Main DNS Google)
PING 184.108.40.206 (22.214.171.124) 56(84) bytes of data.
From 10.0.0.1 icmp_seq=1 Destination Host Unreachable
From 10.0.0.1 icmp_seq=2 Destination Host Unreachable
From 10.0.0.1 icmp_seq=3 Destination Host Unreachable

Ping 22.214.171.124 works
Thanks in advance for you replies and if you find the solution please explain why, this will be easier to understand next time.
Answer: you need to add WAN default gateway in order to get internet on the router and clients.

add gateway as your main WAN IP address - do backup of your pfsense.
edit backup file - find gateway record and change to correct one for ip failover. save.
do restore with that file.
reboot the router.
make sure you have installed shellcmd and added your strings.
route add -inet 126.96.36.199/32 -link -iface em0
route add default 220.127.116.11


Using the Discriminant to find the value of 'k'.

Question: Find the value(s) of $k$ for which the equation, $(x+2)(x+k)=-1$, has equal roots.
(I cannot get the two values as stated in the answer $k=0$ and $k=4$.
My final line of working doesn't seem to factorize, it is $k^2-4k+8=0$)
Answer: $$(x+2)(x+k)=-1\implies x^2+(k+2)x+2k+1=0$$
The discriminant of the above must be zero , thus:
$$(k+2)^2-4(2k+1)=k^2-4k=0\iff k(k-4)=0\;\ldots$$
Answer: the discrimnant is given by $$k^2-4k$$ and we get $$k(k-4)=0$$ if $$k=0$$ or $$k=4$$
Answer: We have: $(x+2)(x+k)=-1$
$\Rightarrow x^{2}+kx+2x+2k=-1$
$\Rightarrow x^{2}+(2+k)\hspace{1 mm}x+2k+1=0$
We know that the equation has equal, or repeated, roots:
$\Rightarrow b^{2}-4ac=0$
$\Rightarrow (2+k)^{2}-4(1)(2k+1)=0$
$\Rightarrow 4+4k+k^{2}-8k-4=0$
$\Rightarrow k^{2}-4k=0$
$\Rightarrow k\hspace{1 mm}(k-4)=0$
Using the null factor law:
$\Rightarrow k=0,4$