Calling delphi function in c#

Question: I am calling a delphi function within a dll from c#.
Here is the delphi function signature -
function CALinkDecode(sCode: PWideChar; SubscriberID, MailshotID, LinkID: PDWORD):
  PAnsiChar; stdcall;

And here is how i am calling it.
 string input = "qvoxyetvr7wksss2zbrmr";
        int cas, cam, cal;
        var errorString = CALinkDecode(input, out cas,
                                         out cam, out cal);

But when i run the app its evaluating this condition as true in delphi function -
if (sCode = nil) or (Length(sCode) <> 21) or (SubscriberID = nil) or (MailshotID = nil) or (LinkID = nil) then
begin
  Result := 'E_INVALIDARG';
end

I would just like to know if i am passing in the correct datatypes for the function signature and what is the correct way to do it. Thanks.
Comment: You could start by identifying which part of the predicate fails. You should also post the C# declaration.
Comment: @MarceloCantos Length(sCode) <> 21 is the predicate that fails.
Comment: Then what's in `input`?
Comment: @MarceloCantos Input is a pointer to a WideChar within a string and not the sting itself. Check answer.
Comment: @MarceloCantos string input = "qvoxyetvr7wksss2zbrmr";
Comment: @PeterVonča: Ah, silly me.
Comment: @StylesYoung: Ignore me. Peter's answer has it covered.
Comment: @MarceloCantos okay no problem. Thanks. How do i convert the string to a pointer ie pwidechar. How do i pass the c# pointer to the string.
Comment: @MarceloCantos, actually I was the silly one, my assumption was wrong, infact the only thing I can see wrong with the OP calling function is that his integer values are not assigned therefor the exception is raised.
Comment: How are you importing this function?  Can you show your C# code?
Answer: Your immediate problem is likely that you are passing ANSI text to the sCode parameter. Unfortunately you omitted your p/invoke declaration which would have made that clear.
That's easy to fix, but you have much bigger problems. This function is hard to call because it has been designed incorrectly. It's only viable if it returns constant strings that are allocated by the compiler rather than heap allocated strings. You could force it to work with that signature if you:

Exported a deallocator function from the native code.
Allocated the string using a shared heap, e.g. the COM heap.

It looks like you are returning COM error codes in string form. That's perverse. Instead you could make your life very easy by returning HRESULT.
Anyway, to answer the direct question you declare the p/invoke like this:
[DllImport(@"Mylib.dll", CharSet=CharSet.Unicode)]
static extern IntPtr CALinkDecode(
    string sCode,
    out uint SubscriberID,
    out uint MailshotID,
    out uint LinkID
);

Then call like this
IntPtr retvalptr = CALinkDecode(...);

Then marshal the pointer to a string
string retval = Marshal.PtrToStringAnsi(retvalptr);

But remember that this can only work if the returned string data is statically allocated. Otherwise you will have leaks, or access violations.
My primary advice is to redesign the interface. Don't return an error code in a string.
On a general note, if you must pass strings from Delphi to C# do it like this:

Declare an out parameter of type WideString in the Delphi code. This wraps a COM BSTR.
On the managed side declare an out parameter of type string. Decorate that parameter with [MarshalAs(UnmanagedType.BStr)].

Note that you cannot marshal a WideString return value to MS tools because Delphi uses a different ABI for return values. That's why you use an out parameter.
Comment: Here we go... I started answering this question on two separate occasions this morning, but eventually gave up on the full expectation that David's answer would show up at some point and trump everything.
Comment: @David Excellent. If i had to return an HRESULT, in my c# code, how would i call the function. In other words, to what type in c# would i map it or set it.
Comment: @David Heffernan. Sorry forgot to Thank you. Works like a charm. *bows*
Comment: @Styles Map HRESULT to uint or perhaps int. Signed integers are easier to work with in C# when you perform arithmetic. But with an HRESULT you usually are doing just comparisons and bit flag tests. So uint works fine.

Calling delphi function in c#


encoding error displaying fontawesome with imagemagick

Question: i'm trying to use fontawesome with imagemagick to display it's character as png ,here is my code:
<code>$size = '50';
$text = '';
$imgW = 200;
$imgH = 200;
$font = 'fontawesome-webfont.ttf';
$image = new Imagick();
$image->setResolution(144,144);
$draw = new ImagickDraw();
$draw->setFont($font);
$draw->setFontSize($size);
//$draw->setTextEncoding('UTF-8');
//$draw->setStrokeAntialias(true);
//$draw->setTextAntialias(true);
$image->newImage($imgW, $imgH, new ImagickPixel('none'));
$image->annotateImage($draw, 10, 45, 0, $text);
$image->setImageFormat('png');
header('Content-type: image/png');
echo $image;
</code>

but the image output looks like this

any idea how to fix such a encoding problem?
Comment: Is the `fontawesome-webfont.ttf` in the same directory?
Comment: for sure cause if not in the same dir or dir is not valid it will through an exception rather than rendering the image
Answer: Using the example from this answer to ensure I had a UTF-8 string being passed into ImageMagick works for me. The only change from your code is the second line:
<code>$size = '50';
$text = html_entity_decode("", ENT_COMPAT, 'UTF-8');
$imgW = 200;
$imgH = 200;
$font = 'fontawesome-webfont.ttf';
$image = new Imagick();
$image->setResolution(144,144);
$draw = new ImagickDraw();
$draw->setFont($font);
$draw->setFontSize($size);
//$draw->setTextEncoding('UTF-8');
//$draw->setStrokeAntialias(true);
//$draw->setTextAntialias(true);
$image->newImage($imgW, $imgH, new ImagickPixel('none'));
$image->annotateImage($draw, 10, 45, 0, $text);
$image->setImageFormat('png');
header('Content-type: image/png');
echo $image;
</code>
Answer: Looks like the Unicode sequence isn't being recognised properly as you are getting multiple characters rather than just one. If you get one missing block displaying - it's a font issue!
I have a unicode escape working in Ruby RMagic so maybe the same sequence will work for you in PHP?
On http://fortawesome.github.io/Font-Awesome/cheatsheet/ the "fa-heart" is listed as "#xf004;"
This appears to be an HTML escape, so the one you need should be:
<code>$text = "\uf004";
</code>
Comment: Have you also set the PHP script to run in UTF-8 mode? mb_internal_encoding("UTF-8"); // More info on: http://php.net/manual/en/function.mb-internal-encoding.php
Comment: Set both character sets to UTF-8. mb_internal_encoding("UTF-8"); and use $draw->setTextEncoding('UTF-8');
Comment: I've tried that but does not work as well , this problem is about how imagemagick read unicode and have no relation to php cause if i use another library such as GD it render icons without any problems


How can write the probability density function of generalized exponential distribution as exponential family?

Question: I want to use GAM method and generalized exponential distribution for response variable. I know GAM method is a generalized GLM method and the distribution of response variable must be in exponential family. The probability density (pdf) of generalized exponential distribution is as following :
$$ f(x ; \alpha, \eta)=\alpha \eta \exp\left\{ -\eta x  \right\}\cdot \left(  1-\exp(-\eta x) \right)^{\alpha - 1}, \quad x>0 $$
CDF of this distribution is as following :
$$
F(x; \alpha, \eta) = \left(1-\exp(-\eta x)\right)^\alpha, \quad x>0
$$
The $\alpha$ is shape parameter and the $\eta# is scale parameter.
How can I write this pdf as exponential family? That is, is the generalized exponential distribution a member of the exponential family? Also known as the *exponentiated exponential distribution, a special case of the Exponentiated Weibull distribution
Comment: At https://stats.stackexchange.com/a/519715/919 I describe a general method to solve this problem -- maybe it will help you with this one.
Comment: Thank you @Whuber I have two parameters. For using this method, should I differentiate first w.r.t x and then w.r.t alpha and finally w.r.t eta?
Answer: No, you cannot. Just take the logarithm of the density, and you will see that the expression do not take the form of the argument to the exponential function needed for an exponential family. You need that the interaction of the argument $x$ and the parameters $\theta$ can be separated as $ \eta(\theta) \cdot T(x)$, where the dot represents a dot product. You don´t get that form.
But you want this for a gam, maybe look into generalized non-linear models, see how to fit a generalized nonlinear model in R?  or https://stackoverflow.com/questions/59493784/writing-a-custom-model-for-gnm-package-but-need-a-function-not-in-derivative-ta


How do I add leading zeros to an image sequence in Windows Batch?

Question: I found this link that explains how to add leading zeros to a sequence of numbers:
@echo off
set count=5
setlocal EnableDelayedExpansion
for /L %%i in (1, 1, %count%) do (
     set "formattedValue=000000%%i"
     echo !formattedValue:~-6!
)

This outputs

000001
000002
000003
000004
000005

I have image sequences that look like this:

%1.j2c_0.j2c
%1.j2c_10.j2c
%1.j2c_100000.j2c

I would like to always have 6 numbers:

%1.j2c_000000.j2c
%1.j2c_000010.j2c
%1.j2c_100000.j2c

The file name before the last underscore can change and include more underscores.
So I guess I need to find out how many numbers are to the right of the last underscore and add the correct number of zeros to get 6 numbers.
How do I do that?
Comment: `%1` constitutes the first argument of the batch file here, right? or is it meant literally??
Comment: it is meant literally :). the software that creates the sequences names them like this for some reason.
Comment: Okay... so what part of the file name could contain more underscores then (as you said in the last paragraph)?
Comment: this part: %1.j2c can look different. the software takes movie files or image sequences as input and converts them to .j2c image sequences. if one inputs a movie it always looks like this: %1.j2c_10.j2c. If you input an image sequence, the software will use the file name of the input sequence.
Answer: As far as I understood your question, the pattern for the image files is *_*.j2c.
Since the leading file name part may contain multiple _, a trick can be used: for provides the possibility to split iterated items into pieces by giving ~ modifiers; supposing the variable is %I, a %~nI returns the file name, %~xI the extension, %~nxI file name plus extension (see for /?).
If no wildcards *, ? are used, splitting is done without accessing the file system; so we can use this for string manipulation operations. In order to apply this to our task at hand, we need to replace every _ by the path separator \.
There are two nested for loops; the outer one iterates through all the image files, and the inner one receives each file name plus ext. from the outer and iterates just once, where the path string splitting feature is (mis-)used for our needs.
The following code snippet uses the described trick  to extracted the part after the last _ of the file name, which is the number to be padded with 6 leading zeros then. See also the explanatory rem comments:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "IMAGES=*_*.j2c"
set "DIGITS=6" & rem add more padding `0`s below if DIGITS > 12!
rem loop through all images that match the pattern given above;
rem since there is a wildcard `*`, the file system is accessed:
for %%I in ("%IMAGES%") do (
    rem now let us extract the portion between the last `_` and the ext.;
    rem for this we replace every `_` by `\`, so the item looks like a file path;
    rem then we split off the file name portion of the path-like string;
    rem since there are no more wildcards, the file system is NOT accessed:
    set "ARG=%%~nxI"
    set "FPTH=%%~fI"
    setlocal EnableDelayedExpansion
    for %%F in ("!ARG:_=\!") do (
        rem extract and pad the numeric file name portion:
        set "VAL=000000000000%%~nF"
        set "VAL=!VAL:~-%DIGITS%!"
        rem rebuild file name; the `|` ensures that only the part
        rem after the last `_` is replaced by the padded number:
        setlocal DisableDelayedExpansion
        set "ARG=%%~F|"
        setlocal EnableDelayedExpansion
        set "ARG=!ARG:%%~nxF|=!!VAL!%%~xF"
    )
    ren "!FPTH!" "!ARG:\=_!"
    endlocal
    endlocal
    endlocal
)
endlocal

The great advantage of this method is that we do not need to know the number of _ characters (opposed to something like for /F "tokens=1,2 delims=_", where the number of tokens is fixed).
Comment: Okay... so see my edit upon your [above comment](http://stackoverflow.com/questions/33938114/how-do-i-add-leading-zeros-to-an-image-sequence-in-windows-batch#comment55640327_33938114);
Comment: How do I use the script? I tried putting it in the same folder as the sequence and I tried to enter the full path into the IMAGES variable. But it only echos "ren path/to/sourcename_1.j2c path/to/000001.j2c", but does not actually rename the files. And as I understand it, the script will remove the file name and only keep the numbers, right?
Comment: Ah, sorry, there was an `ECHO` I used for testing, which I forgot to remove... the script extracts the numeric part of the file name, pads it with `6` leading zeros and reassembes the file name then; you can use the script as is in the directory where the images are, or you can provide the full path to your image files in the `IMAGES` variable, like `set "IMAGES=D:\full\path\to\image\dir\*_*.j2c";
Comment: I saw that and removed the ECHO, but then it says syntax error in the cmd window.
Comment: Yes, the problem was I provided a full path as the new file name for the `ren` command, but it should be a pure file name only -- I fixed that; I also fixed an issue that the original file name was lost, so now the new file names should be like the old one but with the numeric part padded...
Comment: I see, now it works great :)!! Just one more thing: the file %1.j2c_1.j2c will be renamed to %_000001.j2c. The rest of the files will be renamed correctly to %1.j2c_000....j2c. But I can just search replace for %1.j2c before using your script.
Answer: this was a little bit challenging (but a little) :
@echo off
setlocal enableDelayedExpansion

