Are there any simple ways to improve the sound quality of songs I made in MuseScore?

Question: I have composed several tracks using MuseScore.  I exported these as MIDIs and played them on my phone to my friend.  His comment was:  "It sounds like the music from an old computer game like Super Mario".
This was not exactly the vibe I was going for, as I was hoping these would sound a little more like real instruments (or at the least not so tinny and obviously electronic).  I tried exporting in different formats, eg MP3 but the sound was not noticeably different to my friend.
I tried searching for ways to improve the sound quality however a lot of these were not practical for me, such as "adjust the velocity of each note in the composition" and "record yourself playing the real instruments".
Therefore I wanted to ask, does anyone know of any simple methods I could use to improve the sound quality?  I do not mind buying a program if this will improve the quality significantly.  I appreciate anyone's time and thank you in advance.
Comment: The problem are the sound samples. The libraries you use are low quality
Comment: Unfortunately, the problem is not the composition or the software, it's the *performance*, or lack thereof. The performance of a composition is critical to bringing out the feeling of the composition. If you're composing EDM, then having a mechanical/electronic sound works, but for almost every other genre, the only way to make it sound right is to either have it actually performed (and recorded) or to make adjustments to the sequenced data to make it emulate a performance.
Comment: Granted, even MP3s and WAVs using Musescore's default soundfonts sound substantially better to me than MIDIs created with Musescore.
Comment: What version of Musescore are you using?
Answer: An exported MIDI file will play using the default soundset of the device that's playing it.   It's amazing that Windows, Android, iOS etc. even HAVE a built-in MIDI playback system with reasonably recognisable instrument sounds!  But yes, it's a bit basic.
A more interesting question is what can be exported as audio from MuseScore and shared as a MP3.    MuseScore uses SoundFonts for its internal playback.  It's rather old technology.  Alternatives to the default sound set are available, as detailed in the MuseScore manual.
https://musescore.org/en/handbook/3/soundfonts-and-sfz-files
One of the advantages of using the more advanced (and much more expensive) score publishing programs (Finale, Sibelius, Dorico...) is the wider choice of sound sets, and more developed systems of making them sound 'real'.  NotePerformer, available for the high-end scoring programs, is particularly ingenious in making lyrical music styles play effectively. But the best realism currently available is probably from transferring your score into a sequencer program (Cubase, ProTools etc.) where you'll have access to the best available sample sets and meticulous control over their use.
Join a forum dedicated to Sibelius, Dorico or Finale.  Ask if someone will take your .mscz file from Musescore, run it through the various playback systems he has available and return it to you as a MP3.  People are often remarkably happy to be helpful, particularly when it involves showing off a piece of software that they've invested heavily in :-)
(If you want me to do this for you, Googling my name will find my website very easily, contact email is at the top of the homepage.)
Comment: I think this is placing too much emphasis on the samples and not enough on the performance and/or performance humanization algorithms. It's the humanization that makes the biggest difference, in my experience. Also note that Sibelius can be rented on a monthly basis for a reasonable fee for anyone looking to have an affordable way to humanize a few compositions and then stopping the subscription.
Comment: Sibelius, in itself, won't do much 'Humanising'.  I'm not sure if the free trial of Sibelius can be combined with the free trial of NotePerformer?   But remember that NP, while effective in lyrical styles, tends to sabotage 'groove' based music with it's 'expressive' alterations of rhythm.  A feature that isn't optional.   It would be good to see a sample of @Appguy1's music.  Then we could offer informed suggestions on how it might be improved.
Comment: Having been a recent subscriber to Sibelius and a current owner of Finale, I can say with great confidence that they both have reasonably good humanization algorithms.
Answer: The simplest (but not necessarily most effective) way to improve the performance quality is to either export MusicXML or MIDI from MuseScore and open it up in a different software package that can either automatically add humanization or performance data or a package that makes it easy to add and adjust humanization.
As Laurence noted, if you export to MusicXML and then import to Finale, Sibelius, or Dorico, each of those three will create a much more human performance from the same MusicXML data compared to MuseScore. MuseScore has very little in the way of performance humanization.
Alternatively, and more powerfully, you could export MIDI and import the MIDI into a full-featured DAW such as Cubase, Logic, or Pro Tools. All three of those packages include mass MIDI editing tools that you can use for adding randomness to both timing and velocity. Other DAWs may have similar tools and capabilities, but I'm not sure. Randomization is only going to get you a small benefit. It won't sound as good as the humanization of one of the above programs.
The second-best-sounding approach is to use a DAW and make a long series of tweaks to the MIDI data, using what you know about the actual instruments, and also taking advantage of the tools in better sample libraries. For example, a lot of string sample libraries will have different samples for different techniques, like pizzicato, tremolo, etc. So you would go through and make sure you program in the sample switching for each technique. For regular techniques, you also want to make sure there are performance tweaks, such as selecting every other note of eighth note or sixteenth note runs and sliding the timing slightly and bumping the velocity to imitate left hand/right hand switching or upstroke/downstroke or whatever is appropriate for the instrument. I know you don't want to spend all the time to do this, but without this level of effort the results will also be mediocre at best.
The best sounding approach is to hire musicians to perform it for real. Some instruments, such as percussion, can sound very good with sound samples and slightly humanized playback, so you could hire only a couple musicians for the more sensitive instruments. If you have a whole string section, you could just hire one violinist (say) and have them record several takes and use that and some delay and chorus effects to create a much larger sound.
Answer: One way I've found that makes a big improvement to MP3 exports of piano pieces is by bumping up the reverb a notch.   This can be done under View...Synthesizer, selecting the Master Effects tab, and click-dragging the right-most knob Output from its default position, ~25% Dry, to an even mix (as shown below), or even a bit wet.

If your composition has several instrument tracks, it can also be helpful to set their individual left/right panning, in View..Mixer, or F10 to give the instruments some spatial separation.
A search on "Mixing a digital orchestra" brings up a few articles by Mattias Westlund that describe how to roughly pan a digital orchestra and why one should.
Here is an excerpt from that page:

1st violins – halfway left
2nd violins – less than halfway left
    (close to 1st, but you should be able to tell them apart)
Violas – center or slightly right
Celli – less than halfway right
Basses – halfway right
Trumpets – one third right
Horns – one third left
Trombones/tuba – halfway right
Flutes/clarinets – slightly left
Oboes/bassoons – slightly right
Percussion – timpani and bass drum centered or to the left
    (so as not to clash with celli/basses), anything else to taste
Answer: Arn Andersson has an article on this topic, with examples you can listen to, and well explained step-by-step instructions:
https://www.evenant.com/music/more-realistic-string-mockups/


Macro as a case in switch-case not working in c

Question: I was making a college project. I faced a problem here is a over simplified version of this problem..
#include <stdio.h>

#define h1 2
#define h2 3
#define h3 4

const int ar[] = {h1,h2,h3};
int main(int argc, char const *argv[]) {
  for (size_t i = 0; i < 3; i++) {
    switch (i) {
      case ar[i]:
        printf("yes!!!!!!!!");
    }
  }
  return 0;
}

It is showing error. I dont understand.Please explain...
Thank you.
Comment: ...and it works if you replace the macros manually?
Comment: `ar[i]` is not a macro.
Comment: This question violates language rules in a single place and a very obvious way, therefore voting to close as "simple typo"
Comment: ok so if i replace switch with a if statement will it work?
Comment: use if(.......)
Answer: In C language, case labels must be compile time constants. in your code, ar[i] not compile time constant.
C11 184.108.40.206 The switch statement(paragraph 3) :

The expression of each case label shall be an integer constant
  expression and no two of the case constant expressions in the same
  switch statement shall have the same value after conversion.
Answer: case expressions needs to be compile-time constants. The expression ar[i] is not a compile-time constant, it's evaluated at run-time.
You could possibly solve it by some refactoring:
switch (ar[i])
{
case h1:
    printf("got h1\n");
    break;
}

Another way is of course to use if statements, as in
if (i == ar[i]) { ... }


Auto Populate Linked Items in Content Item in Kentico Cloud

Question: I have two content items. One is a doctors profile and the other content item is doctors specialty. In the doctor's profile, I link the specialty content item (Linked Item) into the doctor's profile.
So let's Say Dr. Giedrimas has a "Family Medicine" specialty.

Now what I would like is when I look at the Specialty content item "Family Medicine" Dr. Giedrimas should show up as a doctor that the Specialty "Family Medicine" is used in. Now I do see this on the right side of the "Family Medicine Specialty" under the header but I cannot access that list.

It would be nice to just use the KC SDK to retrieve the content item "Family Medicine" and display "Used In" doctors instead of retrieving all the doctors list and filter each one for "Family Medicine" since It would be more efficient to just retrieve the "Family Medicine" specialty data instead filtering through all the doctors. Anybody have any thoughts on this or perhaps it is possible?

This question is a migrated from official Kentico Cloud Forum, that would be deleted.
Answer: Assuming you use using .NET SDK.
To get doctors with the "Family Medicine" specialty, just use the ContainsFilter implementation for filtering:
<code>var response = await DeliveryClient.GetItemsAsync<DoctorsProfile>(
  new ContainsFilter("elements.specialty", "family_medicine")
);
</code>
This will use the native filtering capabilities, without the need to fetch unnecessary data into your app's memory.


select price having max year in another column

Question: I have following select result
Code  Price  Year
1             200    2013
1             100    2012
2             250    2011
2             275    2012
2             300    2010
But I want following something like this with one extra column which hold price based on maximum year,
Code  Price  Year  ExPrice
1             200     2013     200
1             100     2012     200
2             250     2011     275
2             275     2012    275
2             300     2010     275
Sorry for bad English and wrong way for asking this question.
Comment: I can't see any rule here :O
Answer: Try something like this:
SELECT T1.Code, T1.Price, T1.Year, T2.Price
FROM Table T1
INNER JOIN Table T2 ON T1.Code = T2.Code AND
    T2.Year = (SELECT MAX(Year) FROM Table WHERE Table.Code = T2.Code)

Comment: OP doesn't want the the max price, but the price of the max year.
Comment: I don't want max price , i want price based on max year.
Comment: @user2703597: Oops, I misunderstood the question. I've edited my answer, so that it should give you what you want :-)
Comment: This looks like it will work as far as selecting the values he wants, as long as the two tables are already created. He would need to insert them into his new table.
Answer: You can do it with cross apply and select top 1 ... order by:
select Code, Price, Year, ExPrice
from TableName T
    cross apply (
        select top 1 Price
        from TableName
        where Code = T.Code
        order by Year desc
    ) p(ExPrice)

or row_number and join (whatever you prefer):
;with cte as (
    select Code, Price as ExPrice, rn = row_number() over (partition by Code order by Year desc)
    from TableName
)
select T.Code, Price, Year, ExPrice
from TableName T
    join cte on cte.Code = T.Code and cte.rn = 1

SQLFiddle sample
Comment: Cross apply is better for me.. :)


joint probability distribution independent variables

Question: I got a problem with my upcomming exam. There is no lecture this semester, so I cant ask anyone. I really could need help, to understand, what is going on, and how to get the task done correctly. I think, in theory, it should be easy, i am just completely lost.
I got given the joint probability distribution of three binary variables $A, B$ and $C$.

$P(a_1,b_1,c_1) = 0,0315$
$P(a_1,b_1,c_2) = 0,0585$
$P(a_1,b_2,c_1) = 0,1260$
$P(a_1,b_2,c_2) = 0,2340$
$P(a_2,b_1,c_1) = 0,0990$
$P(a_2,b_1,c_2) = 0,0660$
$P(a_2,b_2,c_1) = 0,2310$
$P(a_2,b_2,c_2) = 0,1540$

