Fastest and efficient way to get record using Entity Framework and C#?

Question: I am beginner and I am using a function which takes approximately 20 seconds to load a record - please is there any other, more efficient and faster way to execute this? How i can include guest id with guest name so that its works faster rather then use for each loop to assign guest name with guest id
Data.Tables.Booking
    [Table("Bookings")]
public class Booking : ITrackable
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public int GuestId { get; set; }

    public int RoomId { get; set; }

    public DateTime CheckInDateTime { get; set; }

    public DateTime CheckOutDateTime { get; set; }

    public decimal DailyPricePerBed { get; set; }

    [StringLength(1000)]
    public string Memo { get; set; }
    public string PriceType { get; set; }

    public bool Paid { get; set; }

    public DateTime CreatedAt { get; set; }

    public DateTime ChangedAt { get; set; }
    public string? InvoiceType { get; set; }

}

Booking model
  public class Booking : BaseModel
{
    [Required]
    public int Id { get; set; }

    [Required(ErrorMessage = Constants.ERROR_REQUIRED)]
    [Display(Name = "Gast")]
    public int GuestId { get; set; }

    [Required(ErrorMessage = Constants.ERROR_REQUIRED)]
    [Display(Name = "Zimmer")]
    public int RoomId { get; set; }

    [Required(ErrorMessage = Constants.ERROR_REQUIRED)]

    [Display(Name = "Check-In")]
    public DateTime CheckInDateTime { get; set; }

    [Required(ErrorMessage = Constants.ERROR_REQUIRED)]
    [Display(Name = "Check-Out")]
    public DateTime CheckOutDateTime { get; set; }

    [Required(ErrorMessage = Constants.ERROR_REQUIRED)]
    [Display(Name = "Tagespreis pro Bett")]

    public decimal DailyPricePerBed { get; set; }

    [Required(ErrorMessage = Constants.ERROR_REQUIRED)]
    [Display(Name = "Price Type required")]
    public string PriceType { get; set; }

    [StringLength(1000, ErrorMessage = Constants.ERROR_MAX_LENGHT)]
    [Display(Name = "Sonstiges")]
    public string Memo { get; set; }

    [Display(Name = "Bezahlt")]
    public bool Paid { get; set; }

    public List<Guest> Guests { get; set; }

    public List<Room> Rooms { get; set; }

    public string GuestName { get; set; }

    public string TitleBooking { get; set; }

    public bool Selected { get; set; }

    public int RoomNumber { get; set; }

    public RoomType RoomType { get; set; }
    public string EncryptedRoomId { get; set; }
    public string EncryptedPartnerId { get; set; }
    public string? InvoiceType { get; set; }

}

}
public List<Booking> Load(int roomId)
{
    var result = _context.Bookings
                         .Where(item => item.RoomId == roomId)
                         .Select(item => _mapper.Map<Booking>(item))
                         .ToList();
     //its takes time here to load each guest name, if i remove
   //this part
 //its works fast but its showing guest ids on calendar, i want
 //to show guest name
    foreach (var booking in result)
    {
        if (_context.Guests.Any(o => o.Id == booking.GuestId)) // update
        {
            booking.GuestName = _guestRepository.GetName(booking.GuestId);
        }
    }

    return result;
}

Comment: Show model classes with navigation properties.
Comment: `if (_context.Guests.Any` you are performing another sql query inside your loop. Don't do that.
Comment: **What** part is slow? Loading the data from the database? Then you need to performance-optimize your database structure. Converting the loaded row using `_mapper` ? Tweak the mappings to improve performance.....
Comment: @marc_s slow is this part `foreach (var booking in result)
    {
        if (_context.Guests.Any(o => o.Id == booking.GuestId)) // update
        {
            booking.GuestName = _guestRepository.GetName(booking.GuestId);
        }
    }`
Comment: @SvyatoslavDanyliv please check my above modified detail question, hope you guys understand
Answer: You code will be slow for two main reasons. Firstly you are selecting a set of Bookings then using Automapper to "Map" these across to copies of Booking entity classes. If Bookings contain navigation property references to other entities and you have Lazy Loading enabled, this is quite likely resulting in a LOT of lazy load hits as Mapper.Map "touches" each navigation property as it's iterating through the Bookings to copy the object graph across. You are then going and iterating over each booking to call to the GuestRepository to get the guest name, and that could also be of varying efficiency. For instance does the repository do something like:
return _context.Guests.Where(g => g.GuestId == guestId).Select(g => g.GuestName).Single();

or does it do something like:
var guest = _context.Guests.Single(g => g.GuestId == guestId);
return guest.GuestName;

The first runs an SQL Statement to retrieve one column for one row. The second reads all columns from Guest and builds a Guest entity for one row, just to return one value.
The first thing would be to ensure that you have navigation properties defined for all relationships, such as between Bookings and Guests.
From your example I'm guessing this code is part of a BookingRepository which has a dependency on a GuestRepository to get guest information. If there is one piece of advice I can give, it is to avoid the Generic Repository pattern in EF, or thinking of Repositories as serving individual entities. Instead, design Repositories to serve business needs, like a Controller in MVC. If I have a BookingController set up to serve everything to do with making/reviewing bookings, then I can have a BookingRepository to handle all Domain interactions  for that Controller. Not just Booking entities, but everything needed for making/reviewing bookings.
The next thing is handling projection. Returning entities outside of the scope of a DbContext that reads them is generally not a great idea. Entities represent data domain. Views have their own concerns with regards to what data they need, and how they want to present it. They should have a purpose-built representation of the domain they are concerned with, a View Model.
So for instance if we have a Booking entity, it should be associated with a Room and a Guest. Each of these would be entities for their respective tables and linked by FKs within the Booking table. What the view is concerned with isn't everything in the Booking, Room, and Guest, just bits of details which can be flattened down from the respective tables and columns. For instance:
public class BookingViewModel
{
    public int BookingId { get; set; }
    public int RoomId { get; set; }
    public int GuestId { get; set; }
    public string RoomNumber { get; set;}
    public string GuestName { get; set; }
}

Now when we fetch bookings for a given room, without diving into Repositories yet, just working with a DbContext and it's entities:
var bookings = _context.Bookings
    .Where(b => b.RoomId == roomId)
    .Select(b => new BookingViewModel
    {
        BookingId = b.BookingId,
        RoomId = b.Room.RoomId,
        GuestId = b.Guest.GuestId,
        RoomNumber = b.Room.RoomNumber,
        GuestName = b.Guest.LastName + ", " + b.Guest.FirstName
    }).ToList();

With Automapper, we can configure mapping rules for translating a Booking and it's related structure into this BookingViewModel and the above can be simplified to something looking like:
var bookings = _context.Bookings
    .Where(b => b.RoomId == roomId)
    .ProjectTo<BookingViewModel>(mapperConfig)
    .ToList();

Where "mapperConfig" is an instance of a MapperConfiguration set up with the rules to translate Booking -> BookingViewModel. This could be one global Config, or a config built as requested by a factory method.
The benefits with either Select or ProjectTo is that the projection goes directly to the SQL Query so the only data returned is what is needed to populate the view model. There are no risks of lazy loading surprises, or even worrying about tracked entities bogging down the DbContext.
When starting out I would get the hang of using the DbContext and projection without introducing a Repository pattern. The EF DbContext acts as both a Unit of Work and Repository in the sense, and trying to abstract that fact from your application can mean introducing significant performance and flexibility penalties.
For introducing repositories I would recommend either a Repository pattern and Unit of Work pattern that leverage IQueryable so that callers can project details as they need, or having repositories that abstract the domain (entities) into the needs of the consumer. (view models)  IQueryable provides a lot of flexibility making Repositories easy to mock for testing, but are coupled to Entity Framework as consumers need to know that fact and manage the DbContext's Scope to use them effectively. Designing repositories that return ViewModels creates a cleaner boundary to isolate consumers from EF, but requires the Repository to have a larger footprint to accommodate all methods, variants, and concerns for the consumer(s). For instance supporting sorting, pagination, etc. Structuring Repositories to serve individual controllers can certainly help compared to repositories-per-entity that serve many controllers with different concerns.
Edit: If updating the result to a view model represents too big of a change, do keep these details in mind for future work because the approach your code base is using is highly inefficient. You Can mitigate the problem to a degree with some smaller changes including loading the entities detached and using Include to Eager-load the required relationships.
The first change would be moving the GuestName into a domain concern that the entity can resolve. Inside the Booking entity change:
public string GuestName { get; set; }