:: path to the directory with the files
set "files_dir=."
pushd "%files_dir%"
for %%# in (*_*j2c) do (
    set "filename=%%~nx#"
    for /f "tokens=1,2,3,4 delims=_." %%A in ("!filename!") do (
        set "num=%%C"
        call ::strlen0 num numlen
        if !numlen! LSS 6 (

            for /l %%] in (!numlen! ; 1 ; 6 ) do (
                set "num=0!num!"
            )

            ren %%A.%%B_%%C.%%D  %%A.%%B_!num!.%%D

        ) else (
           echo %%# will be not processed - more than or 6 numbers
        )
    )
)

endlocal & (
    popd
    exit /b %errorlevel%
)

:: taken from http://ss64.org/viewtopic.php?id=424
:strlen0  StrVar  [RtnVar]
  setlocal EnableDelayedExpansion
  set "s=#!%~1!"
  set "len=0"
  for %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
    if "!s:~%%N,1!" neq "" (
      set /a "len+=%%N"
      set "s=!s:~%%N!"
    )
  )
  endlocal&if "%~2" neq "" (set %~2=%len%) else echo %len%
exit /b

Comment: @schmiddl - if you have questions - go ahead.


Google Cloud Composer failed after restart

Question: I have Google cloud composer running in 2 GCP projects. I have updated composer environment variable in both. One composer restarted fine within few minutes. I have problem in another & it shows below error as shown in images.
Update operation failed. Couldn't start composer-agent, a GKE job that updates kubernetes resources. Please check if your GKE cluster exists and is healthy.
This is the error what I see when I enter the composer
This is the environment overview
GKE cluster notification
GKE pods overview
I am trying to find how to resolve the problem but I didn't find any satisfied answers. My colleagues are assuming firewall & org policies issue but I haven't changed any.
Can some one let me know what caused this problem as the google composer is managed by google & how to resolve this issue now?
Answer: Once the Cloud Composer is the managed resource and when the GKE which serves the environment for your composer is unhealthy you should try to contact Google Cloud Support. That GKE should work just fine and you do not need even know about its existence.
Also check whether you do not reacy any limits or quotas in your project.
When nothing helps recreation of Cloud Composer is always good idea.
Comment: Thanks, the problem was someone deleted proxying Load balancer for managed GKE. But now its solved


How to hook contact form of drupal 7 for custom styling?

Question: how can I alter my contact form for adding custom classes
I am using contact form of Drupal 7 core module.
Answer: Or you can use hook_form_FORM_ID_alter() to get the specific contact form. hook_form_alter() will run for every form on the page.
function MYMODULE_form_contact_site_form_alter(&$form, &$form_state, $form_id) {
  $form['name']['#attributes']['class'] = array('class1', 'class2');

  $form['mail']['#attributes']['class'] = array('class1', 'class2');

  $form['subject']['#attributes']['class'] = array('class1', 'class2');

  $form['message']['#attributes']['class'] = array('class1', 'class2');

  // add class to the overall form
  $form['#attributes']['class'] = array('class1');
}

If you don't know how to use custom modules, you'd have to research that first. Dropping this function in and replacing MYMODULE with your module name will make this work.
Comment: yes this is also the great option. +1
Answer: It can been done by $form_id
 function MYMODULE_form_alter( &$form, &$form_state, $form_id ) {
        if( $form_id == 'contact_site_form' ){
    //PUT YOUR STYLING HERE
    }
}


Average days between multiple date time using Javascript

Question: I would like to get average days between multiple datetime values using Jquery/Javascript.
following are my samples data :
8/8/2017 1:58 AM
8/3/2017 1:58 AM
8/4/2017 2:18 AM
8/5/2017 1:58 AM
8/7/2017 1:58 AM
7/25/2017 1:58 AM
7/24/2017 1:58 AM
7/18/2017 1:58 AM
7/25/2017 1:58 AM
8/1/2017 1:58 AM

Here's my attempt logic :

var sum = ['7/21/2017 1:58 AM','8/3/2017 1:58 AM','8/5/2017 1:58 AM','8/8/2017 1:58 AM'];

var avgval = "";
var total = 0
for(var index=0; index<sum.length; index++){
if (index < (sum.length-1)){
   console.log(sum[index+1], sum[index])
   var days = Math.abs((new Date(sum[index+1])-new Date(sum[index]))/(1000*60*60*24));
   total+=days;
}
else{
    avgval = total / sum.length-1;
}
}
console.log(avgval);
Comment: Why don't you just find the duration between the earliest and latest date and divide it by `array.length - 1`..?
Comment: You question is not clear. How exactly are you planning to get an average from this? Median between first/last? Also, please show the JS code you wrote yourself to attempt this. As it stands this question is too broad so is likely to be downvoted and/or closed.
Comment: What have you tried so far yourself, please show us
Comment: ok you want average days. Did you try anything?
Comment: Is that a math problem?
Comment: Thanks @RoryMcCrossan for your quick update and down vote, I updated my question so it could be more clear.
Comment: I did not downvote. I was trying to be helpful and give you information about how you should improve your question if you want some worthwhile answers. I can downvote if you'd like.
Comment: What is your definition of `days between` for dates, that are not ordered in ascending or descending order. 5-10-8 distances are 5 and 2 or 5 and -2?
Answer: You can use moment.js too to calculate difference in days (days are rounded down to integers).
var sum = 0;
var prevdate = false;

for(var k in dates) {
  var thedate = moment(dates[k], "M/D/YYYY H:mm A");
  if(prevdate) {
    sum += prevdate.diff(thedate, 'days');
  }
  prevdate = thedate;
}

var avg = (sum / (dates.length - 1));

https://jsfiddle.net/ishukshin/s6q0rhzL/
As for your update
You should use parentheses when dividing: avgval = total / (sum.length-1)
You have to define difference in days. Maybe you'll need to add Math.abs for a difference between dates.
Comment: why aren't they ordered by increasing date? This leads to negative distance.
Comment: Are you criticising your own answer?
Comment: I'm criticising a set of dates, provided by the author.
Comment: Thanks @shukshin.ivan I used maths.abs instead of maths.round and seem to working now.
Answer: You can do a simple for loop and then use this function to get difference time between two dates :
 let difference = (new Date(mytime1).getTime() - new Date(mytime2).getTime()) / (1000*60*60*24);

But be careful , it will return difference in millisecond so you have to divide it by (1000*60*60*24)
After you save all difference between date you divide it by there number
Comment: I don't know where you copied this code from, but I don't see how it's in any way relevant to the OP
Comment: That still doesn't make it relevant to the OPs issue of finding an average between dates.
Comment: The main idea to find differences between date in milliseconds  then find the average  for these differences. it make seance


Have a Microsoft Exchange and Google Apps Email Running on the Same Domain

Question: A company that I work for just acquired another company that was using Google Apps for it's email.  We use Microsoft Outlook Exchange.  The founders of the company (who are leaving) want to maintain a few email addresses on Google Apps for them to use over the next year.  Does anyone know if it's possible to have just a few Google Apps email addresses working on a domain and the rest of the domain running on Outlook Exchange?
Comment: http://support.google.com/a/bin/answer.py?hl=en&answer=96855
Answer: I work for a Google Partner and can confirm that this is possible. The best option is to perform "dual delivery" for your users, that way you can move the users who are on Exchange across later without losing any mail or having to waste any time migrating their inboxes.
You will need to decide if you want Gmail to be the primary and forward all emails to Exchange or if you want to have Exchange as the primary and forward all to Gmail.
Details on this can be found at this link:
http://support.google.com/a/bin/answer.py?hl=en&answer=96855


How to implement view with nodes grouped per taxonomy term