Sadly we never discussed an excercise like this ...
And now I have to show, that random variable $B$ and $C$ are independent (if they are), and if $B$ and $C$ are independent given $A$. If yes, show. If no, show a counter example.
How do i do this? And why do i do, what i do? x.x Pls help!
Comment: Welcome to MSE. For some basic information about writing mathematics at this site see, *e.g.*, [basic help on mathjax notation](/help/notation), [mathjax tutorial and quick reference](//math.meta.stackexchange.com/q/5020), [main meta site math tutorial](//meta.stackexchange.com/a/70559) and [equation editing how-to](//math.meta.stackexchange.com/q/1773).
Comment: After you ask a question here, if you get an acceptable answer, you should "accept" the answer by clicking the check mark $\checkmark$ next to it. This scores points for you and for the person who answered your question. You can find out more about accepting answers here: [How do I accept an answer?](http://meta.math.stackexchange.com/questions/3286/), [Why should we accept answers?](http://meta.math.stackexchange.com/questions/3399/), [What should I do if someone answers my question?](https://math.stackexchange.com/help/someone-answers).
Answer: You can show $B$ and $C$ are independent if $P(B=b_1,C=c_1)=P(B=b_1) P(C=c_1)$ (and similarly for the other three related patterns, but in fact showing any one is enough)
You can show $B$ and $C$ are independent given $A$ if both $P(B=b_1, C=c_1 \mid A=a_1)=P(B=b_1\mid A=a_1) P(C=c_1\mid A=a_1)$ and  $P(B=b_1, C=c_1 \mid A=a_2)=P(B=b_1\mid A=a_2) P(C=c_1\mid A=a_2)$
If these fail then you have your counter-example
Comment: Hey, thank you for your reply. Sadly i dont have P(B=b1) given. Or P(C=c1). Actually i have non of these. Just the 8, stated top. So i can't calculate this. Any other way? :(

Also, how would i calculate: P(B=b1,C=c1)? The thing right to the = is multiplication. If i got those, i can do it. But the left thing i have no clue how to calculate, even if i had those values. Like the ... "elemental values?" Dunno how they are called :(
Comment: At least i got 0,255 for B1 and 0,4875 for C1, so the right side of the = should be 0,1243. But the left side, is unclear. Could i also check B2 and C2? I mean i have to check ALL values, right? Or just find ONE counterexample? But why should it be enough, to just check B1 and C1?
Comment: Also, how would someone calculate P(B=b1∣A=a1)P(C=c1∣A=a1)? As said, i dont have A1 or B1 given. I need to check how the formula is for P(B=b1∣A=a1) for instance as well. Dont know what tu put where and how yet.
Comment: Your $0.255$ for $P(B=b_1)$  and $0.4875$ for  $P(C=c_1)$.  You can find $P(B=b_1,C=c_1) = 0.0315+0.099 = 0.1305$ which is not $0.1243125$ and so you can conclude $B$ and $C$ are not independent
Comment: $P(B=b_1 \mid A=a_1) = \frac{0.0315+0.0585}{0.0315+0.0585+0.126+0.234}=0.2$ and $P(C=c_1 \mid A=a_1) = \frac{0.0315+0.126}{0.0315+0.0585+0.126+0.234}=0.35$ and $P(B=b_1, C=c_1 \mid A=a_1) = \frac{0.0315}{0.0315+0.0585+0.126+0.234}=0.07$ and $0.2 \times 0.35 = 0.07$ so you can say that conditional on $A=a_1$ you have $B$ and $C$ independent.  Check whether this is also true given  $A=a_2$
Comment: Henry, thank you very much. You are a true saviour! :) I might not understand the reason behind all those calculations, but at least i see the patterns now, which i can use. (i just dont understand statistics at all, but have to do this course ...) One more question tho. If i have to check for A=a2. Do i have to check B=b2|A=a2, or B=b1|A=a2, and for C the same way? Like, why is it enough to just check b1 and c1, and dont care for b2 and c2 at all?
Comment: Because $P(B=b_1)+P(B=b_2)=1$ and there are just two possibilities, and similarly with $C$, you only have to check $(2-1)\times(2-1)=1$ case, and the other will follow.  Similarly with $C$.  It would be more work if there were say five possibilities each for $B$ and for $C$ as you would have to check $(5-1) \times (5-1) =16$ cases


Lightbox for embeddable JavaScript widget? Like Feedback tabs for UserVoice/GetSatisfaction

Question: There are so many lightboxes to choose from, I'm looking for a very lightweight one to use in an embedded javascript widget that would be used on a number of different web sites.  This would work in a similar way to the GetSatisfaction/UserVoice feedback tab.
Here are the requirements for the lightbox:

Very small javascript download (animation not needed)
Self contained, not dependent on any libraries such as jquery, etc.
Works in major browsers
Lightbox displays HTML content from a given URL
Close button (like GetSatisfaction or UserVoice)
Dims background
Avoids javascript namespace conflicts (or can easily be made to avoid them)
CSS styling of lightbox does not interfere with site styling

Have you used an existing lightbox scripts for this same purpose with similar requirements?  Did you roll your own?  Insights welcome!
Thanks,
Eliot
Comment: Answer (link to tutorial) to a similar question on how to create a JavaScript widget is insightful: http://stackoverflow.com/questions/1428840/how-do-hosted-services-like-uservoice-embed-their-content-on-other-web-sites/6045355#6045355
Answer: So far I found this.... which is not exactly what I am looking for... but you might like it...
domainfunk.com/jquery-feedback-tab


Unit Testing Angular component with service : Cannot read property 'diagonisticData' of undefi

Question: I am new to angular testing. I have a component, nested json and a service. The app works fine but during testing values are not being populated into the component. Please help.I have attached the service, json object,component and spec file.
I am not sure if I am following the right approach in spec file.
App component -Hub-Details-component.ts
export class HubDetailsComponent implements OnInit {
ngOnInit(): void {}
public jsonData:any = []
public diagnosticsData:any = [];
public dummy:any = [];
public hubData:any;
constructor(private dataService: DataService) {}
handleData()
{
  this.dataService.getData()
    .subscribe(response =>{
          if(response!=null)
          {
        this.jsonData=response;
        console.log(this.jsonData);
          this.dummy=this.jsonData.result;

        console.log(this.dummy);
          this.diagnosticsData=this.dummy.diagnosticData;
        const DataArray = [];
        for(const element in this.diagnosticsData)
        {

            DataArray.push({
                id:element,
                name:this.diagnosticsData[element]
            });
        }
        console.log(DataArray);
        this.hubData=DataArray;

      }
      });
   }
}

DataService
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'})
export class DataService {
  public url = '/assets/Data/Data.json'
  constructor(private http: HttpClient ) { }
  getData = () => {
     const url = 'assets/Data/Data.json';
     return this.http.get(url);
 }}

json file
{
  "result"
   {
     "abc"
      {
        "name" :"abc",
        "tag" : "xyz",
        "status": "qwe"
      }
   }
 }

spec.ts
it('should get data from dataservice',fakeAsync(()=>{
const fixture =
TestBed.createComponent(HubDetailsComponent);
const component =
fixture.debugElement.componentInstance;
const service =
fixture.debugElement.injector.get(DataService);
let spy_getPosts =
spyOn(service,'getData').and.callFake(() => {
        return of([{"result"{
                          "abc"
                           {
                           "name" :"abc",
                           "tag" : "xyz",
                           "status": "qwe"
                            }
                          }
                       }]).pipe(delay(2000));});
    fixture.detectChanges();
    component.handleData();
    tick(2000);

    expect(component.jsonData).toEqual([{
                         {"result"{
                          "abc"
                           {
                           "name" :"abc",
                           "tag" : "xyz",
                           "status": "qwe"
                            }
                          }
                       }

    }]);
}));

Thanks in advance.
Comment: @AliF50 Thanks , I was able to solve the error i was returning an array through spyon while the actual return value was observable .
Comment: When you do `console.log(this.dummy )`, what do you get? I am thinking that is undefined.
Comment: @AliF50  the problem is during testing data from service is not being passed on to the component function only . so starting from jsonData everything is null. probably the fault is in spec file on spyon since app in usual is working fine
Comment: Try removing the `delay(2000)`
Answer: Try this:
// In your spec file, mock the service;

@Injectable()
class MockDataService extends DataService {
  getData() {
    const mockData = {
       result: {
          diagnosticData: [
             { mock1: 'value1' },
             { mock2: 'value2' }
          ]
       }
    }
    return of(mockData);
  }
}

describe('Your Component Name you are testing', () => {
  let dataService;
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [],
      imports: [...yourImports],
      schemas: [NO_ERRORS_SCHEMA],
      providers: [
        {
          provide: DataService,
          useClass: MockDataService
        }
      ]
    }).compileComponents();

    dataService = TestBed.get(DataService);
  }));

 // Now your test case:

it('should call #handleData() method', () => {
  spyOn(component, 'handleData').and.callThrough();
  component.handleData();
  expect(component.handleData).toHaveBeenCalled();
});

// Thats it. You do not need to do anything else;

})


How to get the query from the query id?

Question: When I do a search with    "relevance-trace" options, and use cts:relevance-info() method to get the score detail, i found:
<qry:key>#Unsigned-long#</qry:key>
Does any body know how to get the query detail from this query id?
<code><qry:score formula="scoreSum" computation="14706+14742+13923+15066+14263">72700</qry:score>
<qry:term weight="102.125">
    <qry:score formula="8*weight*logtf" computation="817*18">14706</qry:score>
    <qry:key>10230959290518878125</qry:key>
</qry:term>
<qry:term weight="102.375">
    <qry:score formula="8*weight*logtf" computation="819*18">14742</qry:score>
    <qry:key>10778582167837573966</qry:key>
</qry:term>
<qry:term weight="102.375">
    <qry:score formula="8*weight*logtf" computation="819*17">13923</qry:score>
    <qry:key>14635861007755384081</qry:key>
</qry:term>
</code>
Answer: In general there should always be a qry:annotation element that tells you what the key means. You could try running xdmp:plan on your query to see if it gives you more information: sometimes you can piece together that an unannotated key is by looking at the trace elements as well.
Regardless you should report this as a bug: the query + your index settings.

How to get the query from the query id?


Grabbing Get option function from php file in plugin folders

Question: Really need some help i am making a plugin within the main plugin i have the code.
<code>// Some Defaults
$bucket = 'music';

// Put our defaults in the "wp-options" table
add_option("isd-bucket", $bucket);
</code>
so thats great for me i no it adds to the database music.
the problem i am have is i need to pull that out off the database into another php file in my plugin folder.
in my playlist.php i have.
<code>echo get_option("isd-bucket");
</code>
problem is when running this file i get.
Call to undefined function get_option() in
I understand this probably means i need to require other files so it intergrates with wordpress i have tried many options but cant get it to work plese help.
Thanks
Answer: Be sure to remove the extra white space (blank line) between the lines of code.
<code>// Some Defaults
$bucket = 'music';
// Put our defaults in the "wp-options" table
add_option( "isd-bucket" , $bucket );
</code>


Show that $\frac{1}{\sqrt{2\pi}} \cdot \left(\int_{- \infty}^{\infty} e^{itx} \cdot e^{-\frac{1}{2}t^2 } dx \right) = e^{-\frac{1}{2}t^2}$