to:
private string _guestName = null;
public string GuestName
{
    get
    {
        return _guestName ?? (_guestName = Guests.SingleOrDefault(g => g.GuestId == GuestId)?.Name;
    }
    set { _guestName = value; }
}

If you are using .Net Core 6 or 7 this can be simplified to:
private string? _guestName = null;
public string? GuestName
{
    get => _guestName ??= Guests.SingleOrDefault(item => item.GuestId == GuestId)?.Name;
    set => _guestName = value;
}

Rather than going to the repository for every guest record to get the name, let the entity go to its Guests collection. (if available) This is written as to not break existing code, so any code that "Sets" the guest name will still take precedence.
Alternatively you could use the setter like you are and just go to booking.Guests to get the applicable guest's name rather than going to the repository:
foreach (var booking in result)
{
    booking.GuestName = booking.Guests.FirstOrDefault(item => item.GuestId == booking.GuestId)?.Name;
}

If the guest name needs to be formatted from a Guest First and Last Name:
foreach (var booking in result)
{
    var mainGuest = booking.Guests.FirstOrDefault(item => item.GuestId == booking.GuestId);
    if (mainGuest == null) continue;

    booking.GuestName = $"{mainGuest.LastName}, {mainGuest.FirstName}";
}

For methods like this to work, whether using the property or getting the guest from the Guests collection and using the Setter, the entity must have the Guests collection eager loaded, which is the next step:
Eager load any required details your view is going to need about the guest, and detach them:
var result = _context.Bookings
    .Include(item => item.Guests)
    .Where(item => item.RoomId == roomId)
    .AsNoTracking()
    .ToList();

This will do something similar to your original code, but it will eager load the Bookings collection, and it will detach the resulting entities so that they won't be lazy-loadable proxies or have changes tracked by the DbContext. The issue with using Mapper.Map without AsNoTracking is that if your DbContext is set up to use lazy loading, the Mapper.Map call will go through each property, which when it hits a navigation property, trigger a lazy load. This will ensure that all data is mapped, but it is extremely slow and inefficient. The above example eager loads the Bookings collection. If there are other navigation properties your view will touch, these will very likely currently be #null now, so you will need to ensure they are eager loaded using Include as well.
Eager loading with Include does come with some performance issues when dealing with one-to-many relationships, especially eager loading several one-to-many relationships in that it produces Cartesian Products where the total volume of data grows by factors with the more relationships you load. This will typically be faster than lazy loading, but still represents a significant resource and performance cost. This is why projecting to a view model is recommended. You will still generate a Cartesian Product, but across far fewer fields as the projection only selects the fields you actually need rather than everything in the associated table. If you are using Eager Loading, eager load only what you know you will need, not everything, or you will soon be facing similar performance and memory bloat problems.
Comment: I am sorry i cannot create new class of `bookingviewmodel` because i need to change complete code also my class is inherit with base class o i cannot create new class
Comment: please check my updated question , i provide details , please help
Comment: I have updated the answer to include a possible work-around with the existing Entity usage which should help, but definitely consider changing your approach to use view models rather than passing entities around as it will avoid many of the performance pitfalls you are getting tripped up by.
Comment: hi, i apply your code but its showing error [link](https://paste.pics/LM82O)
Comment: You need to add `.Include(item => item.Guests)` to your query for the Bookings. (My original code example had a typo referencing item.Bookings)   The work-around relies on the relationships referenced need to be eager loaded. When declaring navigation properties to Collections you should pre-initialize the collection to a new List(). I.e. `public virtual ICollection Guests { get; set; } = new List();`  This helps avoid nullRefExeptions when setting up new Bookings or situations where they aren't eager loaded or no guests are present without extra null checks everywhere..
Answer: As comments have pointed out, you have not specified which of the parts of that function is consuming the time, nor is it clear what your model looks like.
However, I shall make two assumptions:

the problem is that you are looping through result and then hitting the database for each booking, perhaps twice.
the Guest name property is in the Guests entity / table.

If so, something like the following should speed it up by minimising the database calls to two for the whole function.
// Database Call 1
var result = _context.Bookings
                     .Where(item => item.RoomId == roomId)
                     .Select(item => _mapper.Map<Booking>(item))
                     .ToList();

// Get the Guest Ids used in the booking retrieved above
List<int> guestIds = result.Select(b => b.GuestId).Distinct().ToList();

// Get all Guests that are used - Database Call 2
List<Guest> guests = _context.Guests.Where(g => guestIds.Contains(g.Id)).ToList();

// Now put the name on the bookings
foreach (var booking in result)
{
    Guest? guest = guests.SingleOrDefault(g => g.Id == booking.GuestId);

    if (guest is not null)
    {
        booking.GuestName = guest.Name;
    }
}

Alternatively, if you have a navigation property to guest from booking, then you can retrieve the guest along with the booking in a single database call like so:
var result = _context.Bookings
                 .Include(b => b.Guest)
                 .Where(b => b.RoomId == roomId)
                 ... etc.

Comment: I can understand you choosing not to answer before additional details, but I felt that the answer could help a self-described beginner on their way.  I agree with your comment about `Include` and `Select`, hence my "etc ..." as if the OP moves to using a navigation property the redundancy of Select would become apparent.
Comment: Since you only iterate the result of your call to `.ToList()`, it's better to call `.ToArray()` instead. The iteration is significantly faster. https://www.meziantou.net/copying-a-collection-tolist-vs-toarray.htm
Comment: For me it's obvious which part is slow, so I do not answer before additional details. And `Include` with `Select` has no sense.
Comment: @NeilW your code showing error please check screenshot [link](https://paste.pics/LLGDW)
Comment: @NeilW this is slow due to this part of code  `foreach (var booking in result)
            {

                if (_context.Guests.Any(o => o.Id == booking.GuestId))
                {
                    booking.GuestName = _guestRepository.GetName(booking.GuestId);
                }
            }`
Comment: @NeilW if i just remove above part of code then its works fast,
Comment: @NeilW please check my updated details question
Comment: @don - Have updated my answer with a cast ToList() where you had the error.  Yes, I would expect your code to run fast if you remove that part, as it is that part that is hitting the database multiple times for each item in the result.
Comment: its still showing same error which i send you earlier, i dont know which thing you update


Repair A Pearl Export Series Snare Strainer

Question: I am struggling with a Pearl snare strainer (identical to the one mentioned in a question asked in Aug 2006! And that is how I got here). Someone handed me down the drum kit and there were not one but two screws stuck in the strainer. What bugs me is that, if I put the two screws back to back, they look exactly like the one long screw I am apparently supposed to use to connect the two parts of the strainer. But I can't make sense of that. I can't imagine that they were glued together or anything like that!!! So my guess is that I somehow have to buy a screw...? But where? I didn't have much success researching online, except for this providential site! I tried to take as many pictures as possible (with the screws in and without) to explain things as best as I could.
Comment: Are you saying the short screw (setbolt) won't reach the backplate  it's supposed to screw into? If indeed it is too short, you will have to find another with the same thread, but make sure it's long enough, but not so long that it catches on the side of the drum as the strainer goes up and down. An ironmongers or diy type store may have one, but a call to Pearl may reveal what thread the setscrew is.
Answer: Is this the part you meant when you talk about two screws "back to back"?
This picture is from How do the parts of an '80's Pearl Export snare strainer come together?
It sounds as though your part has broken. I don't expect any sort of glue would be strong enough to join the two parts back together again. You may have a metalworking friend who could join the parts. There's no way you'd find a double-ended screw like that at an ironmongers. So if you can't repair the part I think you'll have to approach Pearl for a replacement strainer unit, as it's probably not worth replacing just the threaded part.
Answer: Take the screws to a diy or ironmonger, or better still, a place that specialises in nuts bolts and screws. You will find they can match the tread (very important), and supply what you need. You may have to cut overlength setscrews to their correct length, but surely that's a small price to pay. Also, at the same time, use a better, stronger, longer lasting material than what looks like a stick of licorice to connect the strainer to the lever!


Why the weak topology is never metrizable?

Question: Let $X$ be an infinite-dimensional normed space.  Why the weak topology on $X$ is never metrizable ? I saw a proof here but I don't really understand the argument. Here is his argument

Assume that there is a metric $d$ on $X$ inducing the weak topology, and consider $U_n:=\{x\in X: d(x,0)<\frac{1}{n}\}$.  We know each $U_n$ is weakly open and so will be unbounded, and thus $$\forall (n)\exists (x_n\in U_n) \:\text{s.t.}\: \|x_n\|\geq n$$ But $x_n\to 0$ in $(X, d)$, so that $x_n \stackrel{w}{\to}0$, and hence $(x_n)$ is bounded. Contradiction.

Q1) Why $U_n$ is weakly open ? Is it because a basis of the weak topology is of the form $$\{B_d(x,\varepsilon)\mid x\in X, \varepsilon>0 \}$$
where $$B_d(x,\varepsilon)=\{y\in X\mid d(x,y)<\varepsilon\} \ ?$$
Q2) Why are $U_n$ unbounded ? Since $U_n\subset B(0,1)$ for all $n\geq 1$, then $U_n$ is bounded (for me, in a metric space $(X,d)$, a set $A$ is bounded, if it's included in a ball $B_d(a,\varepsilon)$... since here $U_n\subset B_d(0,1)$ for all $n\geq 1$, it should be bounded). I really don't understand the argument here.
Answer: Note that we require $X$ to be infinite-dimensional.  For if $X$ is finite-dimensional, there is only one vector topology on $X$, which is metrizable.
Q1: That's correct.  If the metric $d$ induces the weak topology, then the balls in that metric are weakly open.
Q2:  This is part of a more general result that every weakly open subset of $X$ is unbounded in the norm-topology of $X$.  The idea is that if $U$ is weakly open and $x_0\in U$, then $U$ there is some $\varepsilon>0$ and $\varphi_1,\ldots,\varphi_n\in X^*$ such that
$$x_0\in\bigcap_{k=1}^n\{x\in X:|\varphi_k(x-x_0)|<\varepsilon\}\subset U.$$
and the set $\bigcap_{k=1}^n\{x\in X:|\varphi_k(x-x_0)|<\varepsilon\}$ contains the hyperplane $x_0+\bigcap_{k=1}^n\ker(\varphi_k)$, which is unbounded in the norm-topology of $X$.
Comment: Let $\mathcal T_W$ the weak topology, $d$ the metric s.t. $\mathcal T_W=\mathcal T_d$ and $\mathcal T$ the strong topology. When they say that $U_n$ in unbounded, it's wrt $\mathcal T_d$ or $\mathcal T$ ? Because as I'm thinking, it's wrt $\mathcal T_d$, but maybe it's wrt $\mathcal T$ ?
Comment: It's with respect to $\mathcal T$.  That's how you get that $\|x_n\|\geq n$.
Comment: It make sense now. Thanks a lot for your great answer, that helped me a lot. (Why didn't the OP made this precision in his post ? )
Comment: Glad to help!  I'm guessing they knew all this, and took it for granted.
Answer: $U_n$ is weakly open since the topology defined by the metric is the weak topology, so the open subsets of the two topologies coincide.
$U_n$ is open since $U_n$ is open for the weak topology: i.e, there exists a bounded linear function $f$, and an interval $I$ containing the origin  $f^{-1}(I)$ is not empty and contained in $U_n$. Let $x$ such that $f(x)\in U_n$, for every $y\in Ker(f),f(x+y)\in I$, I assume that that the dimension of the space is superior or equal to $2$, then we can assume that $y\neq 0$ and the sequence $x+ny\in f^{-1}(I)$ is unbounded.
Comment: Your proof of the second fact is not quite true.  The collection of sets $f^{-1}(I)$ form a subbasis for the weak topology, not a basis.  And for this reason, we require not only that $\dim X\geq2$, but that $\dim X=\infty$.
Comment: Sorry but I don't understand your answer. How do you know that there exists a bounded linear function f, and an interval I containing the origin $f^{−1}(I)$ is not empty and contained in $U_n$ ? I guess $I$ is an open interval no ?


Does MediaPlayer work in all API versions of android?

Question: I have a question about MediaPlayer. Does it work in android 7? Can it work with any API?
Thanks in advance
Answer: MediaPlayer was added in API Level 1. So, yes it will work on all API's.
Comment: Will sure do HB
Comment: Ohh ok. Thank you


Redundant OpenVPN connections with advanced Linux routing over an unreliable network

Question: I am currently living in a country that blocks many websites and has unreliable network connections to the outside world. I have two OpenVPN endpoints (say: vpn1 and vpn2) on Linux servers that I use to circumvent the firewall. I have full access to these servers. This works quite well, except for the high package loss on my VPN connections. This packet loss varies between 1% and 30% depending on time and seems to have a low correlation, most of the time it seems random.
I am thinking about setting up a home router (also on Linux) that maintains OpenVPN connections to both endpoints and sends all packets twice, to both endpoints. vpn2 would send all packets from home to vpn1. Return trafic would be send both directly from vpn1 to home, and also through vpn2.
       +------------+
       |    home    |
       +------------+
        |          |
        | OpenVPN  |
        |  links   |
        |          |
     ~~~~~~~~~~~~~~~~~~ unreliable connection
        |          |
+----------+   +----------+
|   vpn1   |---|   vpn2   |
+----------+   +----------+
        |
       +------------+
       | HTTP proxy |
       +------------+
             |
         (internet)

For clarity: all packets between home and the HTTP proxy will be duplicated and sent over different paths, to increase the chances one of them will arrive. If both arrive, the first second one can be silently discarded.
Bandwidth usage is not an issue, both on the home side and endpoint side. vpn1 and vpn2 are close to each other (3ms ping) and have a reliable connection.
Any pointers on how this could be achieved using the advanced routing policies available in Linux?
Answer: Use the bonding infrastructure at the 'home' and 'vpn1' side, and specifcally with the mode=3 setting which broadcasts traffic on all interfaces which belong to a bond.
For more information on how to configure bonding, see the excellent manual at http://git.kernel.org/?p=linux/kernel/git/stable/linux-2.6.37.y.git;a=blob;f=Documentation/networking/bonding.txt;h=5dc638791d975116bf1a1e590fdfc44a6ae5c33c;hb=HEAD
Comment: I tested this setup and it works fantastic. Package loss is reduced from around 5% to 0.0-0.1% with a redundant connection to just one server!
Answer: I used the answer provided by @user48116 and it works like a charm. The setup is actually quite easy!
NOTE: I implemented this with two connections to just one single server, as this already solved the problem for me. If you want to try a setup with two servers, the easiest way is probably to use port forwarding to forward the UDP port from the second server to the first, and use the same recipe as described here. I have not tested this myself though.
First, make sure you have a 2.6 kernel with bonding support (default in all modern distributions) and you have ifenslave installed.
Next, put this into your /etc/rc.local or any other place you prefer, but make sure it's run before openvpn is started (because it will try to bind to bond0):
Client:
modprobe bonding mode=broadcast
ifconfig bond0 10.10.0.2 netmask 255.255.255.0 up

You could add some routing if needed here, make sure you do all the proper routing from the other side too though.
route add -net 10.7.0.0/24 gw 10.10.0.1

Server:
modprobe bonding mode=broadcast
ifconfig bond0 10.10.0.1 netmask 255.255.255.0 up

Create a /etc/openvpn/tap-up.sh script (and don't forget to mark it executable with chmod a+x tap-up.sh):
#!/bin/sh
# called as: cmd tap_dev tap_mtu link_mtu ifconfig_local_ip ifconfig_netmask [ init | restart ]
ifenslave bond0 "$1"

Next, add a bridge0a.conf and bridge0b.conf to /etc/openvpn/ together with a shared key. The files are the same for a and b, except for a different port (for example, use 3002 for b). Replace 220.127.116.11 by your server's public IP.
Client:
remote 126.96.36.199
dev tap
port 3001
rport 3001
secret bridge.key
comp-lzo
verb 4
nobind
persist-tun
persist-key
script-security 2
up /etc/openvpn/tap-up.sh

Server:
local 184.108.40.206
dev tap
port 3001
lport 3001
secret bridge.key
comp-lzo
verb 4
script-security 2
up /etc/openvpn/tap-up.sh

Don't forget to edit /etc/defaults/openvpn to make sure your new VPN configurations are started. Reboot you machines, or load rc.local and restart openvpn manually.
Now you're ready to test your setup:
# ping 10.10.0.1
PING 10.10.0.1 (10.10.0.1) 56(84) bytes of data.
64 bytes from 10.10.0.1: icmp_req=1 ttl=64 time=50.4 ms
64 bytes from 10.10.0.1: icmp_req=1 ttl=64 time=51.1 ms (DUP!)
64 bytes from 10.10.0.1: icmp_req=1 ttl=64 time=51.1 ms (DUP!)
64 bytes from 10.10.0.1: icmp_req=1 ttl=64 time=51.1 ms (DUP!)
64 bytes from 10.10.0.1: icmp_req=2 ttl=64 time=52.0 ms
64 bytes from 10.10.0.1: icmp_req=2 ttl=64 time=52.2 ms (DUP!)
64 bytes from 10.10.0.1: icmp_req=2 ttl=64 time=53.0 ms (DUP!)
64 bytes from 10.10.0.1: icmp_req=2 ttl=64 time=53.1 ms (DUP!)
--- 10.10.0.1 ping statistics ---
2 packets transmitted, 2 received, +6 duplicates, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 50.428/51.786/53.160/0.955 ms

If everything goes well and the line is good, you will see four replies for every ICMP package: your packages are duplicated on the local side, and the replies to these two packages are duplicated again on the remote side. This will not be an issue for TCP connections, because TCP will simply ignore all duplicates.
This is an issue for UDP packets, as it's up to the software to handle duplicates. For example, a DNS query will yield four replies instead of the expected two (and use four times the normal bandwidth for the response instead of two times):
# tcpdump -i bond0 -n port 53
listening on bond0, link-type EN10MB (Ethernet), capture size 65535 bytes
13:30:39.870740 IP 10.10.0.2.59330 > 10.7.0.1.53: 59577+ A? serverfault.com. (33)
13:30:40.174281 IP 10.7.0.1.53 > 10.10.0.2.59330: 59577 1/0/0 A 18.104.22.168 (49)
13:30:40.174471 IP 10.7.0.1.53 > 10.10.0.2.59330: 59577 1/0/0 A 22.214.171.124 (49)
13:30:40.186664 IP 10.7.0.1.53 > 10.10.0.2.59330: 59577 1/0/0 A 126.96.36.199 (49)
13:30:40.187030 IP 10.7.0.1.53 > 10.10.0.2.59330: 59577 1/0/0 A 18.104.22.168 (49)

Good luck!


Angular injecting $service results in Unknown provider: $serviceProvider

Question: Why when I run this in the karma runner:
describe('Service tests', function () {

    var DataServiceMock
    var httpBackend;

    beforeEach(angular.mock.module('app'));

 beforeEach(angular.mock.inject(function( $httpBackend, $service, DataService,  $injector){

results in this error

Error: [$injector:unpr] Unknown provider: $serviceProvider <- $service
    http://errors.angularjs.org/1.2.1/$injector/unpr?p0=%24serviceProvider%20%3C-%20%24service
        at /home/site/angular/angular.js:78:12

Edit 2____________________
I'm trying to mock the DataService for the dataHandlerService
describe('Service', function () {

    var DataServiceMock
    var httpBackend;

    var testUrl = "test/";

    beforeEach(angular.mock.module('app'));

  beforeEach(angular.mock.inject(function(){
    module(function ($provide) {
            $provide.value('DataService', DataServiceMock)
        })
    }));

    it('should have', inject(function(DataService) {
        expect('DataService').not.toBe(null);
    }));

and this error:

Error: Injector already created, can not register a module!
        at workFn (/home/me/root/angular/angular-mocks.js:1985:15)
Comment: Because there is not a $service to inject. What is that $service you want to be injected?
Comment: @idursun I have an Angular DataService that I want to mock
Answer: There is no $service. If you want to mock your service then you should do it by using $provide:
beforeEach(function () {
  DataServiceMock= {}
  DataServiceMock.doSomething = function() {}
  module(function ($provide) {
    $provide.value('DataService', DataServiceMock)
  })
})

Comment: Creating a mock is putting there something that mimics the actual implementation. But in your edit, I see that you are trying to get the actual service from the injector. Since you called the injector before the module provide routine, you are getting the error. You should be creating your mocks like in my answer...
Comment: If you encapsulate your `DataService` logic in a service, then you don't even have to deal with `$httpBackend` while testing. You just need to get used to this module injections in tests thingie.
Comment: please see my second edit, sorry if its taking me a while to get this.
Comment: Please make sure `module(function($provide))` is called before any inject call. Can you also add the code that is before the module call?
Comment: actually I just want to instantiate a custom $http (the only think I need to change) into the DataService. I think now I don't need to create a mock of DataService
Comment: In your code it goes like beforeEach -> inject -> module. That is exactly what the error message indicates injector is already created so you cannot add a module. It should be as in my answer, you have to call inject _after_ the module call.


Excel: Still have to use the normal approximation with GAMMA.INV for high values of alpha?

Question: The GAMMAINV function from Excel 2003 had a propensity for generating NA error messages for high alpha values as the iterative process failed to reach convergence. It was therefore common to use a normal approximation if alpha > 300 (the gamma distribution tends towards the normal distribution for high alpha).
In 2010 Microsoft refurbished many of the Excel statistical functions, including the gamma distribution, now called GAMMA.INV. The iterative search algorithm have supposedly been improved.
Q1: Should the NORM.INV still be called in situations where alpha > 300?
I did some analyses trying to input extreme parameters that should/could lead to failed convergence. I found that GAMMAINV and GAMMA.INV returned exactly the same results (also with respect to errors). I expected that there should at least some minor numeric differences between them...
Q2: Does this mean that the GAMMAINV calls on the same algorithm as GAMMA.INV in Excel 2010?
Comment: No doubt you are aware of the pitfalls here. Absence of evidence of difference is not evidence of absence of difference. Not clear what anybody can add to this unless they have found any differences in results themselves; the code is manifestly not public. Have you asked Microsoft? (People are at liberty to regard that as amusing.)
Comment: Dear @NickCox, Thank you for you comment. I took the liberty to use your post as a motivation to contact Microsoft support. Neither the 1st or 2nd line tech support was able to answer. They did, however, recomend posting on the question on [answers.microsoft.com](http://answers.microsoft.com/en-us/office/forum/office_2010-excel/excel-still-have-to-use-the-normal-approximation/4661ea9f-fe7c-4eee-8d46-285c7acdb506). Ill post an update as soon as hear something from MS.
Comment: Why don't you just compare the output to published tables or reliable statistical software?
Comment: @whuber, im using the gammainv in a Monte Carlo simulation; consuliting a stat table for every iteration is slightly tedious. Using "proper" statistical software would defenitely be preferable, but i have to use Excel because it was a requirement from the agency that ordered the model i'm preparing.
Comment: I am not suggesting you do this for your work.  Where I wrote "compare" I meant only that you spend a couple of minutes generating a range of typical values in Excel and compare them against what ought to be the same values as found in a table or produced by other software. That would answer Q1. I don't see any point even to investigating Q2 unless you have to use legacy spreadsheets with legacy versions of Excel.
Comment: @whuber, ok, sorry for the misunderstanding. I have the impression that it's been established that excel has some issues related to the statistical armament and therefore won't pursue comparisons with external software. For those interested, this article provides a nice overview: [accuracy excel 2010](http://www.csdassn.org/software_reports/excel2011.pdf).Investigating Q2 is valuable if you want to find out whether GAMMAINV calls on the old/new algorithm.
Answer: In excel, the inverse gamma distribution need 3 parameters (probability, alpha and beta). To test potential differences between the GAMMAINV and the GAMMA.INV I designed two 2x2 tables (one for the GAMMAINV and one for the GAMMA.INV) where the y-axis contained probabilities ranging from 0,01 to 1 and the x-axis contained beta-values ranging from 0,01 to 1 (1 to 100 were also tested). This generated a table containing 10 000 draws for each version of the gammainv respectively. The alpha parameter where then varied for both tables with an arbritary selection of values between 1.00-E100 and 1.00+E100.
Observation: No differences between the GAMMAINV and the GAMMA.INV were detected and, further, no error messages indicated that excels iterative procedure had failed to reach convergence where present.
A similar analysis were conducted for the GAMMA.INV and the NORM.INV. In this case some minor differences between the distributions where detected. The largest difference between them was 1.48. This is approx 0.5% of the cut-off value of 300 as suggested in the method outlined above. The absolute difference seem constant across different values of the alpha parameter and its relative size diminish with increasing numbers of alpha.
Conclusion: In this simple analysis, i where unable to detect evidence that would motivate a substitution of the GAMMA.INV with the NORM.INV in excel 2010. Further, no differences between using the old GAMMAINV and the new GAMMA.INV commands were detected, this might suggest that they both call on the new algorithm.


Does every algebraically closed field with nonzero characteristic have a unique finite subfield $p^n$?

Question:
Let $F$ be an algebraically closed field such that $char(F)\neq 0$.
Then, $\forall n\in\mathbb{Z}^+$, there exists a unique finite subfield $K$of $F$ such that $|K|=char(F)^n$.

Is this statement true?
My text only introduces this for the algebraic closure of $\mathbb{Z}_p$, but it seems like the same proof works for any algebraically closed field.
Is this true?
Answer: Let $F$ be the field you are talking about, let $p$ be the characteristic of $F$. Then since $F$ is algebraically closed, the equation
$$
x^{p^{n}}-x=0
$$
has precisely $p^{n}$ solutions in $F$ because its derivative is $1$. The solutions would now constitute a subfield $F_{n}$ of $F$. Multiplication and inverse is trivial, and addition follows from the characteristic $p$ property.
Answer: Yes. Any algebraically closed field of characteristic $p$ contains $\mathbb Z/p\mathbb Z$, so it must contain the algebraically closure of $\mathbb Z/p\mathbb Z$, and the subfield of size $p^n$ must be all the roots of $x^{p^n}-x$.
So once you know the result for the algebraic closure of $\mathbb Z_p$, you know it for all algebraically closed fields of characteristic $p$.


Find the version of an ActiveX COM component

Question: I have a web application that uses an ActiveX COM component, for example:
<OBJECT id="SomeObject" style="VISIBILITY: hidden" etc...
    classid="clsid:0A6AD658-F144-4412-AB94-9C8F298973E1"></OBJECT>

I've produced the ActiveX control myself, and I want to be able to determine in JavaScript the version of the control that is installed and being used on the page.
I thought I could expose a method on the object than exposed the version number somehow, for example:

But that seemed really messy. I wondered if there was a built in COM / ActiveX mechanism for finding version numbers - whats the best way of doing this?
Answer: There is nothing in the ActiveX specs that has the construct of version, you can browse around and see that many ActiveX controls have Version property.
You can improve the code (a little) by providing a Version property instead of a method.
You can also provide a version in the codebase and know that you have at least the version ask for.


Regex expression to extra a fields

Question: Give the string below:
';\n;20121110_160816\n;www.google.com\n\n; Test\na=5\n\nend\n'

What is the regex to parse the string as the following fields? :
;\n
;20121110_160816\n
;www.google.com\n
; Test\n

My initial failed attempt is below:
import re
m = re.search(';.+?\\n', mystr)

outputting:
m.group()
';20121110_160816\n'

Thanks.
Comment: I don't have experience with regexes in Python, but I do know regexes in general, and I can tell you that you probably want .*? rather than .+? ... that is, if you want to also match the "empty" `;\n`. + requires one or more characters; * is zero or more.
Comment: `m = re.findall(r';.*?\n', mystr)`
Comment: Have you tried `s.split(';')` ?
Comment: @BurhanKhalid, I need regex for this problem. Thanks tough.
Comment: @Prince, that produces what I want, thanks.
Comment: Wouldn't it be easier to use `split()`? I don't see the need for a regex here...
Comment: Tim, a modified version of this regex goes to PLY's lexer. I don't know if this can be done with split().
Answer: import re
re.findall('(;.*)\n', s)

expression walk-through:

your item starts with a ";" -> ';
followed by practical any sign in any quantities -> ".*" the "."=any sign "*"=0-infinite times
it ends with a newline -> "\n"
for readability i put the repetition-grp into parenthesis

but in this case you should also consider:
s.split(), which splits automatically at newlines if not suplemented with an argument.
update - commenting your comment ;-)
I'm not sure if i really understand your question yobout the question mark. But in general the asterisks, question marks and plus signs are quantifiers. Similar to the ,   and -keys they don't have a meaning in themselves and are alone mostly worthless. But in combination with other key/chars they modify the behaviour of them, or in case of the quantufiers specify if and/or how many times the preceding char/group should be repeated.
Comment: r';.*?\n' produces the exact output, or r';.*\n'
without question mark; though not sure the role "?" here.
Comment: feel free to add this comment to your question as a follow up.

Regex expression to extra a fields


Loop when fetching COUNT in mysql

Question: $query_count="SELECT COUNT(*) FROM table";
$result_count = mysql_query($query_count);
$final_count=mysql_fetch_array($result_count);
echo $final_count[0];

This cause the website to "loop" (page never loads, browser runs out of memory). any idea how to do it?
Comment: Your website "loops?" What do you mean? Does the page never load?
Comment: exactly, browser takes most of my RAM.
Answer: I think it should be :
$query_count="SELECT COUNT(*) FROM table";
$result_count = mysql_query($query_count);
$final_count=mysql_fetch_row($result_count);
echo $final_count[0];

Comment: There's nothing in the original code that will 'loop', but in the original code `$final_count` would be an array and `echo $final_count` would simply print 'Array'. @Blackie123: Are you sure there aren't any infinite loops elsewhere in your script?
Comment: @Blackie123 I simply used `mysql_fetch_row` as we always expect one row, you can continue to use `mysql_fetch_array` if you want. The main difference is the last line. Chris is correct in pointing out that there is nothing in this part of the code which would cause the the page to go into an infinite loop, something else seems to be an issue.
Answer: try replacing echo $result_count; with echo $final_count;
EDIT - another option:
$query_count="SELECT COUNT(*) FROM table";
$result_count = mysql_query($query_count);
$final_count=mysql_fetch_field($result_count, 0);
echo $final_count;

Comment: that one causes the loop but how shall I find out the count other then that?
Answer: Try:
$query_count="SELECT COUNT(*) FROM table";
$result_count = mysql_query($query_count);
echo mysql_result($result_count, 0, 0);

Loop when fetching COUNT in mysql


Visualization of complex exponential and logarithmic map as $z$-plane to $\omega$-plane map.

Question: I am studying complex analysis and themain thing about complex analysis that disturbs me is the difficulty in visualization.I have studied exponential function and logarithmic function in complex analysis.Now I want to visualize them as maps from $z$-plane to $\omega$-plane.I want to know how these maps act on the complex plane.Can someone help me understand this with suitable diagrams or animations?
What I know about exponential map and logarithmic map is the following:
I know that exponential function defined on $\mathbb C$ is $2\pi i$-periodic and hence is not injective,so inverse does not exist.So,we cannot talk about $\log$ function,but exponential function is injective on each strip $S=(-\infty,\infty)\times (-\pi,\pi)$ which projects onto $\mathbb C-(-\infty,0]$.In fact the map $z\mapsto e^z$ is a covering map from $\mathbb C-\{(2n-1)\pi:n\in \mathbb Z\}$ to $\mathbb C-(-\infty,0]$.So,we can see that on each strip $e^z$ is a homeomorphism,in particular injective,so we can define the logarithmic function $\log:\mathbb C-(-\infty,0]\to S$.

Is this all correct?
Comment: How does diagram relate to your issue? I can explain it for you but I am not sure what exactly your issue is
Comment: Hmm... what exactly is the question still?
Comment: Hmm did I answer your question or not? I don't care much for reputation, only if it help you it matters. So if you could confirm it was clear everywhere. That'd be helpful to me.
Comment: @EthakkaappamwithChai Yes,ok
Comment: then I am happy. Good luck!
Answer:
Suppose you have some region $D$ in the complex planes, then you can 'mesh' it by putting two family of non parallel curves. In the picture you have shown, we have the curves of constant x and constant y. Once we have this, we can understand the complex function by the mapp of this whole mesh.

This is actually useful because it gives the characteristic property of analytic function: they preserve infinitesimal squares. In more specific words, suppose you make a mesh in which the whole domain is split into tiny squares, then you would find that the square is mapped to another infinitesimal square. For visualizing the mesh, you can check out this stack over flow post which uses python to show maps of the mesh.

On the point of inverses, it is somewhat complicated because there is a lot of aspect such as branch, branch cut, Riemann surfaces. But, the essential philosophy is that the natural inverse for the $e^z$ function is $\ln(z)$ as a multi-function (mf). THis is because the evaluate of $e^z$ repeats everytime we add $2\pi i$ to it's input, when we invert the function, we get all those values which differ by $2\pi i $ as for one input.

One may think we can say that at a point we can take one of the branchs eg: take one of the angle which correspond to that point for evaluating $\ln(z)$ but if we are to try consider a loop around the origin then again we would realize that the mf issue persists.

To truly dissect the mf into a single valued function, you need a branch cut. The branch cut involves removes removing points in the domain so that we can't even do the loop thing I said.

Once we put a branch cut, we can consider an argand plane for each branch. After this, we can stich together the different planes as a Riemann surface which finally makes our function single valued from a Riemann manifold to an argand plane.
Comment: Maybe typo and English mistake therei n the answer. Please comment if something didn't make sense or conceptually wrong
Comment: References: Tristan Needham's VCA. One issue is he doesn't discuss Riemann surfaces, but ig it is discussed any other standard book so


Push Workspace with multiple projects on Bitbucket repository

Question: I have Xcode workspace which has multiple projects - the main project and a pod project. How do I push the code (the first time) on Bitbucket so that everything is set on repository ? Attached is the project structure just for clarification

This is the folder structure I have
Answer: All you got to do is go to the Project folder in Terminal and follow the below commands in the same order. I am assuming both your main project and pod project are inside the same folder
So in terminal go inside your first AppIT folder. If you run command ls -l in terminal on that folder you will see a list of all folders and files inside it like AppIt, .xcodeProj file, Pod folder, Podfile etc. Just run the below commands on that location where you see this list.
git init
git add -A .
git commit -m "first commit"
git remote add origin https://github.com/.... .git // your complete URL
git push -u origin master

Comment: No. the main and pod projects are in different folder.
Comment: And you want them to be committed in 1 Repository? Then you would have to create a parent folder which contains both of these and then create a repo inside the parent fodler
Comment: Edited my question and added screenshot showing folder structure.
Comment: Man this is in the same folder so you are lucky.  Go till your AppIT folder which is the main folder and inside it run the above commands. It will commit all files including your project and pods
Comment: On git commit -m "first commit" I have getting the following message : Untracked files:
 AppIT.xcodeproj/

 AppIT.xcworkspace/
 AppIT/
 Build/
 Podfile
 Podfile.lock
 Pods/
nothing added to commit but untracked files present
Comment: Yayyyy. That worked. Personally speaking, this is far more easy that setting up from Xcode.
Comment: Out of curiosity, what does **git add -A** do ? Will this be required even if I have a single project ?
Comment: Yes it will be required. This adds all your files to be tracked for git repo. if you do -A . that means all files in the folder, if you say -A filename, it will only add that file. You need to do this everytime if you are adding new files to repo.


How do I find the file without a file type?

Question: My question is the same as this one but for c#: PHP, get file name without file extension
Please see the code below:
[HttpGet("{id}")]
public ActionResult<byte[]> Get(Guid id)
{
    return System.IO.File.ReadAllBytes("Pictures\\" + id );
}

The filetype e.g. jpeg, gif etc is unknown.  How do I find the file without a file type?
Do I have to insist that all images are submitted in a particular file format?
What is the most elegant way of doing this?
Answer: A naive approach it would be to read the filenames in a directory and then read the file that match the one you are looking for.
// You might have to change the location of the directory
var files = Directory.GetFiles(@"Pictures\");

foreach(var file in files)
{
    if(file.Contains(id.ToString()))
    {
         return System.IO.File.ReadAllBytes("Pictures\\"+file);
    }
}

Calling id.ToString() might not be that you are looking for. You should change this appropriately based on the name of your files. E.g. you could have a guid with hyphens 7c7cdab9-0db7-4ad1-93c1-96ddc92aadaa or a guid without hyphens 44e0391b22bb48018aa6aab517a59238. Here you will find all the corresponding formatters, in order you pick the one that fits in your case.
Comment: Thanks +1 for the last paragraph.  Could you confirm that there is nothing wrong with just calling id.tostring i.e. the way I understand it is that the filename will just contain hyphens.
Comment: Thanks.  Do you know an approach to find the file that is not naive?
Comment: You are welcome. Nope, had I known, I would have shared it. I characterize it as a naive approach, since this definitely does not seem to me the best approach, since each time you need to read a file, you have to enumerate all the files in a directory, whereas you just need to read one file.
Comment: I guess I could just store the file on the server without a file extension and always serve jpegs.  Is there a flaw with this approach?
Comment: @w0051977 You are welcome. As you can see here https://referencesource.microsoft.com/#mscorlib/system/guid.cs,a6547a472def7796,references, this call returns the guid in "registry" format, with hyphens.
Comment: @w0051977 If you know that the files are jpegs, I don't see any benefit or at least I am not aware of any benefit of not storing the files with an extension. Furthermore, I would rather say that might be a good practice to request from your users to post images with specific formats (e.g. jpeg, .png).
Answer: A somewhat better approach than the above would be to use the overload that allows patterns:
var files = Directory.GetFiles("", $"*{id}*") (the asterisks, of course, can be omitted as needed)
You still have the same issues with the exact Guid format, but at least you are not fetching all the file names in the directory.
If you expect to have many files in the directory it is better.


Is $i\notin \mathbb{Q}(\zeta_p)$ for all odd primes $p$?

Question: My main question is the title: for an odd prime $p$, denote a primitive $p^{\text{th}}$ root of unity by $\zeta_p$.  Is it true that $i$ is not contained in the cyclotomic extension $\mathbb{Q}(\zeta_p)$? If this is true, is the following proof correct, and if it is not true, where does the following proof break down:
Recall that the unique quadratic subfield of $\mathbb{Q}(\zeta_p)$ is $\mathbb{Q}(\sqrt{\pm p})$ where there is a $"+"$ is $p\equiv 1 \mod 4$ and a $"-"$ if $p\equiv 3 \mod 4$ (Source: exercise 11 of section 14.7 of Dummit and Foote). Assume to the contrary that $i \in \mathbb{Q}(\zeta_p)$. Then $\mathbb{Q}(i)$ is a quadratic extension of $\mathbb{Q}$ of degree 2 contained in $\mathbb{Q}(\zeta_p)$.  But this yields an immediate contradiction since of course $\mathbb{Q}(\sqrt{\pm p}) \neq \mathbb{Q}(i)$. So $i\notin \mathbb{Q}(\zeta_p)$.
Comment: If $i \in \Bbb Q(\zeta_p)$, then $2$ would be ramified in $\Bbb Q(\zeta_p)$ (since it is ramified in $\Bbb Q(i)$), which is not possible as only $p$ ramifies in $\Bbb Q(\zeta_p)$ (and $p$ is odd).
Comment: looks good to me
Comment: I think your proof looks fine..
Answer: Another proof: if $i\in{\mathbb Q}(\zeta_p)$ then ${\mathbb Q}(i\zeta_p) \subseteq {\mathbb Q}(\zeta_p)$. But $i\zeta_p$ is a primitive $(4p)$th root of unity, so we have a field of degree $\phi(4p)=2(p-1)$ over $\mathbb Q$ contained in a field of degree $\phi(p)=p-1$ over $\mathbb Q$, a contradiction.
Comment: Since $p$ is odd, $2p\equiv2\pmod 4$ and thus $(i\zeta_p)^{2p} = i^{2p}(\zeta_p^p)^2 = -1\cdot1\ne1$.
Comment: Hi Greg .Here Im not getting why  is $i\zeta_p$  a primitive $(4p)$th root of unity ?  why not $i\zeta_p$  is a primitive $(2p)$th root of unity ?
Comment: okay @Greg Martin Thanks You Sir.


O que eu preciso é pegar a chave primaria da tabela Telefone e inserir na FK da tabela Cliente. Como faço?

Question: CREATE TABLE Cliente (
    id_Cliente INTEGER PRIMARY KEY IDENTITY(1,1),
    nome_Cliente VARCHAR(50),
    fk_ID_Telefone INTEGER
)

CREATE TABLE Telefone (
    id_Telefone INTEGER PRIMARY KEY IDENTITY(1,1),
    fone_Telefone VARCHAR(11)
)

Comment: quando for fazer um pergunta veja como formata-la para ficar mais organizado.
Comment: Como dito na resposta você pode fazer um `insert` com `select` mas para isso você deverá especificar a qual cliente pertence aquele telefone. Não sei se é realmente isso que você quer. Tente explicar novamente o que você precisa e demostrar através de exemplos.
Answer: Fazendo um INSERT com SELECT:
INSERT INTO Cliente (fk_ID_Telefone) SELECT id_Telefone FROM Telefone

Mas se for um UPDATE com SELECT:
UPDATE Cliente a
JOIN Telefone b
SET a.fk_ID_telefone=b.id_Telefone

Referência do INSERT com SELECT
Referência do UPDATE com SELECT


Do non-parametric tests depend on homoscedastic data?

Question: I am finding myself having to perform non-parametric tests, primarily Mann-Whitney tests, on datasets which are not normally distributed.
I notice that the datasets are not homoscedastic - do these non-parametric tests, such as Mann-Whitney tests, require homoscedastic data?
Comment: If you do a 2-sample Mann-Whitney-Wilcoxon (signed rank test) for the purpose of determining whether two samples have the same median, then the two samples should be of the same shape (possibly differing by a shift in location). 'Same shape' implies 'same dispersion'. If the two samples have different dispersions (variances), then the M-M-W can be interpreted as a test whether one sample 'stochastically dominates' the other (tends to have consistently larger values than the other). But with heteroscedasticity it is not appropriate to talk just in terms of different variances.
Answer: Simple shift. Consider two non-normal samples of the same shape, but with different medians. [Sampling and computations in R.]
set.seed(2020)
x = rgamma(50, 5, 0.1)
y = rgamma(70, 5, 0.1) + 10
summary(x); length(x); sd(x)
    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  17.48   35.56   48.47   49.92   57.80  161.96
[1] 50          # sample size
[1] 23.32434    # sample SD

summary(y); length(y); sd(y)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  22.84   39.90   56.45   60.10   66.46  173.68
[1] 70
[1] 28.04877

stripchart(list(x,y), ylim = c(.5,2.5), pch=20)

The stripchart illustrates and the test confirms at the 5% level
that the two samples have different locations. So we can say
that the median 48.44 of Sample x is significantly different from
the median 56.45 of sample y.
wilcox.test(x, y)

    Wilcoxon rank sum test with continuity correction

data:  x and y
W = 1345, p-value = 0.0313
alternative hypothesis: true location shift is not equal to 0

Different shapes. Although the Wilcoxon signed rank test was originally intended to test for a shift in location, resulting in a difference in medians, it can also be used to detect if one distribution 'dominates' another (tends to give larger values).
Consider the following two samples from beta distributions of
different shapes. Medians are obviously different and variances
are essentially the same.
set.seed(2222)
y1 = rbeta(100, 2, 5)l  y2 = rbeta(100, 5,2)
median(y1); median(y2)
[1] 0.2749052
[1] 0.7259123
sd(y1); sd(y2)
[1] 0.1785754
[1] 0.1538957

Boxplots show that one distribution is right-skewed (bottom)
and the other is left-skewed. Medians are different. Notches in the sides of the boxes show nonparametric confidence intervals
calibrated so that if two of them don't overlap (as here) then the medians
may be significantly different.
 boxplot(y1,y2, notch=T, horizontal=T,
         col=c("wheat","skyblue2", pch=19))

The Wilcoxon signed rank test finds a significant difference between
the population distribution from which the samples were drawn (P-value near 0), but
the difference is not so simple as recognizing that the medians differ. [The output in R always mentions 'location shift' as the
alternative hypothesis, but sometimes one needs to think carefully about the reason for rejection when the P-value is small.]
wilcox.test(y1,y2)

        Wilcoxon rank sum test with continuity correction

data:  y1 and y2
W = 581, p-value < 2.2e-16
alternative hypothesis: true location shift is not equal to 0

Here one could say that the second population 'dominates' the first.
Roughly, this means that the second population tends to have larger values. There are several kinds ('orders') of stochastic dominance, and you can google for explanations that match your theoretical level. One method of judging stochastic dominance is that the
empirical CDF (ECDF) plot of the dominating sample falls below and to the right of the ECDF plot of the other.
plot(ecdf(y1), col="brown",
     main="ECDF plots: Blue dominates")
 lines(ecdf(y2), col="blue")

[Note: To make the ECDF of a sample of size $n$, sort observations from smallest to largest. The ECDF starts at 0 on the left at $0$ and
jumps up by $1/n$ at each sorted value, reaching $1$ at the right. If there is a tie with $k$ observations at the same value, then the jump at that value is $k/n]$
Different dispersions. The Wilcoxon signed rank test may or may not detect a difference between two populations if their samples have markedly different variances. Below the one sample (larger median) clearly dominates the other and the Wilcoxon test rejects.
set.seed(111)
x1 = runif(100, 2,3)
x2 = runif(100, 2,3.5)
median(x1); median(x2)
[1] 2.468052
[1] 2.845346
sd(x1);  sd(x2)
[1] 0.2757188
[1] 0.44725

boxplot(x1,s2, notch=T, horizontal=T,
        col=c("wheat","skyblue2"), pch=19))

    Wilcoxon rank sum test with continuity correction

data:  x1 and x2
W = 3067, p-value = 2.337e-06
alternative hypothesis: true location shift is not equal to 0

plot(ecdf(x2), col="blue",
     main="ECDF plots: Blue dominates")
 lines(ecdf(x1), col="brown")

However, if the two populations were $\mathsf{Beta}(2,2)$ and
$\mathsf{Beta}(7,7),$ then one would not expect a two-sample Wilcoxon test to reject. Both distributions have median $\eta =1/2,$ so
sample medians should not be significantly different.
par(mfrow=c(1,2))
curve(dbeta(x, 2,2), 0, 1, col="brown", lwd=2, ylab="PDF", xlab="W1",
      main="Density of BETA(2,2)")
 abline(h=0, col="green2")
curve(dbeta(x, 7,7), 0, 1, col="blue", lwd=2, ylab="PDF", xlab="W1",
      main="Density of BETA(7,7)")
abline(h=0, col="green2")
par(mfrow=c(1,1))

For the samples below from these two distributions, sample medians are about the same and sample interquartile ranges (IQRs) differ considerably (about 0.26 and 0.19, respectively).
set.seed(1228)
w1 = rbeta(100, 2,2);  w2 = rbeta(100, 7,7)
median(w1); median(w2)
[1] 0.5360535
[1] 0.4734114
IQR(w1); IQR(w2)
[1] 0.2640769
[1] 0.192385
boxplot(w1,w2, notch=T, horizontal=T,
        col=c("wheat","skyblue2"), pch=19)

The P-value of the Wilcoxon test is 0,1136 (not significant at 5%).
w = c(w1,w2)
g = rep(1:2, each=100)
wilcox.test(w~g)

        Wilcoxon rank sum test with continuity correction

data:  w by g
W = 5648, p-value = 0.1136
alternative hypothesis:
 true location shift is not equal to 0

If the main issue is whether one population is more disperse than
another, then a nonparametric test of dispersion would be more
appropriate than a Wilcoxon signed rank test, which is a test of location.
One possible test whether the two samples have significantly
different dispersions is a permutation test using the ratio of
the two sample IQRs as the metric. The observed value of this metric for our two samples is $1.373.$ For the permutation test, we randomly permute the 200 observations between the two groups (100 in each) and
compute the IQR-ratio again. After many iterations we get a
good idea of the permutation distribution of the IQR ratio under the null
hypothesis that the the two populations are equally disperse.
rto.obs = IQR(w1)/IQR(w2);  rto.obs
[1] 1.372648
set.seed(33)
m = 10^4;  rto.prm = numeric(m)
for(i in 1:m) {
 w.prm = sample(w)
 rto.prm[i] = IQR(w.prm[g==1])/IQR(w.prm[g==2])}
mean(rto.prm > rto.obs)
[1] 0.0244

The ratios from permutation are seldom as large as the observed
ratio. So a one-sided test that the first sample is more
disperse than the second rejects the null hypothesis at the the 3% level of significance.
hdr="Permutation Distribution of IQR Ratios"
hist(rto.prm, prob=T, col="skyblue2", main=hdr)
abline(v = rto.obs, col="red", lwd=2, lty="dashed")

Reference: One elementary paper on permutation tests is Eudey (2010).
Comment: Thank you very much for the clarity and depth of your answer+edits, and a happy holidays to you too :).
Comment: I was interested in both cases @BruceET, but your answer was helpful to me in clearing my misunderstandings and allowing me to understand both in turn. I may have accepted the answer prematurely for the question, but I believe I realise now that my initial question wasn't really what I needed to know in the first place.
Comment: It seems to me your comment answers the question but the answer does not.
Comment: @RichardHardy. To clarify: I considered my Ans to be a continuation of my very long Comment for the case where there is a true shift  (even if slightly different SDs).  Was going to illustrate different shapes, but got acceptance and upvote before I got to that, so I figured OP is mainly interested is the pure shift case. [See here](https://stats.stackexchange.com/questions/502584/appropriate-statistical-test/502595#502595) for case with different shapes.
Comment: I see. I just thought the question was about heteroskedasticity but the answer did not discuss that. And just now I realized in addition that heteroskedasticity was probably meant across samples rather than within, an unsual use of the term.
Comment: @RichardHardy.  Thanks. // Anyhow, it's probably best if I add parts to show 2 samples with obviously different variances and two with obviously different shapes. // Increasingly often, it seems I'm getting comments and votes on Answers from several years ago, so it might not hurt to give the full story.
Comment: Upvotes are always appreciated ('premature' or not). Mainly just happy your key issue was settled. Added some additional examples to my Answer; look at them if interested. // Happy Holidays!
Comment: The R Wilcoxon test function above seems to have a null hypothesis that the distributions are equal and an alternative hypothesis that the location shift is non-zero. Is this standard, or do other software implement an alternative hypothesis of the shift is non-zero and potentially the shape or variance is different also?
Comment: @SingleMalt: Here's my opinion: Traditional test is for two samples of same shape. Null is that locations are the same; alternate is shift. As originally conceived this amounted to equal medians vs unequal. // Current views recognize that the effect of looking at rank sums is to test whether populations are same vs.one 'stochastically dominates' the other (with various definitions of 'dominate'}. Alternative is _not_ 'different shapes', unless that amounts to 'dominiation'. Alternative is certainly not different dispersions (measured by variance or otherwise). Other opinions differ somewhat.
Comment: @BruceET that opinion makes a lot of sense. Stochastic dominance is probably under-utilised information content from this test (either evidence for or against). Statistical software should give more information in the help files or interactively, especially since statistics is inherently nuanced.


csf stop emails related to Process Tracking

Question: I have enabled LSF with CSF since I would like to receive some informations but I am being bombed by the CSF with similar emails:
<code>Subject: lfd on [Hostname]: Excessive resource usage: www-data

Time: Sun Jan 19 21:29:17 2014 +0100
Account: www-data
Resource: Virtual Memory Size
Exceeded: 279 > 200 (MB)
Executable: /usr/lib/apache2/mpm-itk/apache2
Command Line: /usr/sbin/apache2 -k start
PID: 22991 (Parent PID:12217)
Killed: No
</code>
I have disabled the Process Tracking section in csf.conf and still receiving these logs.
<code># Set the value to 0 to disable this feature
PT_LIMIT = "0"

# How frequently processes are checked in seconds
PT_INTERVAL = "3600"
</code>
I have checked many times csf.conf but cannot find the right place to stop these logs. Restarting csf does not help... Anyone knows if this is a peculiar configuration?
Answer: Add cmd:/usr/sbin/apache2 -k start
to csf.pignore file and restart CSF and LFD (you might need to kill the lfd process)
Answer: This might be an old thread but did you try running csf -r to reload the config? OR if you want to permanently stop emails you can change LF_PERMBLOCK_ALERT from 1 to 0
Answer: sometimes restart the csf not solving your issue.
I have try change the email like email@example.com to firstname.lastname@example.com. but alert always sending to firstname.lastname@example.com
just restart your server and then will be fix soon


Multiple rules files in prometheus for each targets

Question: I have configured some node in json different-e file based on categorized. & integrated with Prometheus.yml file. i want to use rules.yml with respective json not for global.
Here below is my prometheus.yml code
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
   - "/etc/prometheus/rules.yml"
  # - "first_rules.yml"
  # - "second_rules.yml"

  - job_name: 'Prometheus Node Exporter'
    scheme: https
**// i want to use seprate rules.yml file for this specific node_exp.json file
   tls_config:
      ca_file: node_exporter.crt
      insecure_skip_verify: true
    file_sd_configs:
      - files:
        - node_exp.json

Answer: No you can't specify rules for some jobs by putting them in that jobs segment.
But you can add label filter to metrics used in your rules.
For example:
groups:
  - name: example
    rules:
    - record: code:prometheus_http_requests_total:sum
      expr: sum by (code) (prometheus_http_requests_total{job="Prometheus Node Exporter"})

will calculate code:prometheus_http_requests_total:sum only for metrics prometheus_http_requests_total scraped by job Prometheus Node Exporter


determine cube orientation given one side in a perspective projection

Question: Suppose that we are given an arbitrary quadrilateral T that does not have any parallel edges. I want to draw a cube in a three-point perspective projection such that T is one of its sides. The projection parameters are not given. Is the solution always unique? How to find it?
Answer: Yes, it is always possible, and no, the solution is not unique. There is a simple construction that will give the locus of solutions.
The quadrilateral $T$ will determine two vanishing points, $L$ and $R$. The key is then to look for the vertical axis point, $\textit{VAP}$, from which all angles to the line through $LR$ are true angles. (This is analogous to the station point in one-point perspective.) Observe that $\textit{VAP}$ will lie on the circle with diameter $LR$, for the angle subtended by $LR$ from $\textit{VAP}$ must be a right angle.
Next observe that since $T$ is square, the same applies to the diagonals. In other words, extending the diagonals to the horizon through $LR$ gives two additional vanishing points, and $\textit{VAP}$ must be on the circle having those points as diameter.
It follows that $\textit{VAP}$ is on the intersection of these two circles. Now drop a perpendicular from $LR$ through $\textit{VAP}$ and choose a third vanishing point $V$ on that line; this is the locus of solutions. ($V$ should of course be far enough away such that $LVR$ is an acute triangle, which is to say that it should be on the far side of $\textit{VAP}$.) You may then proceed to construct the remainder of the cube.
Answer: Two opposite sides of the quadrilateral, if prolonged, meet at a vanishing point. So two of your three perspective points are easy to find and the third one should be arbitrary. But it is not true that every quadrilateral can be the face of a cube, see for instance the image below. If $ABCD$ is your quadrlateral, $E$ and $F$ are two vanishing points, while $H$ and $I$ are the corresponding measuring points ($FH=FG$ and $EI=EG$). But for the edges $BC$ and $CD$ to be equal, you should have $KC=JC$, which is not true.
Comment: Is the third perspective point really arbitrary? For example, when given a kite, I would expect the perspective point to be on its axis.
Comment: Right: I meant "arbitrary" only in the sense that its distance from $C$ is not fixed, but the third point should lie on line $CG$.
Comment: This doesn't seem right. It looks like you've arbitrarily dropped a perpendicular through $C$ to locate the third vanishing point, but they won't in general line up like this.
Comment: @Théophile You are right: I mixed up two-point and three-point perspective. But I cannot delete my answer because it was accepted.


Let users download entire folders (zip first) or individual files?

Question: Is there a module or a series of modules that will allow users to download a folder of files (e.g. construction blueprints and sketches) or just individual files via the browser?
The files for user A should be only accessible to user A.
Answer: I suspect that the functionality that you are looking for exist in the elFinder file manager module. It allows downloading individual files and whole folders after compressing them first.
You can manage the roles that you allow to download specific files with the native drupal permission system. Just make sure that you don't give your users access to sensitive files in drupal installation.


Should the fumble rate of NFL teams be a normal distribution?

Question: There's a lot of garbage statistics going around the internet regarding the Patriots, but I was just curious what the well-versed in statistics have to say about this.
The main question is - in what cases would distribution of season fumbles (y) by NFL team (x) be a normal distribution? In what cases would it not?
My thoughts are simply -- it would be a normal distribution if the fumble rate for each team per game/ season is equal (which is a big assumption).
It would appear less and less normal the more of an influence that non-random, independent factors --- such as player skill, play-calling, coaching, incentives, home field qualities, potential cheating, or whatever else -- had on fumble rates.
Is my thought process correct or not?
I've also heard a lot of people commenting that the distribution of fumbles per season by NFL team is actually a Poisson distribution.
I've dealt with Poisson distributions before, and this seems preposterous and completely off-base, though I could be wrong. Isn't a Poisson distribution usually uses in entirely different cases? I thought it was used for modeling when a call might come in within the next hour, or when a dice might come up 6 after N tosses. I can see this modeling fumble distribution of N plays, but comparing the NFL teams season fumbles?
Any ideas appreciated --- I'm not super into this media issue --- I'm barely into football -- I was more interested in the numbers. I don't even think the data in question here (that the Patriots fumble rate is an outlier) was even gathered or appropriated managed to answer the right question in any case.
Comment: What or who are the NFL and the Patriots? I don't really care, and it appears to be just detail here, but I feel obliged to flag that this is an international forum and that it's best not to assume that what is well known locally is well known globally.
Comment: Also, are you referring to the distribution of an individual teams fumbles per game per season, or of all teams per game in a particular season, or of all teams per game over time?
Comment: Essentially, for context, data was produced (possibly bogus) showing that a sports team, named the Patriots, were an outlier in terms of "number of fumbles per season." A fumble is basically a significant event in the game where a player carrying the football gets hit and loses the football. It's considered a relatively random, or unpredictable, event -- although that is more of what's in question. The "# fumbles" charts by # teams depicts a relatively normal distribution, with one team - accused of cheating -- as a significant outlier. Some claim this isn't meant to be a normal distribution.
Comment: I believe this is referencing these two blog posts: [1](http://www.sharpfoo2tballanalysis.com/blog/2015/the-new-england-patriots-prevention-of-fumbles-is-nearly-impossible) and [2](http://www.sharpfootballanalysis.com/blog/2015/the-new-eng;land-patriots-mysteriously-became-fumble-proof-in-2007)
Comment: Poisson distributions model counts of rare events. ("Rare" would typically be less than a $10\%$ success rate out of each trial, often orders of magnitude less.)  Historically the Poisson distribution came to fame when Ladislaus Bortkiewicz used it to model occurrences in which [Prussian soldiers were kicked to death by horses](http://en.wikipedia.org/wiki/Ladislaus_Bortkiewicz). I see a striking parallel with how NFL football is played :-).
Comment: Thanks Affine; now we see it's an inverse distribution: Touches per Fumble puts the fumbles in the denominator, and the article's shocking change from 39TpF to 76TpF is actually a within-measurement-noise change from 2.5% down to 1.3%
Comment: [1](http://www.sharpfootballanalysis.com/blog/2015/the-new-england-patriots-prevention-of-fumbles-is-nearly-impossible) again, accidentally failed at URL copy paste.
Comment: Still not sure where a Poisson distribution fits in, but yeah wasn't 100% on the data integrity -- it's unclear whether that data is total fumbles or fumbles lost, which the former would be more relevant in trying to establish supposed "cheating". The data likely doesn't show anything of terrible note, I guess I just need to study a bit more on Poisson distributions - I thought they were used for separate problems entirely.
Answer: Theoretically, a normal distribution has a nonzero probability of negative numbers. So that's right out. A normal also has fully continuous distribution, whereas fumble rates would be discrete or rational.
It could be very close, and good enough, for example, the sum of many binomials (had a fumble or didnt with x% chance, summed across 100 games) approaches what looks like a normal bell curve.
People go to poisson because it is a discrete counting variable, with integer results defined from independent results; that is to say if each play had a consistent fumble probability, then over 100 plays the final outcome fumble count would be poisson distributed.
If there's any correlation within the ranks then it wont be any theoretical (clean) distribution.  If for example having a lot of fumbles reduces your total number of plays in that game, then it's a self-correlated score and things get messy. I do believe if your first dozen plays all had a fumble (not likely but possible), then you might not get any more. It's definitely not an independent sum of even probabilities.
If the coach is allowed to remove a player who has had several fumbles, then the rate would decrease from that point on, another non-independence of the score.
The real observed distribution sure could look a lot like a normal in any event. Do you have any data we could play with?
EDIT: We see some data at this link:
http://www.sharpfootballanalysis.com/blog/2015/the-new-eng;land-patriots-mysteriously-became-fumble-proof-in-2007  Thanks Affine for finding that.
And in that article the claim is made more explicitly:  "Based on the assumption that plays per fumble follow a normal distribution, you'd expect to see, according to random fluctuation, the results that the Patriots have gotten since 2007 once in 5842 instances."
Which is a malformed hypothesis, you'd never care about the probability of an exact answer, the question of interest is how likely is any result this extreme OR HIGHER, combined. A point result has an extremely rare probability, but if there's a fat tail to the distribution, then perhaps more extreme results can happen, and the outlier event is really not so extreme. As this is an inverse distribution, Touches per Fumble, consider both variables as random poisson, you get so many touches per game and you see so many fumbles per game.  The ratio will have a long tail, because it's possible to have many many touches with few fumbles.
The outlier is to be expected, even looking at the previous decade's results, there was an outlier at 56 TpF which didn't get any comment from the blog author.
Comment: I would have to dig for clean data myself, as most of the popular stuff being passed around is not only poorly defined by also by people with an agenda. Actually, to clarify, the fumble rate is being analyzed, not the total number of fumbles. My mistake. It's the total fumbles over total plays run by a particular offense. I'm still not clear why 'fumble rate' or fumbles/ plays (y) by Team (x) would resemble a Poisson distribution. It still seems like it would be something of a binomial distribution if each team had (roughly) the same general fumble rate over a season.
Comment: It seems logical that some players are more fumble prone than others, but why would this factor not be evenly distributed among the teams over a set of years? Perhaps it is, but perhaps it isn't. A similar argument can be made for benching a fumble-prone player. Surely there are differences in decision here, but not major. Say most teams would bench a player after 2-3 fumbles. Is that minor difference ... enough to offset the, by comparison, much larger influence of random factors in shaping the normality of the distribution?
Comment: A rate can't be Poisson by definition. Poisson is a counting result, like how many goals did a soccer team get? It will be (0,1,2,3, ...) and never 0.14 as a fumble rate would be. The same argument applies for a Normal/Gaussian, because it defines a positive probability to results outside the [0,1] range where your rate should sum 100% probability. That's semantics though, the question is probably about how symmetrical are the results about the mean?
Comment: @JohnBabson: Plays are not called at random. A team that emphasizes passing will tend to throw the ball away when plays fall apart, which means a fumble is not possible. We would also expect fewer fumbles from teams that run in such a way that unexpected hits to their ball carriers are rarer. It doesn't "equal out" over teams and seasons; it's correlated to playing style, the coach's strategy (and what talent they have available to pursue that strategy), plus how much the coach hates turnovers. No coach likes them, but some coaches will bench you for the game, and some for the season.
Comment: I find how popular culture in the US tend to transform statistics a bit strange ... miles per gallon ... touches per fumble, it's like we in the US prefer the 1/x transform of the more meaningful variable x.
Answer:
would distribution of season fumbles (y) by NFL team (x) be a normal distribution?

Under no circumstances is a non-negative and discrete random variable actually normal.
In some circumstances (discreteness aside) it might not be terrible as an approximation, but it wouldn't be the first approximation I'd look at.

"it would be a normal distribution if the fumble rate for each team per game/ season is equal"

-- no, that doesn't do it ... though homogeneity might lead to a less skew distribution than otherwise.

"the distribution of fumbles/ season by NFL Team is actually a Poisson distribution"

-- well, at least it's not immediately ruled out by the domain of the variable, but (except perhaps as a rough approximation) I would think it would be readily rejected as a possibility; I expect that heterogeneity (across team make-up, opposition, conditions etc) would make it more heavily skew; there may also be a possibility of some serial dependence (outside that caused by intermittent changes resulting from heterogeneity).

"* for modeling when a call might come in within the next hour, or when a dice might come up 6 after N tosses*"

when a call might come is continuous, so no.
"when a die might come up 6..." -- again, no. Your description of what the random variable will be isn't completely clear there, but that sounds like one of "number of tosses to the first 6" (a geometric distribution), "number of tosses to the Nth 6" (a negative binomial) or "number of 6's in N tosses" (a binomial) -- but even if you meant something else, it still won't be Poisson. (Note that 'dice' is plural, 'die' is singular, so only ever 'a die'. You need at least two of them to have 'dice')

By comparison, the "fumbles per season" one being Poisson is at least plausible as a suggestion, but I think for a variety of reasons it won't be Poisson either.
Comment: I think I may be confusing Poisson distribution and exponential distribution like illustrated by this chart here: http://www.statlect.com/uddpoi1.htm    --- Thanks for the explanation, though, I may need to read a bit more on Poisson.
Comment: Ah. That would certainly be a plausible model for the "time to next phone call" variable. (Probably not for the die one though, since the tosses are a counting process.)
Comment: I think the exponential distribution is more like (probability of the first six coming up in next N tosses) as opposed to a call arriving in the next N minutes. Aka the probability rises geometrically with N, is the same at each point at time, and is memory-less.
Comment: "first six coming in the next N tosses" is discrete, while the exponential distribution is continuous, so no. You can read more about all the distributions we've named between us (exponential, gamma, normal, Poisson, negative binomial, geometric) on Wikipedia (and elsewhere on the internet)
Comment: Yeah, perhaps I made it up -- I think it just helped me conceptualize why the exponential distribution of next call arrival looked the way it did, even if that example may technically not be an exp. distribution.
Comment: This is a useful (productive, helpful) question, by the way, touching on a lot of relevant issues relating to modelling.
Comment: Play call "arrivals" are _not_ continuous. The clock time and field position are continuous covariates; calls themselves are discrete events once per play.
Comment: That is, the unit of time should be "plays" and not "time"


How to get last Git tag matching regex criteria

Question: I need Git command to get/find last tag starting with 'v' to get last versioning commit (I am using tags with v letter at the beginning to tag next application version (example: v0.9.1beta).
Is there any way to do it?
Comment: Can someone please edit the title, as for this it is not needed to really use a regex. Also all the answer just use the match and "glob" pattern. thx.
Comment: Can you just use `git describe`?
Comment: I'm looking for the answer to the question in the title (regex not glob)!
Comment: @RobFisher [There](https://stackoverflow.com/a/42131335/321973) you go
Comment: Yep .. git describe --match v* was what I needed. Thank You.
Comment: @RobFisher Maybe add a bounty ;)
Comment: @d4Rk Renaming the title would be an option. I [answered](https://stackoverflow.com/a/42131335/321973) instead...
Answer: Use the following command:
git describe --tags --match="v[0-9]*" HEAD

It will also modify the version if you did something with the source tree since your last versioned tag.
NOTE: --match accepts a glob, not a regex, and therefore the command shown may match other tags, e.g. v1234_this_matches_too.
Comment: If you want just the tag use the "short output" with `--abbrev=0`.
Comment: The [`git describe` doc](https://git-scm.com/docs/git-describe) says that `--match` takes a glob, not a regex.
Comment: The question is asking to find the last *tag* matching the pattern. So I think `--tags` must be added at the end. Otherwise only annotated tags will be shown.
Comment: @CraigMcQueen that is a glob, and that's why it is working and filters out `v1.0.0` and not only `v1234`. I added a note to clarify that.
Comment: In case one really needs a RegEx, I added [another answer](https://stackoverflow.com/a/42131335/321973)
Answer: git tag -n v*

-n means --list is implied.
Comment: It lists all tag matching criteria. I need only last one, so git describe --match v*. Anyway thanks for hints as tags and describe command are linked together on the git manual pages.
Comment: this worked for me, as `git describe` wasn't flexible enough (returned an ugly tag, and wasn't always the most recent).  `git tag --list --sort=-version:refname "v*" | head -n 1`
Answer: The problem with using git describe as the other answers do is that git describe will show you tags that are reachable from HEAD (or the commit you specify.)
Imagine you have 3 tags, v1, v2, and v3. If HEAD is at a point between v2 and v3, git describe would return v2 rather than v3.
If you actually want the latest tag, first of all you need annotated tags as lightweight tags have no date metadata.
Then this command will do it:
git for-each-ref --sort=-taggerdate --count=1  refs/tags/v*
Answer: Also with git describe you could get the latest tag not just reachable from HEAD with :
git describe --match "v*" --abbrev=0 --tags $(git rev-list --tags --max-count=1)
Answer: KARASZI István's answer already explains how to find tags matching a glob, which is usually fine enough. Should you ever need a real RegEx though:
for tag in $(git tag | grep YOURREGEX); do
    git describe --tags --long --match="$tag" 2>/dev/null
done | sort -k2 -t"-" | head -n1

If you only want the nearest tag (i.e. what --abrev=0 would achieve), append | cut -d"-" -f1
If you don't want --long's behaviour of also outputting an exactly matching tag including the -0-hash part, append | sed -e's/-0-.*$//' instead.

The question's example tag would probably use a Regex ^v\d+\.\d+\.\d+\D*$ (though the \D*$ might be optional).
Comment: For my usecase (see regex) with inconsistent tag naming (with or without prefix) this helped a lot, and only uses one execution of git describe.
`git describe --abbrev=0 --tags --match="*$(git tag | grep -E "^(releases?\/|v)?[0-9]+\.[0-9]+\.[0-9]+$" | sed -E 's/.*([0-9]+\.[0-9]+\.[0-9]+)$/\1/' | sort -V -r | head -n 1)"`. Maybe this helps for similar further usecases.
Comment: ...though this is obviously worse in terms of performance, since `git describe` is run for _each_ tag matching the RegEx.
Comment: @qwc thanks, the command helped me to get the latest git tag with certain features.
Comment: @qwc
thanks, the command is working, but
can you provide us a brief explaination about your command so that we can use in other use cases as well.
Comment: @Madhan I guess my answer deserves an edit when I have some time, please ping me if I forget to do so by next week ;)
Answer: While a single pattern --match "v[0-9]*" is enough here, know that Git 2.13 (Q2 2017) will improve that:

"git describe" and "git name-rev" have been taught to take more than one refname patterns to restrict the set of refs to base their naming output on, and also learned to take negative patterns to
name refs not to be used for naming via their "--exclude" option.

See commit 77d21f2, commit 43f8080, commit 96415b4, commit 290be66, commit 4a68748 (18 Jan 2017) by Jacob Keller (jacob-keller).
(Merged by Junio C Hamano -- gitster -- in commit 1b32498, 27 Feb 2017)
You now can have multiple match pattern:
--match <pattern>:

Only consider tags matching the given glob(7) pattern,  excluding the "refs/tags/" prefix.
This can be used to avoid leaking private tags from the repository.
If given multiple times, a list of patterns will be accumulated, and tags matching any of the patterns will be considered.
Use --no-match to clear and reset the list of patterns.

And you have also an exclude pattern (or several) now!
--exclude <pattern>::

Do not consider tags matching the given glob(7) pattern, excluding the "refs/tags/" prefix.
This can be used to narrow the tag space and  find only tags matching some meaningful criteria.
If given multiple times, a list of patterns will be accumulated and tags matching any of the patterns will be excluded.
When combined with --match a tag will be considered when it matches at least one --match pattern and does not match any of the --exclude patterns.
Use --no-exclude to clear and reset the list of patterns.
Answer: git describe --match v*

Use this.
Comment: this will include all the tags starts with `v`, so it'll list all the words not just versions
Answer: I use
git tag -l --format "%(objecttype) %(refname:short)" --sort=-version:refname --merged HEAD "v*"

git tag interprets the pattern as glob. So you cannot specify a full blown regex. Just use a minimal glob (v*) or even no glob at all. You will receive more than only one matching tag in sorted order (highest tag version first) and you will be able to regex the output afterwards.
Answer: Something more complex would be along the lines of:
/v[0-9]+(\.[0-9]+).*/

Comment: Have you looked at all the answers? An example of how to use the above string was already given like: git describe --match "/v[0-9]+(\.[0-9]+).*/" --abbrev=4 HEAD
Comment: That is not working, cause --match parameter only supports glob patterns no regex patterns
Comment: It worked for me, so most likely you are missing something or we have different versions of tools used.


How to show Image in a Listview after completely download in jQuery Mobile?

Question: I'm creating an app using Phonegap/jQuery Mobile. I have created a ListView in jQuery mobile. In that page I called an ajax which contains some image url. So I want to show those image and other parameters after all images download is complete.
"data":[
{
"id":"48",
"string_id":"Hello world",
"imgurl":"http://sample.com/hello.jpg"
},
{
"id":"58",
"string_id":"Hello world",
"imgurl":"http://sample.com/world.jpg"
}
]

Here is the list view code.

Answer: Working example: http://jsfiddle.net/zghz4/
HTML:

Javascript:
$(document).on('pageinit', '#index', function(){
    var obj = jQuery.parseJSON('{"data":[{"id":"48","string_id":"Hello world","imgurl":"https://si0.twimg.com/profile_images/3629297899/da2519b27b5b82454d67a3ff42b8c109.png"}, {"id":"58","string_id":"Hello world","imgurl":"https://si0.twimg.com/profile_images/3629297899/da2519b27b5b82454d67a3ff42b8c109.png"}]}');
    $.each(obj.data, function(index, value) {
        $('[data-role="listview"]').append('

' + value.string_id + '

');
    });
    $('[data-role="listview"] li img').load(function(){

        $('[data-role="listview"]').show().listview('refresh');
    });
});

In this example listview is hidden from the start.
Json is converted to an object and each loop is going through it appending new content to listview.
After that function load is used to wait for images to be loaded successfully. When images are loaded callback function is triggered, listview is showed and its content is enhanced with .listview('refresh').
Answer: You can use the jquery lazy load plugin.
Check the plugin over here http://www.appelsiini.net/projects/lazyload
You can go through the codes and watch the demos listed.


populating a dynamic form with javascript

Question: I am trying to get a forloop with input elements to run between a form and create a form dynamically with javascript
1: in scenario one , the form in the script is getting closed before the input elements populate.
2: in scenario two , when i put the for loop variable between the form ,the error that comes is undefined .
PLEASE HELP
 SCENARIO ONE
<form>
No of Feilds <input type="text" id= "numberoffeilds">
<input type="button" value = "Create Feilds" onclick= "addfeilds1();">
</form>

 SCENARIO TWO
<form>
No of Feilds <input type="text" id= "numberoffeilds">
<input type="button" value = "Create Feilds" onclick= "addfeilds2();">
</form>

Comment: am new to using stackoverflow , wat i wanted to imply is to differenciate it as 2 diff scenarios .. can i edit this . ?
Answer: You need to build the HTML elements in a string first and add them to the div as a last step.
Fixed scenario 1:
function addfeilds1()
{
   var totalfeilds = document.getElementById("numberoffeilds").value;
   var i;

   var htmlString = "";
   htmlString += '<form action= "issue.html" method = "POST">';

   for(i=0;i<totalfeilds;i++)
   {
       htmlString += '<input type = "text">';
   }

   htmlString += '<input type = "submit" value="submit" name="submit">';
   document.getElementById("div4").innerHTML = htmlString;
}

This prevents the form tag from being closed before it's populated with inputs.
Fixing scenario 2:
  function forloop()
  {
      var htmlString = "";
      for(i=1 ;i<totalfeilds;i++)
      {
           htmlString  += '<input type = "text">';
      }
      return htmlString; // now forloop returns a string that can be added to the element. It no longers returns undefined.
  }

Actually scenario 2 was fixing scenario 1, but you didn't include a return in your function. If you expect a function to create some text and concat that into a string you need your function to return a string.
Third example (advanced)
function addfeilds1()
{
    var totalFields = parseInt(document.getElementById("numberoffeilds").value); //parse integer from value
    if (isNaN(totalFields) || totalFields < 1)
    {
        //check if the input is valid, if not alert.
        alert("Value is not a valid number or lower than 1.");
    }

    var container = document.getElementById("div4");
    //create the form
    var form = document.createElement("form");
    form.setAttribute("action", "issue.html");
    form.setAttribute("method", "POST");

    for(var i=0; i<totalFields; ++i)
    {
       var node = document.createElement("input");
       node.setAttribute("name", "field[]"); //this sends a array to the request page containing all input field values.
       form.appendChild(node); //add the fields to the form.
    }

    //create the submit button.
    var button = document.createElement("input");
    button.setAttribute("type", "submit");
    button.setAttribute("value", "submit");
    button.setAttribute("name", "submit");
    form.appendChild(button);

    container.appendChild(form); //append the form to the div.
}

Comment: awesome dude , its working now .. kewlll ... thanks a zillion bro .. this is extremely helpful .. ,thanks again ..
Comment: @Sebastian take a look at solution 3, it's a very clean one.
Comment: thanks again bro ... the third solution ...is something that i will  have to spend some time with ... cause its a lil advanced for me right now (am new to programing), but your answers have given me so many new things to learn about .... thanks for guiding me in the right direction :)
Comment: Thats the way to learn fast. Dissecting code step by step. Googling function names. Happy coding!