Question: I have a content type 'Event' which has a 'Category' content taxonomy field (single selection against respective 'Category' vocabulary). If vocabulary has 3 terms, I want to implement a listing where all 3 terms will always be displayed, along with the newest event for each category (sort by publication date in descending order). Something like this:
Category 1
Title of newest event for category 1
Category 2
Title of newest event for category 2
Category 3
Title of newest event for category 3
What is the best way to implement this with views in Drupal 6?
Answer: You must use the group option in the style display. In group option you must select the field taxonomy term. And then you must Exclude from display the field taxonomy term.
Comment: I thought about the grouping, but I am not sure if I can achieve fetching exactly one node (the newest one) per group. I will test and let you know. Why should I exclude the term from display?
Comment: u can sort for the newest one.
Comment: thanks for your reply. grouping and sorting works well, but I was not able to limit the displayed nodes to just one per group. I ended up using this: https://drupal.org/project/views_limit_grouping
Answer: Desired solution implemented using the Views Grouping Row Limit module. Here is a quote about it (from the module's project page):

This is a basic Views style plugin that allows for a limit on the number of rows displayed within a grouping field ...
To use this module, simply change the style plugin for your view to the "Grouping Field (with Limit)" option. You will then be presented with options to choose your limit and offset (the offset just controls what row to start on, e.g. an offset of 1 would omit the first row from each grouping field). Please note that in order for this to take effect, you must set your row limit on the actual view to Unlimited.
Answer: https://drupal.org/project/views_field_view might be the solution to your issue. Granted it must be used with caution as it adds some extra overhead but ultimately it grants the SELECT FROM SELECT query that solves your problem.


Moment generating function and exponentially decaying tails of probability distribution

Question: This is a follow-up to this previous question.
Suppose I have a mean-zero symmetrically-distributed random variable $X$ over the support $\mathbb{R}$. If $X$ has a moment-generating function $M_X(t)$ that is smooth around 0, $X$ has an exponentially decaying tail probability, by Chernoff bound (Lemma 11.9.1 in Cover and Thomas's "Elements of Information Theory" 2nd edition).
Now, suppose that $X$ has an $M_X(t)$ that is not smooth around 0.  Suppose that $\mathbf{E}[X^k]=\infty$ for all even $k>n$, where $n$ is a positive integer.  Is there $X$ that has exponentially-decaying tail probability in that case?  Or would the tail probability always be a power-law?
Also, what happens to the tail if $M_X(t)$ is not defined, i.e. the integral in the transform diverges?
EDITS: Clarified the question based on helpful comments from @cardinal.
Comment: No, quite the opposite. A symmetric distribution about zero need not have any (raw) odd moments at all, but all (raw) even moments will exist, even if they are not finite.
Comment: A somewhat pedantic response to your question is that no such random variable $X$ can exist in the first place based on the set of conditions you've placed on it. Do you see why? (**Hint**: Consider $k > n$ where $k$ is odd.)
Comment: Hmmm...  I see what you are saying.  But does this mean that any symmetric zero-mean $X$ has to have all finite moments?  Or is there a symmetric zero-mean $X$ that has all the moments but for which $M_X(t)$ is not smooth around 0?  Which condition should I weaken?
Comment: I think I am confused about what it means by "not having moment $\mu_n$".  I interpret that only as "$\mu_n=\infty$".  That is, my interpretation of "having moments" means "having finite momemts, possibly equal to zero".  Would removing "Suppose that $\mathbf{E}[X_k]=\infty$ for all $k>n$, where $n$ is a positive integer" put more sense into my question?  I'm mainly interested in what happens for $X$ with $M_X(t)$ that is not smooth around zero.
Comment: And I was confused by your first comment because Student's t has finite moments up to its degree of freedom and further even moments are infinite or the integral in the mgf diverges for odd ones.  Anyway, my bad -- I've edited my question.
Comment: @Bullmoose You should be talking about characteristic function, not the moment generating function, this does not exists if $\mathbb{E}(\vert X \vert^k ) = \infty$ for all $k>n$.
Answer: I assume that "exponentially decaying tail probability" means that $P(|X| > t) \le C e^{-\epsilon t}$ for some $C, \epsilon$.  Any such random variable has finite moments of all orders.  This follows from the formula
$$E[|X|^p] = \int_0^\infty p t^{p-1} P(|X| > t) dt$$
which you can prove with Fubini's theorem and the fundamental theorem of calculus.
Comment: Thanks!  That's the result I've been looking for.
Comment: @Bullmoose: Note that this is the converse of the statement you originally quoted in another question. So, just be sure it actually *is* the result you were looking for. :)
Comment: @cardinal Basically, I wanted to know whether not having finite moments of all orders implies that the tail probabilities do not decay exponentially.  I think this answer answers it (as a contrapositive.)  The statement I made in the comment to the other question states that if MGF is smooth around 0 then its tails decay exponentially, and it has all finite moments.  I understand that having finite moments of all orders doesn't necessarily result in having exponential tails  (the lognormal example), but for my problem it suffices to state that having a smooth MGF implies moments and exp-tails.
Comment: @Bullmoose: I was referring to the following statement of yours in [this question](http://math.stackexchange.com/questions/80674/physical-meaning-of-higher-moments-their-values-and-their-existence): *I've heard somewhere that all finite moments of A with support R means that the tails of A decay exponentially. Is that true?*
Comment: Ahh, right.  And that statement was shown to not be true.  :)


Adjusting precision recall curve for oversampling

Question: I built a model for a binary target using oversampled data.
The population target prevalence is 0.25.
I oversampled to 0.5 by keeping the entirety of the minority class and sampling a portion of the majority class.
I then built a precision recall table using sklearn's:
#y is the binary target
#PR_CV are out of bag predictions
precision, recall, tr = precision_recall_curve(y, PR_CV)

I'd now want to know what precision and recall look like on the original population.
I tried implementing the following, based on this article:
#odds = p/(1-p)
odds = y.mean()/(1-y.mean())
print 'oversampled odds:',odds

original_fraction = 0.25
original_odds = original_fraction/ (1 – original_fraction)
print "original odds:",original_odds

#Scoring_odds = scoring_results / (1 – scoring_results)
scoring_results = PR_CV
print "probability to revert:", scoring_results
scoring_odds = scoring_results/(1-scoring_results)
print"scoring odds:", scoring_odds

#adjusted_odds = Scoring_odds * original_odds / oversampled_odds
adjusted_odds = scoring_odds * original_odds / odds
print "adjusted odds:", adjusted_odds

#adjusted_probability = 1 / (1 + 1/adjusted_odds))
adjusted_probability = 1 / (1 + 1/adjusted_odds)
print "adjusted probability:", adjusted_probability

I then calculated:
precision_adj, recall_adj, tr_adj = precision_recall_curve(y, PR_CV_adj)

What this returns is the exact same values as precision_recall_curve(y, PR_CV)
That fails my intuition...What is the right way to do this?
The question I'm truing to answer is: if on an oversampled population I expect at the top 5th percentile to have x true positives over X predicted positives (precision), then on the entire population what are my x2 true positives and my X2 predicted positives?
Comment: Good news! Class imbalance is not a problem, and you did not need to undersample!
https://stats.stackexchange.com/questions/357466/are-unbalanced-datasets-problematic-and-how-does-oversampling-purport-to-he
https://www.fharrell.com/post/class-damage/
https://www.fharrell.com/post/classification/
https://stats.stackexchange.com/a/359936/247274
https://stats.stackexchange.com/questions/464636/proper-scoring-rule-when-there-is-a-decision-to-make-e-g-spam-vs-ham-email
https://twitter.com/f2harrell/status/1062424969366462473?lang=en
Comment: Does this answer your question? [Are unbalanced datasets problematic, and (how) does oversampling (purport to) help?](https://stats.stackexchange.com/questions/357466/are-unbalanced-datasets-problematic-and-how-does-oversampling-purport-to-he)
Answer: Because the adjustment is a monotonic function, every threshold in the model's probability space has a corresponding one in the adjusted space (indeed, just apply the same adjustment) at which the confusion matrix of the resulting hard classifiers are the same. So the precision-recall curves are the same, it's just that every point corresponds to a different threshold.
Answer: To estimate efficacy of using sampling techniques you should first split your data between train/test set, apply sampling on training data and then estimates results on test set. The way you are doing you can leakage information and get unrealistic performances results.


Colon next to a flat in cello score

Question: I'm seeing this symbol in a couple places in a score I'm transcribing, and wondering what it means:

Next to the flat in each case is a colon symbol.  It only appears in the cello part, though that may or may not be relevant.  They are all on the same note, middle C, I don't see any on other notes.  The piece is a simphonie concertante by Jean-François Tapray (1738-1822) in case that gives a clue for notation that may have been used during that time.
Does it maybe have something to do with C-flat just being enharmonic with B-natural?  Though there are other C-flats in the same part (as well as some of the others) without the colon, so no idea what the difference would be.
I tried searching around but it's impossible to look for "colon" regarding musical notation and not just have every answer be a "repeat bar", which this is clearly not.
Edit: @phoog pointed out another example, this time in the viola part:

Still on a C - doesn't seem to happen on any other note.
Edit again: Just found another example, on an F in the principal violin part:

So much for the "only on C's" theory, but it does still follow the pattern of the flat being enharmonic with the natural of the note below it.  I also think that lines up with my second example from the cello?  At least the line is rhythmically the same, I'll see when I get to it.
Edit: Yet more examples were spotted by @Old Brixtonian, still on C's, but this time in the harpsichord part, and I found a few in the piano as well, which makes even less sense to me, since while the strings have the ability to bend notes to make them more harmonically pure, keyboards definitely do not (at least in that time period, and mostly still today though there are electronic instruments which can, as well as some weird experimental acoustic ones, but it's not a standard feature at all.)
Edit: Ack! Another one that throws the enharmonic theory out of the water, it's on a B in the first violin part:

Which makes it make even less sense because B is already flat from the key signature, so that's entirely redundant.
Note: For anyone curious, I've posted my simulated recording of this piece here:

For the record, I ended up just ignoring the colons and it sounds alright, so I'll just chalk it up to something they maybe needed back in those days that doesn't apply to modern tuning.
Comment: @DarrelHoffman Moreso because Cbb is the second-rarest double flat and it's right next to some Bb's. My long shot is that maybe it indicates the flat carries through the measure—are there any normal flats to contradict this?
Comment: It looks as if the music has been either copied or edited by someone with a poor grasp of musical notation. Those colons look suspiciously like the two dots of a bass clef, the flats in the key-signatures are poorly positioned, and "FF" and "P" should be in lower-case italic. Maybe some hapless apprentice, charged with making a legible copy from the composer's handwriting, decided C's without natural-signs regularly needed a flat-sign and a colon.
Comment: @OldBrixtonian Good catch on the dots resembling those of a bass clef, but it's an engraved publication produced by a [fairly prolific engraver](https://opac.rism.info/metaopac/search?searchCategories%5B0%5D=-1&q=Richomme&View=rism&Language=en).  The lettering style of the dynamic markings, we can assume, arises from the fact that the edition was produced before the style was standardized.
Comment: @Peter I thought about a double-flat, but I ruled it out because that would just be a B-flat, and since B's are already flat from the key signature, that would be an unusually convoluted way to write that.
Comment: Just a wild guess - is there a chance it is some sort of double flat sign? What are the other parts doing against those C-flats?
Comment: I see it in the viola part, too.  Maybe it is a warning that it's not playable as a harmonic of the open C string, but I haven't found any C flats in the violin part (with or without the colon) to test the hypothesis.  And that doesn't explain C flats that lack the dots.
Comment: @Peter the piano has a normal C flat with no dots at, I think, the same spot as the third example in this question (but I didn't count the measures).
Comment: @phoog, yes, that wouldn't sound good for that period.
Comment: @OldBrixtonian Music notation wasn't so standardized as we consider it today until the middle of the 19th century, when it was still partially evolving. There were many publishers having their own slightly different versions of what we now take as granted conventions. Even composers sometimes introduced (as they still do today) their own "symbols" even if a known standard already existed (see [this example](//music.stackexchange.com/q/120771/74331)). We cannot exclude an error by the copyist, but we also cannot dismiss these writings as "wrong", considering the possible publication date.
Comment: @the-baby-is-you That's only potentially relevant in the second cello example and the violin example I just added.  In all the other cases, the flatted note is the only note in the measure, so that wouldn't make any sense.
Comment: @DarrelHoffman No, not particularly, that's why it's a long shot. It's still possible unless there are counterexamples, and I thought it was worth putting the idea out there in case it jogs something else.
Comment: Are there any sharps or naturals in that same score? I wonder if all accidentals were treated that way, not just flats.
Comment: Is this the score you're working from? https://imslp.org/wiki/Symphony_Concertante_in_E-flat_major%2C_Op.9_(Tapray%2C_Jean-Fran%C3%A7ois)
Comment: @MichaelCurtis There are plenty of sharps and naturals, but I haven't seen any with colons.  Also there's plenty of other flats without them as well, including on Cs and Fs in the same parts.  And yes, that is the score.
Comment: Still more puzzlingly, the 2nd violin part in the same passage *doesn't* use the colon on its G flat. I was noticing that there seems to be a musical similarity to all these uses, of a half-step-upper-neighbor, as if a chord is being augmented or diminished as a sort of ornament.
Comment: @MichaelCurtis Violin: p.18, Viola: p.28, Cello: p.31-32
Comment: @MichaelCurtis I see two instances on p 23; 6th line and 11th. But perplexingly, no corresponding instance in the next-to-bottom line of p 25.
Comment: With this upper-neighbor, altered-chord-tone use, I could seriously see executing it as a narrower-than-usual half step, even a quarter tone. I wonder whether that's the meaning, though so far that would be pure speculation.
Comment: @AndyBonner Quarter tones would be very unusual for European music in this time period, but a narrower half step remains a possibility - would explain why it's only on the string parts, because the keyboard instruments are not capable of that.  The fact that it's only found on Cb and Fb, which are both enharmonic with the natural of the note below, would support that theory.
Comment: And I feel like in all these examples they're "decorating" either ^1 or ^5. It would be interesting to look at a score and see what chords are actually happening at these moments. Looks like there is one from A-R Editions...
Comment: @AndyBonner the piece is in E flat major, so C flat and G flat are the minor third and minor sixth, not so unusual.
Comment: [Sorry: I wrote this before seeing the above. If it doesn't add anything new I'll delete it in a minute.] I think it could be to do with string players accommodating the fixed tuning of the keyboard. It might have been useful to warn musicians who weren't yet used to equal temperament, that this next note would need sweetening. It's my unfounded belief that the worst clashes in the piece would occur when the keyboards' fixed B met the orchestra's adaptable Cb, and their E their adaptable Fb.
Comment: @OldBrixtonian I honestly don't remember anymore all corrections normally required by equal temperament, but considering the possible publication time, it should've already been quite common to adjust tuning when necessary without further indication. I've not checked the rest of the composition (I'm not able to get a readable display from the PDF at imslp), but, according to the same principle, further adjustments should've been probably noted as well, especially if the piece has modulations that would eventually use other "delicate" notes. Still, I cannot exclude you may have a valid point.
Comment: @DarrelHoffman Can you add more context to your latest update? What is the tonality in that point? Because if it's in G minor or D minor, that could enforce Old Brixtonian theory.
Comment: @musicamante Well, we've got a constant D, with an alternating Bb/Ab and an alternating Gb/F, so that seems like it's an augmented alternating with a diminished - I guess that'd be a D minor section?  My music theory is a bit rusty.
Comment: @musicamente - The Eb/Fb alternation actually looks like it's decorating a F7 chord near the end of a long exposition section of a sonata-allegro, with a rough Bb chord following (in this B flat major section). The Ab/Bb alternation similarly decorates a Bb7 chord in the recapitulation section, which is finally in the home key of E flat major. There is F/Gb alternation in both passages (and C/Db alternation in the exposition version).
Answer: Sorry to use an answer but I needed to include this graphic. Unfortunately the quality of the print is poor at this point. (59 bars from the end of the movement.)

The first Cb in the right hand has its colon positioned wrongly. (If you can HAVE a wrong position for a thing that isn't needed!) Then there's one in the left hand with a 'correctly-positioned' one, while the right hand has a wrongly-positioned flat WITH a wrongly-positioned colon!
Colons aren't needed in this context of course. Maybe this edition is unique.
There's a modern edition of the piece here. I haven't managed to find a recording you don't need to pay for!
Comment: Funny you should mention the lack of recordings - this is the exact reason I am transcribing this.  I've been creating synthesized recordings of rare pieces like this and posting them on YouTube. [shameless plug](https://www.youtube.com/channel/UCwY5HTt-ZrBmWwQiyQeMmrw)
Comment: From the looks of it, your image comes from the same score I'm using, which is free on IMSLP.  On closer inspection, I found a few more examples in both the harpsichord and piano parts.  I am even more confused now since they can't bend notes like the strings can...
Comment: @DarrelHoffman it never seemed likely to me that it indicated anything about the quality of the flat or the magnitude of the change in pitch, but especially not if I'm right that the piano C flat I found without a colon is the same one as shown in the cello part in the question.
Comment: Tip: if you want to include an image in a comment and don't want to create a "non-answer", you can start creating a new post/reply, upload an image, copy its link, and then discard the answer: the image will be preserved on imgur, and it can be used in comments as a link. I've seen this suggested also by mods in meta SO, as long as it's used with moderation, it's not considered abuse.
Comment: @musicamante: Tvm. Belatedly!
Comment: In retrospect, my theory on the "wrongly positioned" colon is that they just moved it down so it wouldn't be hidden by the staff lines.

Colon next to a flat in cello score


installed and configured munin server monitoring too: but where is ram usage? and mysql usage?

Question: I'm at loss, have been trying to dig out something in documentation,but nothing...
It seems mysql ram usage should be part of default Munin package, but they don't show up. Anyone can confirm me this? do I need plugins to add this very important parameter?
S.
Answer: There seems to be a bug on ubuntu server: https://bugs.launchpad.net/ubuntu/+source/munin/+bug/1000678 which prevents memory plugin to function.


question about p values and significance comparison

Question: I am under the impression that you have to set your alpha value before the experiment, and once the experiment is done, your p value, based on the alpha value, is either significant or not.  It's dichotomous.  There is no "almost significant" or anything like that.
So if I set my alpha at 0.04, but my p value is 0.05 then my results are insignificant.
I do not understand why.  Imagine a beginner researcher who really wants to get significant result, setting alpha at 0.2 and gets 0.19 p so its significant.  Or imagine a researcher so sure that a treatment works that sets alpha at 0.001 but his p value is 0.002 so its insignificant. Why has the significance of the result have anything to do with the researcher's guess about alpha levels?
Why can't we treat p value as a continuous variable, instead of randomly picking some alpha significance level before we start?
Comment: Very closely related: https://stats.stackexchange.com/questions/23142/when-to-use-fisher-and-neyman-pearson-framework : "Neyman-Pearson" is the term used for the "set a threshold in advance approach", while "Fisher(ian)" is the term used for treating $p$ as a continuous measure of evidence. I'm tempted to close this as a duplicate ..
Answer: This issue is very controversial. I imagine that this answer will have many negative votes. It has implications for the daily lives and work of many. But you have to do what you have to do.
That's more or less how I understand the story:

100 years ago Fisher developed a method for data testing.
Neyman and Pearson took what Fisher developed and developed it further.
The two proposals are similar but contradictory on some points.
Since the proposals were published, there have been publications that questioned them.
Towards the 1940s or 1950s (a task for historians), the two theories were almost anonymously combined into a single theory now called NHST (null hypothesis statistical tests).
No one is the author of the NHST. It's one more rule of thumb. So you can't discuss this theory with any author.
Since their anonymity, NHSTs have been criticized. There are hundreds (or thousands) of articles indicating that they are not the most appropriate method of doing scientific research.
Last year the ASA in its conference "a world beyond p<0.05) seems to have started the abandonment of NHSTs.

NHST problems:

Logical: based on failed logical tests.
Real: It is based on ideal distributions, which are rarely found in reality.
They are of anonymous creation: With which author can you argue?.
They give a false sense of security. The problem of inference is hundreds or thousands of years old. Philosophers still can't figure it out. We do?

Researcher positions:

Abandon NHSTs and use Bayesian statistics. Problem: It is not a total solution because it would have the same problem as the NHST, in some cases reasoning in the same way.
Use NHSTs well. Problem: 5 books will explain NHSTs in 5 different ways, because there is no established method for making NHSTs. NHSTs were invented by us, there's no evidence on how to evaluate the evidence!
Create new methods for data testing. That's where some of the authors are.
Comment: Could you summarize the key points from the provided links? The aim of this site is to build a base of high-quality answers; link-only answers are discouraged.
Answer: The purpose of setting a threshold is to remove post hoc fudging and provide clarity in the decision making process. This is not to say this is how it is actually used, but it is the ideal behind it.
The thinking behind the process is that you decide how much risk you are willing to take on missing a real effect of a size that matters to you (powering the study on the assumption of an expected effect size) versus the risk of believing data that would not be distinguishable from no effect if the experiment were repeated a vat number of times.
This is meant to force you to design the whole experiment, including analysis and decision making up front. If you can't do this, then you are probably in exploratory analysis phase and relying on crude rules of thumb, in which case any outcomes that appear interesting should be validated in independent experiments.
Why has the significance of the result have anything to do with the researcher's guess about alpha levels?
The alpha shouldn't be a wild guess, rather an educated one. It is about managing risk and should be matched with appropriate powering procedures during design to be most useful.
Why can't we treat p value as a continuous variable, instead of randomly picking some alpha significance level before we start?
This is reasonable, as long as there are clear rules about how it is handled. For example exploratory analysis may rank p values and filter the top x percent for further testing. I've used this approach to filter principal components for discriminant analysis. Another alternative is to have the post hoc decision tied to the value of p, so one example could be if the results are extremely strong a full R&D budget could be rubber stamped to continue development but is tapered off over a range of p values. In a major multinational R&D intensive organisation this would help balance risk across a research portfolio
The example you give is inappropriate as you want to fudge interpretation once you know your data. This is one aspect of investigator bias.
Clear logical rules need decided before you see the data so as not to bias interpretation. The rules don't have to be the standard rules of thumb or threshold, but they need clearly and unambiguously explained and be logically consistent.
Answer: Interesting question philosophically.  I will give and answer, but I may be corrected mathematically.
The idea is that you go into the experiment with a null hypothesis, or as my statistics professor used to say, "the boring and uninteresting result."  SO, if you are trying to prove that smoking leads to earlier death, the null hypothesis is "smoking does not lead to earlier death", which means that the average age of a smoker is the same as a non-smoker if you draw them randomly from the real world.
The statistics is about proving that the "boring" result does not appear to hold, because the odds of that happening given the data you looked at makes it highly unlikely it was just due to randomness that those averages are not close enough.  Think of it this way: if you had exactly one smoker and non-smoker, you really don't have enough data to make such a determination (especially if the non-smoker was killed by a bus).  But, if you have 10 million of each, you can have a lot of accuracy (or confidence), statistically speaking, if you don't get averages that are close to each other.
I believe the alpha logic comes from coming into it.  The question is, a priori, what level of confidence do you expect (or would be acceptable) to make you believe the null hypothesis was wrong? For one thing, that keeps you from designing the experiment in such a way that what you truly believe in your heart (and is consistent with the experiment you set up)   is that "95% would really be convincing" doesn't lead to your analyzing the data, getting 92%, and then saying "90% makes it true".
The level of significance changes by field.  In some areas of particle physics, for example, where they record trillions of 'events' looking for something elusive, the standard is 5- or 6-sigma.  Otherwise, you will find false positives all the time given the number of events.  That is an example of how the experiment plays into it.
On the other hand, when I did business consulting, we often presented regressions with 75% p-values.  The logic there is that companies must make a lot of decisions, and a lot of decisions that have a p-value of 75% that get done quickly (and can be reversed if they prove wrong) is a lot more useful than proving a small number of things at 99% confidence.
I'm not sure this is entirely helping you or answering what you are asking, but in practice (especially in a field like economics) people sort of do treat it as continuous.  Any variables that come out of regression with a p-value of 95% feel really strong; any above 90% seem quite strong; but you wouldn't ignore a variable at 89%.  In economics, one issue is that you don't really design experiments, and data can be a bit 'dirty' itself.
Maybe another way to see this is lime this: suppose you are testing a diagnostic screening against a disease that is surely fatal, but the treatmen itself kills 10% of the people who get it whether or not they have the disease, but saves 100% of the people who have the disease (imagine Ebola, or AIDS in the early years).  But suppose only 0.1% (one in 1,000) of the population is infected.  If your test gives 5% false positives, and perfect actual positives, that means out of every 1000 people tested 1 will have the disease but 51 will test positive (the one real and 50 false).  That means the drug will kill, out of every 1000 people tested, 5.1 people - only one of whom had the disease.  Therefore, it would be better not to use you test because without it there will only be one death.  Clearly, in that case, you would come in needing a much 'tighter' alpha a priori to justify proving that using the test is a good idea.
That's all a bit sloppy technically, but I think it gets at what you are asking.


How to get event When Apps kill from Task manager in Android?

Question: I want to unRegister form Sip server when some one kills app from task manager.
How to get event when app kills form task manager?
Thanks in advance.
Answer: You are not informed when your app's process is terminated by any means, whether initiated by the user (task killer, Force Stop, etc.) or the OS on its own (e.g., low memory condition).


Blazor server component isn't rerendered

Question: I have a Blazor Server (.NETv5) application with a search page.
On this page I have a form to search by name.
On the form submit event I call the search method of my child component.
That component is doing the actual search. This is working fine.
Because the search might take a few seconds I want to show a spinner when the search starts and hide it when the search is done.
Also when I do a second search I want to hide the previous search results.
Hiding the spinner and showing the search results is working, but showing the spinner before the search doesn't work. The variable is set correctly but the page is not rerendered (I think).
My page:

    <RadzenTemplateForm Data="@searchTerms" Submit="@((SearchTerms args) => { Submit(args); })">

                <RadzenLabel Text="Name" />
                <RadzenTextBox class="col-12" Name="Name" @bind-Value="searchTerms.Name"/>

                <RadzenButton ButtonType="ButtonType.Submit" Icon="search" Text="Search" Disabled="@isSearching" />
                <RadzenButton ButtonStyle="ButtonStyle.Light" Icon="cancel" Text="Cancel" Click="@Cancel" class="ml-2"/>

    </RadzenTemplateForm>

<SearchResultsComponent @ref="searchResultsComponent" />

protected SearchTerms searchTerms = new();
protected SearchResultsComponent searchResultsComponent;
protected bool isSearching;

protected void Submit(SearchTerms args)
{
    if (string.IsNullOrEmpty(args.Name)) return;

    // Disable submit button ==> NOT WORKING:
    isSearching = true;
    // Call search method on child component
    searchResultsComponent.Search(args);
    // Enable submit button again:
    isSearching = false;
}

protected void Cancel()
{
    // Reset form:
    searchTerms = new SearchTerms();
}

My child component:

    @if (isSearching)
    {

            Searching...

        Searching ...
    }

    @if (noResults)
    {

           No results.

    }

    @if (getSearchResults != null && getSearchResults.Any())
    {
        <RadzenHeading Size="H2" Text=@($"Results({getSearchResults.Count})")></RadzenHeading>

            @foreach (var searchResult in getSearchResults)
            {
                <RadzenCard>
                    @searchResult.Name
                </RadzenCard>
            }

    }

private IList<MultiShardSearchResultsWerknemer> _searchResults;
private bool _isSearching = true;
private bool _noResults;

protected bool noResults
{
    get => _noResults;
    set
    {
        if (Equals(_noResults, value)) return;
        _noResults = value;
        InvokeAsync(() => StateHasChanged());
    }
}

protected bool isSearching
{
    get => _isSearching;
    set
    {
        if (Equals(_isSearching, value)) return;
        _isSearching = value;
        InvokeAsync(() => StateHasChanged());
    }
}

protected IList<MultiShardSearchResultsWerknemer> getSearchResults
{
    get => _searchResults;
    set
    {
        if (Equals(_searchResults, value)) return;
        _searchResults = value;
        InvokeAsync(() => StateHasChanged());
    }
}

public void Search(SearchTerms args)
{
    Helpers.ConsoleLog(args);
    if (string.IsNullOrEmpty(args.Name)) return;

    // Reset ==> NOT WORKING:
    isSearching = true;
    noResults = false;
    getSearchResults = null;
    InvokeAsync(() => StateHasChanged());

    getSearchResults = ShardingService.SearchForAllEmployees(args.Name, null).GetAwaiter().GetResult();
    Helpers.ConsoleLog("Found results: " + getSearchResults.Count);
    isSearching = false;
    noResults = !getSearchResults.Any();
}

For debugging purposes, I've set _isSearching = true which shows me the spinner. The spinner is also hidden when the search is done, so that is working. But I can't get the spinner to show when I start searching.
I've tried all options I could find, without success.
I must be missing something. Please advice.
Answer: Have a look at your search handel method

protected void Submit(SearchTerms args)
{
    if (string.IsNullOrEmpty(args.Name)) return;

    // Disable submit button ==> NOT WORKING:
    isSearching = true;
    // Call search method on child component
    searchResultsComponent.Search(args);
    // Enable submit button again:
    isSearching = false;
}

Keep in mind, that rendering will occur once the method has finished. So, before the call isSearching  is false and after it is also false. That's why you don't see the spinner.
Blazor offers a method to kick off a new render cycle: StateHasChanged().
So, you could modify your submit method like
protected void Submit(SearchTerms args)
{
    if (string.IsNullOrEmpty(args.Name)) return;

    isSearching = true;
    StateHasChanged()

    // Call search method on child component
    searchResultsComponent.Search(args);

    isSearching = false;
    StateHasChanged()
}

So, you click the search/submit button on this method is executed.
Or if you like, create a property instead
@code
{
    private Boolean isSearching = false;

    public Boolean IsSearching
    {
        get => isSearching;
        private set
        {
            isSearching = value;
            StateHasChanged();
        }
    }

    protected void Submit(SearchTerms args)
    {
        if (string.IsNullOrEmpty(args.Name)) return;

        IsSearching = true;

        // Call search method on child component
        searchResultsComponent.Search(args);

        IsSearching = false;
    }
}

I haven't tested it but faced a similar problems once.
Comment: Thanks for the suggestion. As you can see in my post I already have a property and am calling `StateHasChanged()` in the property and also tried calling it on other locations but still the spinner isn't shown.
Comment: My apologies.I really haven't seen it.


Two-step process with jQuery

Question: Here is the flow:
The user is required to click a link to google.com and then click the text between
 tags (in no particular order, but both actions must occur).
If he does the above, when he clicks on the pic.png he will go to yahoo.com.
Otherwise, he gets an alert.
So far here is my attempt
http://jsfiddle.net/p8epF/
Comment: You [just asked this same question](http://stackoverflow.com/questions/12168319/double-conditional-jquery-function-event) less than an hour ago...
Comment: Your fiddle is broken because you can only add javascript in the JS window. Pick jQuery in the sidebar, then put your HTML in the top box and the contents of your
Comment: Does the link to google.com open in a new window or something?
Comment: Like @MrOBrian pointed out you already asked this question recently. Please don't spam the site.
Answer: Try this:
eeee

please click

JS
$(function(){
    var clicked = false;

    $("#linkos").on("click", function(){
        if (clicked) {
           window.location = 'http://google.com';
        } else {
           alert("Not clicked!");
        }
    });

    $("#click").on("click", function(){
        clicked = true;
    });

})

Demo: http://jsfiddle.net/Ef7ZT/
Answer: What you need can be done easily with jquery promises: http://jsfiddle.net/zerkms/WXVPf/1/
var yahoo = $('#yahoo'),
    googleClicked = $.Deferred(),
    textClicked = $.Deferred();

yahoo.on('click', function() {
    alert('you need to click google and some text');
});

$('#google').on('click', function() {
    googleClicked.resolve();
});

$('p').on('click', function() {
    textClicked.resolve();
});

$.when(googleClicked, textClicked).done(function() {
    yahoo.off('click')
         .on('click', function() {
             alert('you have finished this strange quest successfully');
         });
});

For the ones who don't use $.Deferred yet - there is an amazing article: http://joseoncode.com/2011/09/26/a-walkthrough-jquery-deferred-and-promise/
Comment: can you add some explanation please... like what happens after we click on google, what do resolve() do
Comment: I think the link to the docs offers a pretty good explanation. This is a fairly simple way of using Deferred objects; quite easy to follow the code if you understand them (which the docs help you to do).
Comment: @sabithpocker: isn't that what documentation for?
Comment: @zerkms Laziness easily gets caught in SO :)
Comment: @sabithpocker: actually I've given a link with perfect explanation. PS: I cannot agree that writing almost ready to use solution is called "laziness" ;-)
Comment: @zerkms I mentioned about my laziness to go and read the docs :) Im reading the link now, thank you for that.

Two-step process with jQuery


Contour Integration with $\cos(n \theta)$

Question: I need to compute the following real integral using complex numbers. I'm unsure how to handle the numerator so that the ensuing calculations do not become too  unwieldily.
$\int_{0}^{2\pi} \frac{ \cos n\theta}{\cos(\theta)-a} d\theta$ Thanks!
Comment: Why do you select an answer as accepted when you still have questions about it?
Comment: $cos (n\theta)=\text{Re}(e^{in\theta})$
Comment: How can I continue computing the integral, then? I'm confused with how to handle the Re().
Answer: Let $I(a)$ be given by
$$I(a)=\int_0^{2\pi} \frac{\cos (n\theta)}{cos \theta -a}\,d\theta$$
Using Euler's Formula, we can write $I(a)$ as
$$I(a)=\text{Re}\left(\int_0^{2\pi} \frac{e^{in\theta}}{cos \theta -a}\,d\theta\right) \tag 1$$
Now, letting $z=e^{i\theta}$ in $(1)$ reveals
$$\begin{align}
I(a)&=\text{Re}\left(-2i\oint_{|z|=1}\frac{z^n}{z^2-2az+1}\,dz\right)\\\\
&=4\pi \text{Res}\left(\frac{z^n}{z^2-2az+1},z=a-\sqrt{a^2-1}\right)\\\\
&=-2\pi \frac{\left(a-\sqrt{a^2-1}\right)^n}{\sqrt{a^2-1}}
\end{align}$$
Comment: Can you explain what's happening in the step with Res?
Comment: Sure.  Since the integrand is analytic in and on $|z|=1$ except for the simple pole at $z=a-\sqrt{a^2-1}$, the integral is $2\pi i \times \text{the residue at the pole}$
Answer: The equivalent contour integral is
$$-i \oint_{|z|=1} \frac{dz}{z^n} \frac{z^{2 n}+1}{z^2-2 a z+1} $$
There are two poles inside this unit circle: $z=0$ and $z=a-\sqrt{a^2-1}$.  Let's first write down the residue at $z=a=\sqrt{a^2-1}$, which the reader should be able to show (the pole is simple) that it is
$$\frac{i}{2\sqrt{a^2-1}} \left [\left (a+\sqrt{a^2-1}\right )^n +\left (a-\sqrt{a^2-1}\right )^n  \right ]$$
The computation of the residue at the pole at $z=0$ is obviously more complicated.  However, rather than attempt to evaluate an $n-1$th derivative of the rational function in the integrand, we will have better luck by expanding the denominator for small $z$ and locating the coefficient of $z^{n-1}$.  Note that
$$\frac1{z^2-2 a z+1} = \sum_{k=0}^{\infty} U_k(a) z^k$$
where $U_k$ is a Chebyshev polynomial of the second kind.  In this case, as $a \gt 1$, we have the residue in this case being
$$-i U_{n-1}(a) = -i \frac{\sinh{(n \operatorname{arccosh}{a})}}{\sinh{( \operatorname{arccosh}{a})}} = -\frac{i}{2\sqrt{a^2-1}}\left [\left (a+\sqrt{a^2-1}\right )^n -\left (a-\sqrt{a^2-1}\right )^n  \right ]$$
The integral is then $i 2 \pi$ times the sum of these residues.  We may then write

$$\int_0^{2 \pi} d\theta \frac{\cos{n \theta}}{a-\cos{\theta}} = \frac{2 \pi}{\sqrt{a^2-1}}\left (a-\sqrt{a^2-1}\right )^n$$


Ember.js - What purpose does Ember.lookup serve

Question: Can anyone tell me what purpose Ember.lookup serves?
It is used to lookup string keys.
An example of its use in the ember source is:
if(typeof modelType === "string"){
  return Ember.get(Ember.lookup, modelType);
} else {
  return modelType;
}

I can see that it returns a type from a string but I don't see where it is set or what the bigger picture is for its usage.
Answer: Ember.lookup was introduced along with Ember.imports and Ember.exports as a way to remove the dependency on window.
If you are running Ember in the browser, all three values will refer to the window, however if you are running without the browser, for instance, through NodeJS or with AMD, you will need to supply values yourself.
See the commit message for more information.


map string to entity for using with generic method

Question: if I have the following classes:
class Fruit {}
class Apple : Fruit {}
class Orange : Fruit {}

and I have method:
public List<Fruit> getFruit<T>() where T : Fruit {

 List<Fruit> fruitList = new List<Fruit>();
 return fruitList.AddRange(session.QueryOver<T>().List());

}

Is it possible to have a dictionary that maps a string to a type that can be passed to this generic method so I can query over the right table?
for example:
Dictionary<string, type> typeMapper = new Dictionary<string, type>()
{
 {"Apple", AppleType??}
};

var myType = typeMapper["Apple"];
List<Fruit> fruitList = getFruit<myType>();

Comment: generic types in c# must be known at compile time. You can't use  because it's not a compiler time operation. From MSDN.com  -  "The type argument can be any type recognized by the compiler."... (and it's not recognized if you use run-time operation)
Answer: How about using Dictionary to store delegates ?
    Dictionary<string, Func<List<Fruit>>> typeMapper = new Dictionary<string, Func<List<Fruit>>>()
    {
        {"Apple", () => { return getFruit<Apple>(); } },
        {"Orange", () => { return getFruit<Orange>(); } }
    };

    List<Fruit> fruitList = typeMapper["Apple"]();

Comment: This is the answer I've been waiting for!
Answer: You can use reflection to call a generic method with a Type
[Fact]
public void Test_WithTypesAndReflection_Succeeds()
{
    var typeMapper = new Dictionary<string, Type>()
    {
        { "Apple", typeof(Apple) },
        { "Orange", typeof(Orange) }
    };

    var method = (
        from m in this.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic)
        where m.Name == "GetFruit"
        where m.GetParameters().Count() == 0
        select m).Single();

    var result = (IEnumerable<Fruit>)method
        .MakeGenericMethod(typeMapper["Apple"])
        .Invoke(this, null);
}