Question: I am struggling to show that $$\frac{1}{\sqrt{2\pi}} \cdot \left(\int_{- \infty}^{\infty} e^{itx} \cdot e^{-\frac{1}{2}x^2 } dx \right) = e^{-\frac{1}{2}t^2}$$
In the same paper we defined  $$F(t):=\left(\int_0^t e^{-x^2}\right)^2 \quad \text{ and} \quad  G(t):=\left( \int _0^1\frac{e^{-t^2(1+x^2)}}{1+x^2} \right)$$
I showed that both functions are differentiable with
 $$F'= 2e^{-t^2} \cdot \int_0^t e^{-x^2}$$ $$G' = -2te^{-t^2} \cdot \int_0^1 e^{-x^2t^2}$$
It follows that $F' + G' = 0$ and $F + G = \frac{\pi}{4}$
With that I showed that $\int_{-\infty}^{\infty} e^{-x^2} = \sqrt{\pi}$.
Any help is appreciated.
Comment: One way is to note that both sides satisfy the ODE $u' + tu = 0$, and consequently must be constant multiples of each other and then determine the constant.
Comment: I don't think what you wish to prove is true. Since the integral is wrt $x$, the $e^{-\frac12 t^2}$ can be factored out, leaving $$\frac1{2\pi}\int_{-\infty}^\infty e^{itx} \, dx=1$$ But the LHS is divergent for ant $t$.
Comment: Thank you for noticing. I made an error instead of $e^{-\frac{1}{2}x^2 }$ i wrote $e^{-\frac{1}{2}t^2 }$
Answer: By completing the square, we find that
$$\begin{align}
\int_{-\infty}^\infty e^{-\frac12t^2+itx}\,dt&=e^{-\frac12 x^2}\int_{-\infty}^\infty e^{-\frac12\left(t-ix\right)^2}\,dt\\\\
&=e^{-\frac12 x^2}\int_{-\infty-ix}^{\infty-ix} e^{-\frac12 t^2}\,dt\\\\
\end{align}$$
Then, invoking Cauchy's Integral Theorem, we can deform the contour back onto the real line, which reveals
$$\int_{-\infty-ix}^{\infty-ix} e^{-\frac12 t^2}\,dt=\int_{-\infty}^{\infty} e^{-\frac12 t^2}\,dt=\sqrt {2\pi}$$
Therefore, we have
$$\frac1{\sqrt{2\pi}}\int_{-\infty}^\infty e^{-\frac12t^2+itx}\,dt=e^{-\frac12 x^2}$$
as was to be shown!
Comment: You're welcome.  My pleasure.
Comment: I've provided a second solution that forgoes CIT and relies instead on Feynman's trick.
Answer: Here is another way forward that relies on Feyman's trick.
Let $f$ be defined by the integral of interest
$$f(x)=\int_{-\infty}^\infty e^{-\frac12 t^2}e^{itx}\,dt$$
Differentiating under the integral yields
$$f'(x) =\int_{-\infty}^\infty ite^{-\frac12 t^2}e^{itx}\,dt$$
Integrating by parts with $u=ie^{itx}$ and $v=-e^{-\frac12 t^2}$ reveals
$$f'(x)=-xf(x)$$
from which it is easy to see that $f(x)=Ae^{-\frac12 x^2}$.  Since $f(0)=\sqrt{2\pi}$, we find that $A=\sqrt{2\pi}$ and
$$\frac1{\sqrt{2\pi}}\int_{-\infty}^\infty e^{-\frac12 t^2}e^{itx}\,dt=e^{-\frac12x^2}$$
as expected!
Comment: You're welcome!  Pleased to hear.
Comment: Thats neat thank you alot


sql Server remove value after point

Question: I Have folowing Sql query in which i am using round function but it dont removes the demimal points after point.
Here is Query
SELECT     animal_code, ROUND(SUM(calve_milk + evening + morning) / 31, 0) AS Expr1
FROM         Status
WHERE     (animal_code = 147) AND (m_date BETWEEN '1/1/2012' AND '1/31/2012')
GROUP BY animal_code

This Query Results
10.00000

And I want to get only 10 from this query.How can i do this in sql server 2005
Answer: Add a CAST around your ROUND.
CAST(ROUND(SUM(calve_milk + evening + morning) / 31, 0) AS INT)
Answer: Use this sql query:
SELECT     animal_code, CAST(ROUND(SUM(calve_milk + evening + morning) / 31, 0) as BIGINT) AS Expr1

Answer: Convert the rounded result to an integer.
For example:
SELECT CONVERT(Int, ROUND(1234.1234, 0))

sql Server remove value after point


Where are the primary and secondary tab blocks?

Question: I installed drupal 8 using the minimal install profile. For the admin pages I enabled the seven admin theme, but in the blocks admin screen I cannot find the Primary tabs and Secondary tabs blocks.
Comment: Yes and this is just the beginning, you will run into a lot of other stuff with the same problem. Not worth the battle.
Comment: Minimal install is just brutal. I highly suggest not using it. You're better off doing normal install and then uninstalling what you don't need.
Comment: Indeed. The tabs seem to be installed by the standard profile
Comment: Issue: https://www.drupal.org/project/drupal/issues/2903576
Answer: They are available under other names (I think one is called "Tabs") when you used the the minimal installation profile.
But you can import the blocks by entering the code of the files
profiles/standard/config/install/block.block.seven_primary_local_tasks.yml
profiles/standard/config/install/block.block.seven_secondary_local_tasks.yml

into the Single config import located at
admin/config/development/configuration/single/import

(this requires the config management module to be enabled)
Answer: I had the same problem, and I've just found the problem (bug).
In Drupal 8 (at least 8.3), there are 4 blocks ('Primary tabs', 'Secondary tabs', 'Page title', and 'Primary admin actions') that are all "install configs" within the standard install profile. In other words, they get created when you install drupal.
But they all have a dependency of the Seven theme. In other words, when you delete Seven, they are deleted. And they don't get re-created when you install Seven, because they aren't install configs of Seven, they're install configs of the Standard install profile.
Try this:
Use Drush or Drupal console or the built-in Configuration Synchronisation to export your current configuration. Then copy the config files...

core/profiles/standard/config/install/block.block.seven_local_actions.yml
core/profiles/standard/config/install/block.block.seven_page_title.yml
core/profiles/standard/config/install/block.block.seven_primary_local_tasks.yml
core/profiles/standard/config/install/block.block.seven_secondary_local_tasks.yml

... into your config folder - which is probably /sites/default/files/config_[hashcode]/sync/. Then do a config import (using one of those 3 tools above).


Showing that a diffusion process is positive recurrent

Question: Consider an one-dimensional diffusion, with values in $(0,\infty)$, solution of the SDE
$$dX_t = \mu(m-X_t)dt + \sigma X^{\psi}_tdB_t$$
Where $B_t$ is a standard one-dimensional brownian motion, $m>0,\sigma>0,\mu>0,\psi \in (0,\frac{1}{2})$ and $X_0 \in (0,\infty)$.
Show that
a) This diffusion is positive recurrent
b) The stationary measure $\pi$ integrates $x^q$ for $q \in \mathbb{R}$
For the positive recurrence, one usually looks at the scale function. The thing is that the presence of the term $X^\psi_t$ on the diffusion function confuses me when building the scale function. Any help or insights are greatly appreciated.
Comment: The diffusion does not start in $\psi$. This is an exponent of $x$. So $a(x)=\sigma x^\psi$.
Comment: Please write down the scale function and what exactly confuses you.
Comment: So, when computing the scale function I have $S(x)=\int^{x}_{0}exp(-2\int^{y}_{0}\frac{b(z)}{a^2(z)}dz)dy$. Where $b(x)$ is the function multiplying the $dt$ term in the diffusion and $a(x)$ is the function function multiplying the $dB_t$ term in the diffusion. The function $b(x)$ is clearly $\mu(m-x)$. But how can I write the function $a(x)$ in terms of x? Can I write something like $(x+\psi)$ instead of x (as the diffusion starts in $\psi$)?
Comment: Oh, you are right! Sorry, this was confusing me as sometimes we represented a process $X_t$ starting at point $x_0$ as $X_t^{x_0}$. But in this case he states that $X_0$ is in $(0,\infty)$ so my logic makes no sense. Thank you!
Answer: We start with the scale function
$$S(x)=\int^x exp(-2\int \frac{1-z}{z^{2p}}dz)dy=c_{2,p}\int^x exp\left(2c_{1,p}(2 p-1)y^{2 - 2 p}   -2c_{1,p} 2y^{1 - 2 p} \right)dy,$$
for $c_{i,p}>0$. This integral diverges as $x\to +\infty$ and so by Karatzas-Shreve 5.22 proposition we get recurrence in $(0,\infty)$.
As mentioned in Lecture 10: Forward and Backward equations for SDEs
 the density $\rho$ for the stationary measure satisfies
$$\partial_{x}\left((1-x)\rho-\partial_{x}(x^{2\pi}\rho)\right)=0$$
$$(1-x)\rho-\partial_{x}(x^{2\pi}\rho)=c$$
which has the solution (from Wolfram-alpha)
$$\rho(x) = x^{-2p} exp(-(x^{1 - 2p} (2p (-x) + 2p + x - 2))/((2p - 2) (2p - 1)))$$
$$\cdot \int_1^x-c exp((\xi^{1 - 2p} (-\xi 2p + 2p + \xi - 2))/((2p - 2) (2p - 1))) d\xi $$
$$+ k_1 exp(-((2p (-x) + 2p + 2p - 2) x^{1 - 2p})/((2p - 2) (2p - 1)) - 2p \log(x)).$$


How to force Margin-left (marginStart) to image in a fragment?

Question: I have a statistic/history screen in my app, which shows the last 7 days history of user's activity. I've been using 7 fragments on the same screen to represent each day (7 days ago, 6 days ago....).
On each fragment there is an image which should be changed, regarding the condition.
The problem is that I cannot margin the images on the fragments to the left side. They are all centered somehow. I need you kind help to solve it
This is the layout of the "History.java" which contains the frameLayouts
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="3"
    android:background="#E0E1FF"
    tools:context=".controller.History">

    <FrameLayout
        android:id="@+id/frame_7_days_ago"
        android:layout_marginStart="0dp"
        android:layout_width="match_parent"
        android:layout_height="85dp">

    </FrameLayout>

    <FrameLayout
        android:id="@+id/frame_6_days_ago"
        android:layout_marginStart="0dp"
        android:layout_width="match_parent"
        android:layout_height="85dp">

    </FrameLayout>  // ... below are all the same, 5 days ago, 4 etc...

This is one of the fragments layout (which is similar to the rest of them).
The first ImageView below (no_color) image, is the one I would like to force to the left in each fragment.  This image is replaced with condition by using
colorBar.setImageResource(R.drawable.green_color);
The color are changing well, but the image is always centered
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".controller.History"
    android:layout_marginStart="0dp">

<ImageView
    android:id="@+id/color_bar_image_7"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="0dp"
    android:src="@drawable/no_color"
    android:contentDescription="fragment_layout_7_days_ago" />

<RelativeLayout
    android:id="@+id/fragment7"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true">

    <TextView
        android:id="@+id/fragment_7_days_ago_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="40dp"
        android:text="@string/_7_days_ago" />

    <ImageView
        android:id="@+id/fragment_7_days_ago_comment_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="250dp"
        android:contentDescription="@string/view_comment_btn"
        android:onClick="comment7DaysAgo"
        android:src="@drawable/ic_comment_black_48px" />