private IEnumerable<Fruit> GetFruit<T>() where T : Fruit
{
    return Enumerable.Empty<T>().Cast<Fruit>();
}

Answer: You can create instances of unknown type using Reflection. This can be done using Activator.CreateInstance(typeof(Apple)) e g.
Having said this you won´t need generics at all:
Dictionary<string, Type> typeMapper = new Dictionary<string, Type>()
{
    {"Apple", typeof(Apple)}
};

var myType = typeMapper["Apple"];
var apple = Activator.CreateInstance(myType);

EDIT: However this method is not typesafe, so all you get is an object instead of your concrete type. You may cast the result to Fruit however and add it to the list:
fruitList.Add((Fruit) apple);


Can we use this simple method instead of using variable itself

Question: I have marked many of developers using
$con1 = mysql_connect('localhost', 'username', 'password');
$rv1 = mysql_select_db('db1'); // not added connection

Instead
$con1 = mysql_connect('localhost', 'username', 'password');
$rv1 = mysql_select_db('db1', $con1); // added connection

Can I know the difference between this both?
Actually this both are giving same result
Answer: The mysql_ functions will implicitly use the last connection which was opened with mysql_connect, if you do not pass an explicit connection parameter. So, yes, in this case it's the same result whether you pass the parameter or not. You will get different results should you happen to open more than one connection using mysql_connect.
Note that implicitly relying on an open connection is bad practice, precisely because your application will screw up if you need to open more connections sometime later. Also note that the mysql_ API is dead, switch to mysqli or PDO.
Answer: Take a look at the PHP manual:
http://www.php.net/manual/en/function.mysql-select-db.php
Without a link identifier, mysql_select_db will use the last connection opened with mysql_connect or it will try to connect without any parameter. So the second one is safer and you could use multiple connections.
mysql_select_db and mysql_connect is deprecated though. You should switch to PDO or mysqli.
Answer: Taken from the documentation:
bool mysql_select_db ( string $database_name [, resource $link_identifier = NULL ] )

link_identifier
  The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

Basically it will used the last known connection object if none is provided. In your case, the last known connection was created on the previous line. This is an optional parameter so the function will operate in the same way if the connection is passed explicitly or not.

On a related note, please notice the big red warning at the top of the documentation page I have linked to. The mysql_* extensions are deprecated and it is recommended that you use a different extension. For a more detailed explanation, please take a look at this thread:
Why shouldn't I use mysql_* functions in PHP?


How to deploy BizTalk 2020 SSO Application with BizTalk Application Projects using Azure DevOps Release Pipelines?

Question: I am looking for a way to deploy a BizTalk 2020 application and the SSO affiliate application along with it.
Until BizTalk 2016, BTDF was used to deploy SSO applications, but in BizTalk 2020 the BTDF is not supported yet. Is there a way that I can use .btaproject to automate the SSO application deployment?
Or do I have to use the PowerShell Script method as one of the tasks in DevOps Release pipeline after the "Deploy BizTalk Server Application" Task?
I have already gone through Configure automatic deployment with Visual Studio Team Services in BizTalk Server, but it does not talk about importing SSO application.
I am looking for the solution as part of my project about upgrading BTS 2016 apps to BTS 2020 apps.
Comment: How do you define the build definitions of your pipeline?
Answer:
Until BizTalk 2016, BTDF was used to deploy SSO applications, but in
BizTalk 2020 the BTDF is not supported yet. Is there a way that I can
use .btaproject to automate the SSO application deployment?
Or do I have to use the PowerShell Script method as one of the tasks
in DevOps Release pipeline after the "Deploy BizTalk Server
Application" Task?