</RelativeLayout>

</FrameLayout>

UPDATE:
I forgot to mention that each fragment is called to replace the frame of that day, by:
getSupportFragmentManager().beginTransaction()
                .replace(R.id.frame_7_days_ago, new StatisticSevenDaysAgo()).commit();

Answer: I am alittle unsure about how exactly the final result is supposed to look, there could be some problems with the relativeLayout having height and width match_paret. But changing framelayout to linearlayout with orientation horizontal should do the trick for the image alignment.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".controller.History"
android:layout_marginStart="0dp">

<ImageView
    android:id="@+id/color_bar_image_7"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginStart="0dp"
    android:src="@drawable/no_color"
    android:contentDescription="fragment_layout_7_days_ago" />

<RelativeLayout
    android:id="@+id/fragment7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true">

    <TextView
        android:id="@+id/fragment_7_days_ago_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="40dp"
        android:text="@string/_7_days_ago" />

    <ImageView
        android:id="@+id/fragment_7_days_ago_comment_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="250dp"
        android:contentDescription="@string/view_comment_btn"
        android:onClick="comment7DaysAgo"
        android:src="@drawable/ic_comment_black_48px" />
</RelativeLayout>

</LinearLayout>

Comment: Thank you for your time, i tried to do it, but it didn't work.
Here is a link to how it looks like: (you can see that all colored images are centered instead of being marged to the left
https://ibb.co/hyXk531
Comment: edited my answer. the height and width of the imageview cannot be match_parent.
Comment: still nothing happens. I'm thinking of using in my code FrameLayout.LayoutParams disapp = new FrameLayout.LayoutParams(700, 300);  it seems to work
Comment: if nothing happends hten either you didnt copy the code i pasted, or you have some logic that is changing the layout, cause in the designer this xml gives you a 60x60 image aligned to the left of the text.
Comment: I do have some logic that is changing in the layout. probably it is the reason why it became complicated.
Anyway, I really appreciate your help, because it helped me to solve other problem that I had (-:


How to find the last letter of a line with TUSTEP

Question: I am using Tustep and want to identify the last letter of each line in a poem, i.e the last letters of the rhyme-words.
In the following example i want to get e,t,e,t - and nothing else:
Das Glück ist eine leichte Dirne,
Und weilt nicht gern am selben Ort;
Sie streicht das Haar dir von der Stirne
Und küßt dich rasch und flattert fort.

zn,,,-{00}{&a}{]}-  gets me any word at the line end and with zn,,,-{00}{&a}{]}-{00}{&a}[.,;]{]}-  I get even the words that are followed by a punctuation mark (and the line end).
But what I actually want is not the words, but the last letters of these words.
Answer: If your text has no diacritics, the following TUSTEP search parameter expression should do the trick:
zn,,,--{&a}{]}{0}{@} --{&a}{]}{0}{@}-

Here's what it does: The principal pattern {&a}{]}{0}{@} looks for a single letter ({&a}) optionally followed by a non-letter/non-digit character ({0}{@}). Those two elements are separated with a right-hand marker ({]}), which ensures that only the letter is part of the marked string. If we would only use this search pattern, we would find all the last letters of all words. As you are only interested in the end of the lines, we simple exclude all words that are followed by an optional punctuation character and a mandatory space character. This exclusion pattern is basically the same as the search pattern, with the difference of having a space character in the end: --{&a}{]}{0}{@} --.
Certainly it must be noted that this whole search pattern fails, if your lines have trailing white space in the end. So you should trim your lines first before applying this search pattern.

EDIT:
If your text does have diacritics, the answer above will yield some wrong matches, e. g.:
Sehr schlimm: eingeladen sein,
wenn zu Hause die Räume stiller,
der Café besser
und keine Unterhaltung nötig ist.
(Note the matched "f" before the "é"). So in a scenario with diacritics I recommend using
zn,,,--{&a}{]}{0}\%{0}{%} --{&a}{]}{0}{%}-

Comment: Perfect!  That solves my problem. There should not be any trailing white-spaces in the texts I am working with.
Comment: I've edited may answer to also handle text with diacritics


is the tensor product of coordinate rings always a coordinate ring?

Question: given an arbitrary field $k$ (in particular, it does not need to be algebraically closed or even perfect) and sets $S ⊆ \mathbb A^m(k)$ and $T ⊆ \mathbb A^n(k)$, is there an isomorphism of coordinate rings
$$k[S × T] \cong k[S] \otimes_k k[T]?$$
from the commutative diagram with the natural arrows
$$
 \require{AMScd}
 \begin{CD}
  k[\mathbb A^m × \mathbb A^n] @= k[\mathbb A^m] \otimes_k k[\mathbb A^n] \\
  @VVV @VVV \\
  k[S × T] @<<< k[S] \otimes_k k[T]
 \end{CD}