According to latest update from this ticket. the BTDF has started to support BizTalk 2020.
So if you have configured a self-agent which downloads this v5.8.0 release, you can try deploy the SSO via Command-line task.


The mod97 operation in IBAN validation

Question: The international bank account number(IBAN) is validated by a $\bmod 97$ operation. Suppose an account number is like sd1234abcd78965h then the following steps are performed:

the first four characters of the IBAN number are pulled out from the beginning and are appended at the end of the string.

All the letters in the hence obtained string of characters are replaced by the ASCII value of their corresponding uppercase letter decreased by $55$. (ascii value $-55$)

The modulus of the hence obtained number, let's say  $x$, with respect to $97$ is checked.

If the modulus is $1$, then it's a valid IBAN number .

For the third step, Wikipedia mentions an algorithm, which goes as follows:
A nine digit number is formed by taking the leftmost $9$ digits of $x$. The mod of this number with respect to $97$, $r$ is obtained. Then another nine digit number, $q$ is formed by concatenating $r$ and the next $7$ digits of the number. This process is continued till the last value of $q\bmod {97}$ is obtained.
If it is $1$ then that validates the number .
But, I couldn't prove that a number, $n$, for whom $n \bmod 97$ is $t$ where $t$ is in between $1$ to $96$, when subjected to above algorithm, will in the end yield $t$. Or is this a special case for numbers $n$ for whom $ n\bmod{97}$ is $1$?
Can you show me a proof of this algorithm or disprove it?
Answer: The algorithm works because
 $a\equiv a'\pmod {97}$ implies that also
$$a\cdot 10^k+b\equiv a'\cdot 10^k+b\pmod{97}  $$
Comment: If $x<10^9$ then $x<2^{31}$ so that the numbers fit into signed 32 bit integers
Comment: And the reason behind choosing the 9 digits ?

The mod97 operation in IBAN validation


Physical drive file match with database file in ASP.NET MVC

Question: I have an upload button on my website and that upload button saves a file the user selected on my hard drive (for now), and it also saves the name (with Guid.NewGuid) + a friendly name (actual name) up in the database.
The file is saved at Content/Usercontent (the Guid.NewGuid name), but what I want, is that when you delete a row from your database, that the file in the map Content/Usercontent is also deleted, how do I do this with ASP.NET MVC
EDIT: Why was I voted down? Atleast tell me what I should improve on my question.
Answer: As long as you have hooked into the event called OnDelete (for example) then you could sync this with routine that deletes that associated file.
I would create a class that is the sole manager for adding and removing files from the database. What I would do when you upload a file is then call the class documentMgr.Save(string fileName, string serverPath, byte[] fileContents).
On the DocumentManager class I would create a event called FileDeleted where the deegate takes the folowing args (object sender, FileEventArgs fileArgs) and create an event handler on that class called OnFileDeleted(object sender, FileEventArgs fileArgs) and raise the OnFileDeleted event when the DocumentManager.DeleteDocument(string name, string whatever) is called.
The event handler can be handled from an where then (if it event is public).
You can hook up the event in the constructor of the DocumentManager then the call with dea with the deleting of the file automatically and by using events, you can opt to do more and extend the functionality later.
Let me know how you get on


Como obter apenas o domínio de uma URL?

Question: O domínio não é o do localhost.
Se o utilizador digita um link, por exemplo, http://exemplo.com/pasta/pasta2/past/ficheiro.php, eu pretendo que o PHP retorne apenas esta parte da url  exemplo.com
Comment: Verifique a variável $_SERVER, ela irá retornar os valores que precisa.
Answer: O PHP já tem uma função que separa todos os elementos de um URL.
É justamente a parse_url:
$url = 'http://exemplo.com/pasta/pasta2/past/ficheiro.php';
$elementos = parse_url($url);