$$
we would only need a natural arrow in the other direction in the bottom row to conclude that. for this, it would suffice for a regular functions $h \colon \mathbb A^m × \mathbb A^n → k$ vanishing on $S × T$ to come from a sum of products of regular functions $f \colon \mathbb A^m → k$ and $g \colon \mathbb A^n → k$ vanishing on $S$ and $T$ respectively. is that so? how can i show this?
note that i consider classical affine space, not schemes and $\mathbb A^m (k) = k^m$ and $\mathbb A^n (k) = k^n$ as sets.
(by the way: i don't see that the tensor product of two reduced $k$-algebras of finite type need not be reduced as an obstruction here as, to my knowledge, the counter-examples arise from inseparable extensions, wich are, to my knowledge, not formed by coordinate rings.)
Comment: To be clear, your definition of $k[S]$ is $k[x_1,\dots,x_m]/I$ where $I$ is the ideal of polynomials that vanish on $S$?
Comment: You need to add a little more information about your setup here - are you working with "classical varieties" or schemes, etc. The answer is a more-or-less obvious yes if you work with schemes, but it looks like you may not be doing that and the answer probably depends on some of the details of your setup.
Comment: yes, classical algebraic loci. i'll add that.
Comment: @EricWofsey yeah - is there a different, non-equivalent definition?
Comment: It's clear that $S \times T = V(I(S)+I(T))$, where we used inclusions of the form $k[x_1, \dots, x_m] \hookrightarrow k[x_1, \dots, x_{n+m}]$, so the question reduces to showing that $I(V(I(S)+I(T)))=I(S)+I(T)$. Not sure how to do that, though
Comment: yeah. (well, not quite. i didn't assume $S$ and $T$ to be closed, so it's $V(I(S)) × V(I(T)) = V(I(S) + I(T))$ technically speaking, but it reduces to the same question nonetheless.)
Comment: I'd say that if $I(S),I(T)$ are primes it works because $\overline{k}\cap k[S]=k$ so $I(T)(Y)$ remains prime in $k[X,Y]/(I(S)(X))$. Next it is about writing $I(S),I(T)$ as intersection of prime ideals.
Comment: @reuns i can't follow your reasoning here. do you care to elaborate? how do you conclude that $I(T)(Y)$ remains prime in $k[X,Y]/(I(S)(X))$ and how does the assertion then follow?
Answer: Functors that have a left adjoint preserve $\underleftarrow{\textrm{lim}}$ (inverse limits), in particular finite products. So perhaps this could (be made to) work.
Let $\mathrm{Aff}_k$ be the category of the algebraic sets $S$ living in some $\mathbb A^m(k)$ (any $m$). Following Hartshorne, regular functions $S\to k$ and morphisms $S\to T$ can be defined just like when $k$ is algebraically closed ([H], §I.3).
Let $_k\mathrm{Fta}$ be the category of $k$-algebras of finite type that are coordinate rings. These can be given as pairs $(n,I)$, where $I$ is an ideal of $k[X_1,\cdots\,X_n]$ which any $f$ $\in$ $k[X_1,\cdots\,X_n]$ that vanishes at all common zeroes in $\mathbb A^n(k)$ of the $i$ $\in$ $I$ must belong to. I.e., we need "$I$ $=$ $I(Z(I))$". Morphisms are $k$-algebra homomorphisms. Denote the opposite category (with all arrows reversed) by $_k\mathrm{Fta}^{\circ}$.
Then we have covariant functors $A:$ $\mathrm{Aff}_k$ $\to$ $_k\mathrm{Fta}^{\circ}$ and $Z:$ $_k\mathrm{Fta}^{\circ}$ $\to\mathrm{Aff}_k$ given, for $S$ $\subseteq$ $\mathbb A^m(k)$, by $S$ $\mapsto$ $(m,I(S))$, which is just the $k$-algebra $k[S]$, and by $(n,I)$ $\mapsto$ $\{\vec{\lambda}\in k^n$ $\mid$ $\forall_{i\in I}\,i(\vec{\lambda})=0\}$, which is of course closed.
By corollary I, 3.8 of [H], we have $\mathrm{Aff}_k(Z(n,I),S)$ $\cong$ $_k\mathrm{Fta}^{\circ}(A(Z((n,I))),A(S))$. I think the proof goes through for arbitrary fields $k$. But $A(Z((n,I)))$ $=$ $(n,I)$ because of the restriction we imposed on $I$.
Hence $\mathrm{Aff}_k(Z(n,I),S)$ $\cong$ $_k\mathrm{Fta}^{\circ}((n,I),A(S))$, and these isomorphisms are natural. So $Z\dashv A$, i.e. $Z$ is a left adjoint of $A$, and hence $A$ preserves finite products. Now direct products in $_k\mathrm{Fta}^{\circ}$ are the same thing as direct sums in $_k\mathrm{Fta}$, and those are given, like in the category of all $k$-algebras (for any ring $k$), by $\otimes_k$. So $A(S\times T)$ $\cong$ $A(S)\otimes_kA(T)$.
$A$ also respects any other forms of $\underleftarrow{\textrm{lim}}$ that exist in $\mathrm{Aff}_k$, subject to the fact that all arrows are reversed, so that $A$ will send them to the corresponding $\underrightarrow{\textrm{lim}}$ in $_k\mathrm{Fta}$. So pullbacks become pushouts, etc.
[H] Robin Hartshorne, Algebraic Geometry.
Comment: How do you know that coproducts in $_k\mathrm{Fta}$ are given by $\otimes_k$?
Comment: Haha, you are right. That is precisely the original question! Coproducts exist ($A$ produces them), but they may not necessarily be given by the tensor product (which needs not be in $_k\mathrm{Fta}$). Also, if the argument had worked, there should have been an easy direct proof avoiding category theory.


How to live stream audio from a url into an outgoing twilio phonecall?

Question: I would like to make an outgoing phone call from my twilio number(A) to another phone number(B). When the call is picked up, I would like to livestream audio from a public url. How do I do this?
I've been looking at the <stream> verb in TwiML, but my understanding is that it only streams audio from an active phonecall. I'd like to stream audio into the phonecall.
Any leads would be appreciated.
Answer: Twilio developer evangelist here.
You are right that the <Stream> verb only streams audio from the phone call to your server.
Currently there is no simple way to stream audio back into a call using a TwiML verb.
If you knew your way around a PBX and how to stream audio over SIP, then you could connect a call using <Sip> and stream the audio that way.
Also, if you were making the call from a mobile application or browser, you could get access to the audio stream and send audio that way too.
Answer: If you are making an outgoing call, you can simply use the Play verb.
For example:
<Response>
    <Dial>415-123-4567</Dial>
    <Play>https://api.twilio.com/cowbell.mp3</Play>
</Response>


Connectivity and the triangle inequality

Question: It is well known that if a weight function of a graph satisfies the triangle inequality, then 2 vertex connectivity is same as 2 edge connectivity in that graph. Can someone find a proof for this?
I looked at many papers, but couldn't find anything clear about the proof.
Comment: @Yikai Normally the weight function would be the distance between two nodes. Let $u,v$ and $w$ be three nodes, then the triangle inequality for the weight function will be $d(u,v)<=d(u,w)+d(w,v)$. A graph is connected if for any two vertices $x, y ∈ V (G)$, there is a path whose endpoints are $x$ and $y$. A connected graph G is called 2-connected if for every vertex $x ∈ V (G), G − x$ is connected.
Answer: Connectivity and triangle inequality for distance function don't have anything common. There is a graph $G = \overline{\overline{2K_2}\cup K_1}$ that is 2-edge-conncted but is not 2-vertex-connected. You may use any distance function for it.
So, $G = (\{\,1, 2, 3, 4, 5\,\}, \{\,\{\,1, 2\,\}, \{\,3, 4\,\}, \{\,1, 5\,\}, \{\,2, 5\,\}, \{\,3, 5\,\}, \{\,4, 5\,\}\,\})$. Let $d(u, v) = A_{uv}$ for
$$A = \begin{bmatrix}
0 & 1 & 2 & 2 & 1\\
1 & 0 & 2 & 2 & 1\\
2 & 2 & 0 & 1 & 1\\
2 & 2 & 1 & 0 & 1\\
1 & 1 & 1 & 1 & 0
\end{bmatrix}.$$
Probably it was meant that 2-vertex-connected graph is 2-edge-connected. That is true since $\kappa(G) \le \lambda(G)$ for any graph $G$, and this faact has no relation to distance function.


Logistic regression model does not converge

Question: I've got some data about airline flights (in a data frame called flights) and I would like to see if the flight time has any effect on the probability of a significantly delayed arrival (meaning 10 or more minutes). I figured I'd use logistic regression, with the flight time as the predictor and whether or not each flight was significantly delayed (a bunch of Bernoullis) as the response. I used the following code...
flights$BigDelay <- flights$ArrDelay >= 10
delay.model <- glm(BigDelay ~ ArrDelay, data=flights, family=binomial(link="logit"))
summary(delay.model)

...but got the following output.
> flights$BigDelay <- flights$ArrDelay >= 10
> delay.model <- glm(BigDelay ~ ArrDelay, data=flights, family=binomial(link="logit"))
Warning messages:
1: In glm.fit(x = X, y = Y, weights = weights, start = start, etastart = etastart,  :
  algorithm did not converge
2: In glm.fit(x = X, y = Y, weights = weights, start = start, etastart = etastart,  :
  fitted probabilities numerically 0 or 1 occurred
> summary(delay.model)

Call:
glm(formula = BigDelay ~ ArrDelay, family = binomial(link = "logit"),
    data = flights)

Deviance Residuals:
       Min          1Q      Median          3Q         Max
-3.843e-04  -2.107e-08  -2.107e-08   2.107e-08   3.814e-04

Coefficients:
            Estimate Std. Error z value Pr(>|z|)
(Intercept)  -312.14     170.26  -1.833   0.0668 .
ArrDelay       32.86      17.92   1.833   0.0668 .
---
Signif. codes:  0 â***â 0.001 â**â 0.01 â*â 0.05 â.â 0.1 â â 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 2.8375e+06  on 2291292  degrees of freedom
Residual deviance: 9.1675e-03  on 2291291  degrees of freedom
AIC: 4.0092

Number of Fisher Scoring iterations: 25

What does it mean that the algorithm did not converge? I thought it be because the BigDelay values were TRUE and FALSE instead of 0 and 1, but I got the same error after I converted everything. Any ideas?
Comment: If you have the actual delay times, you are likely to get better information by modeling them, rather than reducing them to a binary variable.
Comment: First thought: *Perfect separation*, meaning the predictor is 'too good', the logits go to +/- infinity and everything falls over. Second thought: Does the code do what you think it does? Your variable names don't seem to quite match your description. You might elaborate what the data is more precisely, since it looks like you *might* be trying to predict something with itself.
Comment: not sure I deserve the "accept". @Conjugate Prior's answer explained what was wrong with your model. I thought it worth explaining the warning you mentioned in terms of the algorithm.
Comment: related [question](http://stats.stackexchange.com/questions/45803/logistic-regression-in-r-resulted-in-hauck-donner-phenomenon-now-what/45831#comment88933_45831)
Comment: you can try glm1() function. It overcome the problem converge
Answer: glm() uses an iterative re-weighted least squares algorithm. The algorithm hit the maximum number of allowed iterations before signalling convergence. The default, documented in ?glm.control is 25. You pass control parameters as a list in the glm call:
delay.model <- glm(BigDelay ~ ArrDelay, data=flights, family=binomial,
                   control = list(maxit = 50))

As @Conjugate Prior says, you seem to be predicting the response with the data used to generate it. You have complete separation as any ArrDelay < 10 will predict FALSE and any ArrDelay >= 10 will predict TRUE. The other warning message tells you that the fitted probabilities for some observations were effectively 0 or 1 and that is a good indicator you have something wrong with the model.
The two warnings can go hand in hand. The likelihood function can be quite flat when some $\hat{\beta}_i$ get large, as in your example. If you allow more iterations, the model coefficients will diverge further if you have a separation issue.
Comment: By convergence I mean that the parameters being estimated in the model don't change (or only change less than some small tolerance) between iterations. Here the parameters get increasingly large and fitting stops because of the limit on iterations but the parameter estimates changed a lot between the penultimate and the last iterations and as such haven't converged.
Comment: Could you explain what exactly do you mean by model convergence here?
Answer: You could try to check if Firth's bias reduction works with your dataset. It is a penalized likelihood approach that can be useful for datasets which produce divergences using the standard glm package. Sometimes it can be used instead of eliminating that variable which produces complete/almost complete separation.
For the formulation of the bias reduction (the $O(n^{-1})$-term in the asymptotic expansion of the bias of the maximum likelihood estimator is removed using classical cumulants expansion as motivating example) please check
http://biomet.oxfordjournals.org/content/80/1/27.abstract
Firth's bias reduction is implemented in the R-package logistf:
http://cran.r-project.org/web/packages/logistf/logistf.pdf


redis sentinels in AWS and static IPs

Question: I want to set up a 3-instance sentinel cluster in AWS and then have a master and standby redis instance controlled by the sentinel cluster. The question is how to deal with DHCP in an AWS VPC. Since the redis and sentinel cluster have to know where to look for each other, it seems to me there is no way to get around assigning static IPs to all of the instances.

Am I assuming correctly or is there a way to create a sentinel/redis cluster without static IPs?
If that's the case and I have to assign static IPs to the instances, there's a risk that the IP will be taken by another instance in the VPC and therefore the launching of the instance will fail. Does anyone know if there's a way around that problem?
Comment: Why not use elasticache for redis?
Comment: Woops. I misread that. We want to remain as independent of Amazon's more specialized services if we can. Also, we're migrating an established infrastructure from our datacenter to AWS, and trying to keep as much the same in the configuration as we can.
Comment: Can you use domain names? You can use a private route53 hosted zone to give machines names that map to their IPs.
Comment: I was thinking that I could go that route as a last resort. I would ideally like to stay with IP addresses because I don't want to deal with TTL of the records. I will also have to figure out a way for replacement instances to automatically take the records, probably in Terraform. I'm discovering the less you have to put in TF the better.
Answer: The only way I can find to assign a private static IP to an EC2 instance is to create an Elastic Network Interface (ENI), specify the IP when you create it, then attach it to your EC2 instance.
Comment: It's not a matter of how to assign the IP that I'm after. My two questions are: 1. Is there a way to build the cluster without the use of static IPs (or DNS for that matter)? 2. If not, is there a way to guarantee the static IP will always be available _only_ for the given instance?
Comment: 1) Can't help sorry. 2) There doesn't seem to be a lot of documentation in this area. I have to assume that AWS is smart enough not to assign an IP twice. If you really need to know this is a question for AWS support, which costs $29 per month.

redis sentinels in AWS and static IPs


Find consecutive duplicate values in a multi dimensional array in Lua

Question: data = {{1,2,3},{1,2,1},{1,2,3},{1,2,1},{1,2,3},{1,2,1}}
qty = 1 -- dynamic variables

function find_sequence(qty)
    if qty < 1 or qty > #data then
        print("INVALID INPUT or NOT ENOUGH DATA")
        return
    end

    local results = {}
    for i = 1, #data - qty + 1 do
        local sequence = {}
        for j = i, i + qty - 1 do
            table.insert(sequence, table.concat(data[j]))
        end
        local key = table.concat(sequence, " ")
        if not results[key] then
            results[key] = 1
        else
            results[key] = results[key] + 1
        end
    end

    local count = 0
    for sequence, occurrences in pairs(results) do
        if occurrences >= 2 then
            count = count + 1
            print("FOUND:", sequence:gsub("%d", "%1 "), "Occurs:", occurrences, "times")
        end
    end

    if count == 0 then
        print("NOT FOUND")
    end
end

find_sequence(qty)

The script is working when qty = 0, qty = 1, qty = 2, qty = 3, qty = 5, qty = 6 and qty > #data.
But it gives wrong output when qty = 4:
FOUND: 123 121 123 121, Occurs: 2 times

The expected output should be NOT FOUND, as there is no duplicate of 4 consecutive values in the table.
Comment: Please clarify what the problem statement is. Do you want to find a consecutive sequence of `qty` duplicate values? Currently you're counting how often (overlapping) subsequences of length `qty` occur. Are you trying to determine whether there are duplicate subsequences of length `qty`?
Answer: Here's what your script does:

It rejects invalid inputs (qty < 1 or qty > #data)
Then it stringifies all subsequences of length qty and uses them as keys into a table to count their occurrences. There are two maybe-issues with this:

table.concat(data[j]) uses no separator. You can't distinguish 11, 2 and 1, 12 that way. If your values are guaranteed to be single digits, this is fine, although it raises the question of why you aren't using strings right away.
The subsequences are overlapping.

Finally, you check whether any subsequence appeared twice or more times. Again, a subsequence and an overlapping subsequence will be considered a duplicate.

What happens when qty = 4 is that you do indeed have two overlapping subsequences with the same contents:

The first 4 items: {1,2,3},{1,2,1},{1,2,3},{1,2,1}
The last 4 items: {1,2,3},{1,2,1},{1,2,3},{1,2,1}
The middle 2 items are the overlap

In string form, both of these are 123 121 123 121, which is what you're seeing. Since that is not what you want, I assume that the goal of your code is to find disjoint duplicate subsequences.
One simple approach to achieve this would be to just remember where the first and the last subsequence was to only count non-overlapping subsequences. Change
if not results[key] then
    results[key] = 1
else
    results[key] = results[key] + 1
end

to
if results[key] then
    results[key].max = i
else
    results[key] = {min = i, max = i}
end

This gives you a range. If the indices are at least qty apart, at least two subsequences don't overlap. Thus you can change your check in the loop from if occurrences >= 2 then to if occurrences.max - occurrences.min >= qty then. You could even avoid this second loop altogether and directly perform this check when a duplicate subsequence is encountered.
For counting duplicate subsequences, you'll first have to define how to count them.


new to R, receiving an error in my function

Question: true_RA<-seq(-2,2 ,length.out = 13)  #true vector with -2<RA<2 in
# even intervals for 13 players

true_RA

current_RA<-true_RA   #assign true_RA to the current ratings

ratings_overtime<-matrix(current_RA, nrow=13, ncol=10,000) #create a #matrix of 13 players
# and 10000 ratings

p<-0.5825702

simulate_game<-function(i,j){
    old_RA_i<-current_RA[i] #assign the rate from
#my current rating vector from vector

    old_RA_j<-current_RA[j]
    win<rbinom(1,1,p)
if (win==1){current_RA_i<-old_RA_j+400}
else{current_RA_j<-old_RA_i+400}
}
 return(c(current_RA_i,current_RA_j))
}

my function should do simulates a game between players i and j given their true underlying ratings from rbinom(1,1,p).
I am getting an Error: object 'current_RA_i' not found.
Comment: How do you call `simulate_game` function?
Comment: Has your problem been resolved?
Answer: You have an extra close curly bracket, so the function ends before the return statement.
Since the current_RA_i and current_RA_j variables are defined within the function, they are not global in scope, and thus have no value after the function ends.
Removing the } character just before your return statement should resolve the error.


android button click and clicked

Question: I am fairly new to android programming (but not to Java programming). I have a use case where a user clicks a 'Go >>' Button. Once the button is clicked, the text on the button changes to 'Done!' and the user can click the same button once he is 'done'.
I am wondering what is the best approach to implement this button behaviour... One approach that I can think of is to check for the button text and trigger appropriate action - is there a better way that you could recommend? Thanks!
Comment: i think you may maintain a triggerCounter in type of integer.
Answer: The "best" approach is probably to check a boolean flag in the OnClickListener if the Button has already been clicked.
<code>OnClickListener listener = new OnClickListener() {
    boolean isFirstClick = true;
    @Override
    public void onClick(View v) {
        if(isFirstClick) {
            isFirstClick = false;
            // Do something
        }
    }
}
</code>
An alternative approach is to check the value of the Buttons' text, but this is a few milliseconds slower.

android button click and clicked


Determine the Lie algebra of the unit-quaternions $S^3 \subset \mathbb{H}^*$

Question: Determine the Lie algebra of the unit-quaternions $S^3 \subset \mathbb{H}^*$ and their left-invariant vector fields.
Unfortunately I am struggling with quaternions. I computed the differential of left-multiplication in $\mathbb{H}^*$ but that didn't get me very far.
Thanks in advance for any help !
Comment: There are several posts here with an answer, e.g. [this one](https://math.stackexchange.com/questions/3041948/deriving-quaternions-via-the-cross-product). I don't know which duplicate is the best. See also [this post](https://math.stackexchange.com/questions/2218186/relationship-between-spin3-su2-unit-quaternions-and-so3?noredirect=1&lq=1). You should say what you can use.
Comment: See also the answer [here](https://math.stackexchange.com/questions/2206677/induced-lie-algebra-homomorphism-from-mapping-unit-quaternions-to-a-rotation).
Comment: I would like to also advertise my answer https://math.stackexchange.com/a/4020854/96384. It is much more general, but includes (see "Note 1") your case as the basic case $SL_1(\mathbb H)$ and writes the Lie algebra explicitly as $\mathfrak{su}_2$ and/or the space of pure quaternions with commutator bracket (which by the way is further $\simeq (\mathbb R^3, \times)$, the cross-product).
Answer: Thanks for all the help guys !
I decided to use the Lie group-Lie algebra correspondence https://en.wikipedia.org/wiki/Lie_group%E2%80%93Lie_algebra_correspondence.\
We know that $\mathbb{H} \cong \mathbb{R}\cdot SU(2)$, then it is easy to see that $S^3 \cong SU(2)$. By the above correspondence, it suffices to compute $T_{id}SU(2)$. For that, say we have a smooth curve $c:(-\epsilon,\epsilon) \to SU(2)$ with $c(0) = id$ and $c'(0) = X \in T_{id}SU(2)$. Since $c(t)$ lies in $SU(2) \: \forall t \in (-\epsilon,\epsilon)$ we have that $c^{*}(t)\cdot c(t) = id$. Differentiating both sides for t and evaluating at $t = 0$ yields:
$X+X^{*} = 0$. Therefore $T_{id}SU(2) \subset V:=\{X \in SU(2) \:| X^{*}+X = 0\}$. And since both spaces have $\mathbb{R}-$dimension 3 they must be equal. If we write $X^{*}+X$ explicitly we get:
\begin{equation}
    \begin{bmatrix}
      u & -\overline{v} \\
      v & \overline{u}
    \end{bmatrix}
    +
    \begin{bmatrix}
      \overline{u} & \overline{v}\\
      -v & u\\
    \end{bmatrix}
    =
    \begin{bmatrix}
      0 & 0\\
      0 & 0\\
    \end{bmatrix}
  \end{equation}
and therefore that $u + \overline{u} = 0$ which means u must be purely imaginary. But with the standard identification $\mathbb{H} \cong SU(2)$ $u = a+i\cdot b$. So $T_{id}S^3 = Im(\mathbb{H})$.
Now for the left-invariant vector-fields:
Let $z \in S^3$. To compute $(l_z)_{*,id}$, take $h \in T_{id}S^3 = Im(\mathbb{H})$, and a curve $c:(-\epsilon,\epsilon) \to S^3$ with $c(0) = id$ and $c'(0) = h$. Then $(l_z)_{*,id}(h) = \frac{d}{dt}|_{t=0}(l_z \circ c(t)) = \frac{d}{dt}|_{t=0}(z \cdot c(t)) = z \cdot h$. So the differential is just left-multiplication. I now choose the basis $\{i,j,k\}$ of $T_{id}S^3$. This is then a basis of the left-invariant vector-fields on S^3. To compute them explicitly:
$X_i(a,b,c,d) = (a+b\cdot i + c\cdot j + d \cdot k)\cdot i = -b + a\cdot i + d\cdot j - c\cdot k$.
$X_j(a,b,c,d) = (a+b\cdot i + c\cdot j + d \cdot k)\cdot j = -c - d\cdot i + a\cdot j + b \cdot k$.
$X_k(a,b,c,d) = (a+b\cdot i + c\cdot j + d \cdot k)\cdot k = -d + c\cdot i-b \cdot j + a\cdot k$.
I hope everything is correct.


Undefined result in Console by Input to Array Fields

Question: Why when I pus the Submit button is undefined all of them?
I want to create an array just by using input fields like this.
Is there a nother way to do this?
I tried to do it with Class name and the result is still undefined

function clicked() {
    var input_value = document.querySelectorAll('#data, #data1, #data2, #data3, #data4').value;
    console.log(input_value)
}

document.getElementById('btn').addEventListener('click', clicked);
<input id="data">
<input id="data1">
<input id="data2">
<input id="data3">
<input id="data4">
<button id="btn">Click me</button>
Answer: querySelectorAll will return a "NodeList", which is similar to an array. NodeLists don't have a value property, so it's returning undefined.
If you want to get the value from each of the input boxes, you'll need to loop through the NodeList and pull the value from each HTML element individually.
function clicked() {
    var nodeList = document.querySelectorAll('#data, #data1, #data2, #data3, #data4');
    for (var i = 0; i < nodeList.length; i++) {
      console.log(nodeList[i].value);
    }
}

document.getElementById('btn').addEventListener('click', clicked);

Comment: I want it to return a pure array in console
Answer: Change fn to this:
function clicked() {
  var inputs= document.querySelectorAll('#data, #data1, #data2, #data3, #data4');
  inputs.forEach(i => {
    console.log(i.value);
  });
}


How many $3$-digit even numbers less than $700$ can be formed from the digits $0, 2, 4, 5, 6, 8$ if repetition of digits is not allowed?

Question: Consider the digits $0,2,4,5,6,8$. How many $3$-digit even numbers less than $700$ can be formed if repetition of digits is not allowed? Note the first digit can't be zero.
Comment: What have you done so far? Do you have an answer which you want to double check? Please add your working to the question always, since it helps people answer your question better. If you have no working to show, then try to form some three digit numbers from the set, and see if you get any pattern.
Comment: The very first thing is to choose the first digit (the hundreds digit). Because it cannot be zero, the only options left are $2, 4, 5,6$. Surely, you can continue from here.
Answer: Even Number will end with $\left \{ 0,2,4,6 \text{  or  }8 \right \}$.

Let us divide the possiblities of even number with number less than $700$ in $3$ parts.

Ending with $0$
Ending with $\left \{ 2,4 \text{  or  }6 \right \}$.
Ending with $8$.

Ending with $0$

$3$ digit number possible$= \underbrace{4}_{\text{4 choices among  2,4,5,6}} \times  \underbrace{4}_{\text{4 choices among  2,4,5,6,8}} \times 1$

Ending with $\left \{ 2,4 \text{  or  }6 \right \}=
 \underbrace{3}_{\text{3 choices among  2,4,5,6}} \times  \underbrace{4}_{\text{4 choices among  0,2,4,5,6,8}} \times \underbrace{3}_{\text{3 choices among  2,4,6}}$

Ending with $8$
  $= \underbrace{4}_{\text{4 choices among  2,4,5,6}} \times  \underbrace{4}_{\text{4 choices among  0,2,4,5,6}} \times 1$

$=16+36+16=68$
Comment: If the units digit is $2$, $4$, or $6$, then $8$ is a potential tens digit.  The other two cases are correct.
Comment: You have a typographical error for the middle case.  You meant to multiply by $4$ choices for the tens digit.  Otherwise, everything is correct.
Comment: In your second case, you wrote $4$ choices among $2, 4, 5, 6$ but wrote $3$ for the number of choices.  The number of choices for the hundreds digit depends on whether or not $8$ is the units digit.
Comment: @N.F.Taussig sir.Sorry it was a typo.
Comment: for the second case, i am first selecting units place by $4$ ways and then selecting the thousand's pace by $3$ ways$
Comment: If the units digit is $8$, then you have four choices for the hundreds digit: $2$, $4$, $5$, $6$.  On the other hand, if the units digit is $2$, $4$, or $6$, the restriction that the number is less than $700$ means you only have three choices for the units digit: $5$ or the two even numbers among $2$, $4$, or $6$ that has not been used in the units digit.  I suggest you use three cases:  units digit is $0$; units digit is $2$, $4$, or $6$; units digit is $8$.
Comment: edited .Although i am afraid if my answer is correct!
Comment: thanks a lot .i have edited it
Answer: Let the set of $3$ digit even numbers be represented by _ _ _.
Last digit can be $0, 2, 4, 6 \text{ or } 8$; rest $2$ digits can be anything among remaining $5$ digits (including the digit $5$) $\implies 5 \times 5 \times 4 = 80$. That's the total number of 3 digit even numbers formed by these digits.  But we also counted $3$-digit numbers starting with $0$ or $8$, count the number of such integers and subtract.
Comment: We want the number of $3$-digit even numbers less than $700$ that can be formed from the given digits without repetition.  Why are you adding the one-digit and two-digit numbers to the total?
Comment: @N.F.Taussig yes, there is no need for that
Answer: The first digit can be either 2,4,5 or 6
The second digit can be any of the digits
The last digit can be any of the digits except 5
Case 1: When first digit is 5. $$Number\ of\ ways\ = 1*5*4=20$$
Case 2: When first digit is not 5 and second digit is 5. $$Number\ of\ ways\ = 3*1*4=12$$
Case 3: When neither first not second digit is 5.
$$Number\ of\ ways\ = 3*4*3=36$$
Total number of ways= $36+12+20=68$


How to determine the sign of R underlying R-squared?

Question: So we know that:
$$
R^{2}=\frac{SSR}{SSTO}
$$
If we want to know the  value of $R$, how do we know what the proper sign is?
Answer: In multiple regression, $R$ represents the correlation between $\hat{y}$ and $y$, and as such is always non-negative.
e.g. see Wikipedia on $R^2$

[...] $R^2$ equals the square of the Pearson correlation coefficient between the observed and modeled (predicted) data values of the dependent variable.

In simple regression you could regard $R$ as more than one thing (since $R^2$ can be seen meaningfully as the square of more than one correlation), in which case you need to say which thing you want. If you mean the correlation between $\hat{y}$ and $y$, that's still non-negative. If you mean the correlation between $y$ and $x$, the sign will be the same as the sign of the regression coefficient.
Comment: @Bill no, I mean what I have there in the first line -- $R$ is not the correlation squared. $R^2$ is the correlation squared.
Comment: "represents the correlation squared" I think you mean


How to fix "AttributeError: 'JpegImageFile' object has no attribute 'read'?

Question: I am a beginner and I am learning to code an image classifier. My goal is to create a predict function.
Any suggestion to fix it?
In this project, I want to use the predict function to recognize different flower species. So I could check their labels later.
Attempt to fix: Unfortunately, the error is still persistent. I have already tried these codes:
img = process_image(Image.open(image))
img = torch.from_numpy(img).type(torch.FloatTensor)

This is the error that I need to fix now.

AttributeError: 'JpegImageFile' object has no attribute 'read'

Code:
# Imports here
import pandas as pd
import numpy as np

import torch
from torch import nn
from torchvision import datasets, transforms, models
import torchvision.models as models
import torch.nn.functional as F
import torchvision.transforms.functional as F
from torch import optim
import json

from collections import OrderedDict
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
from PIL import Image

def predict(image, model, topk=5):

#Predict the class (or classes) of an image using a trained deep #learning model.
#Here, image is the path to an image file, but input to process_image #should be Image.open(image).

    img = process_image(Image.open(image))
    img = torch.from_numpy(img).type(torch.FloatTensor)

    output = model.forward(img)
    probs, labels = torch.topk(output, topk)
    probs = probs.exp()

    # Reverse the dict
    idx_to_class = {val: key for key, val in
model.class_to_idx.items()}
    # Get the correct indices
    top_classes = [idx_to_class[each] for each in classes]

    return labels, probs

predict(image,model)
print(probs)
print(classes)

Error:
AttributeError                            Traceback (most recent call last)
<ipython-input-32-b49fdcab5791> in <module>()
----> 1 probs, classes = predict(image, model)
      2 print(probs)
      3 print(classes)

<ipython-input-31-6f996290ea63> in predict(image, model, topk)
      5       Image.open(image)
      6     '''
----> 7     img = process_image(Image.open(image))
      8     img = torch.from_numpy(img).type(torch.FloatTensor)
      9

/opt/conda/lib/python3.6/site-packages/PIL/Image.py in open(fp, mode)
   2587         exclusive_fp = True
   2588
-> 2589     prefix = fp.read(16)
   2590
   2591     preinit()

AttributeError: 'JpegImageFile' object has no attribute 'read'

All I want is to get these similar result. Thank you!
tensor([[ 0.5607,  0.3446,  0.0552,  0.0227,  0.0054]], device='cuda:0')
tensor([[  8,   1,  31,  24,   7]], device='cuda:0')

Comment: Hi @Paula could you show me where is `process_image()`function definition?
Answer: Converting image to RGB after openning solved the issue for me
   PIL.Image.open(image_path).convert('RGB')

Answer: What is image? Where does it come from?
It looks like it's something that's already been Image.open()ed, and you're attempting to re-read it, as if it were a file or a path.
Comment: You asked an interesting question. image is an image_path.
Comment: It certainly doesn't look like it, based on the exception you're getting.
Comment: Maybe, should I change the name of the pathway?
Comment: This answer helped me to realize I was insisting on an already opened image. Thanks.
Answer: Try to use function numpy.asarray(image) to convert JpegImageFile into numpy array and then you will be able to work with image.
Comment: this doesn't seem to help
Answer: I had the same issue when Trying to open an image that had already had Image.open() called on it. the following will work for you:
img = process_image(image)
img = torch.from_numpy(img).type(torch.FloatTensor)

if you the type of the image and/or the image you will see it is already a tensor:
print(image
print(type(image))

here is a related link that is answering the same questions:
https://discuss.pytorch.org/t/typeerror-img-should-be-pil-image-got-class-str/49644
Comment: It appears as if you are actually passing an image path (i.e. a string) to your predict function for which reason it is giving you that error. Since that is the case the OP's original line of code should work for you: ``img = process_image(Image.open(image))
img = torch.from_numpy(img).type(torch.FloatTensor)`` It did not work for him because his image input parameter was already an Image.open(image_path) object, since yours is an image path it should open the image and convert it into an array which will then be converted into a tensor @rachelvsamuel
Comment: Note: Note img = process_image(Image.open(image)) <- converts image to np array img = torch.from_numpy(img).type(torch.FloatTensor) <- converts np array to a pytorch tensor. @rachelsamuel
Comment: I tried this and got:
```
 in ()
      2
      3 image_path = './flowers/test/1/image_06743.jpg'
----> 4 predict(image_path, model, topk=5)

 in predict(image_path, model, topk)
     11     # Open
     12     img = process_image(image_path)
---> 13     img = torch.from_numpy(img).type(torch.FloatTensor)
     14
     15     # Pre-process the image

TypeError: expected np.ndarray (got tuple)```

Any idea why it expected an array and how to convert it into an array?
Answer: I have faced a similar issue and solved it by opening the image as follows:
import io
from PIL import Image as pil_image

fname = 'my_image.jpg'

with open(fname, 'rb') as f:
    img = pil_image.open(io.BytesIO(f.read()))
    pass

rather than:
from PIL import Image as pil_image

fname = 'my_image.jpg'

with pil_image.open(fname, 'r') as img:
    pass


WPF DataGrid - on row selection/lose focus prevent color change

Question: I want to prevent my program from changing row's color when datagrid loses focus and has one of the rows selected. The code I have right now is:
        <DataGrid.Resources>
            <SolidColorBrush
            x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
                     Color="Red"/>
        </DataGrid.Resources>

Im looking for something like

Color="Remain Unchanged"
Answer: You have to customize the IsSelected property trigger / multi-trigger for the DataGridCell style, as below:

    </Style>

And then apply your custom style:
        <DataGrid ItemsSource="{Binding Data}" CellStyle="{DynamicResource DataGridCellStyle1}"/>

EDIT: Adding full XAML:
MainWindow.xaml:
<Window x:Class="WpfApplication333.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication333"
    mc:Ignorable="d"
    Title="MainWindow"
    Height="300"
    Width="300">

<Window.Resources>

    </Style>

</Window.Resources>

<Window.DataContext>
    <local:MyViewModel/>
</Window.DataContext>

<Grid>

    <DataGrid x:Name="dataGrid" ItemsSource="{Binding Data}" HorizontalAlignment="Left" Margin="8,7,0,0" VerticalAlignment="Top" Height="248" Width="113" CellStyle="{DynamicResource DataGridCellStyle1}"/>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="143,116,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>

</Grid>

Comment: I added full XAML to my post, please see my **EDIT**.
Comment: I somehow managed to make it work before your edit, nonetheless your answer fully solves my problem. Thanks.
Answer: This is kind of late, but I want to share my approach. Since at this year 2023, I cannot find any solution on the internet that work.
Short answer:
    private void DataGrid_LostFocus(object sender, RoutedEventArgs e)
    {
        (sender as DataGrid)?.SelectedIndex = -1;
    }

But if you do care about selection, you listen to two events:
    private void DataGrid_LostFocus(object sender, RoutedEventArgs e)
    {
        DataGrid? dg = sender as DataGrid;
        if (dg == null)
            return;

        // Try to store indexes like row indexes, column indexes.
        // And then just remove selections
        // If you want to know what was selected before it lost focus,
        // you could create properties for the indexes you stored.

        dg.SelectedIndex = -1;
    }

    private void dataGrid_GotFocus(object sender, RoutedEventArgs e)
    {
        // Restore selection if needed
    }

So, the idea is not to have "inactive selection" at all. You store and then remove all the selections and then when you got back focus, restore the selection.
Be careful when you have something that depends on selection changed because this solution will raise a lot of selection changed events.
Hope this helps someone.
Answer: The below code works for me...
 <UserControl.Resources>

        </Style>
</UserControl.Resources>


Can I apply Bayes' Theorem here?

Question: I'm quite inexperienced in mathematics so I apologize in advance for using incorrect terms or fundamentally misunderstanding the application of Bayes' Theorem.
My question is about whether you can apply Bayes' Theorem in the given scenario:

A student is attempting to answer a question.
The underlying probability that a student will get a question in this category wrong is $50\%$. This is based on past data of how all questions have been answered so far.
A teacher thinks that the probability a student will get one specific question wrong is $70\%$. The teacher is accurate $90\%$ of the time.
What is the overall probability the student will get this specific question wrong?

My current understanding is that Bayes' Theorem gives this relationship:
$$
\text{Q wrong} = \text{"Student will get this specific question wrong"} \\
\text{T's guess} = \text{"Teacher thinks they will get this question wrong"}  \\
$$
$$
P(\text{Q wrong}|\text{T's guess}) = \frac{P(\text{T's guess}|\text{Q is wrong})P(\text{Q is wrong})}{P(\text{T's guess})}
$$
The interpretation being:

$P(\text{Q wrong})$ = Prior belief or "base rate"
$P(\text{Q wrong}|\text{T's guess})$ = Posterior, updated belief based on teacher's estimate

However, this model doesn't seem right. It doesn't fit the normal examples of Bayes' Theorem I've seen, like a medical test with a false positive rate since the teacher isn't just stating that they will get the question wrong, instead giving an estimate of the probability.
Very un-mathematically blindly plugging the numbers in doesn't work unfortunately:
$$
\frac{0.7 \times 0.5}{0.9} \approx 0.389
$$
Which isn't correct because the probability should be above the baseline estimate.
Is it possible to answer this question using Bayes' Theorem or using some other method? Am I missing something crucial that makes this question impossible to ask without other information?
Thanks in advance.
Comment: What does it mean for the teacher to be 90% accurate when he thinks that the student will "get one specific question wrong with probability 70%"?
Comment: @BenjaminWang This is the kind of thing I didn't think of... thank you :) Maybe if it was said as "The teacher has predicted the probabilities of groups of students getting the question correct with 70% accuracy in the past"?
Answer: You actually need the law of total probability here. The sample space is partitioned into when the teacher is right and when the teacher is wrong. If the teacher is right, the student should get this specific question wrong with chance 70%. If the teacher is wrong, with probability 10%, we use the baseline probability that the student gets the question wrong (for there is no other estimate). We write
$$\begin{split}P(question\_wrong)&=P(question\_wrong|teacher\_right)P(teacher\_right)+P(question\_wrong|teacher\_wrong)P(teacher\_wrong)\\
&=.7(.9)+.5(.1)\\
&=.68\end{split}$$
This is between the baseline estimate and the teacher's shenanigans.
Comment: I think this is what I was looking for, it's kind of like giving weight to the different outcomes based on the teacher's accuracy. It makes a lot more sense like this, thank you!

Can I apply Bayes' Theorem here?


Preprocessing: Why do we remove constant/invariant predictors?

Question: For a recent research project we used machine-learning. In the preprocessing phase we removed 2 predictors because they contained mostly uninformative 0 values (x1 = 100%, x2 = 99%). Is it the right terminology to call them invariant?
My main justification for the exclusion is, that it reduces the degrees of freedom and therefore the risk of overfitting.
I am looking for a paper / source to justify this procedure, besides the face validity of doing so. Do you have a recommendation for such a source?
Thanks to mkt: I found what I was looking for in this book: Kuhn, M., & Johnson, K. (2013). Applied predictive modeling. Applied Predictive Modeling (Vol. 26). New York: Springer. https://doi.org/10.1007/978-1-4614-6849-3
Comment: Suppose $x_2$ is an indicator of gender (0=male, 1=female) in an almost predominantly male population (99% zeros) and the response is whether a subject was ever pregnant.  Would you still want to drop $x_2$ from the model??
Comment: That's fine--it was exactly my intention that you conclude judgment is needed.  The point is that we cannot expect to find any authority to justify throwing out any explanatory variable based on how close it comes to being constant.  Indeed, any source that might make a blanket statement of that nature should be discredited in light of the example I offered.
Comment: Thanks for pointing this out. I think you are right, so of cause it is important to consider the data structure and the purpose of the analysis. In our case it is not a dummy coded variable. It is the probabilty of a feature is being active. So in the case of x2 there was only one unique value of 0.01, meaning a 1% chance that this feature was active. I think it is justified to drop it.
Answer: When you are fitting a regression-type model, you are trying to explain or predict variation in one variable with variation in others. If all values are identical, there is no variation for you to work with! There is no meaningful information in there to use, either as a predictor or a target/dependent variable. So you are completely justified in excluding it.
This is a very basic point, and so I don't think you need a citation to justify it. But if you do, I'd suggest looking at your favourite introductory text - it is probably mentioned there.
This would be different in the case where values are not 100% identical. If you have a few different values in there, then excluding the variable may or may not be justified depending on the question and the nature of the data.
Comment: I would argue that in the case were 99% of the values are identical it is also problematic if you use resampling (i.e. k-Fold CV).
Anyways thanks for the tip with the introductory text. I tried to search for it in an Introduction to statistical learning (James et al.). But didn't found it.
However I went through my books and found something in Kuhn, M., & Johnson, K. (2013). Applied predictive modeling. Applied Predictive Modeling (Vol. 26). New York: Springer. https://doi.org/10.1007/978-1-4614-6849-3


What happens if I use a single scalar output with MSE Loss for classification tasks?

Question: Rather than using Cross Entropy Loss and one hot encoding for neural network classification tasks, if my model outputs a single scalar value and I use mean squared error loss what will happen?
Comment: What do you mean that the mode outputs a single scalar? What is the alternative?
Comment: Please explain whether (a) you have only two classes coded 0 and 1, and your output is a number between 0 and 1 that can be interpreted as a probability (in which case the MSE is the Brier score, and cross entropy is the log loss, and your question is a duplicate of [this one](https://stats.stackexchange.com/q/274088/1352)), or (b) you have $n$ classes encoded $1$ to $n$, and your output is a number between $1$ and $n$ (in which case [Zurienu's answer](https://stats.stackexchange.com/a/639792/1352) applies).
Answer: My answer to this is: It's terrible in most situations, and I wouldn't recommend it, except it the classes are values on an interval or ratio scale, or you have two classes.
First, I'll clarify some definitions and the answer will follow.
One-hot encoding is basically a function that takes a class index, say $i \in \{1, 2, 3, \ldots, k\}$, with $k$ being the number of different classes in your classification task, and returns a vector of only 0s except in the i-th position, where it's a 1. The goal is to encode the "belonging of a class" as a positional bit vector, instead of as a single integer. However, this vector could also be seen as a discrete probability distribution with all the probability put on the class the datum belongs to. I'll name this function $\mathcal{O}$, for One-hot encoding. So if $k=3$, then $\mathcal{O}(2) = [~0\quad 1\quad 0~]$.
On the other hand, cross-entropy is a function derived from a divergence function (similar but different to a distance function), and it aims to give a score to how "unlikely" a data from distribution Q would be seen from distribution P. So if $H$ is the cross entropy function:
$$H(P, Q) := -\sum_{x} P(x) \cdot log(Q(x)).$$
In neural network (NN) classification tasks, you would use the cross-entropy function to penalize the output of your NN, namely a vector of size $k$. Say your NN is a function $\mathcal{N} : \mathbb{R}^m \to (0,1)^k$, with $m$ being the dimension/size of the input. Note that I mention that the output is in $(0,1)^k$, which is the space of all vectors with elements between 0 and 1, exclusively. You could achieve such output with a Softmax layer, etc.
Let's suppose also that we write a data pair as $(x_i, {\rm class}_j)$, where $x_i$ is the ith input datum and ${\rm class}_j$ is its associated class. The cross-entropy thus compares $\mathcal{O}({\rm class}_j)$ with $\mathcal{N}(x_i)$, comparing the probability distribution of "certainty of class $i$", to what could be considered an estimate of the probability distribution that $x_i$ is in each class, given by the NN.
(And this approach is so intuitive, that it actually is equivalent to an estimation paradigm from Statistics, named Maximum Likelihood Estimation.)
But what would happen if, instead of comparing a vector output with the one-hot encoding, you let your neural network output a single real number and compare it with the class $i \in \{1, 2, 3, \ldots, k\}$? Well, if the output can be anywhere between -$\infty$ and $\infty$.. it makes little sense to compare, ... for example, 836.1 to the classes' numbers. If $k$ is 14 and current class output is 12, the squared error is $|12-839.1|^2 = 684~094.41$. It's certainly useful to minimize this value, so that the output of the NN is around the classes integers, but then... your NN can output values outside the classes' integers set. You need a rule to fit the value to a class, since we're doing classification.
If otherwise the output is strictly in the set of indices ($\mathcal{N} : \mathbb{R}^m \to \{1, 2, 3, \ldots, k\}$), then comparing the actual class with the output would end up comparing pairs like $\mathcal{N}(x_j)=3$ and ${\rm class}_j = 8$, thus penalizing the squared distance between numbers which... don't necessarily have any numerical meaning. Does the gap between class 3 and class 7 have the same numerical meaning as the difference between class 1 and class 5? If the answer is yes, then you aren't really doing classification (or more aptly said, you use a classification method on a regression problem disguised as a classification problem). It is so because the scale you use is on an interval or ratio scale, as I mentioned in the summarized answer.
So that's the first exception to using mean squared error (MSE) on a scalar for classification problem.
The other exception is if the class number is $k=2$, but I would suggest having your NN output a value between 0 and 1 instead of a real number (for stability, mostly). I can explain this suggestion in more details if anyone asks.


ls strange behavior when i give it `-- ` as argument

Question: When I execute ls --:
$ls --
a.out       ft_ls.c     ft_ls.h     ft_ls.h.gch libft`

it display the content of the current directory, but the - alone is interpreted like a directory named '-':
$ls -
ls: -: No such file or directory

and ls --- is illegal flag:
$ls ---
ls: illegal option -- -
usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

and can I know why --- isn't interpreted by default as a file name?
Comment: You'll find that many programs recognize `--` as an option marking the end of the option list, so that any subsequent arguments are treated as non-options, even if they start with `-`.  Beyond that, it's unclear what you're asking.
Comment: @pmg i am trying to implement ls using C. but i forgot that is a wrong flag for the question, thanks.
Comment: For some programs, they support the _convention_ that `--` separates options from arguments [and gets silently absorbed], so `ls --` is just like `ls`. Try: `ls -- -l` for example
Comment: I removed the C tag. You may like to read [POSIX Utility Syntax Guidelines](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02)
Answer: -- has a special meaning as the marker of "end of options"; whereas --- or - has no such special meaning.
POSIX convention says:

The first -- argument that is not an option-argument should be
  accepted as a delimiter indicating the end of options. Any following
  arguments should be treated as operands, even if they begin with the
  '-' character.

If you are implementing ls then you might be interested in using getopt function and you can easily implement similar option processing as the ls command.
Answer: There are a few instances when you have to run ls -- command.
Here is one, if you have any file that start with a dash (-) for example -file.pdf ; using ls with any option will cause errors. You will need to use ls with double dash ls -- which tells Linux to ignore everything else after that has a dash and then type the file name with a *.
You can use ls -- *.pdf to display your directory contents.


PHP DOMDocument - save children of a node to string - excluding the parent node

Question: I'm loading up an HTML fragment into a DOMDocument, modifying it, and then converting back to a string.
The closest I can get is this...
$html_string = "my html fragment
more child nodes
";
$doc_html = new DOMDocument();
$doc_html->loadHTML( $html_string );
//do stuff
$html_string = $doc_html->saveHTML( $doc_html->getElementsByTagName('body')->item(0) );

The problem here is that loadHTML wraps it in a  and  tag.  So I use getElementsByTagName to stringify just the body node, but it includes the body node it's self...
my html fragment
more child nodes

But i just want the child nodes like the input string.
Other than looping all children and concatenating a string / running a req ex on the resulting string, is there a simple way to do this?
m
Comment: Try `saveXML` instead? saveHTML tries to save an html DOCUMENT, which means it's going to add missing body/html tags.
Comment: Yeah, it still will serialise the containing node though.  Currently i'm using this.. http://php.net/manual/en/class.domelement.php#101243
Answer: <?php
$doc = doc();
$xp = new DOMXpath($doc);
$nl = $xp->query('//a');
foreach($nl as $n) {
    $df = $doc->createDocumentFragment();
    $nodes = array();
    foreach( $n->childNodes as $cn ) {
        $nodes[] = $cn;
    }
    foreach($nodes as $cn) {
        $df->appendChild(   $cn );
    }
    echo $doc->savexml( $df );
}

function doc() {
    $doc = new DOMDocument;
    $doc->loadxml(<<< eox
<doc>

        1
        2
        3
        4

</doc>
eox
    );
    return $doc;
}

prints
    1
    2
    3
    4

see http://docs.php.net/domdocumentfragment


Wait for apache2 to shutdown on Ubuntu 10.04

Question: I've almost completed automating the installation and configuration of apache2 across our infrastructure, but am hung up on an intermittent issue where apache does not restart when I ask it to.
I'm installing apache via AMT get; copying across files including ports.conf, apache2.conf and the default site; then calling within my script:
<code>...
/etc/init.d/apache2 restart
...
</code>
I'm expecting apache to be listening on 443 ONLY when all's said and done, but it's intermittently still bound to port 80.  Does anyone have any ideas as to how I can guarantee the restart?
Comment: Should we guess that you didn't configure it to listen on port 443? Should we guess something else? You only say that it doesn't work. And I can tell you it works for me that way.
Answer: You'll likely need to open up the apache2 init script and find out how it's doing the restart. Older init scripts might stop, sleep, then start. You may need to write in a loop where you sleep, then check to see if it's still running before starting again.
Answer: 1) You can always Leave Port 80 open from apache2 config and block it with iptables
<code>iptables -A INPUT -p tcp -m tcp --dport 80 -j DROP
</code>
2) You can bind 80 to localhost.