echo $elementos['host']; // 'exemplo.com'

Se quiser uma parte específica utilize o segundo parâmetro:
echo parse_url($url, PHP_URL_HOST);  // 'exemplo.com'

Veja funcionando no IDEONE.
Estas são as opções para o segundo parâmetro:
 PHP_URL_SCHEME     // retorna string  - ex: http
 PHP_URL_HOST       // retorna string  - ex: site.com
 PHP_URL_PORT       // retorna inteiro - ex: 443
 PHP_URL_USER       // retorna string  - ex: admin
 PHP_URL_PASS       // retorna string  - ex: swordfish
 PHP_URL_PATH       // retorna string  - ex: /blog
 PHP_URL_QUERY      // retorna string  - ex: secao=esportes
 PHP_URL_FRAGMENT   // retorna string  - ex: ancora2

Se for usar mais de uma parte, melhor pegar o array inteiro:
$partes = parse_url($url); // retorna array associativo

e usar todas as partes que interessam:
echo $partes['scheme'].'://'.$partes['host'].$partes['path'];

Mais detalhes no manual:

http://php.net/manual/pt_BR/function.parse-url.php

Como obter apenas o domínio de uma URL?


Is $S^n/(v∼−v)$ diffeomorphic to $\Bbb RP^n$?

Question: $S^{n}/(v∼−v)$  is homeomorphic to $\mathbb{RP}^n$.
But,  is  $S^{n}/(v∼−v)$   diffeomorphic  to $\mathbb{RP}^n$?
If $n=1$, the answer is yes.
Comment: (note that if you don't define a smooth structure on source and target then the question is a little broken.)
Comment: Yes, although for my definition of the standard smooth structures on both of these it is obvious that they are diffeomorphic (because the smooth structure on $\mathbb{R}P^n$ is inherited from the quotient map from $S^n$). What definition do you have in mind?
Answer: In fact, consider the smooth map:
$$f: \mathbb R^{n+1}-\{0\}\longrightarrow S^n, x\longmapsto \frac{x}{|x|}.$$
Let $\pi_2: S^n\longrightarrow S^n/(v\sim -v)$ be the canonical projection. This is also smooth.  In particular, the composite is smooth $$\pi_2\circ f: \mathbb R^{n+1}-\{0\}\longrightarrow S^n/(v\sim -v).$$
This induces a smooth map at the level of quotient:
$$\overline{f}: \mathbb R\mathbb P^n\longrightarrow S^n/(v\sim -v)$$
This is the unique map such that $$\pi_2\circ f=\overline{f}\circ \pi_1$$ where $\pi_1: \mathbb R^{n+1}-\{0\}\longrightarrow \mathbb R\mathbb P^n$ is the canonical projection.
The map $\overline{f}$ will be the desired diffeomorphism.


Is there a way to solve $\int \frac1{(x^2+a^2)^2}$ without $x=\tan t$ substitution?

Question: This is from an integral problems book, it is a part of the solution of a larger problem.
$$\int_0^1\frac1{(x^2+4)^2}~dx=\frac18\Biggl( \Bigl(\frac x{x^2+4} \Bigr) \bigg\vert _0^1~+~\int_0^1\frac{dx}{x^2+4}   \Biggr ) = ~~ ... $$
It looks like integration by parts but it isn't.
I suppose this technique works generally for integrals of the type $ \frac1{(x^2+a^2)^2}$
What is this technique ?
Comment: https://math.stackexchange.com/a/689932/1242
Answer: In fact, more generally, you can compute $\displaystyle\int\dfrac{1}{(x^2+a^2)^{n+1}} {\rm d}x$ in terms of $\displaystyle\int\dfrac{1}{(x^2+a^2)^{n}} {\rm d}x$.
For, observe that $\dfrac{1}{(x^2+a^2)^{n}}=x\cdot\dfrac{x}{(x^2+a^2)^{n+1}}+\dfrac{a^2}{(x^2+a^2)^{n+1}}$. Now apply integration by part on the first term of the RHS.
Answer: Like this:
$$ I=\int \frac{1}{(x^2+4)^2}dx=\frac{1}{B} \int \frac{1}{2x} d(A-\frac{B}{x^2+4})=\frac{1}{B} \left[ \frac{1}{2x} (A-\frac{B}{x^2+4})+\int \frac{Ax^2+4A-B}{x^2+4} \frac{1}{2x^2} dx  \right] $$
We note
$$ \frac{Ax^2+4A-B}{x^2+4} \frac{1}{2x^2} $$
We can let
$$ A=2,4A-B=0 \implies A=2,B=8 $$
$$ \frac{Ax^2+4A-B}{x^2+4} \frac{1}{2x^2}=\frac{1}{x^2+4} $$
Thus
$$ I=\frac{1}{8} \left[ (\frac{x}{x^2+4})+\int \frac{1}{x^2+4} dx  \right] $$


Retornar dados de tabelas diferentes em uma view - Laravel (Resolvido)

Question: Solução do problema: Nesse exemplo, estou retornando na view de usuário, dados referentes à 2 tabelas diferentes (Intro, About), que poderão ser recuperadas a partir de um @foreach
Exemplo: View usuario.index

@foreach($intros as $intro)
    {{$intro->title_intro}}
    {{$intro->desc_intro}}
@endforeach
@foreach($abouts $about )
    {{$about ->title_about}}
    {{$about ->desc_about}}
@endforeach

Controller do usuário:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Intro;
use App\About;

class UserController extends Controller
{
    public function index()
    {
        return view('user.index',
        [
           'intros' => Intro::paginate(5),
           'abouts' => About::paginate(5)
        ]);
    }
}
Comment: é só fazer assim: `return view('user.index', ['intros' => Intro::paginate[5],'abouts' => About::paginate[5], 'skills' => Skill::paginate[5]]);` claro pode colocar tudo isso em uma variavel e passar ai também melhor inclusive. O método também é válido, mas, acho assim melhor (pura opinião)
Comment: `return view('caminho.nomeDaView')->with(['info1' => model::metodo($dado), 'info2' => model::metodo($dado2) ... ]);`

e por ai vai
Comment: Pera, ainda não consegui entender... Desculpe rs
depois do ->with, esse 'info1' seria referente ao que? E após o model:: essa parte: método($dado) seria referente ao que? Agradeço pela ajuda
Comment: info1 é referente ao nome do array que vc vai visualizar na view, o método, é o método po... Quanto vc conhece de orientação a objeto?
Comment: Básico/Intermediário... só que, não tenho muita experiência com o laravel fiquei confuso sobre qual método iria chamar ali... Desse jeito, é dada a informação que a variável intro é indefinida

return view('user.index')->with( [ 'intros' => Intro::index($intro), 'abouts' => About::index($abouts) ]);
Comment: Acabei de testar aqui e deu certo dessa forma Virgilio, muito obrigado! E ao Hanania também, por tirar um tempinho pra tentar me ajudar... :)
só o [] que troquei pelo () porque tinha dado erro mas de resto está tudo certinho heheh
Answer: O seu retorno deve ser assim:
<code>return view('user.index',
    [
        'intros' => Intro::paginate(5),
        'abouts' => About::paginate(5),
        'skills' => Skill::paginate(5)
    ]);
</code>
Para um melhor entendimento, leia sobre na documentação do Laravel 5.7
Depois, na view, é só acessar o dado desejado pela Key desse array.


Decomposition of semimartingales

Question: From Kal97, pg. 446:

Theorem 23.14 Any semimartingale $X$ has an a.s. unique decomposition $X=X_0 + X^c + X^d$ where $X^c$ is a continuous local martingale with $X_0^c=0$ and $X^d$ is a purely discontinuous semimartingale.

Q1: When is it true that
$X_t^d = \sum_{0 \leq u \leq t} \Delta X_u$
(where $\Delta X_t \equiv X_t - X_{t-}$ and $X_{t-} \equiv \lim_{u \nearrow t} X_u$)?
Is it always true?
Pro05, pg.221 seems to require:

Hypothesis A. $\sum_{0 \leq u \leq t} | \Delta X_u | \lt \infty$ a.s., each $t \gt 0$

But I thought this was trivially satisfied by semimartingales since their quadratic variation exists and is finite.
Q2: When is it true that $X^d$ is a finite variation process?
I suppose if someone answers my first question the second is necessarily answered?
(Thx for bearing with me. This material is way over my head. Just formulating a good question has been a challenge.)
Comment: Really, nothing on this? Anyone have suggestions for a better way to pose my question or different forum? Is this more appropriate for mathoverflow?
Comment: This is fine for math.stackexchange. I'll answer this properly later this week if nobody else does. Btw, your Hypothesis A is missing some absolute-value signs. If you put them in then the hypothesis is equivalent to $X^d$ being a finite variation process. This is *not* satisfied by all semimartingales (e.g., Cauchy process). Q1 does not have a good answer though, it's just true when it is true. In general there is no reason to expect this to hold.
Comment: Good catch! I added the absolute value signs in the question. For Q1, I'd be content with an example for when it holds and when it doesn't. I'm really just trying to wrap my head around what these different processes are supposed to *look like*.
Comment: @GeorgeLowther: If you add a short answer below, I'll almost surely accept it. Still have something? :)
Answer: Q1: In general it is not true that $X^d_t$ is equal to $\sum_{s\le t}\Delta X_s$. In order for this equality to make sense, you need assume something like that the sum of the jumps of $X$ is absolutely convergent (Hypothesis A), although that still does not imply that the identity holds. The term $X^d$ is the purely discontinuous part of the semimartingale $X$, but this terminology can be confusing. A purely discontinuous semimartingale is not in general equal to the sum of its jumps, and need not be discontinuous at all! All (adapted and right-continuous) finite variation processes are "purely discontinuous" semimartingales, regardless of whether they are continuous or not. So, for example, take any continuous finite variation process $X$, such as $X_t=t$. Then, you simply have $X^d_t=X_t-X_0$ whereas $\sum_{s\le t}\Delta X_s=0$. The terminology "purely discontinuous" does make a bit more sense for local martingales, where you can show that purely discontinuous local martingales are, in a sense, orthogonal to the continuous local martingales and are themselves (almost surely) continuous only if they are constant. I've also seen the term "quadratic pure jump semimartingale" to describe purely discontinuous semimartingales (in, e.g., Protter, Stochastic Integration and Differential Equations). This does make some sense, as a semimartingale is purely discontinuous if and only if its quadratic variation is a pure jump process, $[X]_t=\sum_{s\le t}(\Delta X_s)^2$.
Anyway, the short answer to Q1 is that $X^d_t=\sum_{s\le t}\Delta X_s$ if and only if $X$ is the sum of a continuous local martingale and an (absolutely convergent) sum of jump terms. This is really just tautological and saying that the identity holds only when it holds, but I doubt you are going to get any better general statement than that.
You also mention Hypothesis A: $\sum_{s\le t}\vert \Delta X_s\vert < \infty$ (a.s.). This property is not true for all semimartingales. In general, the existance of the quadratic variation ensures that
$$
\sum_{s\le t}(\Delta X_s)^2\le[X]_t < \infty.
$$
This does not imply Hypothesis A. You do have the inequality $\sum_{s\le t}\vert\Delta X_s\vert\ge\sqrt{\sum_{s\le t}(\Delta X_s)^2}$, but that is the wrong way round to tell us anything useful here. Examples of semimartingales which fail this hypothesis include the Cauchy process (I can link you to my blog post on properties of Levy processes for this, although any book on Levy processes should give more details), which are purely discontinuous semimartingales with finite variation.
Q2: This question has a very nice answer, $X^d$ is a finite variation process if and only if Hypothesis A holds! This can be proven without too much difficulty (assuming some facts about continuous semimartingales).
Proof: Suppose that $X^d$ is a finite variation process. As $\Delta X_t=\Delta X^d_t$, the sum $\sum_{s\le t}\vert\Delta X_s\vert$ is bounded by the variation of $X^d$ on the interval $[0,t]$, so is finite (a.s.)
In the other direction, suppose that Hypothesis A holds, so we can define the finite variation process $Y_t=\sum_{s\le t}\Delta X_s$. Then $X-Y$ is a continuous semimartingale. By a standard result on decomposition of continuous semimartingales, we can decompose $X-Y=M+V$ for a continuous local martingale $M$ and continuous finite variation process $V$. Then, $Y+V$ is a finite variation process, so is a purely discontinuous semimartingale. Also, $X=M+(Y+V)$ implies that $X^d=Y+V$ is a finite variation process.
Comment: Awesome answer. This was exactly what I was looking for. Thanks George. By the way, your blog has been a huge help for me.
Answer:
Protter defines purely discontinuous to mean orthogonal to all continuous path martingales.  As a result he would have (problem 6.9) $N_t - \lambda t, N_t \sim \;poiss(\lambda)$,  purely discontinuous.
quadratic variation would control sum of square of the jumps.
for Levy processes q2 has a nice answer in terms of the Levy measure, Cont and Tankov, chapter 3 is a readable source.

Decomposition of semimartingales


Programmarically selecting a particular HdyPreferencesPage in libhandy

Question: In GNOME libhandy, I got a HdyPreferencesWindow which contains some HdyPreferencesPage instances (that display as tabs).
How do I programmatically select/show a particular tab? There seems to be no public API to do this.
Thanks!
Answer: I was told in https://matrix.to/#/#libhandy:talk.puri.sm that there is no way to do this in libhandy and was suggested to migrate to GTK4 and libadwaita.
You can, however, fish for the GTK pages stack and talk to it directly, but that bypasses the public API.


$20$ students takes an exam what is the possibilities to grade them while between each two students the gap between grade is $4$

Question: I have the following question :
$20$ students takes an exam what is the possibilities to grade them while between each two students the gap between grade is $4$.
The grades are between $0-100$.
This is what I did :
$$20!*CC^{25}_{21}$$
$20!$ - The students are different so the order between them matters.
$CC^{25}_{21}$ - we have total of 101 grades possible, between each two student the gap must be at least $4$ so lets use the ball and cells methods, we have 21 cells while two of them (the one after the max grade, the one after the min grade) doesn"t matter. so we get $101-19*4=25$ so we have $25$ ball into $21$ cells.
For some reason the this answer is wrong, The answer is $$20!*CC^{24}_{21}$$
I don't understand why my answer is wrong.
Any help will be appreciated, thanks.
Comment: Does "the gap between grade is $4$" mean that two students getting scores of $1$ and $5$ is possible, or not?
Comment: Yes it is possible
Comment: 25 balls into  21 cells (you can put more than 1 ball or 0 balls in each cell) is ${25+21\choose 21}
Comment: the right answer is what i wrote in the bottom meaning 24 balls to 21 cells unlike my answer for some reason which i dont understand
Answer: It looks like you didn't subtract off the top student (or bottom student) - your available balls for the floating grade points are $101-19\times \color{blue}{4} \color{red}{-1} = 24$, going into the $21$ cells among and around the students grade zones. The $19$ sets of $\color{blue}{4}$ grades are pre-allocated to preserve a student grade plus nearby grades above and the $\color{red}{1}$ other grade point to exclude is the top student.
Answer: The main idea is totally fine. But as the devil is in the detail, I would like to have your attention on the "bar and balls" part. You have $101$ balls (grades) and $20$ bars (students). If a bar is put in front of the ball number $i$, then the grade, associated with that bar is $i$. Firstly, a bar can be put in a place where there is at least one ball in front of it. otherwise a student would be assigned to grade $102$. There are $20$ students, which means that we need $21$ cells ($x_1,x_2,...,x_{21}$). The last cell should have at least $1$ ball in it ($x_{21}\geq$1). For $x_1$ there is no limit. For other cells, we should have $x_j\geq4$. So the problem is
$x_1+x_2+...+x_{21}=101-1-4\times19=24$
Comment: why is this true? a bar can be put in a place where there is at least one ball in front of it. otherwise a student would be assigned to grade 102.
Comment: This is just my way of modelling the problem. we have 101 possible grades, which we want to show by 101 balls. A student is a bar and if we put a student in front of ball $i$, it means the grade, that assigned to the student, is $i$. Now, if we put a bar at the end of the row of balls and bars, which means there is no ball in front of it, then the bar (student) would be assigned to no grade.


How exactly does drush determine PROJECTROOT and WEBROOT for a project?

Question: In the Drush Configuration section of the official documentation it is said:

drush.yml files are discovered as below, in order of precedence:

Drupal site folder (e.g. sites/{example.com}/drush.yml).
sites/all/drush, WEBROOT/drush, or PROJECTROOT/drush.
...

There you can see "WEBROOT" and "PROJECTROOT", but there's no explanation on what they are. In the Drush code there's no mention either apart from the configuration itself:
$ grep PROJECTROOT * -r
drush/docs/using-drush-configuration.md:2.  `sites/all/drush`, `WEBROOT/drush`, or `PROJECTROOT/drush`.

$ grep WEBROOT * -r
drush/docs/using-drush-configuration.md:2.  `sites/all/drush`, `WEBROOT/drush`, or `PROJECTROOT/drush`.

How Drush determines the values of PROJECTROOT and WEBROOT?
The questions is what those names means, but how Drush decide what they are. If they are used to search for different configuration files it means that Drush resolved those names to actual folders or paths in the current system. How Drush resolves them?
My guess is that Drush takes the root folder of the site (defined ion the Drush alias. If that folder is a Drupal root then WEBROOT and PROJECTROOT are the same, the defined root folder. If the configured root folder has a composer.json and a web folder then PROJECTROOT is the confiugred root path, and the web folder is the WEBROOT... but it is just a guess.
Comment: I'd imagine it literally means the root folder of your project vs the folder in your project which contains the web root.
Comment: Yes, that's true. But how Drush knows that? In the alias configuration you define the root path. That could be the webroot, but that's not true because you can point to the folder that contains the composer.json.

I guess Drush is intelligent enough to check if the root configuration value is a drupal root (in that case WEBROOT and PROJECTROOT are that folder) or is boilerplate scheme like the one created by `composer create-project`, so the folder with composer.json is PROJECTROOT and the web folder is WEBROOT... but I would like a confirmation.
Comment: Yes, drush is clever enough to traverse up the folder structure until it can find something it identifies as a Drupal project root (you'd have to look at the code to get the exact criteria). Similar to how Git, Composer and other tools work
Answer: Drush uses webflow/drupal-finder to determine these directories. The logic is in the DrupalFinder::findAndValidateRoots()
WEBROOT is the Drupal root folder and PROJECTROOT is the folder where the composer.json file is placed.
DrupalFinder is used in several places, but the interesting part is the Preflight::setSelectedSite function.
There Drush locates the "Drupal Root" and the "Composer Root". The "Drupal Root" is... well, the Drupal Root folder and what the Drush documentation calls WEBROOT. And the "Composer Root" is the folder where composer.json file is located and it is what the documentation calls PROJECTROOT.
Comment: Yes, I understand that, the question is more about how Drush determines the values for those folders. I've updated my question.
Comment: Updated @sanzante
Comment: Thanks! That's what I was looking for! I'm updating my question with my findings.
Answer: When you download Drupal using Composer, you get directory (folder) with some files. This is the project root, as it is the top-level of the entire project.
However, when viewing a Drupal site through a browser, you are not viewing the project root, rather, you are viewing one directory below it; by default, this folder is called /web (though it can be changed in composer.json). The web folder is the webroot, as it is the top-level folder that is viewed when accessing the project through the web.
Comment: That's the standard definition of a web root and a project root. I already know that. What I am asking is how Drush determines the value of WEBROOT and PROJECTROOT, not the general definition of web and project root in the context of a web project.


Binary Logistic Regression Multicollinearity Tests

Question: I like Peter Flom's answer to an earlier question about multicollinearity in logistic regression, but David Garson's Logistic Binomial Regression states that there is no valid test for multicollinearity for binary-dependent logistic regression, even if the independent variables are ratio scale. Can anyone supply one or more references? My own experience is that OLS correlation matrices and VIF worked for me, as my logistic coefficients went haywire before removing entangled independent variables based on the OLS tests for multicollinearity. But I have to publish my results and methods, and would like a reputable way to cite the practice, if one or more exist.
Answer: I'm glad you like my answer :-)
It's not that there is no valid method of detecting collinearity in logistic regression: Since collinearity is a relationship among the independent variables, the dependent variable doesn't matter.
What is problematic is figuring out how much collinearity is too much for logistic regression. David Belslely did extensive work with condition indexes. He found that indexes over 30 with substantial variance accounted for in more than one variable was indicative of collinearity that would cause severe problems in OLS regression. However, "severe" is always a judgment call. Perhaps the easiest way to see the problems of collinearity is to show that small changes in the data make big changes in the results.
[this paper http://www.medicine.mcgill.ca/epidemiology/joseph/courses/epib-621/logconfound.pdf] offers examples of collinearity in logistic regression. It even shows that R detects exact collinearity, and, in fact, some cases of approximate collinearity will cause the same warning:
Warning message:
glm.fit: fitted probabilities numerically 0 or 1 occurred

Nevertheless, we can ignore this warning and run
set.seed(1234)
x1 <- rnorm(100)
x2 <- rnorm(100)
x3 <- x1 + x2 + rnorm(100, 0, 1)

y <- x1 + 2*x2 + 3*x3 + rnorm(100)
ylog <- cut(y, 2, c(1,0))

m1<- glm(ylog~x1+x2+x3, family = binomial)
coef(m1)

which yields -2.55, 1.97, 5.60 and 12.54
We can then slightly perturb x1 and x2, add them for a new x3 and run again:
x1a <- x1+rnorm(100,0,.01)
x2a <- x2+rnorm(100,0, .01)
x3a <- x1a + x2a + rnorm(100, 0, 1)

ya <- x1a + 2*x2a + 3*x3a + rnorm(100)
yloga <- cut(ya, 2, c(1,0))

m2<- glm(ylog~x1a+x2a+x3a, family = binomial)
coef(m2)

this yields wildly different coefficients: 0.003, 3.012, 3.51 and -0.41
and yet, this set of independent variables does not have a high condition index:
library(perturb)
colldiag(m1)

says the maximum condition index is 3.54.
I am unaware if anyone has done any Monte Carlo studies of this; if not, it seems a good area for research